【问题标题】:Sample textures from array into Voronoi cells将阵列中的纹理采样到 Voronoi 单元中
【发布时间】:2020-03-06 09:41:05
【问题描述】:

https://ibb.co/82WMNCt

我编写了一个片段着色器,它为每个 voronoi 单元采样不同的纹理。现在我循环遍历每个像素的所有位置,这是非常低效的。

关于如何优化它的任何提示?我需要使用 1000 个“单元”运行 2 x 1080p - 这是我的 box2d 的 cpu 最大值。

也许在顶点着色器中绘制单元格然后对其进行采样?我对此很陌生,任何提示表示赞赏!

奇怪的尺寸(*20 等),因为我的 box2d 世界很大,用于测试。

干杯,A

Texture2DArray texArray <string uiname="Texture Array";>;
Texture2D tex <string uiname="Texture";>;

int id;
int scale= 20;

float4x4 tWVP: WORLDVIEWPROJECTION;

SamplerState linearSampler : IMMUTABLE
{
    Filter = MIN_MAG_MIP_LINEAR;
    AddressU = Clamp;
    AddressV = Clamp;
};

cbuffer cbPerDraw : register( b0 )
{
    float4x4 tVP : LAYERVIEWPROJECTION; 
};

cbuffer cbPerObj : register( b1 )
{
    float4x4 tW : WORLD;
};

StructuredBuffer<float2> posBuffer;
StructuredBuffer<int> idBuffer;

struct vsInput
{
    float4 PosO : POSITION;
    float4 TexCd : TEXCOORD0;
};

struct psInput
{
    float4 PosWVP: SV_Position;
    float4 TexCd: TEXCOORD0;
};

psInput VS(vsInput In)
{
    return In;  
}


float4 PS(psInput In): SV_Target
{
    uint count, stride;
    posBuffer.GetDimensions(count, stride); 

    float minDist = 100;
    float2 uvRaw = In.TexCd.xy;
    float2 uv = ( uvRaw -.5) * 20;
    float4 col = 1;
    uint id;

    for (uint i=0; i<count; i++)
    {
        id = idBuffer[i];
        float2 p = posBuffer[i]*1;
        float d = length(uv-p) * .2;

        if (d < minDist)
        {
            minDist = d;
            col = texArray.SampleLevel(linearSampler, float3(uvRaw - p *0.05, i), 0);
        }       
    }   
    return col;      
}


technique10 Constant
{
    pass P0
    {
        SetVertexShader( CompileShader( vs_5_0, VS() ) );
        SetPixelShader( CompileShader( ps_5_0, PS() ) );
    }
}


  [1]: https://ibb.co/82WMNCt

【问题讨论】:

    标签: arrays shader textures fragment-shader voronoi


    【解决方案1】:

    生成Voronoi噪声最常见的优化是将纹理划分为一个网格,每个单元格有1个点,找到当前片段的网格单元格,然后只比较这个单元格和它的8个邻居的距离。所以基本上,你应该能够将你的点存储在一个二维数组中,然后通过使用单元大小划分和覆盖 UV 来找到单元索引。 Sebastian Lague 在他的云渲染视频中提到了这一点,你可以查看here,他还在 GitHub 上提供了源代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多