繁体   English   中英

在顶点着色器中创建纹理并将其传递给Fragment,以实现Metal的涂抹效果?

[英]Creating texture in Vertex shader and passing to Fragment to achieve smudging brush with Metal?

我正在尝试为我的画笔项目添加污点效果。 为此,我想我需要从笔触坐标的开始处采样当前结果(在paintedTexture ),并将其传递给片段着色器。

我有一个顶点着色器,例如:

vertex VertexOut vertex_particle(
                 device Particle *particles [[buffer(0)]],
                 constant RenderParticleParameters *params [[buffer(1)]],
                 texture2d<half> imageTexture [[ texture(0) ]],
                 texture2d<half> paintedTexture [[ texture(1) ]],
                 uint instance [[instance_id]])
{
    VertexOut out;

绘制片段着色器,例如:

fragment half4 fragment_particle(VertexOut in [[ stage_in ]],
               half4 existingColor [[color(0)]],
               texture2d<half> brushTexture [[ texture(0) ]],
               float2 point [[ point_coord ]]) {

是否可以从paintedTexture创建裁剪的纹理并将其发送到片段着色器?

paintedTexture是已绘制到画布上的当前结果。 我想使用与画笔纹理相同的区域从paintedTexture创建一个新纹理, paintedTexture其传递给片段着色器。

片段着色器中的existingColor [[color(0)]]无用,因为它是当前颜色,而不是笔画开始时的颜色。 如果我使用现存的颜色,就像使用透明性(或基于将其与新颜色结合使用的数学方法的传输模式)。

如果我在错误的树上吠叫,那么关于如何使用Metal达到污迹效果的任何建议都可能是可接受的答案。

更新:我试图在用一个Texture2D VertexOut结构:

struct VertexOut {
    float4 position   [[ position ]];
    float  point_size [[ point_size ]];
    texture2d<half> paintedTexture;
}

但是它无法编译并显示以下错误:

vertex function has invalid return type 'VertexOut'

VertexOut结构中似乎也不可能有一个数组(虽然它不如纹理理想,但可能是前进的道路):

struct VertexOut {
    float4 position   [[ position ]];
    float  point_size [[ point_size ]];
    half4 paintedPixels[65536];
}

给我错误:

type 'VertexOut' is not valid for attribute 'stage_in'

着色器无法创建纹理。 他们可以填补现有的一个,但我认为这不是您想要或需要的。

我希望您可以将paintedTexture传递给片段着色器,并使用顶点着色器来记录从该纹理进行采样的位置。 因此,只需进行协调即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM