【Unity Shader】反射

cpp 复制代码
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "Custom/Reflection"{
	Properties{
		_Color("Color Tint", Color) = (1,1,1,1)
		_ReflectColor("Reflection Color", Color) = (1,1,1,1)//控制反射颜色
		_ReflectAmount("Reflect Amount", Range(0, 1)) = 1//控制反射程度
		_Cubemap("Reflection Cubemap", Cube) = "_Skybox"{}//用于模拟反射的环境映射纹理
	}
	SubShader{
		//Opaque意味渲染不透明对象
		Tags{"RenderType" = "Opaque" "Queue" = "Geometry"}
		Pass{
			Tags{"LightMode" = "ForwardBase"}
			CGPROGRAM

			#pragma multi_compile_fwdbase;	//前向渲染
			#pragma vertex vert
			#pragma fragment frag
			#include "Lighting.cginc"
			#include "AutoLight.cginc"

			fixed4 _Color;
			fixed4 _ReflectColor;
			fixed _ReflectAmount;
			samplerCUBE _Cubemap;

			struct a2v{
				float4 vertex : POSITION;
				float3 normal : NORMAL;
			};

			struct v2f{
				float4 pos : SV_POSITION;
				float3 worldPos : TEXCOORD0;
				float3 worldNormal : TEXCOORD1;
				fixed3 worldViewDir : TEXCOORD2;
				fixed3 worldRefl : TEXCOORD3;
				SHADOW_COORDS(4)
			};

			v2f vert(a2v v){
				v2f o;
				o.pos = UnityObjectToClipPos(v.vertex);
				o.worldNormal = UnityObjectToWorldNormal(v.normal);
				o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
				o.worldViewDir = UnityWorldSpaceViewDir(o.worldPos);
				//o.worldViewDir是物体朝向视角的向量,取负号表示从视角方向看向物体的方向。
				o.worldRefl = reflect(-o.worldViewDir, o.worldNormal);

				TRANSFER_SHADOW(o);
				return o;
			};

			fixed4 frag(v2f i) : SV_Target{
				fixed3 worldNormal = normalize(i.worldNormal);
				fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
				fixed3 worldViewDir = normalize(i.worldViewDir);

				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;	//环境光颜色
				fixed3 diffuse = _LightColor0.rgb * _Color.rgb * max(0, dot(worldNormal, worldLightDir));
				//texCUBE(_Cubemap, i.worldRefl).rgb:这是从立方体贴图(_Cubemap)中根据反射向量 i.worldRefl 采样出的颜色值。
				//_ReflectColor.rgb:是材质的反射颜色。用于调节反射光的颜色强度和色调。这个值通常由材质属性指定。
				fixed3 reflection = texCUBE(_Cubemap, i.worldRefl).rgb * _ReflectColor.rgb;		//反射颜色
				//Unity 着色器中的一个宏,用于计算光的衰减(Attenuation)
				//atten:这是用来存储光衰减值的变量, i:这是包含片元(像素)数据的结构体,它用于提供片元的位置信息给衰减计算。
				//i.worldPos:这是当前片元的世界空间位置,用来计算光源与片元之间的距离,从而决定光的衰减。
				UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
				//_ReflectAmount:控制漫反射光和反射光之间的混合比例。
				// 当 _ReflectAmount 靠近 0 时,lerp 倾向于选择 diffuse(漫反射光)。
				// 当 _ReflectAmount 靠近 1 时,lerp 倾向于选择 reflection(反射光)。
				fixed3 color = ambient + lerp(diffuse, reflection, _ReflectAmount) * atten;
				//这行代码将颜色 color(RGB 值)和不透明度 1.0(A 值)组合成一个 fixed4 类型的向量,并返回给渲染管线
				return fixed4(color, 1.0);
			}

			ENDCG
		}
	}
	FallBack "Reflective/VertexLit"
}
相关推荐
那个村的李富贵18 小时前
Unity自适应文本提示框:从原理到实战
unity·游戏引擎
HonestGoat18 小时前
Unity3d之鼠标光标
unity
WarPigs18 小时前
Unity人物翻越功能
unity·游戏引擎
游乐码19 小时前
Unity基础(四)向量相关
游戏·unity·游戏引擎
VT LI20 小时前
Cocos2d-x 引擎架构全面深度解析:从底层渲染到上层交互的系统性技术全景
游戏引擎·cocos·引擎架构
Kurisu57521 小时前
探灵直播2026最新官方正版免费下载 一键转存 永久更新 (看到速转存 资源随时走丢)
游戏·游戏引擎·游戏程序·动画·关卡设计
神码编程1 天前
【Unity】MiniGame编辑器小游戏(十五)中国象棋局域网对战【Chinese Chess】(上)
unity·编辑器·游戏引擎·小游戏
伽蓝_游戏1 天前
第四章:AssetBundle 核心机制与文件结构
unity·c#·游戏引擎·游戏程序
郝学胜-神的一滴1 天前
中级OpenGL教程 006:高光反射原理与 Shader 实现
c++·unity·godot·图形渲染·three.js·opengl·unreal
神码编程1 天前
【Unity】MiniGame编辑器小游戏(十六)中国象棋局域网对战【Chinese Chess】(下)
unity·编辑器·游戏引擎·小游戏