RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
Unity3DShader实现贴图切换效果-创新互联

本文实例为大家分享了shader实现基于世界坐标的贴图置换效果。

华州网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、自适应网站建设等网站项目制作,到程序开发,运营维护。创新互联于2013年创立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联

效果如下:

Unity3D Shader实现贴图切换效果

设置面板如下:

Unity3D 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实现贴图切换效果-创新互联
本文路径:http://scyingshan.cn/article/deccsj.html