summaryrefslogtreecommitdiff
path: root/VRCSDK3Worlds/Assets/Editor/x64/Bakery/shaderSrc/ftTransformFarSphere.compute
blob: 163416f7b38e64baebc8c23b4c6f789be9520d98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#pragma kernel CSMain

RWStructuredBuffer<float3> verts;
Texture2D<float> _DepthTex;
SamplerState sampler_DepthTex;
float3 objectCenter;
float4 _InvProj0, _InvProj1, _InvProj2, _InvProj3;
float3 wnormal;
uint vertWidth;

float4 ComputeClipSpacePosition(float2 positionNDC, float deviceDepth)
{
    float4 positionCS = float4(positionNDC * 2.0 - 1.0, deviceDepth, 1.0);
    positionCS.y = -positionCS.y;
    return positionCS;
}

float3 ComputeViewSpacePosition(float2 positionNDC, float deviceDepth, float4x4 invProjMatrix)
{
    float4 positionCS  = ComputeClipSpacePosition(positionNDC, deviceDepth);
    float4 hpositionVS = mul(invProjMatrix, positionCS);
    return hpositionVS.xyz / hpositionVS.w;
}

float planeIntersect(float3 ro, float3 rd, float4 p)
{
    return -(dot(ro,p.xyz)+p.w)/dot(rd,p.xyz);
}

[numthreads(16,16,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
    if (id.x >= vertWidth) return;
    if (id.y >= vertWidth) return;

    int vertID = id.y * vertWidth + id.x;
    float3 lpos = verts[vertID];

    float4x4 invProjMatrix = float4x4(_InvProj0, _InvProj1, _InvProj2, _InvProj3);

    float2 uv = (id.xy) / (float)(vertWidth-1);
    uv.y = 1 - uv.y;

    float texelSize = 1.0f / vertWidth;

    uv = lerp(uv-texelSize*0.5, uv+texelSize*0.5, uv);

    float2 centerUV = uv;//floor(uv * (vertWidth-1)) / (vertWidth-1);

    float2 leftUV = centerUV - float2(texelSize, 0);
    float2 rightUV = centerUV + float2(texelSize, 0);
    float2 bottomUV = centerUV - float2(0, texelSize);
    float2 topUV = centerUV + float2(0, texelSize);

    float depth = _DepthTex.SampleLevel(sampler_DepthTex, centerUV, 0).r;

    //depth = -ComputeViewSpacePosition(uv * 2 - 1, depth, invProjMatrix).z;
    //float3 dir = normalize(lpos);
    //float3 worldPos = dir * planeIntersect(0, dir, float4(-wnormal.xyz, depth));

    float3 worldPos = ComputeViewSpacePosition(uv, depth, invProjMatrix) - objectCenter;

    verts[vertID] = worldPos;
}