当前位置:网站首页>HDRP Custom Pass Shader Get world coordinates and near clipping plane coordinates

HDRP Custom Pass Shader Get world coordinates and near clipping plane coordinates

2022-08-11 09:25:00 East Bear in the morning

获取世界坐标

posInputThe coordinates need to be added to the coordinates of the camera,honey juice pit…

float3 ws = _WorldSpaceCameraPos + posInput.positionWS;

在这里插入图片描述

Get the near clipping plane coordinates

用到函数 ComputeWorldSpacePosition().

float3 nearPositionWS = ComputeWorldSpacePosition(posInput.positionNDC, 1, UNITY_MATRIX_I_VP);
nearPositionWS += _WorldSpaceCameraPos; 

The internal implementation of this function is of course still usedNDCCoordinates push world space coordinates,But the parameters passed in here are screen coordinatesUV.(Although he named itNDC- -,But it's actually screen coordinatesUV,左下角(0, 0) , 右上角(1, 1))

另外,Here the near plane depth is 1,远平面是0.

This link details how to use itNDCCoordinates and depths derive world space coordinates.上善若水_2019-Reconstructs the coordinates of screen pixels in the world from depth information-简书

Implemented in vertex shader

注释掉引用 CustomPassCommon.hlsl 的那一行,把 CustomPassCommon.hlsl Copy the content inside to your shader,Unlock vertex shader.
在这里插入图片描述
Change the code in the vertex shader to look like this

    struct Varyings
    {
    
        float4 positionCS : SV_POSITION;
        // Add a register
        float3 positionWS : TEXCOORD0;
        UNITY_VERTEX_OUTPUT_STEREO
    };

    Varyings Vert(Attributes input)
    {
    
        Varyings output;
        UNITY_SETUP_INSTANCE_ID(input);
        UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
        // 注释掉
        // output.positionCS = GetFullScreenTriangleVertexPosition(input.vertexID, UNITY_RAW_FAR_CLIP_VALUE);
        
        // 改成
        float2 uv = float2((input.vertexID << 1) & 2, input.vertexID & 2);
        output.positionCS =  float4(uv * 2.0 - 1.0, UNITY_RAW_FAR_CLIP_VALUE, 1.0);
        
        float3 nearPositionWS = ComputeWorldSpacePosition(uv, 1, UNITY_MATRIX_I_VP);
        nearPositionWS += _WorldSpaceCameraPos; 
        output.positionWS = nearPositionWS; 
        return output;
    }
原网站

版权声明
本文为[East Bear in the morning]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/223/202208110907163468.html