KITASENJU DESIGN BLOG

memo, html, javascript, unity

Depthの位置取得

#pragma kernel MainCS

//#include "UnityCG.cginc"
#include "./CGINC/SimplexNoise3Db.cginc"
#include "./StencilUV.hlsl"//

// スレッドグループのスレッドサイズ
#define ThreadBlockSize 64//256

// data
struct CubeData
{
    // 座標
    float3 position;
    float3 velocity;
    float4 color;
    float3 basePos;
    float2 uv;
    float time;
};

RWStructuredBuffer<CubeData> _CubeDataBuffer;//_DokabenDataBuffer;
RWStructuredBuffer<float3> _Result;//_DokabenDataBuffer;

//float _Time;

Texture2D<float4> _DepthTex;
Texture2D<float4> _StencilTex;
Texture2D<float4> _ColorTex;
float4 _DepthTexSize;
float4 _StencilTexSize;
float4 _ColorTexSize;

//float4  _Positions[1000];
float _Near;
float _Far;
float4x4 _InvProjMat;
float4x4 _InvViewMat;

float _VelocityRatio;

float _Duration;
float _GlobalIntensity;
float _Time;

float rand(float3 co)
{
    return frac(sin(dot(co.xyz, float3(12.9898, 78.233, 45.5432))) * 43758.5453);
}
            
float rand(float2 co)
{
    return frac(sin(dot(co.xy, float2(12.9898, 78.233))) * 43758.5453);
}

//色を渡して、発生させる

[numthreads(ThreadBlockSize, 1, 1)]
void MainCS(uint3 id : SV_DispatchThreadID)
{
    const unsigned int index = id.x;

    float2 uvv = float2(0,0);
    float hasPixel = 0.0;
        
    //100回思考する
    for( int i = 0; i<1000; i++ ){

        uvv.x = rand(float2(i,_Time*100));
        uvv.y = rand(float2(i+1000,_Time*100));
        //発生する場所を探す
        if( _StencilTex[ GetStencilUV(uvv) * _StencilTexSize.xy ].x > 0.5 ){
            hasPixel += 1.0;
            break;
        }

    }


    if( hasPixel > 0){

        float depthCol = (_DepthTex[ GetStencilUV(uvv) * _DepthTexSize.xy ].x);
        
        //発生位置
        float3 ScreenPos = float3(
            uvv.x,
            uvv.y,
            depthCol
        );

        float n = _Near;//near
        float f = _Far;//far
        
        float w = ScreenPos.z;
        float z = w * (2*(w-n)/(f-n)-1);
        float2 xy = w * (2*ScreenPos.xy-1);
        float4 clipPos = float4(xy,z,w);

        float4 camPos = mul( _InvProjMat, clipPos );//カメラ座標
        camPos.w = 1;
        float4 worldPos = mul(_InvViewMat, camPos);//ワールド座標

        //data.position = worldPos.xyz;
        
        _Result[index%1000] = worldPos.xyz;

    }
        


}
"FOOTER"