赞
踩
如有更多问题,请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"
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。