How to compute normal in PostProcessStage

Hi All,

I am trying to create model’s outline using PostProcessStage. I refers the article: Unity Outline Shader Tutorial - Roystan . So I need normal information for my purpose.

Currently, I can compute normal by use the function. The function is get from AmbientOcclusionGenerate.glsl: cesium/packages/engine/Source/Shaders/PostProcessStages/AmbientOcclusionGenerate.glsl at main · CesiumGS/cesium · GitHub

vec3 getNormalXEdge(vec2 positionSC)
    {
        // Find the 3D surface positions at adjacent screen pixels
        vec3 positionEC = pixelToEye(positionSC);
        vec3 positionLeft = pixelToEye(positionSC + vec2(-1.0, 1.0));
        vec3 positionRight = pixelToEye(positionSC + vec2(1.0, -1.0));
        vec3 positionUp = pixelToEye(positionSC + vec2(1.0, 1.0));
        vec3 positionDown = pixelToEye(positionSC + vec2(-1.0, -1.0));

        // Compute potential tangent vectors
        vec3 dx0 = positionEC - positionLeft;
        vec3 dx1 = positionRight - positionEC;
        vec3 dy0 = positionEC - positionDown;
        vec3 dy1 = positionUp - positionEC;

        // The shorter tangent is more likely to be on the same surface
        vec3 dx = magnitude(dx0) < magnitude(dx1) ? dx0 : dx1;
        vec3 dy = magnitude(dy0) < magnitude(dy1) ? dy0 : dy1;

        return normalize(cross(dx, dy));
    }

However, the output is not good because the accuracy of normal is not good. I am finding the way to compute normal accurately or the way to pass the normal information from custom shader stage to post process stage. Do you have any idea ? Thanks a lot!

This is my current result:

Hi @Hung, can you explain what your screenshot is showing? I don’t understand how that picture shows an inaccuracy in the normals. How is this result different from what you expected?

A Sandcastle link would help us investigate further.

Thanks!