Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. float4x4 gTransform : WorldViewProjection;
  2. Texture2D gSpriteTexture;
  3. float2 gTextureSize;
  4.  
  5. SamplerState samPoint
  6. {
  7. Filter = MIN_MAG_MIP_POINT;
  8. AddressU = WRAP;
  9. AddressV = WRAP;
  10. };
  11.  
  12. BlendState EnableBlending
  13. {
  14. BlendEnable[0] = TRUE;
  15. SrcBlend = SRC_ALPHA;
  16. DestBlend = INV_SRC_ALPHA;
  17. };
  18.  
  19. DepthStencilState NoDepth
  20. {
  21. DepthEnable = FALSE;
  22. };
  23.  
  24. RasterizerState BackCulling
  25. {
  26. CullMode = BACK;
  27. };
  28.  
  29. //SHADER STRUCTS
  30. //**************
  31. struct VS_DATA
  32. {
  33. uint TextureId: TEXCOORD0;
  34. float4 TransformData : POSITION; //PosX, PosY, Depth (PosZ), Rotation
  35. float4 TransformData2 : POSITION1; //PivotX, PivotY, ScaleX, ScaleY
  36. float4 Color: COLOR;
  37. };
  38.  
  39. struct GS_DATA
  40. {
  41. float4 Position : SV_POSITION;
  42. float4 Color: COLOR;
  43. float2 TexCoord: TEXCOORD0;
  44. };
  45.  
  46. //VERTEX SHADER
  47. //*************
  48. VS_DATA MainVS(VS_DATA input)
  49. {
  50. return input;
  51. }
  52.  
  53. //GEOMETRY SHADER
  54. //***************
  55. void CreateVertex(inout TriangleStream<GS_DATA> triStream, float3 pos, float4 col, float2 texCoord, float rotation, float2 rotCosSin, float2 offset, float2 pivotOffset)
  56. {
  57. if (rotation != 0)
  58. {
  59. //Step 3.
  60. //Do rotation calculations
  61. //Transform to origin
  62. //pos.xy -= offset + pivotOffset;
  63. //float2 origPos = pos.xy;
  64. ////Rotate
  65. //pos.x = (origPos.x * rotCosSin.x) - (origPos.y * rotCosSin.y);
  66. //pos.y = (origPos.y * rotCosSin.x) + (origPos.x * rotCosSin.y);
  67. //Retransform to initial position
  68. //pos.xy += offset + pivotOffset;
  69. }
  70. else
  71. {
  72. //Step 2.
  73. //No rotation calculations (no need to do the rotation calculations if there is no rotation applied > redundant operations)
  74. //Just apply the pivot offset
  75. pos.xy -= pivotOffset;
  76. }
  77.  
  78. //Geometry Vertex Output
  79. GS_DATA geomData = (GS_DATA) 0;
  80. geomData.Position = mul(float4(pos, 1.0f), gTransform);
  81. geomData.Color = col;
  82. geomData.TexCoord = texCoord;
  83. triStream.Append(geomData);
  84. }
  85.  
  86. [maxvertexcount(4)]
  87. void MainGS(point VS_DATA vertex[1], inout TriangleStream<GS_DATA> triStream)
  88. {
  89. //Given Data (Vertex Data)
  90. float3 position = vertex[0].TransformData.xyz; //Extract the position data from the VS_DATA vertex struct
  91. float2 offset = vertex[0].TransformData2.xy; //Extract the offset data from the VS_DATA vertex struct (initial X and Y position)
  92. float rotation = vertex[0].TransformData.w; //Extract the rotation data from the VS_DATA vertex struct
  93. float2 pivot = vertex[0].TransformData2.xy; //Extract the pivot data from the VS_DATA vertex struct
  94. float2 scale = vertex[0].TransformData2.zw; //Extract the scale data from the VS_DATA vertex struct
  95. float2 texCoord = float2(0, 0); //Initial Texture Coordinate
  96.  
  97. // LT----------RT //TringleStrip (LT > RT > LB, LB > RB > RT)
  98. // | / |
  99. // | / |
  100. // | / |
  101. // | / |
  102. // LB----------RB
  103.  
  104. float2 rotCosSin;
  105. if(rotation == 0)
  106. rotCosSin = float2(2, 0);
  107. else
  108. {
  109. rotCosSin.x = cos(rotation);
  110. rotCosSin.y = cos(rotation);
  111. }
  112.  
  113. float2 pivotOffset;
  114. pivotOffset = pivot * gTextureSize * scale;
  115.  
  116. //VERTEX 1 [LT]
  117. texCoord = float2(0, 0);
  118. CreateVertex(triStream, position, float4(1, 1, 1, 1), texCoord, rotation, rotCosSin, offset, pivotOffset); //Change the color data too!
  119.  
  120. //VERTEX 2 [RT]
  121. position.x += gTextureSize.x * scale.x;
  122. texCoord = float2(1, 0);
  123. CreateVertex(triStream, position, float4(1, 1, 1, 1), texCoord, rotation, rotCosSin, offset, pivotOffset); //Change the color data too!
  124.  
  125. //VERTEX 3 [LB]
  126. position.x = offset.x;
  127. position.y += gTextureSize.y * scale.y;
  128. texCoord = float2(0, 1);
  129. CreateVertex(triStream, position, float4(1, 1, 1, 1), texCoord, rotation, rotCosSin, offset, pivotOffset); //Change the color data too!
  130.  
  131. //VERTEX 4 [RB]
  132. position.x += gTextureSize.x * scale.x;
  133. texCoord = float2(1, 1);
  134. CreateVertex(triStream, position, float4(1, 1, 1, 1), texCoord, rotation, rotCosSin, offset, pivotOffset); //Change the color data too!
  135. }
  136.  
  137. //PIXEL SHADER
  138. //************
  139. float4 MainPS(GS_DATA input) : SV_TARGET {
  140.  
  141. return gSpriteTexture.Sample(samPoint, input.TexCoord) * input.Color;
  142. }
  143.  
  144. // Default Technique
  145. technique11 Default {
  146.  
  147. pass p0 {
  148. SetRasterizerState(BackCulling);
  149. SetBlendState(EnableBlending,float4(0.0f, 0.0f, 0.0f, 0.0f), 0xFFFFFFFF);
  150. //SetDepthStencilState(NoDepth,0);
  151. SetVertexShader(CompileShader(vs_4_0, MainVS()));
  152. SetGeometryShader(CompileShader(gs_4_0, MainGS()));
  153. SetPixelShader(CompileShader(ps_4_0, MainPS()));
  154. }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement