美文网首页
Shader—双面渲染

Shader—双面渲染

作者: 李昀熹1112 | 来源:发表于2018-08-07 12:20 被阅读0次
    红旗飘飘

    1、双面渲染

    Shader " DoubleSideShader" {
                  Properties {
                   //正面5个参数
                        _Color ("Main Color", Color) = (1,1,1,1)
                         _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
                         _Shininess ("Shininess", Range (0.03, 1)) = 0.078125
                         _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
                        _BumpMap ("Normalmap", 2D) = "bump" {}
                    //反面拷贝 改名 也是5个
                        _BackColor ("Back Main Color", Color) = (1,1,1,1)
                        _BackSpecColor ("Back Specular Color", Color) = (0.5, 0.5, 0.5, 1)
                        _BackShininess ("Back Shininess", Range (0.03, 1)) = 0.078125
                        _BackMainTex ("Back Base (RGB) Gloss (A)", 2D) = "white" {}
                        _BackBumpMap ("Back Normalmap", 2D) = "bump" {}
                    }
                    SubShader {
                    Tags { "RenderType"="Opaque" }
                        LOD 400
                        Cull back
                    //开始渲染正面    
                    CGPROGRAM
                    //表明是surface渲染方式 主渲染程序是surf 光照模型是BLinnPhong
                    #pragma surface surf BlinnPhong              
                   sampler2D _MainTex;
                   sampler2D _BumpMap;
                    fixed4 _Color;
                    half _Shininess;
                    
                    struct Input {
                        float2 uv_MainTex;
                        float2 uv_BumpMap;
                    };            
                    void surf (Input IN, inout SurfaceOutput o) {
                        fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
                       o.Albedo = tex.rgb * _Color.rgb;
                        o.Gloss = tex.a;
                        o.Alpha = tex.a * _Color.a;
                        o.Specular = _Shininess;
                       o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));}
                    ENDCG      
                        Cull front
                    //开始渲染反面 其实和就是拷贝了一份正面渲染的代码 除了变量名要改    
                    CGPROGRAM
                 #pragma surface surf BlinnPhong 
                    sampler2D _BackMainTex;
                    sampler2D _BackBumpMap;
                    fixed4 _BackColor;
                    half _BackShininess;
                    struct Input 
                  {
                        float2 uv_BackMainTex;
                        float2 uv_BackBumpMap;
                  };
                    
                    void surf (Input IN, inout SurfaceOutput o) {
                         fixed4 tex = tex2D(_BackMainTex, IN.uv_BackMainTex);
                         o.Albedo = tex.rgb * _BackColor.rgb;
                       o.Gloss = tex.a;
                        o.Alpha = tex.a * _BackColor.a;
                         o.Specular = _BackShininess;
                         o.Normal = UnpackNormal(tex2D(_BackBumpMap, IN.uv_BackBumpMap));}
                     ENDCG
                    }
                     FallBack "Specular"
                    }
    

    2、旗帜飘扬

    / Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
    // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    
    Shader "Demo_Flag/Flag"  // 旗帜飘动
    {
        Properties
        {
            _MainTex("Main Tex", 2D) = "white" {}                         //主纹理
        _FlagColor("Flag Color", Color) = (1, 1, 1, 1)                //旗子颜色
            _Frequency("Frequency", float) = 5                            //波动频率
            _AmplitudeStrength("Amplitude Strength", float) = 5          // 振幅强度
            _InvWaveLength("Inverse Wave Length", float) = 1             //波长的倒数(_InvWaveLength越小,波长越大)
            _Fold("Fold", Range(0.0, 2.0)) = 0.5                         //旗帜褶皱程度
        }
            SubShader
        {
            // Need to disable batching because of the vertex animation
            Tags{ "DisableBatching" = "True" "RenderType" = "Opaque" }
    
            Pass
        {
            Tags{ "LightMode" = "ForwardBase" }
    
            Cull Off
    
            CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    
    #include "UnityCG.cginc"
    
            sampler2D _MainTex;
        float4 _MainTex_ST;
        float4 _FlagColor;
        float _Frequency;
        float _AmplitudeStrength;
        float _InvWaveLength;
        float _Fold;
    
        struct a2v
        {
            float4 vertex : POSITION;
            float4 texcoord : TEXCOORD0;
        };
    
        struct v2f
        {
            float4 pos : SV_POSITION;
            float2 uv : TEXCOORD0;
        };
    
        v2f vert(a2v v)
        {
            v2f o;
            o.uv = v.texcoord.xy;
            float4 offset;
            // 初始化顶点偏移量
            offset.xyzw = float4(0.0, 0.0, 0.0, 0.0);
            // 计算偏移之前的顶点位置
            float4 v_before = mul(unity_ObjectToWorld, v.vertex);
            // 我们只希望对顶点的 Y 方向进行偏移(正弦型函数y=Asin(ωx+φ)+b)
            offset.y = _AmplitudeStrength * sin(_Frequency * _Time.y + (v_before.x + v_before.y* _Fold) * _InvWaveLength) * o.uv.x;
    
            //我们只需要把偏移量添加到顶点位置上,再进行正常的顶点变换即可
            o.pos = UnityObjectToClipPos(v.vertex + offset);
    
            o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
            return o;
        }
    
        fixed4 frag(v2f i) : SV_Target
        {
            fixed4 col = tex2D(_MainTex, i.uv);
        col.rgb *= _FlagColor.rgb;
        return col;
        }
            ENDCG
        }
        }
            FallBack "VertexLit"
    }
    

    相关文章

      网友评论

          本文标题:Shader—双面渲染

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