MuskStandard.shader 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Maps Unity SDK Standard Shader.
  2. // Diffuse and specular lighting with fog and shadows (casting and receiving).
  3. Shader "Google/Maps/Shaders/Standard" {
  4. Properties {
  5. _MainTex("Texture", 2D) = "white" {}
  6. _Color("Color", Color) = (1.0, 1.0, 1.0, 1.0)
  7. _Shininess("Specular Shininess", Float) = 10.0
  8. }
  9. SubShader {
  10. Pass {
  11. Tags { "LightMode" = "ForwardBase" }
  12. Cull Back // Cull backfaces.
  13. ZWrite On // Use Z-buffer.
  14. ZTest LEqual // Use normal (less-equal) z-depth check.
  15. CGPROGRAM
  16. #pragma vertex vert // Vertex shader.
  17. #pragma fragment frag // Fragment shader.
  18. #pragma multi_compile_fog // To make fog work.
  19. #include "UnityCG.cginc" // Standard unity helper functions.
  20. #include "Lighting.cginc" // Lighting functions.
  21. #include "AutoLight.cginc" // Shadow functions and macros.
  22. // User defined values.
  23. sampler2D _MainTex;
  24. float4 _MainTex_ST;
  25. uniform float4 _Color;
  26. uniform float _Shininess;
  27. // Vertex Shader input.
  28. struct vertexInput {
  29. float4 vertex : POSITION; // Vertex worldspace position.
  30. float2 uv : TEXCOORD0; // Vertex UV coordinates.
  31. float3 normal : NORMAL; // Vertex normal vector.
  32. };
  33. // Fragment Shader Input.
  34. struct vertexOutput {
  35. float4 pos : SV_POSITION; // Vertex screenspace position.
  36. float3 normal : NORMAL; // Vertex normal vector.
  37. float2 uv : TEXCOORD0; // Vertex UV coordinates.
  38. float4 vertex : TEXCOORD1; // Vertex worldspace position.
  39. SHADOW_COORDS(2) // Shadow data as TEXCOORD2.
  40. UNITY_FOG_COORDS(3) // Per-vertex fog as TEXCOORD3.
  41. };
  42. // Vertex Shader.
  43. // Note that input must be called 'v' for TRANSFER_SHADOW to work.
  44. vertexOutput vert(vertexInput v) {
  45. // Computer screenspace position.
  46. vertexOutput output;
  47. output.pos = UnityObjectToClipPos(v.vertex);
  48. // Generate texture coordinates.
  49. output.uv = v.uv;
  50. // Get vertex worldspace position and normals for lighting calculation
  51. // in fragment shader.
  52. float4x4 modelMatrix = unity_ObjectToWorld;
  53. float4x4 modelMatrixInverse = unity_WorldToObject;
  54. output.vertex = mul(modelMatrix, v.vertex);
  55. output.normal = normalize(mul(float4(v.normal, 0.0),
  56. modelMatrixInverse).xyz);
  57. // Compute shadows data (requires input to be 'v').
  58. TRANSFER_SHADOW(output)
  59. // Apply fog.
  60. UNITY_TRANSFER_FOG(output, output.pos);
  61. return output;
  62. }
  63. // Fragment Shader.
  64. fixed4 frag(vertexOutput input) : SV_Target {
  65. // Start with color.
  66. fixed3 diffColor = tex2D(_MainTex, input.uv) * _Color;
  67. fixed3 specColor = _Color;
  68. // Calculate shadow value.
  69. fixed shadow = SHADOW_ATTENUATION (input);
  70. // Calculate lighting.
  71. float3 normalDirection = normalize(input.normal);
  72. float3 viewDirection = normalize(_WorldSpaceCameraPos
  73. - input.vertex.xyz);
  74. float3 lightDirection;
  75. float attenuation;
  76. // If light is directional, have no attenuation (no falloff).
  77. if (0.0 == _WorldSpaceLightPos0.w) {
  78. attenuation = 1.0;
  79. lightDirection = normalize(_WorldSpaceLightPos0.xyz);
  80. } else { // For point or spot light, use linear attenuation.
  81. float3 vertexToLightSource = _WorldSpaceLightPos0.xyz
  82. - input.vertex.xyz;
  83. float distance = length(vertexToLightSource);
  84. attenuation = 1.0 / distance;
  85. lightDirection = normalize(vertexToLightSource);
  86. }
  87. // Calculate ambient and diffuse lighting.
  88. float3 ambientLighting = UNITY_LIGHTMODEL_AMBIENT.rgb
  89. * diffColor;
  90. float3 diffuseReflection = shadow * attenuation * _LightColor0.rgb
  91. * diffColor * max(0.0, dot(normalDirection, lightDirection));
  92. // If light source is on the wrong side, have no specular light.
  93. float3 specularReflection;
  94. if (dot(normalDirection, lightDirection) < 0.0) {
  95. specularReflection = float3(0.0, 0.0, 0.0);
  96. } else {
  97. specularReflection =
  98. shadow * attenuation * _LightColor0.rgb * specColor *
  99. pow(max(0.0, dot(reflect(-lightDirection, normalDirection),
  100. viewDirection)), _Shininess);
  101. }
  102. // Collection lighting together.
  103. fixed4 finalColor = fixed4(ambientLighting + diffuseReflection
  104. + specularReflection, 1.0);
  105. // Apply fog.
  106. UNITY_APPLY_FOG(input.fogCoord, finalColor);
  107. return finalColor;
  108. }
  109. ENDCG
  110. }
  111. // Pull in shadow caster pass from VertexLit built in-shader.
  112. // Remove to make this receive shadows only (but not cast them).
  113. UsePass "Legacy Shaders/VertexLit/SHADOWCASTER"
  114. }
  115. // Fallback to default diffuse textured shader.
  116. Fallback "Standard"
  117. }