Search Unity

Beginner question: how do I set the strength of a cube map reflection (pic+code)

Discussion in 'Shaders' started by monkeybrother, Nov 29, 2010.

  1. monkeybrother

    monkeybrother

    Joined:
    Oct 14, 2010
    Posts:
    83
    Hello. I asked other questions about this shader in another thread, but since this is a new problem, here's another one. I'm a complete beginner when it comes to shaders.

    I'm doing my own shader in Strumpy's Shader Editor, with minor tweaks by hand, and it works well enough. Now I've added a cube map reflection to my shader and I can't figure out how to control the strength of the reflection. It's either full chrome or nothing. How do I control the reflection with either a slider, a color or an alpha (or some other way)? It doesn't really matter how. Any tips appreciated!

    Code (csharp):
    1.  
    2. Shader "WhiteLightmapAlphaReflections"
    3. {
    4.     Properties
    5.     {
    6. _SamplerCube("_SamplerCube", Cube) = "" {}
    7. _Diffuse("_Diffuse", 2D) = "white" {}
    8. _Lightmap("_Lightmap", 2D) = "white" {}
    9. _GlossOchSpecferg("_GlossOchSpecferg", Color) = (0.1716418,0.1716418,0.1716418,1)
    10. _Specular("_Specular", Range(0,1) ) = 0.75
    11. _Clip("_Clip", 2D) = "white" {}
    12.  
    13.     }
    14.    
    15.     SubShader
    16.     {
    17.         Tags
    18.         {
    19. "Queue"="Transparent+100"
    20. "IgnoreProjector"="False"
    21. "RenderType"="Transparent"
    22.  
    23.         }
    24.  
    25.                     Pass {ColorMask 0}
    26. Cull Back
    27. ZWrite On
    28. ZTest LEqual
    29.  
    30.  
    31.         CGPROGRAM
    32. #pragma surface surf BlinnPhongEditor alpha vertex:vert
    33. #pragma target 3.0
    34.  
    35.  
    36. samplerCUBE _SamplerCube;
    37. sampler2D _Diffuse;
    38. sampler2D _Lightmap;
    39. float4 _GlossOchSpecferg;
    40. float _Specular;
    41. sampler2D _Clip;
    42.  
    43.             struct EditorSurfaceOutput {
    44.                 half3 Albedo;
    45.                 half3 Normal;
    46.                 half3 Emission;
    47.                 half3 Gloss;
    48.                 half Specular;
    49.                 half Alpha;
    50.             };
    51.            
    52.             inline half4 LightingBlinnPhongEditor_PrePass (EditorSurfaceOutput s, half4 light)
    53.             {
    54. half3 spec = light.a * s.Gloss;
    55. half4 c;
    56. c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
    57. c.a = s.Alpha + Luminance(spec);
    58. return c;
    59.  
    60.             }
    61.  
    62.             inline half4 LightingBlinnPhongEditor (EditorSurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
    63.             {
    64.                 viewDir = normalize(viewDir);
    65.                 half3 h = normalize (lightDir + viewDir);
    66.                
    67.                 half diff = max (0, dot (s.Normal, lightDir));
    68.                
    69.                 float nh = max (0, dot (s.Normal, h));
    70.                 float3 spec = pow (nh, s.Specular*128.0) * s.Gloss;
    71.                
    72.                 half4 res;
    73.                 res.rgb = _LightColor0.rgb * (diff * atten * 2.0);
    74.                 res.w = spec * Luminance (_LightColor0.rgb);
    75.  
    76.                 return LightingBlinnPhongEditor_PrePass( s, res );
    77.             }
    78.            
    79.             struct Input {
    80.                 float3 simpleWorldRefl;
    81. float4 meshUV;
    82.  
    83.             };
    84.  
    85.  
    86.             void vert (inout appdata_full v, out Input o) {
    87. float4 VertexOutputMaster0_0_NoInput = float4(0,0,0,0);
    88. float4 VertexOutputMaster0_1_NoInput = float4(0,0,0,0);
    89. float4 VertexOutputMaster0_2_NoInput = float4(0,0,0,0);
    90. float4 VertexOutputMaster0_3_NoInput = float4(0,0,0,0);
    91.  
    92. o.simpleWorldRefl = -reflect( normalize(WorldSpaceViewDir(v.vertex)), normalize(mul((float3x3)_Object2World, SCALED_NORMAL)));
    93. o.meshUV.xy = v.texcoord.xy;
    94. o.meshUV.zw = v.texcoord1.xy;
    95.  
    96.             }
    97.            
    98.  
    99.             void surf (Input IN, inout EditorSurfaceOutput o) {
    100.                 o.Albedo = 0.0;
    101.                 o.Normal = float3(0.0,0.0,1.0);
    102.                 o.Emission = 0.0;
    103.                 o.Gloss = 0.0;
    104.                 o.Specular = 0.0;
    105.                 o.Alpha = 1.0;
    106. float4 TexCUBE0=texCUBE(_SamplerCube,float4( IN.simpleWorldRefl, 1.0));
    107. float4 Tex2D1=tex2D(_Diffuse,(IN.meshUV.xyxy).xy);
    108. float4 Tex2D3=tex2D(_Lightmap,(IN.meshUV.xyxy).xy);
    109. float4 Multiply0=Tex2D1 * Tex2D3;
    110. float4 Multiply1=TexCUBE0 * Multiply0;
    111. float4 Tex2D2=tex2D(_Clip,(IN.meshUV.zwzw).xy);
    112. float4 Master0_0_NoInput = float4(0,0,0,0);
    113. float4 Master0_1_NoInput = float4(0,0,1,1);
    114. float4 Master0_6_NoInput = float4(1,1,1,1);
    115. o.Emission = Multiply1;
    116. o.Specular = _GlossOchSpecferg;
    117. o.Gloss = _Specular.xxxx;
    118. o.Alpha = float4( Tex2D2.a);
    119.  
    120.             }
    121.         ENDCG
    122.     }
    123.     Fallback "Diffuse"
    124. }
    125.  

    $fasad3.jpg

    $strumpys.jpg