Warm tip: This article is reproduced from serverfault.com, please click

Custom Point Shader in Autodesk Forge Viewer behaves weirdly with orthograpic Camera

发布于 2020-11-30 14:42:00

I am using a PointCloud for fast rendering of sprites after this blog post. Everything works fine with the perspective camera. However, if I switch to the orthographic camera via viewer.navigation.toOrthographic() the points' sizes are not calculated correctly. Does anyone know what the issue is or where I might find some clue?

My vertex shader

#if NUM_CUTPLANES > 0
varying highp vec3 vWorldPosition;
#endif
attribute float mVisible;
attribute float mSize;
varying float vMVisible;
uniform float scale;
void main() {
    vMVisible = mVisible;
    
    #if NUM_CUTPLANES > 0
    vec4 _worldPosition = modelMatrix * vec4( position, 1.0 );
    vWorldPosition = _worldPosition.xyz;
    #endif

    vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);
    gl_Position = projectionMatrix * mvPosition;
    gl_PointSize = mSize * (scale / (-mvPosition.z) );
}

Zoomed out

Normal sized points

Zoomed in

Very large points

Zoomed in a little bit more

No Points shown anymore

Questioner
Dominik
Viewed
0
Petr Broz 2020-11-30 23:06:20

The problem is in the last few lines of the fragment shader:

vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);
gl_Position = projectionMatrix * mvPosition;
gl_PointSize = mSize * (scale / (-mvPosition.z) );

When using an orthographic projection instead of a perspective projection, the mvPosition (position transformed into the "normalized device coordinates") may have very different values, and so dividing the point size by mvPosition.z may yield unexpected results.

You may need to clamp the final point size by some constants (or by uniforms you provide from your JavaScript code), e.g.:

gl_PointSize = clamp(mSize * scale / -mvPosition.z, 10.0, 100.0);