gpt4 book ai didi

unity3d - 如何为两个图像之间的共享空间着色?

转载 作者:行者123 更新时间:2023-12-01 13:17:11 24 4
gpt4 key购买 nike

我的新游戏概念需要这个想法,但我自己甚至在统一论坛中都找不到解决方案。

请问有没有 Unity 开发人员可以帮助我?

EXAMPLE

谢谢

最佳答案

我用来实现这个结果的解决方案是使用 2 pass 模板着色器并将相机背景修改为纯色 - 白色。

着色器中有 channel (颜色是硬编码的)。然后只需使用此着色器创建 Material 并将其放置在网格上。

是的,这个版本只适用于正确形状的网格。因此,当您想要圆形或三角形时,可以从 3D 建模引擎导出网格,并在 Unity 中使用此着色器。

Shader "Custom/NewSurfaceShader"{
Properties {}
SubShader
{
Tags { "RenderType"="Geometry" }

Pass
{
Stencil
{
Comp always
Pass IncrSat
}

CGPROGRAM
#pragma vertex vert
#pragma fragment frag

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

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

sampler2D _MainTex;
float4 _MainTex_ST;

v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;

return o;
}

fixed4 frag (v2f i) : SV_Target
{
fixed4 col = fixed4(0, 0, 0, 1);
return col;
}
ENDCG
}

Pass
{
Stencil
{
Ref 1
Comp Less
}

CGPROGRAM
#pragma vertex vert
#pragma fragment frag

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

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

sampler2D _MainTex;
float4 _MainTex_ST;

v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;

return o;
}

fixed4 frag (v2f i) : SV_Target
{
fixed4 col = fixed4(1, 1, 1, 1);
return col;
}
ENDCG
}
}

This is the result


编辑:

刚刚升级了着色器,在此解决方案中它也适用于 Sprite 形状。您不再需要创建网格。唯一的缺点是现在它在 3 个着色器 channel 上。

    Shader "Unlit/NewUnlitShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100

Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag

struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 uv : TEXCOORD0;
};

struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
float2 uv : TEXCOORD0;
};

sampler2D _MainTex;

v2f vert (appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
o.color = v.color;

return o;
}

fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
col = col * i.color;

if(col.a < .5f)
{
discard;
}

return col;
}

ENDCG
}

Pass
{
Stencil
{
Comp always
Pass IncrSat
}

CGPROGRAM
#pragma vertex vert
#pragma fragment frag

struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 uv : TEXCOORD0;
};

struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
float2 uv : TEXCOORD0;
};

sampler2D _MainTex;

v2f vert (appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
o.color = v.color;

return o;
}

fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = fixed4(0, 0, 0, 1);
return col;
}
ENDCG
}

Pass
{
Stencil
{
Ref 1
Comp Less
}

CGPROGRAM
#pragma vertex vert
#pragma fragment frag

struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 uv : TEXCOORD0;
};

struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
float2 uv : TEXCOORD0;
};

sampler2D _MainTex;

v2f vert (appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
o.color = v.color;

return o;
}

fixed4 frag (v2f i) : SV_Target
{
fixed4 col = fixed4(1, 1, 1, 1);
return col;
}
ENDCG
}
}
}

enter image description here

关于unity3d - 如何为两个图像之间的共享空间着色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53762372/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com