unity贴花实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
Shader "Decal"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _MixedColor("MixedColor",Color)=(1,1,1,1) //颜色混合
        _Alpha ("Alpha",Range(0,1.0)) = 1 //透明度
    }
    SubShader
    {
        Tags {
            "RenderType"="Opaque"
            "Queue" = "Geometry+1"
        }
        LOD 100
         
        ZWrite Off
        ZTest Off
        Cull Front        
         
        Blend SrcAlpha OneMinusSrcAlpha
         
        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;
                float4 screenPos : TEXCOORD1;
            };
 
            sampler2D _MainTex;
            float _Alpha;
            float4 _MainTex_ST;
            float4 _MixedColor;
            sampler2D _CameraDepthTexture;
 
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                 
                o.screenPos = ComputeScreenPos(o.vertex);
                return o;
            }
 
            float3 DepthToWorldPosition(float4 screenPos)
            {
                float depth = Linear01Depth(UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture,screenPos)));
                float4 ndcPos = (screenPos/screenPos.w) * 2 - 1;
                float3 clipPos = float3(ndcPos.x,ndcPos.y,1) * _ProjectionParams.z;
                float3 viewPos = mul(unity_CameraInvProjection,clipPos.xyzz).xyz * depth;
                float3 worldPos = mul(UNITY_MATRIX_I_V,float4(viewPos,1)).xyz;
                return worldPos;
            }
            //颜色混合
            float4 MixedColor(float4 a,float4 b)
            {
                return float4(a.r*b.r,a.g*b.g,a.b*b.b,a.a*b.a);
            }
            fixed4 frag (v2f i) : SV_Target
            {
                float3 worldPos = DepthToWorldPosition(i.screenPos);
                float4 localPos = mul(unity_WorldToObject,float4(worldPos,1.0));
                clip(float3(0.5,0.5,0.5) - abs(localPos.xyz));
                fixed2 decalUV = fixed2(localPos.x,localPos.z);
                decalUV = decalUV + 0.5;
                fixed4 color = tex2D(_MainTex,decalUV);
                fixed4 c=fixed4(color.r,color.g,color.b,color.a*_Alpha);
                return MixedColor(c,_MixedColor);
            }
            
            ENDCG
        }
    }
}

  

posted @   游戏鼻祖  阅读(45)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2017-10-23 unity 同一标签里内容拥有不同颜色
点击右上角即可分享
微信分享提示