Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions examples/jsm/tsl/display/OutlineNode.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DepthTexture, FloatType, RenderTarget, Vector2, TempNode, QuadMesh, NodeMaterial, RendererUtils, NodeUpdateType } from 'three/webgpu';
import { Loop, int, exp, min, float, mul, uv, vec2, vec3, Fn, textureSize, orthographicDepthToViewZ, screenUV, nodeObject, uniform, vec4, passTexture, texture, perspectiveDepthToViewZ, positionView, reference } from 'three/tsl';
import { DepthTexture, FloatType, RenderTarget, Vector2, TempNode, QuadMesh, NodeMaterial, SpriteNodeMaterial, RendererUtils, NodeUpdateType } from 'three/webgpu';
import { Loop, int, exp, min, float, mul, uv, vec2, vec3, Fn, textureSize, orthographicDepthToViewZ, screenUV, nodeObject, uniform, vec4, passTexture, texture, perspectiveDepthToViewZ, positionView, reference, color } from 'three/tsl';

const _quadMesh = /*@__PURE__*/ new QuadMesh();
const _size = /*@__PURE__*/ new Vector2();
Expand Down Expand Up @@ -293,9 +293,13 @@ class OutlineNode extends TempNode {
* @type {NodeMaterial}
*/
this._depthMaterial = new NodeMaterial();
this._depthMaterial.fragmentNode = vec4( 0, 0, 0, 1 );
this._depthMaterial.colorNode = color( 0, 0, 0 );
this._depthMaterial.name = 'OutlineNode.depth';

this._depthSpriteMaterial = new SpriteNodeMaterial();
this._depthSpriteMaterial.colorNode = color( 0, 0, 0 );
this._depthSpriteMaterial.name = 'OutlineNode.depthSprite';

/**
* The material for preparing the mask.
*
Expand All @@ -305,6 +309,9 @@ class OutlineNode extends TempNode {
this._prepareMaskMaterial = new NodeMaterial();
this._prepareMaskMaterial.name = 'OutlineNode.prepareMask';

this._prepareMaskSpriteMaterial = new SpriteNodeMaterial();
this._prepareMaskSpriteMaterial.name = 'OutlineNode.prepareMaskSprite';

/**
* The copy material
*
Expand Down Expand Up @@ -459,14 +466,14 @@ class OutlineNode extends TempNode {

// 1. Draw non-selected objects in the depth buffer

scene.overrideMaterial = this._depthMaterial;

renderer.setRenderTarget( this._renderTargetDepthBuffer );
renderer.setRenderObjectFunction( ( object, ...params ) => {
renderer.setRenderObjectFunction( ( object, scene, camera, geometry, material, group, lightsNode, clippingContext ) => {

if ( this._selectionCache.has( object ) === false ) {

renderer.renderObject( object, ...params );
const overrideMaterial = object.isSprite ? this._depthSpriteMaterial : this._depthMaterial;

renderer.renderObject( object, scene, camera, geometry, overrideMaterial, group, lightsNode, clippingContext );

}

Expand All @@ -477,14 +484,14 @@ class OutlineNode extends TempNode {

// 2. Draw only the selected objects by comparing the depth buffer of non-selected objects

scene.overrideMaterial = this._prepareMaskMaterial;

renderer.setRenderTarget( this._renderTargetMaskBuffer );
renderer.setRenderObjectFunction( ( object, ...params ) => {
renderer.setRenderObjectFunction( ( object, scene, camera, geometry, material, group, lightsNode, clippingContext ) => {

if ( this._selectionCache.has( object ) === true ) {

renderer.renderObject( object, ...params );
const overrideMaterial = object.isSprite ? this._prepareMaskSpriteMaterial : this._prepareMaskMaterial;

renderer.renderObject( object, scene, camera, geometry, overrideMaterial, group, lightsNode, clippingContext );

}

Expand Down Expand Up @@ -587,13 +594,16 @@ class OutlineNode extends TempNode {
}

const depthTest = positionView.z.lessThanEqual( viewZNode ).select( 1, 0 );
return vec4( 0.0, depthTest, 1.0, 1.0 );
return vec3( 0.0, depthTest, 1.0 );

};

this._prepareMaskMaterial.fragmentNode = prepareMask();
this._prepareMaskMaterial.colorNode = prepareMask();
this._prepareMaskMaterial.needsUpdate = true;

this._prepareMaskSpriteMaterial.colorNode = prepareMask();
this._prepareMaskSpriteMaterial.needsUpdate = true;

// copy material

this._materialCopy.fragmentNode = this._maskTextureUniform;
Expand Down Expand Up @@ -712,7 +722,9 @@ class OutlineNode extends TempNode {
this._renderTargetComposite.dispose();

this._depthMaterial.dispose();
this._depthSpriteMaterial.dispose();
this._prepareMaskMaterial.dispose();
this._prepareMaskSpriteMaterial.dispose();
this._materialCopy.dispose();
this._edgeDetectionMaterial.dispose();
this._separableBlurMaterial.dispose();
Expand All @@ -733,7 +745,7 @@ class OutlineNode extends TempNode {
const selectedObject = this.selectedObjects[ i ];
selectedObject.traverse( ( object ) => {

if ( object.isMesh ) this._selectionCache.add( object );
if ( object.isMesh || object.isSprite ) this._selectionCache.add( object );

} );

Expand Down
22 changes: 13 additions & 9 deletions src/materials/nodes/manager/NodeMaterialObserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,26 +393,23 @@ class NodeMaterialObserver {
const attributes = geometry.attributes;
const storedAttributes = storedGeometryData.attributes;

const storedAttributeNames = Object.keys( storedAttributes );
const currentAttributeNames = Object.keys( attributes );

if ( storedGeometryData.id !== geometry.id ) {

storedGeometryData.id = geometry.id;
return false;

}

if ( storedAttributeNames.length !== currentAttributeNames.length ) {
// attributes

renderObjectData.geometry.attributes = this.getAttributesData( attributes );
return false;
let currentAttributeCount = 0;
let storedAttributeCount = 0;

}
for ( const _ in attributes ) currentAttributeCount ++; // eslint-disable-line no-unused-vars

// compare each attribute
for ( const name in storedAttributes ) {

for ( const name of storedAttributeNames ) {
storedAttributeCount ++;

const storedAttributeData = storedAttributes[ name ];
const attribute = attributes[ name ];
Expand All @@ -435,6 +432,13 @@ class NodeMaterialObserver {

}

if ( storedAttributeCount !== currentAttributeCount ) {

renderObjectData.geometry.attributes = this.getAttributesData( attributes );
return false;

}

// check index

const index = geometry.index;
Expand Down