unity3d - Unity Shader: Deform a 3D model using vertex shader then apply specular using surface shader -
i need in applying specular shade in 3d model after deformed in cg vertex shader. don't know how in unity shader. searched in google , there no hits of looking for.
below current solution. 3d object 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" }
if understood correctly, want specular lighting calculations inside cg shader?
unity gives lighting info can access own calculations if wish:
http://docs.unity3d.com/documentation/components/sl-builtinvalues.html
however, doc seems outdated , variables wrong point out here:
http://answers.unity3d.com/questions/411114/shaderlab-builtin-lighting-properties-are-not-corr.html
once use these variables can calculate lighting want
Comments
Post a Comment