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

相关文章

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

Unity协程搭配队列开发Tips弹窗模块

概述 在Unity游戏开发过程中,提示系统是提升用户体验的重要组成部分。一个设计良好的提示窗口不仅能及时传达信息给玩家,还应当做到不干扰游戏流程。本文将探讨如何使用Unity的协程(Coroutine)配合队列(Queue)数据结构来构建一个高效且可扩展的Tips弹窗模块。 技术模块介绍 1. Unity协程(Coroutines) 协程是Unity中的一种特殊函数类型,允许异步操作的实现

Unity 资源 之 Super Confetti FX:点亮项目的璀璨粒子之光

Unity 资源 之 Super Confetti FX:点亮项目的璀璨粒子之光 一,前言二,资源包内容三,免费获取资源包 一,前言 在创意的世界里,每一个细节都能决定一个项目的独特魅力。今天,要向大家介绍一款令人惊艳的粒子效果包 ——Super Confetti FX。 二,资源包内容 💥充满活力与动态,是 Super Confetti FX 最显著的标签。它宛如一位

Unity数据持久化 之 一个通过2进制读取Excel并存储的轮子(4)

本文仅作笔记学习和分享,不用做任何商业用途 本文包括但不限于unity官方手册,unity唐老狮等教程知识,如有不足还请斧正​​ Unity数据持久化 之 一个通过2进制读取Excel并存储的轮子(3)-CSDN博客  这节就是真正的存储数据了   理清一下思路: 1.存储路径并检查 //2进制文件类存储private static string Data_Binary_Pa

Unity Adressables 使用说明(一)概述

使用 Adressables 组织管理 Asset Addressables 包基于 Unity 的 AssetBundles 系统,并提供了一个用户界面来管理您的 AssetBundles。当您使一个资源可寻址(Addressable)时,您可以使用该资源的地址从任何地方加载它。无论资源是在本地应用程序中可用还是存储在远程内容分发网络上,Addressable 系统都会定位并返回该资源。 您

Unity Adressables 使用说明(六)加载(Load) Addressable Assets

【概述】Load Addressable Assets Addressables类提供了加载 Addressable assets 的方法。你可以一次加载一个资源或批量加载资源。为了识别要加载的资源,你需要向加载方法传递一个键或键列表。键可以是以下对象之一: Address:包含你分配给资源的地址的字符串。Label:包含分配给一个或多个资源的标签的字符串。AssetReference Obj

在Unity环境中使用UTF-8编码

为什么要讨论这个问题         为了避免乱码和更好的跨平台         我刚开始开发时是使用VS开发,Unity自身默认使用UTF-8 without BOM格式,但是在Unity中创建一个脚本,使用VS打开,VS自身默认使用GB2312(它应该是对应了你电脑的window版本默认选取了国标编码,或者是因为一些其他的原因)读取脚本,默认是看不到在VS中的编码格式,下面我介绍一种简单快