Skip to content

PostproductionRenderer: edges, AO and gloss passes ignore local clipping planes (Clipper.localClippingPlanes = true) #838

Open

Description

@Chaosnico9000

Package: @thatopen/components-front 3.6.0 (v3.6.0 @ bd53d29), PostproductionRenderer in PostproductionMode.COMPOSER (the default)

With Clipper.localClippingPlanes = true, a section plane is kept out of renderer.three.clippingPlanes (setPlane) and only reaches the fragments materials through material.clippingPlanes (applyClippingToFragmentMaterial). Three composer passes draw the model with a material of their own, and none of these materials gets the local planes. So these passes see the model uncut:

  1. Edges. EdgeDetectionPass swaps every tile's material for _overrideMaterial (setMaterialToMesh), or sets it as scene.overrideMaterial in GLOBAL mode (L299-L305). The material is built with clipping: true and the clipping chunks (L93-L156), but its clippingPlanes is never set.
  2. AO. AOPass extends three's GTAOPass, which renders normals and depth with its own normalMaterial (a MeshNormalMaterial) as scene.overrideMaterial. The AO is computed from that depth, so the occlusion of the removed part falls on the cut face.
  3. Gloss. GlossPass renders with glossOverrideMaterial, next to the comment "clippingPlanes would need to be set separately if needed" (L97-L98).

What the user sees depends on the style (style setter):

  • PEN: no base pass, so the cut disappears completely. The edges of the whole model are drawn.
  • COLOR_PEN, PEN_SHADOWS, COLOR_SHADOWS, COLOR_PEN_SHADOWS: the colour pass is cut, but edges and AO of the removed part are drawn over the cut face.
  • COLOR with glossEnabled: the gloss of the removed part is drawn over the cut.

With global planes (localClippingPlanes = false, the default) all three passes are cut, because three applies renderer.clippingPlanes to every material. The outline pass already handles local planes: SimpleOutlinePass.syncLocalClippingPlanes copies them onto its mask materials.

Going by the code, PostproductionMode.DEFERRED is not affected: its capture draws the surface materials themselves.

Reproduction

Observed in Chromium with three 0.184.0 on components-front 3.4.3, using a PEN style and a *_SHADOWS style. On 3.6.0 I checked it with the script below. It runs the published 3.6.0 bundle under bun, with a renderer stub that logs which material each pass draws a fragments tile with. EdgeDetectionPass and AOPass are not exported, so the script copies dist/index.js and appends one line, export { fi as __EdgeDetectionPass, pi as __AOPass }; (the minified names in 3.6.0).

repro.mjs
import * as THREE from "three";
import { GlossPass } from "@thatopen/components-front";
import { __EdgeDetectionPass as EdgeDetectionPass, __AOPass as AOPass } from "./front-3.6.0-exposed.mjs";

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera();

// What Clipper does in local mode: the plane stays out of three.clippingPlanes and
// the fragments materials point at the renderer's component-side list instead.
const plane = new THREE.Plane(new THREE.Vector3(0, -1, 0), 1);
plane.isLocal = true;
const componentPlanes = [plane];
const fragmentMaterial = new THREE.MeshLambertMaterial();
fragmentMaterial.clippingPlanes = componentPlanes;
const tile = new THREE.Mesh(new THREE.BoxGeometry(), [fragmentMaterial]);
scene.add(tile);

const fragments = {
  initialized: true,
  core: { models: { list: new Map([["model", { tiles: new Map([[0, tile]]) }]]), materials: { list: new Map() } } },
};
const world = { scene: { three: scene }, camera: { three: camera } };

function recordingRenderer(label) {
  const target = {
    clippingPlanes: [], // three.clippingPlanes: the local plane is filtered out of it
    localClippingEnabled: true,
    autoClear: true,
    getClearColor: (c) => c.set(0x000000),
    getClearAlpha: () => 1,
    getRenderTarget: () => null,
    render(obj) {
      if (obj !== scene) return;
      const mats = [].concat(scene.overrideMaterial ?? tile.material);
      for (const m of mats) {
        console.log(`${label.padEnd(10)} draws the tile with ${m.type.padEnd(18)} clippingPlanes: ${m.clippingPlanes?.length ?? 0}`);
      }
    },
  };
  return new Proxy(target, { get: (t, k) => (k in t ? t[k] : () => {}) });
}

const rt = () => new THREE.WebGLRenderTarget(4, 4);

recordingRenderer("base pass").render(scene);

const edges = new EdgeDetectionPass({ currentWorld: world, three: recordingRenderer("edges") }, fragments);
edges.render(recordingRenderer("edges"), rt(), rt());

const ao = new AOPass(fragments, scene, camera, 4, 4);
ao.render(recordingRenderer("AO"), rt(), rt());

const gloss = new GlossPass(new THREE.Vector2(4, 4), world);
gloss.render(recordingRenderer("gloss"), rt(), rt());

Output:

base pass  draws the tile with MeshLambertMaterial clippingPlanes: 1
edges      draws the tile with ShaderMaterial     clippingPlanes: 0
AO         draws the tile with MeshNormalMaterial clippingPlanes: 0
gloss      draws the tile with ShaderMaterial     clippingPlanes: 0

Minimal app steps:

  1. Set components.get(OBC.Clipper).localClippingPlanes = true before creating any plane, and load a model.
  2. Set world.renderer.postproduction.enabled = true and world.renderer.postproduction.style = OBCF.PostproductionAspect.PEN.
  3. Create a clipping plane through the model. The model is drawn uncut.
  4. Switch to COLOR_PEN_SHADOWS. The colour is cut, but edges and AO of the removed part show on the cut face.

Expected

With local planes, the edges, AO and gloss passes are cut like the colour pass, the same way they are with global planes.

Suggested fix

Do for these three materials what SimpleOutlinePass.syncLocalClippingPlanes does for the outline masks. Before each render, set the local planes of the world's renderer (renderer.clippingPlanes filtered by isLocal) on:

  • EdgeDetectionPass._overrideMaterial
  • AOPass.normalMaterial, from AOPass.render before super.render
  • GlossPass.glossOverrideMaterial

Only assign a new array when the set of planes changed, as the outline pass does. A different plane count recompiles the shader, so a new array on every frame would be wasted work. Global planes are left out because three already applies them; setting them on the material as well would only clip twice against the same plane.

As a workaround in an app, assigning the renderer's clippingPlanes array to edgesPass._overrideMaterial.clippingPlanes and aoPass.normalMaterial.clippingPlanes fixes the edges and AO in Chromium on 3.4.3. _overrideMaterial is private, so this is fragile.

Environment

  • @thatopen/components-front 3.6.0, @thatopen/components 3.6.0, @thatopen/fragments 3.6.0, three 0.184.0
  • Browser observation: Chromium, components-front 3.4.3. Check on 3.6.0: bun 1.4.4, script above
  • Package manager: bun

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions