TRANSFORM_TEX

套用_ST。

o.uv = TRANSFORM_TEX(v.uv, _MainTex);

定義:

// Transforms 2D UV by scale/bias property
#define TRANSFORM_TEX(tex,name) (tex.xy * name##_ST.xy + name##_ST.zw)

用世界座標的Mask

// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'

Shader "Unlit/UVtoWorldShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _NoiseTex("Noisy Texture",2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Transparent"   "RenderQueue"="Transparent" "RenderType"="TransparentCutout"}
       
        Blend SrcAlpha OneMinusSrcAlpha
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;      
                float4 vertex : SV_POSITION;
                float3 worldPos : TEXCOORD1;
                
            };

            sampler2D _MainTex;
            sampler2D _NoiseTex;
            float4 _MainTex_ST;
            float4 _NoiseTex_ST;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
                o.uv = TRANSFORM_TEX(v.uv, _MainTex); 
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);
                fixed4 noisy = tex2D(_NoiseTex , i.worldPos * _NoiseTex_ST.xy + _NoiseTex_ST.zw);

                fixed4 res= col;
                res.a= length( noisy.rgb);

                return res ;
            }
            ENDCG
        }
    }
}

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/8634ceb6-39c1-41fa-a539-69256cddd296/Untitled.png

問題:選全白的顏色會爆曝

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/89455c0d-9408-47ef-9027-9eeb54320152/Untitled.png

解決:加上saturate去clamp01

res.a= saturate( length( noisy.rgb));

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/6f6e178f-bcd4-4bcd-bea1-426a383c2cba/Untitled.png


Depth Buffer:

Shaders Case Study - No Man's Sky: Topographic Scanner