当前位置:网站首页>UnityShader——MaterialCapture材质捕捉效果 (翡翠斧头)
UnityShader——MaterialCapture材质捕捉效果 (翡翠斧头)
2022-07-03 15:22:00 【ۓ明哲ڪ】
声明:本文为个人笔记,用于学习研究使用非商用,内容为个人研究及综合整理所得,若有违规,请联系,违规必改。
UnityShader学习目录
前言
Unity2019.3.6
AmplifyShaderEditor
一、实现原理
贴图跟随摄像机移动
二、效果及源码展示
1.光球效果
效果描述:
通过相机空间,把贴图uv跟随相机移动.
效果图如下:
AmplifyShaderEditor节点图如下:
2.翡翠斧头效果
效果描述
在效果一的基础上叠加了渐变贴图,又叠加了一次matcap贴图
效果图如下:
AmplifyShaderEditor节点图如下:
参数设置如下图:
代码如下:
Shader "Unlit/MatCap_Code"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {
}
_MatcapTex("MatcapTex",2D)="white"{
}//渐变贴图
_RampTex("RampTex",2D)="white"{
}
_MatcapAddTex("MatcapAddTex",2D)="white"{
}
_MatcapIntensity("Matcap Intensity",Float) = 1.0
_MatcapAddIntensity("MatcapAdd Intensity",Float)=1.0
}
SubShader
{
Tags {
"RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata//模型数据
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal:NORMAL;
};
struct v2f//自定义数据 顶点的输出,片元的输入
{
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
float3 worldNormal : TEXCOORD1;
float3 worldPos:TEXCOORD2;
};
sampler2D _MainTex;
sampler2D _MatcapTex;
sampler2D _RampTex;
sampler2D _MatcapAddTex;
float4 _MainTex_ST;
float _MatcapIntensity;
float _MatcapAddIntensity;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
float3 worldNormal=mul(float4(v.normal,0.0),unity_WorldToObject);//可适用非等比缩放而不出错.
//float3 worldNormal=UnityObjectToWorldNormal(v.normal);//通用,多数时候可替换上行代码
o.worldNormal=worldNormal;
o.worldPos=mul(unity_ObjectToWorld,v.vertex).xyz;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
half3 worldNormal= i.worldNormal;
//material capture MainTex
half3 viewSpaceNormal=mul(UNITY_MATRIX_V,float4(worldNormal,0.0)).xyz; //相机空间下法相向量
half2 matcapUV=(viewSpaceNormal.xy+float2(1.0,1.0))*0.5;
half4 matcapColor=tex2D(_MatcapTex,matcapUV) *_MatcapIntensity;
half4 diffuseColor = tex2D(_MainTex, i.uv);
//fresnel 渐变映射
// 计算世界空间视图方向
half3 worldViewDir=normalize(UnityWorldSpaceViewDir(i.worldPos));
half ndotv=saturate(dot(i.worldNormal,worldViewDir));
half fresnel=1.0-ndotv;
half2 rampUV=half2(fresnel,0.5);
half4 rampColor=tex2D(_RampTex,rampUV);
half4 matcapAddColor=tex2D(_MatcapAddTex,matcapUV)*_MatcapAddIntensity;
half4 combined_color= diffuseColor*matcapColor * rampColor+matcapAddColor;
return combined_color;
}
ENDCG
}
}
}
后处理:
相机上添加色调映射,让颜色更鲜艳一些.
注意:相机上也要添加上对应层级,不然不显示
总结
保持饥饿,保持愚蠢.
这世界唯一能够相信的就是你付出的努力和你走过的路.
边栏推荐
- Qt常用语句备忘
- 驱动与应用程序通信
- 【云原生训练营】模块七 Kubernetes 控制平面组件:调度器与控制器
- Digital image processing -- popular understanding of corrosion and expansion
- 求字符串函数和长度不受限制的字符串函数的详解
- 【Transformer】入门篇-哈佛Harvard NLP的原作者在2018年初以逐行实现的形式呈现了论文The Annotated Transformer
- Enable multi-threaded download of chrome and edge browsers
- Using notepad++ to build an arbitrary language development environment
- Calibre LVL
- 使用JMeter对WebService进行压力测试
猜你喜欢

如何使用 @NotNull等注解校验 并全局异常处理

高并发下之redis锁优化实战

Summary of JVM knowledge points

【Transformer】入门篇-哈佛Harvard NLP的原作者在2018年初以逐行实现的形式呈现了论文The Annotated Transformer

Jvm-06-execution engine

Matplotlib drawing label cannot display Chinese problems

Detailed pointer advanced 2

函数栈帧的创建和销毁

What is machine reading comprehension? What are the applications? Finally someone made it clear
![[Yu Yue education] scientific computing and MATLAB language reference materials of Central South University](/img/83/922efb4f88843f1b7feaccf2b515b9.jpg)
[Yu Yue education] scientific computing and MATLAB language reference materials of Central South University
随机推荐
Enable multi-threaded download of chrome and edge browsers
秒杀系统1-登录功能
Chapter 04_ Logical architecture
Mysql报错:[ERROR] mysqld: File ‘./mysql-bin.010228‘ not found (Errcode: 2 “No such file or directory“)
[daily training] 395 Longest substring with at least k repeated characters
SQL server installation location cannot be changed
Jvm-03-runtime data area PC, stack, local method stack
Use of Tex editor
秒杀系统2-Redis解决分布式Session问题
The first character of leetcode sword offer that only appears once (12)
Redis lock Optimization Practice issued by gaobingfa
mysql innodb 存储引擎的特性—行锁剖析
在MapReduce中利用MultipleOutputs输出多个文件
Incluxdb2 buckets create database
Leetcode the smallest number of the rotation array of the offer of the sword (11)
QT common sentence notes
Using multipleoutputs to output multiple files in MapReduce
Digital image processing -- popular understanding of corrosion and expansion
Seckill system 2 redis solves the problem of distributed session
Kubernetes - YAML文件解读