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

BatchedMesh: update example, fix depth conversion & auxiliary buffer #27228

Merged
merged 4 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 9 additions & 13 deletions examples/jsm/utils/SortUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@ const POWER = 3;
const BIT_MAX = 32;
const BIN_BITS = 1 << POWER;
const BIN_SIZE = 1 << BIN_BITS;
const BIN_MAX = BIN_SIZE - 1;
const ITERATIONS = BIT_MAX / BIN_BITS;

const bins = new Array( ITERATIONS );
const caches = new Array( ITERATIONS );
const bins_buffer = new ArrayBuffer( 2 * ITERATIONS * BIN_SIZE * 4 );
const bins_buffer = new ArrayBuffer( ( ITERATIONS + 1 ) * BIN_SIZE * 4 );

let c = 0;
for ( let i = 0; i < ITERATIONS; i ++ ) {

for ( let i = 0; i < ( ITERATIONS + 1 ); i ++ ) {
bins[ i ] = new Uint32Array( bins_buffer, c, BIN_SIZE );
c += BIN_SIZE * 4;
caches[ i ] = new Uint32Array( bins_buffer, c, BIN_SIZE );
c += BIN_SIZE * 4;

}

const defaultGet = ( el ) => el;
Expand Down Expand Up @@ -48,7 +44,7 @@ export const radixSort = ( arr, opt ) => {
recurse = ( cache, depth, start ) => {

let prev = 0;
for ( let j = BIN_SIZE - 1; j >= 0; j -- ) {
for ( let j = BIN_MAX; j >= 0; j -- ) {

const cur = cache[ j ], diff = cur - prev;
if ( diff != 0 ) {
Expand Down Expand Up @@ -135,21 +131,21 @@ export const radixSort = ( arr, opt ) => {

const shift = ( 3 - depth ) << POWER;
const end = start + len;

const bin = bins[ depth ];
const cache = caches[ depth ];
const cache = bins[ depth ];
const bin = bins[ depth + 1 ];

bin.fill( 0 );

for ( let j = start; j < end; j ++ )
bin[ ( get( a[ j ] ) >> shift ) & ( BIN_SIZE - 1 ) ] ++;
bin[ ( get( a[ j ] ) >> shift ) & BIN_MAX ] ++;

accumulate( bin );

cache.set( bin );

for ( let j = end - 1; j >= start; j -- )
b[ start + -- bin[ ( get( a[ j ] ) >> shift ) & ( BIN_SIZE - 1 ) ] ] = a[ j ];
b[ start + -- bin[ ( get( a[ j ] ) >> shift ) & BIN_MAX ] ] = a[ j ];

if ( depth == ITERATIONS - 1 ) return;

Expand Down
21 changes: 14 additions & 7 deletions examples/webgl_mesh_batch.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
let stats, gui, guiStatsEl;
let camera, controls, scene, renderer;
let geometries, mesh, material;
let aux = [];
const ids = [];
const matrix = new THREE.Matrix4();

Expand All @@ -52,6 +53,12 @@

//

const buffer = new ArrayBuffer( 4 );
const f32 = new Float32Array( buffer );
const u32 = new Uint32Array( buffer );

//

const MAX_GEOMETRY_COUNT = 20000;

const Method = {
Expand Down Expand Up @@ -188,6 +195,8 @@
mesh = new THREE.BatchedMesh( geometryCount, vertexCount, indexCount, createMaterial() );
mesh.userData.rotationSpeeds = [];

aux = new Array( geometryCount );

// disable full-object frustum culling since all of the objects can be dynamic.
mesh.frustumCulled = false;

Expand Down Expand Up @@ -285,20 +294,18 @@
function sortFunction( list, camera ) {

// initialize options
this._options = this._options || {
const options = {
get: el => el.z,
aux: new Array( list.length ),
aux: aux,
sciecode marked this conversation as resolved.
Show resolved Hide resolved
};

const options = this._options;
options.reversed = this.material.transparent;

// convert depth to unsigned 32 bit range
const den = camera.far;
// bit-cast depth to unsigned 32-bit
for ( let i = 0, l = list.length; i < l; i ++ ) {

const el = list[ i ];
el.z = ( 1 << 30 ) * ( el.z / den );
f32[ 0 ] = list[ i ].z;
list[ i ].z = u32[ 0 ];
sciecode marked this conversation as resolved.
Show resolved Hide resolved

}

Expand Down