码迷,mamicode.com
首页 > 其他好文 > 详细

Shader 之 顶点变形

时间:2015-09-18 11:49:54      阅读:659      评论:0      收藏:0      [点我收藏+]

标签:

可以使3D物体通过顶点变形弯曲,常见于跑酷游戏的跑道。可向左、右、上、下弯曲。

Shader "Custom/VertexColorCurved" {
    Properties {
        // Shader需要的6个参数
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _QOffset ("Offset", Vector) = (0,0,0,0)
        _Dist ("Distance", Float) = 100.0
        
        _BrightnessAmount ("Brightness Amount", Float) = 1.0  
        _SaturationAmount ("Saturation Amount", Float) = 1.0  
        _ContrastAmount ("Contrast Amount", Float) = 1.0
    }
    
    SubShader {
        Tags { "Queue" = "Transparent"}
        Pass
        {
            
            Blend SrcAlpha OneMinusSrcAlpha 
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            sampler2D _MainTex;
            
            float4 _QOffset;
            float _Dist;
            
            fixed _BrightnessAmount;  
            fixed _SaturationAmount;  
            fixed _ContrastAmount;  
            float _OpacityAmount;
            
            struct v2f {
                float4 pos : SV_POSITION;
                float4 uv : TEXCOORD0;
                float3 viewDir : TEXCOORD1;
                fixed4 color : COLOR;
            };

            v2f vert (appdata_full v)
            {
                   v2f o;
               
                   float4 vPos = mul (UNITY_MATRIX_MV, v.vertex);
                float zOff = vPos.z/_Dist;
                   vPos += _QOffset*zOff*zOff;
                   o.pos = mul (UNITY_MATRIX_P, vPos);
                   o.uv = v.texcoord;
               
           //o.color.rgb = v.color.rgb;  // 取顶点色
               
                   return o;
            }
            
            float3 ContrastSaturationBrightness (float3 color, float brt, float sat, float con) {  
                // Increase or decrease these values to  
                // adjust r, g and b color channels separately  
                float avgLumR = 0.5;  
                float avgLumG = 0.5;  
                float avgLumB = 0.5;  
                  
                // Luminance coefficients for getting luminance from the image  
                float3 LuminanceCoeff = float3 (0.2125, 0.7154, 0.0721);  
                  
                // Operation for brightmess  
                float3 avgLumin = float3 (avgLumR, avgLumG, avgLumB);  
                float3 brtColor = color * brt;  
                float intensityf = dot (brtColor, LuminanceCoeff);  
                float3 intensity = float3 (intensityf, intensityf, intensityf);  
                  
                // Operation for saturation  
                float3 satColor = lerp (intensity, brtColor, sat);  
                  
                // Operation for contrast  
                float3 conColor = lerp (avgLumin, satColor, con);  
                  
                return conColor;  
            }  

            half4 frag (v2f i) : COLOR0
            {
                fixed4 renderTex = tex2D(_MainTex, i.uv);  
      
//                  // Apply vertex color
//                  renderTex.rgb *= i.color.rgb;  // 顶点色和贴图混合
                  
                // Apply the brightness, saturation, contrast operations  
                renderTex.rgb = ContrastSaturationBrightness (renderTex.rgb, _BrightnessAmount, _SaturationAmount, _ContrastAmount);
                  
                return renderTex;
            }

            ENDCG
        }
    }
    
    FallBack "Diffuse"
}

 

Shader 之 顶点变形

标签:

原文地址:http://www.cnblogs.com/jeason1997/p/4818631.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!