unity 弹痕_UnityDecal——贴花方法总结

本文详细介绍了Unity中实现弹痕效果的贴花方法,通过顶点着色器和片段着色器的配合,利用深度纹理和法线纹理来重建世界位置并应用贴花。关键步骤包括屏幕坐标到世界坐标的转换、法线的处理以及贴花的裁剪。
摘要由CSDN通过智能技术生成

struct v2f

float4 pos : SV_POSITION;

half2 uv : TEXCOORD0;

float4 screenUV : TEXCOORD1;

float3 ray : TEXCOORD2;

half3 orientation : TEXCOORD3;

};

v2f vert (float3 v : POSITION)

v2f o;

o.pos = mul (UNITY_MATRIX_MVP, float4(v,1));

o.uv = v.xz+0.5;

o.screenUV = ComputeScreenPos (o.pos);

o.ray = mul (UNITY_MATRIX_MV, float4(v,1)).xyz * float3(-1,-1,1);

o.orientation = mul ((float3x3)unity_ObjectToWorld, float3(0,1,0));

return o;

CBUFFER_START(UnityPerCamera2)

// float4x4 _CameraToWorld;

CBUFFER_END

sampler2D _MainTex;

sampler2D_float _CameraDepthTexture;

sampler2D _NormalsCopy;

fixed4 frag(v2f i) : SV_Target

i.ray = i.ray * (_ProjectionParams.z / i.ray.z);

float2 uv = i.screenUV.xy / i.screenUV.w;

// read depth and reconstruct world position

float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, uv);

depth = Linear01Depth (depth);

float4 vpos = float4(i.ray * depth,1);

float3 wpos = mul (unity_CameraToWorld, vpos).xyz;

float3 opos = mul (unity_WorldToObject, float4(wpos,1)).xyz;

clip (float3(0.5,0.5,0.5) - abs(opos.xyz));

i.uv = opos.xz+0.5;

half3 normal = tex2D(_NormalsCopy, uv).rgb;

fixed3 wnormal = normal.rgb * 2.0 - 1.0;

clip (dot(wnormal, i.orientation) - 0.3);

fixed4 col = tex2D (_MainTex, i.uv);

if (col.a < 0.8)

col.a = 0;

return col;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值