UnityShader Demo01:冰塊材質(zhì)

簡單版本效果如下

這里寫圖片描述

原理
使用法線貼圖扭曲透明顏色貼圖的uv值
Shader 代碼1

Shader"PengLu/normal/iceTrans" {
Properties {
    _Color ("Main Color", Color) = (1,1,1,1)
    _MainTex ("BaseTex ", 2D) = "white" {}
    _BumpMap ("Normalmap", 2D) = "bump" {}
    _BumpAmt ("Distortion", range (0,2)) = 0.1

}

SubShader {
    Tags { "Queue"="Transparent""RenderType"="Opaque" }
    ZWrite off



CGPROGRAM
#pragma surface surfLambert nolightmap nodirlightmap alpha:blend

 sampler2D _BumpMap;
 sampler2D _MainTex;
 float4 _Color;
 float _BumpAmt;

struct Input {
    float2 uv_MainTex;
    float2 uv_BumpMap;
};


void surf (Input IN,inout SurfaceOutput o) {
    fixed3 nor = UnpackNormal (tex2D(_BumpMap, IN.uv_BumpMap));
    fixed4 trans =tex2D(_MainTex,IN.uv_MainTex+nor.xy*_BumpAmt)*_Color;

    o.Albedo = trans.rgb;
    o.Alpha =trans.a;
    o.Emission = trans; 
}
ENDCG
}

FallBack"Transparent/VertexLit"
}

surface版本 shader代碼:

Shader "PengLu/Custom/iceRefrationSurf" {
Properties {
    _Color ("Main Color", Color) = (1,1,1,1)
    _MainTex ("BaseTex", 2D) = "white" {}
    _BumpMap ("Normalmap", 2D) = "bump" {}
    _BumpAmt ("Distortion", range (0,1)) = 0.12
}

SubShader {
    Tags { "Queue"="Transparent" "RenderType"="Opaque" }
     ZWrite off
     Lighting off

    GrabPass {                          
            Name "BASE"
            Tags { "LightMode" = "Always" }
        }

CGPROGRAM
#pragma surface surf Lambert nolightmap nodirlightmap
#pragma target 3.0
#pragma debug

float4 _Color;
sampler2D _MainTex;
sampler2D _BumpMap;
sampler2D _GrabTexture;
float _BumpAmt;



struct Input {
    float2 uv_MainTex;
    float2 uv_BumpMap;
    float4 screenPos;
};


void surf (Input IN, inout SurfaceOutput o) {
    fixed3 nor = UnpackNormal (tex2D(_BumpMap, IN.uv_BumpMap));
    fixed4 col = tex2D(_MainTex,IN.uv_MainTex);
    float4 screenUV2 = IN.screenPos;
    screenUV2.xy = screenUV2.xy / screenUV2.w;
    screenUV2.xy += nor.xy * _BumpAmt;

    fixed4 trans = tex2D(_GrabTexture,screenUV2.xy)*_Color;
    trans*=col; 
    o.Albedo = trans.rgb;
    o.Emission = trans.rgb;
;   
}                                                                                                                                                                                                                                                                                                   
ENDCG
}

FallBack "Transparent/VertexLit"
}
surface版本效果

surface版本目前還有問題,在編輯視圖里面結(jié)果是對的,但是再camera顯示不正確。暫時能力不夠沒找到方法修改

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容