Lux TreeCreatorBarkRendertex.shader 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
  2. Shader "Hidden/Nature/Lux Tree Creator Bark Rendertex" {
  3. Properties {
  4. _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
  5. _BumpSpecMap ("Normalmap (GA) Spec (R)", 2D) = "bump" {}
  6. _TranslucencyMap ("Trans (RGB) Gloss(A)", 2D) = "white" {}
  7. // These are here only to provide default values
  8. _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
  9. }
  10. SubShader {
  11. Pass {
  12. CGPROGRAM
  13. #pragma vertex vert
  14. #pragma fragment frag
  15. #include "UnityCG.cginc"
  16. struct v2f {
  17. float4 pos : SV_POSITION;
  18. float2 uv : TEXCOORD0;
  19. float3 color : TEXCOORD1;
  20. float2 params1: TEXCOORD2;
  21. float2 params2: TEXCOORD3;
  22. float2 params3: TEXCOORD4;
  23. half3 ambient : TEXCOORD5;
  24. UNITY_VERTEX_OUTPUT_STEREO
  25. };
  26. CBUFFER_START(UnityTerrainImposter)
  27. float3 _TerrainTreeLightDirections[4];
  28. float4 _TerrainTreeLightColors[4];
  29. CBUFFER_END
  30. float2 CalcTreeLightingParams(float3 normal, float3 lightDir, float3 viewDir)
  31. {
  32. float2 output;
  33. half nl = dot (normal, lightDir);
  34. output.r = max (0, nl);
  35. half3 h = normalize (lightDir + viewDir);
  36. float nh = max (0, dot (normal, h));
  37. output.g = nh;
  38. return output;
  39. }
  40. v2f vert (appdata_full v) {
  41. v2f o;
  42. UNITY_SETUP_INSTANCE_ID(v);
  43. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  44. o.pos = UnityObjectToClipPos(v.vertex);
  45. o.uv = v.texcoord.xy;
  46. float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
  47. /* We used to do a for loop and store params as a texcoord array[3].
  48. * HLSL compiler, however, unrolls this loop and opens up the uniforms
  49. * into 3 separate texcoords, but doesn't do it on fragment shader.
  50. * This discrepancy causes error on iOS, so do it manually. */
  51. o.params1 = CalcTreeLightingParams(v.normal, _TerrainTreeLightDirections[0], viewDir);
  52. o.params2 = CalcTreeLightingParams(v.normal, _TerrainTreeLightDirections[1], viewDir);
  53. o.params3 = CalcTreeLightingParams(v.normal, _TerrainTreeLightDirections[2], viewDir);
  54. o.color = v.color.a;
  55. o.ambient = ShadeSH9(half4 (v.normal, 1));
  56. return o;
  57. }
  58. sampler2D _MainTex;
  59. sampler2D _BumpSpecMap;
  60. sampler2D _TranslucencyMap;
  61. fixed4 _SpecColor;
  62. void ApplyTreeLighting(inout half3 light, half3 albedo, half gloss, half specular, half3 lightColor, float2 param)
  63. {
  64. half nl = param.r;
  65. light += albedo * lightColor * nl;
  66. float nh = param.g;
  67. float spec = pow (nh, specular) * gloss;
  68. light += lightColor * _SpecColor.rgb * spec;
  69. }
  70. fixed4 frag (v2f i) : SV_Target
  71. {
  72. fixed3 albedo = tex2D (_MainTex, i.uv).rgb;
  73. half gloss = tex2D(_TranslucencyMap, i.uv).a;
  74. half specular = tex2D (_BumpSpecMap, i.uv).r * 128.0;
  75. //half3 light = UNITY_LIGHTMODEL_AMBIENT * albedo;
  76. half3 light = i.ambient * albedo * i.color;
  77. ApplyTreeLighting(light, albedo, gloss, specular, _TerrainTreeLightColors[0], i.params1);
  78. ApplyTreeLighting(light, albedo, gloss, specular, _TerrainTreeLightColors[1], i.params2);
  79. ApplyTreeLighting(light, albedo, gloss, specular, _TerrainTreeLightColors[2], i.params3);
  80. fixed4 c;
  81. c.rgb = light;
  82. c.a = 1.0;
  83. UNITY_OPAQUE_ALPHA(c.a);
  84. return c;
  85. }
  86. ENDCG
  87. }
  88. }
  89. FallBack Off
  90. }