Unity 溶解shader(通用)

2024-01-15 11:08
文章标签 通用 unity shader 溶解

本文主要是介绍Unity 溶解shader(通用),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

今天我写一个最简单的溶解shader,在标准surface基础上写

下面是新建的surface shader

Shader "Custom/dissolve" {Properties {_Color ("Color", Color) = (1,1,1,1)_MainTex ("Albedo (RGB)", 2D) = "white" {}_Glossiness ("Smoothness", Range(0,1)) = 0.5_Metallic ("Metallic", Range(0,1)) = 0.0}SubShader {Tags { "RenderType"="Opaque" }LOD 200CGPROGRAM// Physically based Standard lighting model, and enable shadows on all light types#pragma surface surf Standard fullforwardshadows// Use shader model 3.0 target, to get nicer looking lighting#pragma target 3.0sampler2D _MainTex;struct Input {float2 uv_MainTex;};half _Glossiness;half _Metallic;fixed4 _Color;// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.// #pragma instancing_options assumeuniformscalingUNITY_INSTANCING_BUFFER_START(Props)// put more per-instance properties hereUNITY_INSTANCING_BUFFER_END(Props)void surf (Input IN, inout SurfaceOutputStandard o) {// Albedo comes from a texture tinted by colorfixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;o.Albedo = c.rgb;// Metallic and smoothness come from slider variableso.Metallic = _Metallic;o.Smoothness = _Glossiness;o.Alpha = c.a;}ENDCG}FallBack "Diffuse"
}

溶解我们需要加参数,溶解图,溶解颜色,溶解进度

		_DissolveTex("dissolve tex",2D) = ""{}//溶解纹理,根据这张图的R值(因为是黑白图所以用rgb其中一个通道都可以)来做溶解判断,如果是彩色图自己去判断用哪个通道//其实就是用这张图的特征来实现溶解样式_DissolveColor("dissolve color",Color) = (0,0,0,1)//溶解颜色_DissolveProgress("dissolve Progress",Range(0,10)) = 0//溶解进度

下面是溶解的计算

原理是拿UV上面的像素颜色R和当前的进度做对比,如果进度大于像素颜色值就抛弃像素就不显示出来

这个我取颜色的R通道,因为是黑白图用哪个通道都可以

            //获取uv坐标上的像素颜色float dissolve_c_r = tex2D(_DissolveTex, IN.uv_MainTex).r;//计算进度(0,1)范围内float Progress = saturate(_DissolveProgress / 10);//如果进度大于像素颜色则抛弃这个像素if (Progress > dissolve_c_r){//抛弃像素discard;}//计算单个像素颜色插值速度float rate = Progress / dissolve_c_r;//原本颜色和溶解颜色插值c.rgb = lerp(c.rgb, _DissolveColor.rgb, rate);

下面是效果

用什么shader在颜色赋值之前加入溶解代码就能实现溶解,如果有描边那些就不好处理,可以搞个开关屏蔽描边再做溶解效果

下面是完整shader

Shader "Custom/dissolve" {Properties {_Color ("Color", Color) = (1,1,1,1)_MainTex ("Albedo (RGB)", 2D) = "white" {}_Glossiness ("Smoothness", Range(0,1)) = 0.5_Metallic ("Metallic", Range(0,1)) = 0.0_DissolveTex("dissolve tex",2D) = ""{}//溶解纹理,根据这张图的R值(因为是黑白图所以用rgb其中一个通道都可以)来做溶解判断,如果是彩色图自己去判断用哪个通道//其实就是用这张图的特征来实现溶解样式_DissolveColor("dissolve color",Color) = (0,0,0,1)//溶解颜色_DissolveProgress("dissolve Progress",Range(0,10)) = 0//溶解进度}SubShader {Tags { "RenderType"="Opaque" }LOD 200CGPROGRAM// Physically based Standard lighting model, and enable shadows on all light types#pragma surface surf Standard fullforwardshadows// Use shader model 3.0 target, to get nicer looking lighting#pragma target 3.0sampler2D _MainTex;sampler2D _DissolveTex;float4 _DissolveColor;float _DissolveProgress;struct Input {float2 uv_MainTex;};half _Glossiness;half _Metallic;fixed4 _Color;// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.// #pragma instancing_options assumeuniformscalingUNITY_INSTANCING_BUFFER_START(Props)// put more per-instance properties hereUNITY_INSTANCING_BUFFER_END(Props)void surf (Input IN, inout SurfaceOutputStandard o) {// Albedo comes from a texture tinted by colorfixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;//获取uv坐标上的像素颜色float dissolve_c_r = tex2D(_DissolveTex, IN.uv_MainTex).r;//计算进度(0,1)范围内float Progress = saturate(_DissolveProgress / 10);//如果进度大于像素颜色则抛弃这个像素if (Progress > dissolve_c_r){//抛弃像素discard;}//计算单个像素颜色插值速度float rate = Progress / dissolve_c_r;//原本颜色和溶解颜色插值c.rgb = lerp(c.rgb, _DissolveColor.rgb, rate);o.Albedo = c.rgb;// Metallic and smoothness come from slider variableso.Metallic = _Metallic;o.Smoothness = _Glossiness;o.Alpha = c.a;}ENDCG}FallBack "Diffuse"
}

上面的效果是最基础的,还是少了溶解边效果

下面是加边缘颜色,加了一个开始溶解参数和开始出现溶解边参数,有边缘溶解效果更好

Shader "Custom/dissolvePro" {Properties{_Color("Color", Color) = (1,1,1,1)_MainTex("Albedo (RGB)", 2D) = "white" {}_Glossiness("Smoothness", Range(0,1)) = 0.5_Metallic("Metallic", Range(0,1)) = 0.0_DissolveTex("dissolve tex",2D) = ""{}//溶解纹理,根据这张图的R值(因为是黑白图所以用rgb其中一个通道都可以)来做溶解判断,如果是彩色图自己去判断用哪个通道//其实就是用这张图的特征来实现溶解样式_DissolveColor("dissolve color",Color) = (0,0,0,1)//溶解颜色_EdgeColor("edge olor",Color) = (0,0,0,1)//边颜色,因为看起来是边在溶解,其实是溶解到某个程度换个颜色_DissolveProgress("dissolve Progress",Range(0,10)) = 0//溶解进度_DissolveStartParma("dissolve Start Parma",range(0,1)) = 0.7//开始溶解参数_DissolveEdgeParma("dissolve Edge Parma",range(0,1)) = 0.9//开始溶解边参数}SubShader{Tags{ "RenderType" = "Opaque" }LOD 200CGPROGRAM// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0sampler2D _MainTex;sampler2D _DissolveTex;float4 _DissolveColor;float4 _EdgeColor;float _DissolveProgress;float _DissolveStartParma;float _DissolveEdgeParma;struct Input {float2 uv_MainTex;};half _Glossiness;half _Metallic;fixed4 _Color;// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.// #pragma instancing_options assumeuniformscalingUNITY_INSTANCING_BUFFER_START(Props)// put more per-instance properties hereUNITY_INSTANCING_BUFFER_END(Props)void surf(Input IN, inout SurfaceOutputStandard o) {// Albedo comes from a texture tinted by colorfixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;//获取uv坐标上的像素颜色float dissolve_c_r = tex2D(_DissolveTex, IN.uv_MainTex).r;//计算进度(0,1)范围内float Progress = saturate(_DissolveProgress / 10);//如果进度大于像素颜色则抛弃这个像素if (Progress > dissolve_c_r){//抛弃像素discard;}//计算单个像素颜色插值速度float rate = Progress / dissolve_c_r;//开始溶解if (rate > _DissolveStartParma){c.rgb = lerp(c.rgb, _DissolveColor.rgb, rate);//开始显示溶解边颜色(看起来是便溶解),其实就是溶解到某个程度换一种颜色if (rate>_DissolveEdgeParma){c.rgb = lerp(c.rgb, _EdgeColor.rgb, rate);}}o.Albedo = c.rgb;// Metallic and smoothness come from slider variableso.Metallic = _Metallic;o.Smoothness = _Glossiness;o.Alpha = c.a;}ENDCG}FallBack "Diffuse"
}

下面是工程链接

链接:https://pan.baidu.com/s/1n-orx6V4yDbswEMC4mcmHg 
提取码:nj42 

这篇关于Unity 溶解shader(通用)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/608684

相关文章

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

C#和Unity中的中介者模式使用方式

《C#和Unity中的中介者模式使用方式》中介者模式通过中介者封装对象交互,降低耦合度,集中控制逻辑,适用于复杂系统组件交互场景,C#中可用事件、委托或MediatR实现,提升可维护性与灵活性... 目录C#中的中介者模式详解一、中介者模式的基本概念1. 定义2. 组成要素3. 模式结构二、中介者模式的特点

MyBatis-Plus通用中等、大量数据分批查询和处理方法

《MyBatis-Plus通用中等、大量数据分批查询和处理方法》文章介绍MyBatis-Plus分页查询处理,通过函数式接口与Lambda表达式实现通用逻辑,方法抽象但功能强大,建议扩展分批处理及流式... 目录函数式接口获取分页数据接口数据处理接口通用逻辑工具类使用方法简单查询自定义查询方法总结函数式接口

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

使用Java实现通用树形结构构建工具类

《使用Java实现通用树形结构构建工具类》这篇文章主要为大家详细介绍了如何使用Java实现通用树形结构构建工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录完整代码一、设计思想与核心功能二、核心实现原理1. 数据结构准备阶段2. 循环依赖检测算法3. 树形结构构建4. 搜索子

详解Python中通用工具类与异常处理

《详解Python中通用工具类与异常处理》在Python开发中,编写可重用的工具类和通用的异常处理机制是提高代码质量和开发效率的关键,本文将介绍如何将特定的异常类改写为更通用的ValidationEx... 目录1. 通用异常类:ValidationException2. 通用工具类:Utils3. 示例文

j2EE通用jar包的作用

原文:http://blog.sina.com.cn/s/blog_610901710101kx37.html IKIKAnalyzer3.2.8.jar // 分词器 ant-junit4.jar // ant junit antlr-2.7.6.jar // 没有此包,hibernate不会执行hql语句。并且会报NoClassDefFoundError: antlr

Unity Post Process Unity后处理学习日志

Unity Post Process Unity后处理学习日志 在现代游戏开发中,后处理(Post Processing)技术已经成为提升游戏画面质量的关键工具。Unity的后处理栈(Post Processing Stack)是一个强大的插件,它允许开发者为游戏场景添加各种视觉效果,如景深、色彩校正、辉光、模糊等。这些效果不仅能够增强游戏的视觉吸引力,还能帮助传达特定的情感和氛围。 文档

通用内存快照裁剪压缩库Tailor介绍及源码分析(一)

背景 我们知道内存快照是治理 OOM 问题及其他类型的内存问题的重要数据源,内存快照中保存了进程虚拟机的完整的堆内存数据,很多时候也是调查其他类型异常的重要参考。但是dump出来的堆转储文件.hprof往往很大,以 LargeHeap 应用为例,其 OOM 时的内存快照大小通常在512M左右,要有效的存储和获取都是一个问题。 线下拿到hprof文件相对容易,也可以预防OOM,但覆盖的场景十分有

SpringBoot中利用EasyExcel+aop实现一个通用Excel导出功能

一、结果展示 主要功能:可以根据前端传递的参数,导出指定列、指定行 1.1 案例一 前端页面 传递参数 {"excelName": "导出用户信息1725738666946","sheetName": "导出用户信息","fieldList": [{"fieldName": "userId","fieldDesc": "用户id"},{"fieldName": "age","fieldDe