Unity Shader:使用顶点着色器变形3D模型,然后使用曲面着色器应用镜面反射

时间:2013-04-01 10:47:44

标签: unity3d shader vertex-shader

在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"
    }

1 个答案:

答案 0 :(得分:1)

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

Unity为您提供照明信息,您可以根据自己的意愿进行自己的计算:

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

然而,Doc似乎已经过时,而且有些变量是错误的,因为它在这里指出:

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

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