Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebGLRenderer: Sort objects in clip space. #28571

Merged
merged 6 commits into from
Jun 6, 2024
6 changes: 6 additions & 0 deletions docs/api/en/math/Vector4.html
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,12 @@ <h3>[method:this setAxisAngleFromRotationMatrix]( [param:Matrix4 m] )</h3>
and [page:.w w] to the angle.
</p>

<h3>[method:this setFromMatrixPosition]( [param:Matrix4 m] )</h3>
<p>
Sets this vector to the position elements of the
[link:https://en.wikipedia.org/wiki/Transformation_matrix transformation matrix] [page:Matrix4 m].
</p>

<h3>
[method:this setComponent]( [param:Integer index], [param:Float value] )
</h3>
Expand Down
13 changes: 13 additions & 0 deletions src/math/Vector4.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,19 @@ class Vector4 {

}

setFromMatrixPosition( m ) {
eiriklegernaes marked this conversation as resolved.
Show resolved Hide resolved

const e = m.elements;

this.x = e[ 12 ];
this.y = e[ 13 ];
this.z = e[ 14 ];
this.w = e[ 15 ];

return this;

}

min( v ) {

this.x = Math.min( this.x, v.x );
Expand Down
16 changes: 9 additions & 7 deletions src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ class WebGLRenderer {

const _vector3 = new Vector3();

const _vector4 = new Vector4();

const _emptyScene = { background: null, fog: null, environment: null, overrideMaterial: null, isScene: true };

let _renderBackground = false;
Expand Down Expand Up @@ -1305,7 +1307,7 @@ class WebGLRenderer {

if ( sortObjects ) {

_vector3.setFromMatrixPosition( object.matrixWorld )
_vector4.setFromMatrixPosition( object.matrixWorld )
.applyMatrix4( _projScreenMatrix );

}
Expand All @@ -1315,7 +1317,7 @@ class WebGLRenderer {

if ( material.visible ) {

currentRenderList.push( object, geometry, material, groupOrder, _vector3.z, null );
currentRenderList.push( object, geometry, material, groupOrder, _vector4.z, null );

}

Expand All @@ -1333,16 +1335,16 @@ class WebGLRenderer {
if ( object.boundingSphere !== undefined ) {

if ( object.boundingSphere === null ) object.computeBoundingSphere();
_vector3.copy( object.boundingSphere.center );
_vector4.copy( object.boundingSphere.center );

} else {

if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
_vector3.copy( geometry.boundingSphere.center );
_vector4.copy( geometry.boundingSphere.center );

}

_vector3
_vector4
.applyMatrix4( object.matrixWorld )
.applyMatrix4( _projScreenMatrix );

Expand All @@ -1359,15 +1361,15 @@ class WebGLRenderer {

if ( groupMaterial && groupMaterial.visible ) {

currentRenderList.push( object, geometry, groupMaterial, groupOrder, _vector3.z, group );
currentRenderList.push( object, geometry, groupMaterial, groupOrder, _vector4.z, group );

}

}

} else if ( material.visible ) {

currentRenderList.push( object, geometry, material, groupOrder, _vector3.z, null );
currentRenderList.push( object, geometry, material, groupOrder, _vector4.z, null );

}

Expand Down
13 changes: 13 additions & 0 deletions test/unit/src/math/Vector4.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,19 @@ export default QUnit.module( 'Maths', () => {

} );

QUnit.test( 'setFromMatrixPosition', ( assert ) => {

const a = new Vector4();
const m = new Matrix4().set( 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53 );

a.setFromMatrixPosition( m );
assert.strictEqual( a.x, 7, 'Check x' );
assert.strictEqual( a.y, 19, 'Check y' );
assert.strictEqual( a.z, 37, 'Check z' );
assert.strictEqual( a.w, 53, 'Check w' );

} );

QUnit.todo( 'min', ( assert ) => {

assert.ok( false, 'everything\'s gonna be alright' );
Expand Down