Skip to content

WebGPU background has an unnecessary precision issue #32418

@SebastianTusk

Description

@SebastianTusk

Description

Having large scale scenes (in my case earth orbit scale) leads to background jittering. This can be mitigated by enabling Renderer.highPrecision. But this is unnecessary with a small change in the way the background is rendered.

Solution

The solution would be to ignore the camera position instead of moving the background mesh to the camera location. This would also avoid copying of the camera position in backgroundMesh.onBeforeRender.

Example code that can also be used as a drop-in instead of the default background.

const backgroundTexture = await new HDRLoader().loadAsync( "background.hdr" );
backgroundTexture.mapping = EquirectangularReflectionMapping;

const backgroundNode = texture(backgroundTexture, equirectUV());
const backgroundMeshNode = context( vec4( backgroundNode ).mul( backgroundIntensity ), {
    // @TODO: Add Texture2D support using node context
    getUV: () => backgroundRotation.mul( normalWorldGeometry ),
    getTextureLevel: () => backgroundBlurriness
} );

const positionView = modelViewMatrix.mul( vec4(positionLocal.xyz, 0) ).xyz;
const modelViewProjection = cameraProjectionMatrix.mul( vec4(positionView, 0) );
const viewProj = modelViewProjection.setZ( modelViewProjection.w );

const nodeMaterial = new NodeMaterial();
nodeMaterial.name = 'Background.material';
nodeMaterial.side = BackSide;
// nodeMaterial.depthTest = false;
nodeMaterial.depthWrite = false;
nodeMaterial.allowOverride = false;
nodeMaterial.fog = false;
nodeMaterial.lights = false;
nodeMaterial.vertexNode = viewProj;
nodeMaterial.colorNode = backgroundMeshNode;

const backgroundMesh = new Mesh( new SphereGeometry( 1, 32, 32 ), nodeMaterial );
backgroundMesh.frustumCulled = false;
backgroundMesh.name = 'Background.mesh';

/*backgroundMesh.onBeforeRender = function ( renderer, scene, camera ) {

    this.matrixWorld.copyPosition( camera.matrixWorld );

};*/

this.ar.scene.add(backgroundMesh);

Alternatives

Renderer.highPrecision

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions