0

I need help in applying a specular shade in a 3D model after I deformed it in CG vertex shader. I don't know how to do it in Unity shader. I already searched in google and there are no hits of what I am looking for.

Below is my current solution. The 3D object is rendered twice.

    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"
    }
Renier
  • 1,523
  • 4
  • 32
  • 60
acegs
  • 2,621
  • 1
  • 22
  • 31
  • Worth noting that any vertex deformation will not update normal vectors. So if models have high deformation, some values calculated of normal and tangents may be incorrect. – Iurii Selinnyi Feb 20 '18 at 16:28

1 Answers1

1

If I understood you correctly, you want to do specular lighting calculations inside your CG shader?

Unity gives you lighting info that you can access to do your own calculations if you wish:

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

However, the Doc seems outdated and some variables are wrong as its point out here:

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

once you use these variables you can calculate the lighting as you want

YNK
  • 869
  • 6
  • 12
  • Hi YNK, thank you for your reply and links. I already solved my problem. I added specular computation code together with the deforming code in CG shader. I hoped before that I can use ShaderLab for lightning and CG for deformation but it seems that's impossible. They are on the same CG code now and works fine. It feels like redundant since I have to add another specular computation code while Unity already have that feature. – acegs May 29 '13 at 07:39