Skip to content

Commit 783185a

Browse files
authored
Examples: Use new gpuType field in GPU Picking, fix bugs (#26001)
* Fix attribute merging * Use new int attribute type * Fix firefox gpu picking
1 parent efdd7de commit 783185a

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

examples/jsm/utils/BufferGeometryUtils.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ function mergeAttributes( attributes ) {
304304
let TypedArray;
305305
let itemSize;
306306
let normalized;
307+
let gpuType = - 1;
307308
let arrayLength = 0;
308309

309310
for ( let i = 0; i < attributes.length; ++ i ) {
@@ -341,6 +342,14 @@ function mergeAttributes( attributes ) {
341342

342343
}
343344

345+
if ( gpuType === - 1 ) gpuType = attribute.gpuType;
346+
if ( gpuType !== attribute.gpuType ) {
347+
348+
console.error( 'THREE.BufferGeometryUtils: .mergeAttributes() failed. BufferAttribute.gpuType must be consistent across matching attributes.' );
349+
return null;
350+
351+
}
352+
344353
arrayLength += attribute.array.length;
345354

346355
}
@@ -356,7 +365,14 @@ function mergeAttributes( attributes ) {
356365

357366
}
358367

359-
return new BufferAttribute( array, itemSize, normalized );
368+
const result = new BufferAttribute( array, itemSize, normalized );
369+
if ( gpuType !== undefined ) {
370+
371+
result.gpuType = gpuType;
372+
373+
}
374+
375+
return result;
360376

361377
}
362378

examples/webgl_interactive_cubes_gpu.html

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@
8787
pickingTexture = new THREE.WebGLRenderTarget( 1, 1, {
8888

8989
type: THREE.IntType,
90-
format: THREE.RedIntegerFormat,
91-
internalFormat: 'R32I',
90+
format: THREE.RGBAIntegerFormat,
91+
internalFormat: 'RGBA32I',
9292

9393
} );
9494
const pickingMaterial = new THREE.ShaderMaterial( {
@@ -122,10 +122,11 @@
122122
function applyId( geometry, id ) {
123123

124124
const position = geometry.attributes.position;
125-
const array = new Int32Array( position.count );
125+
const array = new Int16Array( position.count );
126126
array.fill( id );
127127

128-
const bufferAttribute = new THREE.BufferAttribute( array, 1, false );
128+
const bufferAttribute = new THREE.Int16BufferAttribute( array, 1, false );
129+
bufferAttribute.gpuType = THREE.IntType;
129130
geometry.setAttribute( 'id', bufferAttribute );
130131

131132
}
@@ -263,7 +264,7 @@
263264
camera.clearViewOffset();
264265

265266
// create buffer for reading single pixel
266-
const pixelBuffer = new Int32Array( 1 );
267+
const pixelBuffer = new Int32Array( 4 );
267268

268269
// read the pixel
269270
renderer.readRenderTargetPixels( pickingTexture, 0, 0, 1, 1, pixelBuffer );

0 commit comments

Comments
 (0)