Shader

作者: 虫小白 | 来源:发表于2022-02-16 20:06 被阅读0次
一个不受光照影响的白色物体
Shader "white" {
        SubShader{
            Tags { "Queue" = "Geometry" "RenderType" = "Opaque" }

            pass {
                CGPROGRAM

                #pragma vertex vert
                #pragma fragment frag

                #include "UnityCG.cginc"

                struct a2v {
                    float4 vertex : POSITION;
                };

                struct v2f {
                    float4 pos : SV_POSITION;
                };

                v2f vert(a2v v) {
                    v2f o;
                    o.pos = UnityObjectToClipPos(v.vertex);
                    return o;
                }

                fixed4 frag(v2f i) : SV_Target {
                    return fixed4(1.0,1.0,1.0,1.0);
                }

                ENDCG
            }
        }

        FallBack "Diffuse"
}
一个不受光照影响的贴图物体(uv计算方法)
Shader "white" {
        Properties{
            _MainTex("贴图",2D)="white"
        }
        SubShader{
            Tags { "Queue" = "Geometry" "RenderType" = "Opaque" }

            pass {
                CGPROGRAM

                #pragma vertex vert
                #pragma fragment frag
                #include "UnityCG.cginc"

                sampler2D _MainTex;
                float4 _MainTex_ST;

                struct a2v {
                    float4 vertex : POSITION;
                    float4 texcoord:TEXCOORD0;
                };

                struct v2f {
                    float4 pos : SV_POSITION;
                    float2 uv:TEXCOORD0;
                };

                v2f vert(a2v v) {
                    v2f o;
                    o.pos = UnityObjectToClipPos(v.vertex);
                    o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
                    return o;
                }

                fixed4 frag(v2f i) : SV_Target{
                    fixed3 texColor = tex2D(_MainTex,i.uv).rgb;
                    return fixed4(texColor,1.0);
                }

                ENDCG
            }
        }

        FallBack "Diffuse"
}
基础的漫反射材质(顶点着色器)
Shader "white" {
        SubShader{
            Tags { "Queue" = "Geometry" "RenderType" = "Opaque" }

            pass {

                Tags{"Lightmode"="ForwardBase"}
                CGPROGRAM

                #pragma vertex vert
                #pragma fragment frag
                #include "UnityCG.cginc"
                #include "Lighting.cginc"

                struct a2v {
                    float4 vertex : POSITION;
                    float4 normal:NORMAL;
                };

                struct v2f {
                    float4 pos : SV_POSITION;
                    fixed3 color : COLOR;
                };

                v2f vert(a2v v) {
                    v2f o;
                    o.pos = UnityObjectToClipPos(v.vertex);//顶点着色器函数的基本功能
                    
                    float3 worldNormal = UnityObjectToWorldNormal(v.normal);//保证法线和光线在同一个空间(世界空间)下计算
                    float3 worldLight = normalize(_WorldSpaceLightPos0.xyz);//_WorldSpaceLightPos0:指向光源
                    fixed3 diffuse = saturate(dot(worldNormal, worldLight));//只受光源方向影响
                    //diffuse = _LightColor0.rgb * diffuse;//受到光源强度影响。_LightColor0:场景中的光源
                    //diffuse = _Diffuse * diffuse;//受到自身颜色影响。_Diffuse:自身颜色
                    //diffuse = UNITY_LIGHTMODEL_AMBIENT.xyz+diffuse;//受到环境光影响。UNITY_LIGHTMODEL_AMBIENT:环境光

                    o.color = diffuse;

                    return o;
                }

                fixed4 frag(v2f i) : SV_Target{
                    return fixed4(i.color,1.0);
                }

                ENDCG
            }
        }

        FallBack "Diffuse"
}
基础高光模型(顶点着色器,单位光照)
Shader "white" {
        Properties{
            _Gloss("光泽度",float)=1
        }
        SubShader{
            Tags { "Queue" = "Geometry" "RenderType" = "Opaque" }

            pass {

                Tags{"Lightmode" = "ForwardBase"}
                CGPROGRAM

                #pragma vertex vert
                #pragma fragment frag
                #include "UnityCG.cginc"
                #include "Lighting.cginc"

                float _Gloss;

                struct a2v {
                    float4 vertex : POSITION;
                    float4 normal:NORMAL;
                };

                struct v2f {
                    float4 pos : SV_POSITION;
                    fixed3 color : COLOR;
                };

                v2f vert(a2v v) {
                    v2f o;
                    o.pos = UnityObjectToClipPos(v.vertex);
                    
                    float3 worldNormal = UnityObjectToWorldNormal(v.normal);
                    float3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
                    fixed3 reflectDir = normalize(reflect(-worldLight, worldNormal));//世界空间中的反射方向
                    fixed3 viewDir = WorldSpaceViewDir(v.vertex);//世界空间中的观察方向
                    fixed3 specular = pow(saturate(dot(reflectDir, viewDir)), _Gloss);//高光颜色
                    //specular = _LightColor0.rgb * specular;//受到光源颜色和强度影响
                    //specular = _Specular.rgb * specular;//调整高光颜色和强度
                    o.color = specular;

                    return o;
                }

                fixed4 frag(v2f i) : SV_Target{
                    return fixed4(i.color,1.0);
                }

                ENDCG
            }
        }

        FallBack "Diffuse"
}

相关文章

网友评论

      本文标题:Shader

      本文链接:https://www.haomeiwen.com/subject/edsdlrtx.html