Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.1 KiB

  1. //========== Copyright (c) Valve Corporation, All rights reserved. ==========//
  2. // STATIC: "CONTACT_SHADOW" "0..1"
  3. // STATIC: "VOLUME_TEXTURE" "0..1"
  4. // DYNAMIC: "COMPRESSED_VERTS" "0..1"
  5. // DYNAMIC: "SKINNING" "0..1"
  6. // Includes
  7. #include "common_vs_fxc.h"
  8. // Globals
  9. const float g_flUVScale : register( SHADER_SPECIFIC_CONST_0 );
  10. const float3 g_vUVProjOffset : register( SHADER_SPECIFIC_CONST_1 );
  11. const float3 g_vBBMin : register( SHADER_SPECIFIC_CONST_2 );
  12. const float3 g_vBBMax : register( SHADER_SPECIFIC_CONST_3 );
  13. // Structs
  14. struct VS_INPUT
  15. {
  16. float4 vPos : POSITION; // Position
  17. float4 vNormal : NORMAL; // Normal
  18. float4 vBoneWeights : BLENDWEIGHT; // Skin weights
  19. float4 vBoneIndices : BLENDINDICES; // Skin indices
  20. #if CONTACT_SHADOW == 1
  21. float4 vClosestSurfaceDir : TEXCOORD0;
  22. #else
  23. float4 vColor : TEXCOORD0;
  24. #endif
  25. float4 vTangent : TANGENT;
  26. };
  27. struct VS_OUTPUT
  28. {
  29. float4 vProjPosition : POSITION; // Projection-space position
  30. float4 vWorldNormal : TEXCOORD0; // w is proj. z coord (for depth stuff)
  31. #if CONTACT_SHADOW == 1
  32. float4 vClosestSurfaceDir : TEXCOORD1;
  33. #else
  34. float4 vColor : TEXCOORD1;
  35. #endif
  36. float4 vWorldPos : TEXCOORD2; // w is proj. w coord
  37. float4 vUV0 : TEXCOORD3;
  38. float4 vUV1 : TEXCOORD4;
  39. float4 vLightAtten : TEXCOORD5;
  40. float3 vAmbient : TEXCOORD6;
  41. };
  42. float3 CubeUV( float3 vWorldPosition, float3 vWorldNormal, float3 vUVSpacePos )
  43. {
  44. // Generate texcoords for a cubemap wrapped around the blob's bounding box
  45. float3 vDir = normalize( lerp( normalize( vUVSpacePos.xyz ), vWorldNormal.xyz, 0.5 ) );
  46. float3 tMins = ( g_vBBMin - vWorldPosition ) / vDir;
  47. float3 tMaxs = ( g_vBBMax - vWorldPosition ) / vDir;
  48. tMaxs = max( tMaxs, tMins ); // weed out negative t's per axis
  49. float t = min( tMaxs.x, min( tMaxs.y, tMaxs.z ) );
  50. float3 vIntersectionPos = vWorldPosition + t * vDir;
  51. // transform into unit cube
  52. float3 vCubeUV = vIntersectionPos;
  53. vCubeUV -= 0.5*( g_vBBMax + g_vBBMin );
  54. vCubeUV /= 0.5*( g_vBBMax - g_vBBMin );
  55. vCubeUV = normalize( vCubeUV );
  56. return vCubeUV;
  57. }
  58. // Main
  59. VS_OUTPUT main( const VS_INPUT i )
  60. {
  61. VS_OUTPUT o;
  62. float4 vObjPosition = i.vPos;
  63. float4 vObjTangent = i.vTangent;
  64. float3 vObjNormal;
  65. DecompressVertex_Normal( i.vNormal, vObjNormal );
  66. // Transform the position and normal
  67. float3 vWorldPosition = { 0.0f, 0.0f, 0.0f };
  68. float3 vWorldNormal = { 0.0f, 0.0f, 0.0f };
  69. float3 vWorldTangent = { 0.0f, 0.0f, 0.0f };
  70. float3 vWorldBinormal = { 0.0f, 0.0f, 0.0f };
  71. SkinPositionNormalAndTangentSpace( SKINNING ? true : false,
  72. vObjPosition, vObjNormal.xyz, vObjTangent.xyzw,
  73. i.vBoneWeights, i.vBoneIndices,
  74. vWorldPosition, vWorldNormal, vWorldTangent, vWorldBinormal );
  75. vWorldNormal.xyz = normalize( vWorldNormal.xyz );
  76. o.vWorldNormal.xyz = vWorldNormal.xyz;
  77. o.vWorldPos.xyz = vWorldPosition.xyz;
  78. // Transform into projection space
  79. float4 vProjPosition = mul( float4( vWorldPosition, 1.0f ), cViewProj );
  80. o.vProjPosition = vProjPosition;
  81. o.vWorldNormal.w = vProjPosition.z;
  82. o.vWorldPos.w = vProjPosition.w;
  83. // Seamless texturing UV projections
  84. float3 vUVSpacePos = vWorldPosition.xyz - g_vUVProjOffset.xyz;
  85. o.vUV0.xy = g_flUVScale * vUVSpacePos.yz;
  86. o.vUV0.wz = g_flUVScale * vUVSpacePos.xz;
  87. o.vUV1.xy = g_flUVScale * vUVSpacePos.xy;
  88. // Cubemap UV gen
  89. //o.vCubeUVW.xyz = CubeUV( vWorldPosition, vWorldNormal, vUVSpacePos );
  90. // Screen-space texcoord
  91. float2 vScreenPos = float2( 0.5, -0.5 ) * vProjPosition.xy + 0.5 * vProjPosition.w;
  92. o.vUV1.wz = vScreenPos;
  93. // Dynamic light attenuation factors
  94. o.vLightAtten.x = GetVertexAttenForLight( vWorldPosition, 0 );
  95. o.vLightAtten.y = GetVertexAttenForLight( vWorldPosition, 1 );
  96. o.vLightAtten.z = GetVertexAttenForLight( vWorldPosition, 2 );
  97. o.vLightAtten.w = GetVertexAttenForLight( vWorldPosition, 3 );
  98. // Ambient light
  99. o.vAmbient = AmbientLight( vWorldNormal );
  100. #if CONTACT_SHADOW == 1
  101. // Closest surface's direction and distance, for contact shadowing
  102. o.vClosestSurfaceDir.xyz = i.vClosestSurfaceDir.xyz;
  103. o.vClosestSurfaceDir.w = length( i.vClosestSurfaceDir.xyz );
  104. #else
  105. // Vertex colour for self-illumination glow
  106. o.vColor.rgba = i.vColor.rgba;
  107. #endif
  108. return o;
  109. }