Unity3D Shader实现贴图切换效果
作者:星空不语 发布时间:2021-07-10 00:31:20
标签:Unity3D,Shader,贴图切换
本文实例为大家分享了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


猜你喜欢
- 前言最近面试时,面试官问了一个列表倒计时效果如何实现,然后脑袋突然懵的了O(∩_∩)O,现在记录一下。运行效果图实现思路实现方法主要有两个:
- 本文实例为大家分享了C语言实现学生成绩管理系统的具体代码,供大家参考,具体内容如下结构体版的学生成绩管理系统主要功能有按1 输入学生信息按2
- 程序如下:View Code /* * Hanoi塔游戏 问题描述: * 汉诺塔:汉诺塔(又称河内塔)问
- Java Benchmark 基准测试的实例详解import java.util.Arrays; import java.util.conc
- 图库在播放幻灯片时,按power键灭屏,然后再亮屏,会发现幻灯片继续在播放,没有显示keyguard。如何在亮屏后显示解锁界面。 修改方法是
- 为了保持类型的安全性,默认情况下 C# 是不支持指针的,但是如果使用 unsafe 关键字来修饰类或类中的成员,这样的类或类中成员就会被视为
- 一、数组的基本用法1.什么是数组数组:存储一组相同数据类型的数据的集合。2.定义数组 int[] :int类型数组 do
- JAVA多线程断点下载原理如图:代码如下:import java.io.BufferedReader; import java.io.Fil
- 加密解密本身并不是难事,问题是在何时去处理?定义一个过滤器,将请求和响应分别拦截下来进行处理也是一个办法,这种方式虽然粗暴,但是灵活,因为可
- 前言在分布式场景下为了保证数据最终一致性。在单进程的系统中,存在多个线程可以同时改变某个变量(可变共享变量)时,就需要对变量或代码块做同步(
- 相信大部分使用Intellij的同学都会遇到这个问题,即使项目使用了spring-boot-devtools,修改了类或者html、js等,
- 新建项目IDEA上方工具栏点击:文件->新建->模块此时的目录结构:需要在main文件夹下补全两个文件夹,点击main,右键-&
- 概述 Kotlin 是一种在 Java 虚拟机上运行的静态类型编程语言,被称之为 Android 世界的Swift,由 JetBr
- 下面给大家介绍C#使用ICSharpCode.SharpZipLib.dll进行文件的压缩与解压功能,具体代码如下所示:using Syst
- 本文实例为大家分享了Java NIO实现聊天功能的具体代码,供大家参考,具体内容如下server code : package c
- 一、下载Xxl-Job源代码并导入本地并运行Github地址:https://github.com/xuxueli/xxl-job中文文档地
- 这篇文章主要介绍了Java并发CopyOnWrite容器原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值
- 背景当一个项目分了很多模块,很多个服务的时候,一些公共的配置就需要统一管理了,于是就有了元数据驱动!简介什么是Calcite?是一款开源SQ
- 进行软件开发时,通常我们都喜欢使用较新版本的工具,但这里我为什么使
- SpringMvc代码jar包commons-fileuploadcommons-iospring-mvc.xml配置<bean id