void final(Input IN, SurfaceOutput o, inout fixed4 color)
void final(Input IN, SurfaceOutputStandard o, inout fixed4 color)
void final(Input IN, SurfaceOutputStandardSpecular o, inout fixed4 color)
插入函数
光照模型
顶点和最终颜色修改
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#pragma surface surf Mao fullforwardshadows vertex:vert finalcolor:final //重要定义中的最后两个就是设定两个函数
。。。。。。
voidvert(inout appdata_full v) { }
voidfinal(Input IN, SurfaceOutputStandard o, inout fixed4 color) { }
插入位置
以新建的SurfaceShader为标准,顶点修改和最终颜色插入到它下面,自定义光照模型在它上面
1 2 3 4 5 6 7 8 9
//大致上要将顶点修改和最终颜色修改加入到这个位置
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader. // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing. // #pragma instancing_options assumeuniformscaling UNITY_INSTANCING_BUFFER_START(Props) // put more per-instance properties here UNITY_INSTANCING_BUFFER_END(Props)
halfasview Pass half-direction vector into the lighting function instead of view-direction. Half-direction will be computed and normalized per vertex. This is faster, but not entirely correct.
CGPROGRAM // Physically based Standard lighting model, and enable shadows on all light types #pragma surface surf BlinnPhong fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting #pragma target 3.0
//surfaceShader中不需要声明贴图的ST sampler2D _MainTex;
structInput { float2 uv_MainTex; };
half _Gloss; half _Specular; fixed4 _Color;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader. // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing. // #pragma instancing_options assumeuniformscaling UNITY_INSTANCING_BUFFER_START(Props) // put more per-instance properties here UNITY_INSTANCING_BUFFER_END(Props)
voidsurf(Input IN, inout SurfaceOutput o) { // Albedo comes from a texture tinted by color fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color; o.Albedo = c.rgb; // Metallic and smoothness come from slider variables o.Specular = _Specular; o.Gloss = _Gloss; o.Alpha = c.a; } ENDCG } FallBack "Diffuse" }
法线贴图采样
唯一的代码只需要在Surf函数中输入以下代码就行
Normal在使用法线贴图的时候就代表贴图法线
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
voidsurf(Input IN, inout SurfaceOutputStandard o) { // Albedo comes from a texture tinted by color fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color; o.Albedo = c.rgb; // Metallic and smoothness come from slider variables o.Metallic = _Metallic; o.Smoothness = _Glossiness; //法线贴图 float3 normal = UnpackNormal(tex2D(_BumpMap,IN.uv_BumpMap)); normal.xy *= _BumpScale; o.Normal = normal; o.Alpha = c.a; }
最终颜色控制
Properties控制
1
_FinalColor("FinalColor",Color) = (1,1,1,1)
关键 pragma设置
1
#pragma surface surf Standard fullforwardshadows finalcolor:final
函数
1 2 3 4
voidfinal(Input IN, SurfaceOutputStandard o, inout fixed4 color) { color *= _FinalColor; }
CGPROGRAM // Physically based Standard lighting model, and enable shadows on all light types #pragma surface surf Toon fullforwardshadows nolightmap finalcolor:final
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader. // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing. // #pragma instancing_options assumeuniformscaling UNITY_INSTANCING_BUFFER_START(Props) // put more per-instance properties here UNITY_INSTANCING_BUFFER_END(Props)
voidsurf(Input IN, inout SurfaceOutputStandardSpecular o) { // Albedo comes from a texture tinted by color fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color; o.Albedo = c.rgb; // Metallic and smoothness come from slider variables o.Smoothness = _Smoothness; o.Normal = UnpackNormal(tex2D(_Normal,IN.uv_MainTex)); //现在使用的x的速度是最慢的,不过自己加上一个系数进行控制就没什么影响 float2 uvFlow = IN.uv_MainTex * _Time.x * _FireSpeed; //遮罩颜色 和 火焰贴图颜色 和 变化强度 o.Emission = (tex2D(_Mask,IN.uv_MainTex) * tex2D(_Fire,uvFlow) * (_FirePower *(_SinTime.w + _FireLightChangeSpeed))).rgb; o.Specular = tex2D(_Specular,IN.uv_MainTex).rgb; o.Alpha = c.a; } ENDCG } FallBack "Diffuse" }
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader. // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing. // #pragma instancing_options assumeuniformscaling UNITY_INSTANCING_BUFFER_START(Props) // put more per-instance properties here UNITY_INSTANCING_BUFFER_END(Props)
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader. // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing. // #pragma instancing_options assumeuniformscaling UNITY_INSTANCING_BUFFER_START(Props) // put more per-instance properties here UNITY_INSTANCING_BUFFER_END(Props)
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader. // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing. // #pragma instancing_options assumeuniformscaling UNITY_INSTANCING_BUFFER_START(Props) // put more per-instance properties here UNITY_INSTANCING_BUFFER_END(Props)
voidsurf(Input IN, inout SurfaceOutputStandard o) { // Albedo comes from a texture tinted by color fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
CGPROGRAM // Physically based Standard lighting model, and enable shadows on all light types //启动alphatest并禁止接受阴影的,但是在alpha的前提下投射阴影 //noshadow 禁止接受投影 //addShadow 在透明度测试的前提下投射阴影 #pragma surface surf Standard alphatest:_Cutoff noshadow addshadow
CGPROGRAM // Physically based Standard lighting model, and enable shadows on all light types //透明度混合宏,干掉阴影,加上混合,关闭接受阴影 #pragma surface surf Standard alpha:blend noshadow
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader. // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing. // #pragma instancing_options assumeuniformscaling UNITY_INSTANCING_BUFFER_START(Props) // put more per-instance properties here UNITY_INSTANCING_BUFFER_END(Props)
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader. // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing. // #pragma instancing_options assumeuniformscaling UNITY_INSTANCING_BUFFER_START(Props) // put more per-instance properties here UNITY_INSTANCING_BUFFER_END(Props)