Unity3D Shader实现贴图切换效果
作者:星空不语 时间:2021-07-10 00:31:20
本文实例为大家分享了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"
}
配合使用的脚本如下:
using System;
using System.Collections;
using UnityEngine;
namespace XM.Effect
{
public class SwapTexture : MonoBehaviour
{
public enum SwapDirection
{
Up,
Down,
Left,
Right,
Forward,
Back
}
public Shader _shader;//"XM/Effect/SwapTexture” shader
public SwapDirection _swapDir;//切换方向
public Renderer _target;//目标对象
[Range(0, 1)]
public float _speed;//速度
private Material _matOld;
private Material _matNew;
public void Swap(Texture tex, Action<bool> onComplete)
{
if (_matOld != null)
{
StopAllCoroutines();
if (null != onComplete)
{
onComplete(false);
}
_target.material = _matOld;
}
_matOld = _target.material;
_matNew = new Material(_shader);
_matNew.SetTexture("_MainTex", _target.material.GetTexture("_MainTex"));
_matNew.SetTexture("_TargetTex", tex);
_matNew.SetInt("_mode", (int)_swapDir);
_matNew.SetFloat("_SwapBlend", 0);
StartCoroutine("_StartChange", onComplete);
}
private IEnumerator _StartChange(Action<bool> onComplete)
{
float deltaVal = 0;
_target.material = _matNew;
Vector3 vtMin;
Vector3 vtMax;
float minVal = 0;
float maxVal = 0;
while (deltaVal != 1)
{
vtMin = _target.bounds.min;
vtMax = _target.bounds.max;
switch (_swapDir)
{
case SwapDirection.Up:
case SwapDirection.Down:
minVal = vtMin.y;
maxVal = vtMax.y;
break;
case SwapDirection.Left:
case SwapDirection.Right:
minVal = vtMin.x;
maxVal = vtMax.x;
break;
case SwapDirection.Forward:
case SwapDirection.Back:
minVal = vtMin.z;
maxVal = vtMax.z;
break;
}
minVal -= 0.01f;
maxVal += 0.01f;
_matNew.SetFloat("_SwapMin", minVal);
_matNew.SetFloat("_SwapMax", maxVal);
deltaVal = Mathf.Clamp01(deltaVal + _speed * Time.deltaTime);
_matNew.SetFloat("_SwapBlend", deltaVal);
yield return null;
}
_matOld.SetTexture("_MainTex", _matNew.GetTexture("_TargetTex"));
_target.material = _matOld;
_matNew = null;
_matOld = null;
if (null != onComplete)
{
onComplete(true);
}
}
public void OnDrawGizmos()
{
if (_target != null)
{
Gizmos.DrawWireCube(_target.bounds.center, _target.bounds.size);
Gizmos.DrawWireSphere(_target.bounds.min, 0.1f);
Gizmos.DrawWireSphere(_target.bounds.max, 0.1f);
}
}
//test
public Texture testTex;
private void OnGUI()
{
if (GUILayout.Button("SwapTexture"))
{
Swap(testTex, (t) =>
{
Debug.Log("Swap>" + t);
});
}
}
//
}
}
来源:https://blog.csdn.net/u012741077/article/details/53948802
标签:Unity3D,Shader,贴图切换
0
投稿
猜你喜欢
SpringBoot利用切面注解及反射实现事件监听功能
2022-09-25 16:55:00
spring boot微服务场景下apollo加载过程解析
2022-05-20 13:55:32
浅谈MyBatis通用Mapper实现原理
2022-11-18 18:45:16
SpringMVC利用dropzone组件实现图片上传
2022-01-19 00:37:00
java语言基础之标识符和命名规则详解
2023-04-21 16:50:18
Java数据类型分类与基本数据类型转换
2023-08-10 08:33:37
SpringCloud微服务架构升级汇总
2022-05-04 06:09:19
idea 打包maven项目忽略test文件的操作
2021-10-16 07:21:14
Android开发实现判断通知栏是否打开及前往设置页面的方法
2022-10-21 10:43:45
创建动态代理对象bean,并动态注入到spring容器中的操作
2021-09-04 01:02:43
Android Scroll实现弹性滑动_列表下拉弹性滑动的示例代码
2022-02-07 16:01:49
Android自定义弹框Dialog效果
2022-09-21 02:40:41
浅谈java封装
2023-05-05 22:48:20
java arrayList遍历的四种方法及Java中ArrayList类的用法
2023-11-17 17:49:55
Springboot项目打war包docker包找不到resource下静态资源的解决方案
2022-01-01 07:03:55
Java多文件以ZIP压缩包导出的实现方法
2023-10-08 14:05:20
Unity3D 计时器的实现代码(三种写法总结)
2022-02-10 08:51:08
Android中RecyclerView 滑动时图片加载的优化
2021-07-29 09:58:12
java 指定某个jdk版本方法
2023-10-20 08:07:28
java中dart类详细讲解
2022-01-23 13:01:13