Unity3DShader实现贴图切换效果-创新互联
本文实例为大家分享了shader实现基于世界坐标的贴图置换效果。

效果如下:

设置面板如下:

可在面板上设置切换方向,与切换对象,及其切换速度。
shader实现如下:
Shader "XM/Effect/SwapTexture" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_TargetTex ("Target Tex", 2D) = "white" {}//目标贴图
[KeywordEnum(Up, Down, Left, Right, Forward, Back)] _mode ("Mode", Int) = 0//切换方向
_SwapBlend ("Blend", Range(0,1)) = 0//0-1混合值
_SwapMin("Min Value", Float) = 0//最小世界坐标
_SwapMax("Max Value", Float) = 0//大世界坐标
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows vertex:vert
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
sampler2D _TargetTex;
struct Input {
float2 uv_MainTex;
float3 worldPos;
};
half _mode;
half _SwapBlend;
float _SwapMin;
float _SwapMax;
half _Glossiness;
half _Metallic;
fixed4 _Color;
void vert (inout appdata_full v, out Input o) {
UNITY_INITIALIZE_OUTPUT(Input,o);
}
void surf (Input IN, inout SurfaceOutputStandard o) {
half useTarget = 0;
float targetValue = 0;
switch(_mode)//模式选择
{
case 0://up
targetValue = (_SwapMax - _SwapMin) * _SwapBlend + _SwapMin;
useTarget = IN.worldPos.y > targetValue?0:1;
break;
case 1://down
targetValue = (_SwapMax - _SwapMin) * (1 - _SwapBlend) + _SwapMin;
useTarget = IN.worldPos.y < targetValue?0:1;
break;
case 2://left
targetValue = (_SwapMax - _SwapMin) * (1 - _SwapBlend) + _SwapMin;
useTarget = IN.worldPos.x < targetValue?0:1;
break;
case 3://right
targetValue = (_SwapMax - _SwapMin) * _SwapBlend + _SwapMin;
useTarget = IN.worldPos.x > targetValue?0:1;
break;
case 4://forward
targetValue = (_SwapMax - _SwapMin) * _SwapBlend + _SwapMin;
useTarget = IN.worldPos.z > targetValue?0:1;
break;
case 5://back
targetValue = (_SwapMax - _SwapMin) * (1 - _SwapBlend) + _SwapMin;
useTarget = IN.worldPos.z < targetValue?0:1;
break;
}
// Albedo comes from a texture tinted by color
fixed4 c;
if(useTarget == 1)
{
c = tex2D (_TargetTex, IN.uv_MainTex);
}
else
{
c = tex2D (_MainTex, IN.uv_MainTex);
}
c *= _Color;
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
本文题目:Unity3DShader实现贴图切换效果-创新互联
URL分享:http://www.scyingshan.cn/article/deccsj.html


咨询
建站咨询
