Unity3d Shaderlab Cloth Coloring Device
Cloth shaders are the shaders we use in virtual reality often. This article is to complete a relatively simple cloth shader.
Create a new Shader,material,interactivecloth "cloth". The completed code is as follows
Shader"91ygame/clothshader"{Properties {//parameters;_maintint ("Base Color", Color) = (1,1,1,1) _bumpmap ("Normal Map", 2D) ="Bump"{} _detailbump ("Detail Normal", 2D) =""{} _detailtex ("Fabric Weave", 2D) =""{} _fresnelcolor ("Fresnel Color", Color) = (1,1,1,1) _fresnelpower ("Fresnel Power", Range (0.01,Ten))=3_rimpower ("Rim Falloff", Range (0.01,Ten))=3_specintesity ("Specular Intensity", Range (0.01,1))=0.3_specwidth ("Specular Width", Range (0.01,1))=0.2} subshader {Tags {"Rendertype"="Opaque"} LOD $Cgprogram#pragmaSurface Surf Velvetcloth#pragmaTarget 3.0//variables;sampler2d _bumpmap; Sampler2d _detailbump; Sampler2d _detailtex; FLOAT4 _maintint; FLOAT4 _fresnelcolor; float_fresnelpower; float_rimpower; float_specintesity; float_specwidth; //enter the structure body; structInput {float2 uv_bumpmap; FLOAT2 Uv_detailbump; FLOAT2 Uv_detailtex; }; Inline fixed4 lightingvelvetcloth (surfaceoutput s,fixed3 lightdir, Fixed3 Viewdir,fixedatten) { //light vector;Viewdir =normalize (Viewdir); Lightdir=normalize (Lightdir); Half3 Halfvec= Normalize (lightdir+Viewdir); fixedNdotl = Max (0.01, Dot (s.normal,lightdir)); //Calculate high light; floatNdoth = Max (0.01, Dot (S.normal,halfvec)); floatSpec = POW (ndoth,s.specular* -)*S.gloss; //The closer the line of sight is to the surface of the cloth, the more light on the back of the fiber absorbs and the stronger the highlight. floatHdotv = POW (1-max (0, Dot (halfvec,viewdir)), _fresnelpower); floatNdote = POW (1-max (0, Dot (s.normal,viewdir)), _rimpower); floatFinalspecmask = ndote*Hdotv; //final color;fixed4 C; C.rgb= (S.ALBEDO*NDOTL*_LIGHTCOLOR0.RGB) + (spec* (Finalspecmask*_fresnelcolor)) * (atten*2); C.A=1; returnC; } voidSurf (Input in, InOut surfaceoutput o) {half4 c=tex2d (_detailtex, In.uv_detailtex); //extracting normals;Fixed3 normals =Unpacknormal (tex2d (_bumpmap,in.uv_bumpmap)). RGB; Fixed3 detailnormals=Unpacknormal (tex2d (_detailbump,in.uv_detailbump)). RGB; Fixed3 finalnormals= FLOAT3 (normals.x+detailnormals.x,normals.y+detailnormals.y,normals.z+detailnormals.z); O.normal=normalize (finalnormals); O.specular=_specwidth; O.gloss=_specintesity; O.albedo= c.rgb*_maintint; O.alpha=C.A; } ENDCG} FallBack"Diffuse"}
Return to unity and set the variable. The final effect is as follows:
In the above implementation, we combine two normal maps with different tile rates. Calculates a new normal vector.
So use Unpacknormal to extract normal vectors from the change normal map to produce a new normal map. The final vector is then normalized using normalize, which prevents the normal map from appearing cluttered.
Finally, the results of Fresnel calculation and high-light calculation are combined to create the fiber effect of the cloth.
Unity3d Shaderlab Cloth Coloring Device