UnityShader源码2017---学习笔记与自我拓展037

源自Decal

只是单独这个shader,其实没有什么特别的,就是用DecalTex的A通道,去线性插值maintex和decaltex的rgb。

 

这里重点说一下这篇文章里的思路

这里先不说NormalCopy的由来,等写CB的时候在一起说吧。

先来推算一下computescreenpos的吧。权当给自己捋一遍。

inline float4 ComputeScreenPos(float4 pos) 
{
    float4 o = pos * 0.5f;
    o.xy = float2(o.x, o.y*_ProjectionParams.x) + o.w;
    o.zw = pos.zw;
    return o;
}
//--------------------float4 _ProjectionParams--------------
// x = 1 or -1 (-1 if projection is flipped)
// y = near plane
// z = far plane
// w = 1/far plane
//--------------------float4 _ProjectionParams--------------

_ProjectionParams这个参数的x分量存的是1或者-1,这个应该是跟OpenGL和DX有关吧。估计是跟UNITY_UV_STARTS_AT_TOP有异曲同工之妙,这些我都是瞎说的,没有去验证。

可以看到这个方法的返回值并没有除以齐次坐标。。这个要注意一下了。

忽略_ProjectionParams.x带来的影响。我们把公式整理一下

o.xy = o.pos.xy * 0.5 + o.pos.w * 0.5;

也就是o.xy = (o.pos.xy+o.pos.w)*0.5;

我们都知道,传进来的pos(o.pos)是齐次坐标系下的坐标,那么pos.xy的分量范围是[-w,w].

我们来假设一下,某点的屏幕坐标是screenPosX,screenPosY.屏幕的宽高width,height

那么,

我们先把x = o.pos.x转换到[0,1]的范围

newX = (x/w + 1)*0.5;

这个newX是不是可以理解为这个屏幕坐标在屏幕上的百分比,其实这个就是uv

screenPosX = newX * width

到这里o.xy的演算已经结束了

 

看一下vert里的计算,ray里存的是物体在viewSpace的坐标,为啥最后又乘了个(1,1,-1)呢,

Camera.worldToCameraMatrix的文档中有这样一句话:

Note that camera space matches OpenGL convention: camera's forward is the negative Z axis. This is different from Unity's convention, where forward is the positive Z axis.

也就是说,相机的transform的z是相机空间下的-z。

--------------------------------------这一段是自己的理解-------------------------------------------------

我们现在要获取到物体与地面的相交部分,我们先求出depth(远裁面(1)近裁面(0)),码中的depth就是了。

有了depth之后,我们只需要得到以远裁面为基准的viewspace的坐标就好了,设为newVPos

newVPos.x/farPlane = i.ray.x/i.ray.z * depth;

newVPos.y/farPlane = i.ray.y/i.ray.z * depth;

newVPos.z/farPlane = i.ray.z/i.ray.z * depth=1 * depth = depth;

整理一下就是

newVPOS = (i.ray/i.ray.z) * farPlane * depth

对应的就是码中的vpos

这也我们可以最终求出objectspace 的pos  ------ opos

--------------------------------------这一段是自己的理解-------------------------------------------------

这一段先删除,,,把我自己绕盟了,过几天在捋捋吧

 

Shader "ShaderStore/Decal/DecalShaderDiffuseOnly_阉割"
{
	Properties
	{
		_MainTex ("Diffuse", 2D) = "white" {}
	}
	SubShader
	{
		
		Tags{"Queue"="Transparent" "RenderType"="Transparent" "DisableBatching"="True"}
		Pass
		{
			ZWrite Off
			Blend SrcAlpha OneMinusSrcAlpha

			CGPROGRAM
			#pragma target 3.0
			#pragma vertex vert
			#pragma fragment frag
			
			#include "UnityCG.cginc"

			struct v2f
			{
				float4 pos : SV_POSITION;
				float4 screenUV : TEXCOORD1;
				float3 ray : TEXCOORD2;
			};

			v2f vert (float2 uv:TEXCOORD0,float3 v : POSITION)
			{
				v2f o;
				o.pos = UnityObjectToClipPos (float4(v,1));
				o.screenUV = ComputeScreenPos (o.pos);
				o.ray = mul (UNITY_MATRIX_MV, float4(v,1)).xyz * float3(1,1,-1);
				return o;
			}
			sampler2D _MainTex;
			sampler2D_float _CameraDepthTexture;
			fixed4 frag(v2f i) : SV_Target
			{
				i.ray = i.ray * (_ProjectionParams.z / i.ray.z);
				
				float2 uv = i.screenUV.xy / i.screenUV.w;
				float depth = tex2D(_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;
				
				//超出立方体的范围 就是大于0.5,clip掉
				clip (float3(0.5,0.5,0.5) - abs(opos.xyz));
				
				float2 newuv = opos.xz+0.5;
				fixed4 col = tex2D (_MainTex, newuv);
				return col;
			}
			ENDCG
		}		

	}

	Fallback Off
}

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值