0

在 CG 顶点着色器中变形后,我需要帮助在 3D 模型中应用镜面反射阴影。我不知道如何在 Unity 着色器中做到这一点。我已经在谷歌搜索过,没有找到我要找的东西。

以下是我目前的解决方案。3D 对象被渲染两次。

    Shader "myShader/DeformSpecular" {
    Properties {
            _Color ("Main Color", Color) = (1,1,1,0.5)
            _Shininess ("Shininess", Range (0.01, 1)) = 0.7
            _MainTex ("Base (RGB)", 2D) = "white" { }
            _BumpMap ("Normalmap", 2D) = "bump" {}
            _cubeSize("Cube Size", Range (1, 10)) = 1


        }

        SubShader { 
            Tags { "RenderType"="Opaque" }
            LOD 250

            Pass{
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag

                #include "UnityCG.cginc"
                #include "myShaderFuncs.cginc"

                sampler2D _MainTex;
                sampler2D _BumpMap;
                half _Shininess;

                float4 _Color;
                float4x4 _localOrient;  //local orientation 
                float4x4 _parentWOrient;  //world orientation of parent node
                float _deformParam;

                struct v2f {
                    float4  pos : SV_POSITION;
                    float2  uv : TEXCOORD0;
                };


                float4 _MainTex_ST;

                v2f vert (appdata_base v)
                {  
                    v2f o; 


                    o.pos = mul(_localOrient, v.vertex); //apply local transformation

                    o.pos = deformShape(_deformParam);   //apply deform

                    o.pos = mul(_parentWOrient, o.pos);  //apply parents world orientation
                    o.pos = mul (UNITY_MATRIX_VP, o.pos);
                    o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
                    return o;
                }


                half4 frag (v2f i) : COLOR
                {
                    half4 texcol = tex2D (_MainTex, i.uv);
                    return texcol * _Color;
                }

                ENDCG


            }//Pass

            CGPROGRAM
            #pragma surface surf MobileBlinnPhong exclude_path:prepass nolightmap noforwardadd halfasview

            inline fixed4 LightingMobileBlinnPhong (SurfaceOutput s, fixed3 lightDir, fixed3 halfDir, fixed atten)
            {
                fixed diff = max (0, dot (s.Normal, lightDir));
                fixed nh = max (0, dot (s.Normal, halfDir));
                fixed spec = pow (nh, s.Specular*128) * s.Gloss;

                fixed4 c;
                c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * spec) * (atten*2);
                c.a = 0.0;
                return c;
            }

            sampler2D _MainTex;
            sampler2D _BumpMap;
            half _Shininess;

            struct Input {
                float2 uv_MainTex;
            };

            void surf (Input IN, inout SurfaceOutput o) {
                fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
                o.Albedo = tex.rgb;
                o.Gloss = tex.a;
                o.Alpha = tex.a;
                o.Specular = _Shininess;
                o.Normal = UnpackNormal (tex2D(_BumpMap, IN.uv_MainTex));
            }
            ENDCG

        }//SubShader

        FallBack "Mobile/VertexLit"
    }
4

1 回答 1

1

如果我理解正确,您想在 CG 着色器中进行镜面光照计算吗?

Unity 为您提供照明信息,如果您愿意,您可以访问这些信息以进行自己的计算:

http://docs.unity3d.com/Documentation/Components/SL-BuiltinValues.html

但是,该文档似乎已过时,并且此处指出的某些变量是错误的:

http://answers.unity3d.com/questions/411114/shaderlab-builtin-lighting-properties-are-not-corr.html

使用这些变量后,您可以根据需要计算照明

于 2013-04-12T16:31:44.493 回答