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.

113 lines
3.9 KiB

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