3

Unity 4 で使用するコンピュート シェーダーを作成しています。3D ノイズを取得しようとしています。

目標は、C# コードから計算シェーダーに多次元 float3 配列を取得することです。これは簡単な方法 (ある種の宣言を使用) で可能ですか、それとも Texture3D オブジェクトを使用してのみ達成できますか?

私は現在、個々の float3 ポイントで動作するシンプレックス ノイズの実装を持っており、単一の float -1 から 1 を出力します。計算シェーダー用にここにあるコードを移植しました。

Vector3[,,]これを拡張して、配列内の各 float3 ポイントにノイズ操作を適用することにより、float3 の 3D 配列 (C# で最も近い比較は になると思います) で動作するようにしたいと思います。

他にもいくつか試してみましたが、それらは奇妙に感じられ、並列アプローチを使用するポイントを完全に見逃しています。上記は、私がそれがどのように見えるべきかを想像するものです。

また、Scrawk の実装を頂点シェーダーとして機能させることもできました。Scrawk は、 Texture3D を使用してシェーダーに 3D float4 配列を取得しました。しかし、テクスチャからフロートを抽出できませんでした。コンピュート シェーダーも同様に機能しますか? テクスチャに依存していますか? テクスチャから値を取得することに関して、おそらく何かを見落としているでしょう。これは、このユーザーがこの投稿でデータを取得する方法のようです。私と同様の質問ですが、私が探しているものとはまったく異なります。

一般的にシェーダーは初めてで、コンピュート シェーダーとそのしくみについてかなり基本的なことが抜けているように感じます。目標は、(ご想像のとおり) コンピューティング シェーダー (またはこの種の作業に最適なシェーダー) を使用して、マーチング キューブを使用してノイズ生成とメッシュ計算を GPU で行うことです。

Constraints は Unity 4 の無料試用版です。

私が使用しているC#コードのスケルトンは次のとおりです。

    int volumeSize = 16; 
    compute.SetInt ("simplexSeed", 10); 

    // This will be a float[,,] array with our density values. 
    ComputeBuffer output = new ComputeBuffer (/*s ize goes here, no idea */, 16);
    compute.SetBuffer (compute.FindKernel ("CSMain"), "Output", output);  

    // Buffer filled with float3[,,] equivalent, what ever that is in C#. Also what is 'Stride'? 
    // Haven't found anything exactly clear. I think it's the size of basic datatype we're using in the buffer?
    ComputeBuffer voxelPositions = new ComputeBuffer (/* size goes here, no idea */, 16); 
    compute.SetBuffer (compute.FindKernel ("CSMain"), "VoxelPos", voxelPositions);    


    compute.Dispatch(0,16,16,16);
    float[,,] res = new float[volumeSize, volumeSize, volumeSize];

    output.GetData(res); // <=== populated with float density values

    MarchingCubes.DoStuff(res); // <=== The goal (Obviously not implemented yet)

そして、これが計算シェーダーです

#pragma kernel CSMain

uniform int simplexSeed;
RWStructuredBuffer<float3[,,]> VoxelPos;  // I know these won't work, but it's what I'm trying
RWStructuredBuffer<float[,,]> Output;     // to get in there. 

float simplexNoise(float3 input)
{
    /* ... A bunch of awesome stuff the pastebin guy did ...*/

    return noise;
}

/** A bunch of other awesome stuff to support the simplexNoise function **/
/* .... */

/* Here's the entry point, with my (supposedly) supplied input kicking things off */
[numthreads(16,16,16)] // <== Not sure if this thread count is correct? 
void CSMain (uint3 id : SV_DispatchThreadID)
{
    Output[id.xyz] = simplexNoise(VoxelPos.xyz); // Where the action starts.     
}
4

2 に答える 2