Shader实现玻璃效果

 

一、类似镜子窗户似的玻璃表现

这是一张燥波图片

 

提供给玻璃的具有透明效果的,一般用黑白灰贴图,Alpha通道里有一种说法是黑透白不透,灰色是半透 。

Shader为


Shader "Mirrors/Transparent Bumped Specular Flat" {
Properties {
	_Transparency("Transparency", Range (0, 1)) = 1
	_Distortion ("Distortion", range (0,1)) = 0
	_Color ("Main Color", Color) = (1,1,1,1)
	_MainTex ("Base (RGB) TransGloss (A)", 2D) = "white" {}
	_BlendLevel("Main Material Blend Level",Range(0,1))=1
	_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 0)
	_Shininess ("Shininess", Range (0.01, 1)) = 0.078125
	_BumpMap ("Normalmap", 2D) = "bump" {}
	_Bumpness ("Bump Rate",Range(0,1))= 1
	_Ref ("For Mirror reflection,don't set it!", 2D) = "white" {}
}

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

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

CGPROGRAM
#pragma surface surf BlinnPhong
#pragma target 3.0
#pragma debug

sampler2D _MainTex;
sampler2D _BumpMap;
sampler2D _Ref;
fixed4 _Color;
half _BlendLevel;
half _Transparency;
half _Bumpness;
half _Shininess;
sampler2D _GrabTexture;
float4 _GrabTexture_TexelSize;
half _Distortion;



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 tex = tex2D(_MainTex, IN.uv_MainTex);
	
	float2 screenUV = IN.screenPos.xy / IN.screenPos.w;
	screenUV += nor.xy * _Distortion ;
	fixed4 ref = tex2D(_Ref, screenUV);
		
	float4 screenUV2 = IN.screenPos;
	#if UNITY_UV_STARTS_AT_TOP
	float scale = -1.0;
	#else
	float scale = 1.0;
	#endif
	screenUV2.y = (screenUV2.y - screenUV2.w*0.5)* scale+ screenUV2.w * 0.5;
	screenUV2.xy = screenUV2.xy / screenUV2.w;
	screenUV2.xy += nor.xy * _Distortion;
	
	fixed4 trans = tex2D(_GrabTexture,screenUV2.xy);
		
	o.Albedo = tex.rgb * _Color.rgb * _BlendLevel;
	o.Emission = lerp(ref.rgb,trans.rgb,_Transparency);
	o.Normal = nor.rgb * _Bumpness;
	o.Gloss = tex.a;
	o.Alpha = tex.a * _Color.a;
	o.Specular = _Shininess;	
}
ENDCG
}

FallBack "Transparent/VertexLit"
}

 

 

不需要法线贴图只调透明度参数就可以了

二 、水杯,高脚杯似的玻璃表现Shader


Shader "Mirrors/Transparent Specular Sphere" {
Properties {
	_Transparency("Transparency", Range (0, 1)) = 1
	_Distortion ("Distortion", range (0,30)) = 10
	_Color ("Main Color", Color) = (1,1,1,1)
	_MainTex ("Base (RGB) TransGloss (A)", 2D) = "white" {}
	_BlendLevel("Main Material Blend Level",Range(0,1))=1
	_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 0)
	_Shininess ("Shininess", Range (0.01, 1)) = 0.078125
	_Ref ("For Mirror reflection,don't set it!", 2D) = "white" {}
}

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

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

CGPROGRAM
#pragma surface surf BlinnPhong
#pragma target 3.0
#pragma debug

sampler2D _MainTex;
sampler2D _Ref;
fixed4 _Color;
half _BlendLevel;
half _Transparency;
half _Shininess;
sampler2D _GrabTexture;
float4 _GrabTexture_TexelSize;
half _Distortion;



struct Input {
	float2 uv_MainTex;
	float4 screenPos;
};


void surf (Input IN, inout SurfaceOutput o) {
	fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
	
	#if UNITY_UV_STARTS_AT_TOP
	float scale = -1.0;
	#else
	float scale = 1.0;
	#endif
	
	float4 screenUV = IN.screenPos;
	float2 offset = (_Distortion * o.Normal) ;
    screenUV.xy  += offset ;
    float3 ref = tex2Dproj( _Ref, screenUV);
	
	float4 screenUV2 = IN.screenPos;
	screenUV2.y = (screenUV2.y - screenUV2.w*0.5)* scale+ screenUV2.w * 0.5;
	offset = _Distortion * o.Normal;
    screenUV2.xy  += offset  ;
	float3 trans = tex2Dproj( _GrabTexture, screenUV2);
	
	o.Albedo = tex.rgb * _Color.rgb * _BlendLevel;
	o.Emission = lerp(ref.rgb,trans.rgb,_Transparency);
	o.Gloss = tex.a;
	o.Alpha = tex.a * _Color.a;
	o.Specular = _Shininess;	
}
ENDCG
}

FallBack "Transparent/VertexLit"
}

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值