Unity Shader:Projective Texture Mapping

來源:互聯網
上載者:User

標籤:

Projective-Texture-Mapping是用於投影一個texture到一個物體上。通過設定一個攝像機在投影點上,擷取投影攝像機的 VP 矩陣,來擷取相對於紋理的uv座標。具體描述見:GPU 編程與CG 語言之陽春白雪下裡巴人 中的12.3節。

Shader "Custom/Projective-Texture-Mapping" {    Properties {        _Tex ("Proj Tex", 2D) = "white" {}        _Color ("Main Color", Color) = (1, 1, 1, 1)        _Ck ("Color Coef", float) = 1    }    SubShader {        Pass {            CGPROGRAM            #include "UnityCG.cginc"            #pragma vertex vert            #pragma fragment frag            sampler2D _Tex;            float4 _Color;            float _Ck;            float4x4 _texProjMat;            struct v2f {                float4 pos : POSITION;                float4 shadowUV : TEXCOORD0;                float3 normal : TEXCOORD1;                float3 light : TEXCOORD2;            };            v2f vert(appdata_base v) {                v2f o;                o.pos = mul(UNITY_MATRIX_MVP, v.vertex);                float4 worldPos = mul(_Object2World, v.vertex);                o.shadowUV = mul(_texProjMat, worldPos);                o.normal = v.normal;                o.light = ObjSpaceLightDir(v.vertex);                                return o;            }            float4 frag(v2f i) : COLOR {                float nl = dot(i.normal, i.light);                float2 uv = float2(i.shadowUV.x, i.shadowUV.y) / i.shadowUV.w * 0.5 + 0.5;                if (uv.x < 0 || uv.x > 1 || uv.y < 0 || uv.y > 1)                    return _Ck * _Color * max(0, nl);                float4 t = tex2D(_Tex, uv);                return _Ck * t * _Color * max(0, nl);            }            ENDCG        }    }     FallBack "Diffuse"}

效果如下:

該代碼並沒有考慮到物體的遮擋:plane面上處在cube後面的地區應該是沒有紋理的。要做到這個效果,要加上深度比較的過程。

Unity Shader:Projective Texture Mapping

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.