1. 程式人生 > >Unity3D shader(15)——材質(玻璃)

Unity3D shader(15)——材質(玻璃)

如有更多問題,請qq254033230諮詢。

有了前面的基礎,就可以隨意用Unity Fragement Shader來實現自己想要的效果。

這裡就主要以玻璃為例。

首先實現玻璃的透明的特性;

裡面的Tags { "Queue" = "Transparent" }是為了實現透明的效果。Blend SrcAlpha OneMinusSrcAlpha為了顯示透明的物體不改變後面的物體的顯示效果。最後,顏色是一個思維陣列,透明度是顏色的最後一個元素,所以用col[3]來表示透明度。_AlphaVal控制透明度。如果想實現彩色玻璃的效果,只需要改變_MainTint和_AlphaVal的值。

Shader "Custom/FragmentShader" {
properties{
        _MainTint("Main Color", Color) = (1, 1, 1, 1)
        _AlphaVal("Alpha", Range(0, 1)) = 0.1
}
	SubShader {
	    Tags { "Queue" = "Transparent" }

		PASS{

		Blend SrcAlpha OneMinusSrcAlpha
				
		CGPROGRAM
		#pragma vertex vert  
        #pragma fragment frag

        #include "UnityCG.cginc"
        #include "Lighting.cginc"


        float4 _MainTint;
        float _AlphaVal;


        struct v2f{
        float4 pos:POSITION;
        float4 col:texcoord0;
        };  

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

        float4 frag(v2f IN):COLOR{
        float4 col;
        col=_LightColor0*_MainTint;
        col[3]*=_AlphaVal;
        return col;
        }
		ENDCG
		}
	}
	FallBack "Diffuse"

}


如果想玻璃效果更逼真,帶上漫反射的光,加上環境光,以及自身的顏色的組合。

Shader "Custom/FragmentShader" {
properties{
        _MainTint("Main Color", Color) = (1, 1, 1, 1)
        _AlphaVal("Alpha", Range(0, 1)) = 0.1
}
	SubShader {
	    Tags { "Queue" = "Transparent" }

		PASS{

		Blend SrcAlpha OneMinusSrcAlpha
				
		CGPROGRAM
		#pragma vertex vert  
        #pragma fragment frag

        #include "UnityCG.cginc"
        #include "Lighting.cginc"


        float4 _MainTint;
        float _AlphaVal;


        struct v2f{
        float4 pos:POSITION;
        float3 normal1:texcoord1;
        float4 vertex1:texcoord2;
        float4 col:texcoord0;
        };  

        v2f vert(appdata_base v){
        v2f o;
        o.pos=UnityObjectToClipPos(v.vertex);
        o.normal1=v.normal;
        o.vertex1=v.vertex;
        return o;
        }

        float4 frag(v2f IN):COLOR{
        float4 col= UNITY_LIGHTMODEL_AMBIENT;  
        float3 N=UnityObjectToWorldNormal(IN.normal1);  
        float3 L=normalize(WorldSpaceLightDir(IN.vertex1));  
        float diffuseScale=saturate(dot(N,L));
        col+=_LightColor0*diffuseScale+_MainTint; 
        col[3]*=_AlphaVal;
        return col;
        }
		ENDCG
		}
	}
	FallBack "Diffuse"

}

如果想呈現周圍景色,那麼需要加上立方體貼圖。用上立方體貼圖,造成反射的假象。
Shader "Custom/FragmentShader" {
properties{
        _MainTint("Main Color", Color) = (1, 1, 1, 1)
        _AlphaVal("Alpha", Range(0, 1)) = 0.1
        _CubeMap("CubeMap",CUBE)=""{}
}
	SubShader {
	    Tags { "Queue" = "Transparent" }

		PASS{

		Blend SrcAlpha OneMinusSrcAlpha
				
		CGPROGRAM
		#pragma vertex vert  
        #pragma fragment frag

        #include "UnityCG.cginc"
        #include "Lighting.cginc"

        samplerCUBE _CubeMap;
        float4 _MainTint;
        float _AlphaVal;


        struct v2f{
        float4 pos:POSITION;
        float3 normal1:texcoord1;
        float4 vertex1:texcoord2;
        float3 R:texcoord3;
        float4 col:texcoord0;

        };  

        v2f vert(appdata_base v){
        v2f o;
        o.pos=UnityObjectToClipPos(v.vertex);
        o.normal1=v.normal;
        o.vertex1=v.vertex;
        return o;
        }

        float4 frag(v2f IN):COLOR{
        float4 col= UNITY_LIGHTMODEL_AMBIENT;  
        float3 N=UnityObjectToWorldNormal(IN.normal1);  
        float3 L=normalize(WorldSpaceLightDir(IN.vertex1));  
        float diffuseScale=saturate(dot(N,L));
        col+=_LightColor0*diffuseScale+_MainTint;

        float3 N2=normalize(mul(IN.normal1,(float3x3)_World2Object));
        float3 V= -WorldSpaceViewDir(IN.vertex1);
        col+=texCUBE(_CubeMap,reflect(V,N2));

        col[3]*=_AlphaVal;
        return col;
        }
		ENDCG
		}
	}
	FallBack "Diffuse"

}