UnityShader Demo01:冰块材质

简单版本效果如下

UnityShader Demo01:冰块材质_第1张图片
这里写图片描述

原理
使用法线贴图扭曲透明颜色贴图的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"
}
UnityShader Demo01:冰块材质_第2张图片
surface版本效果

surface版本目前还有问题,在编辑视图里面结果是对的,但是再camera显示不正确。暂时能力不够没找到方法修改

你可能感兴趣的:(UnityShader Demo01:冰块材质)