Skip to content

Commit

Permalink
CSS2D/3DRenderer: Make the code more reasonable. (#28393)
Browse files Browse the repository at this point in the history
  • Loading branch information
lafflan authored May 16, 2024
1 parent a12adba commit de367cb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
16 changes: 10 additions & 6 deletions examples/jsm/renderers/CSS2DRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ class CSS2DRenderer {
_viewMatrix.copy( camera.matrixWorldInverse );
_viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, _viewMatrix );

hideObject( scene );
renderObject( scene, scene, camera );
zOrder( scene );

Expand Down Expand Up @@ -129,23 +128,28 @@ class CSS2DRenderer {

function renderObject( object, scene, camera ) {

if ( object.visible === false ) return;
if ( object.visible === false ) {

hideObject( object );

return;

}

if ( object.isCSS2DObject ) {

_vector.setFromMatrixPosition( object.matrixWorld );
_vector.applyMatrix4( _viewProjectionMatrix );

const visible = ( _vector.z >= - 1 && _vector.z <= 1 ) && ( object.layers.test( camera.layers ) === true );

const element = object.element;
element.style.display = visible === true ? '' : 'none';

if ( visible === true ) {

object.onBeforeRender( _this, scene, camera );

const element = object.element;

element.style.display = '';

element.style.transform = 'translate(' + ( - 100 * object.center.x ) + '%,' + ( - 100 * object.center.y ) + '%)' + 'translate(' + ( _vector.x * _widthHalf + _widthHalf ) + 'px,' + ( - _vector.y * _heightHalf + _heightHalf ) + 'px)';

if ( element.parentNode !== domElement ) {
Expand Down
15 changes: 10 additions & 5 deletions examples/jsm/renderers/CSS3DRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ class CSS3DRenderer {

}

hideObject( scene );
renderObject( scene, scene, camera, cameraCSSMatrix );

};
Expand Down Expand Up @@ -266,12 +265,21 @@ class CSS3DRenderer {

function renderObject( object, scene, camera, cameraCSSMatrix ) {

if ( object.visible === false ) return;
if ( object.visible === false ) {

hideObject( object );

return;

}

if ( object.isCSS3DObject ) {

const visible = ( object.layers.test( camera.layers ) === true );

const element = object.element;
element.style.display = visible === true ? '' : 'none';

if ( visible === true ) {

object.onBeforeRender( _this, scene, camera );
Expand Down Expand Up @@ -304,11 +312,8 @@ class CSS3DRenderer {

}

const element = object.element;
const cachedObject = cache.objects.get( object );

element.style.display = '';

if ( cachedObject === undefined || cachedObject.style !== style ) {

element.style.transform = style;
Expand Down

0 comments on commit de367cb

Please sign in to comment.