diff --git a/examples/jsm/exporters/GLTFExporter.js b/examples/jsm/exporters/GLTFExporter.js index 8ec912b4f7269f..1c2e67ef4f236a 100644 --- a/examples/jsm/exporters/GLTFExporter.js +++ b/examples/jsm/exporters/GLTFExporter.js @@ -1852,12 +1852,12 @@ class GLTFWriter { ! ( array instanceof Uint8Array ) ) { console.warn( 'GLTFExporter: Attribute "skinIndex" converted to type UNSIGNED_SHORT.' ); - modifiedAttribute = new BufferAttribute( new Uint16Array( array ), attribute.itemSize, attribute.normalized ); + modifiedAttribute = GLTFExporter.Utils.toTypedBufferAttribute( attribute, Uint16Array ); } else if ( ( array instanceof Uint32Array || array instanceof Int32Array ) && ! attributeName.startsWith( '_' ) ) { console.warn( `GLTFExporter: Attribute "${ attributeName }" converted to type FLOAT.` ); - modifiedAttribute = GLTFExporter.Utils.toFloat32BufferAttribute( attribute ); + modifiedAttribute = GLTFExporter.Utils.toTypedBufferAttribute( attribute, Float32Array ); } @@ -3538,9 +3538,9 @@ GLTFExporter.Utils = { }, - toFloat32BufferAttribute: function ( srcAttribute ) { + toTypedBufferAttribute: function ( srcAttribute, TypedArray ) { - const dstAttribute = new BufferAttribute( new Float32Array( srcAttribute.count * srcAttribute.itemSize ), srcAttribute.itemSize, false ); + const dstAttribute = new BufferAttribute( new TypedArray( srcAttribute.count * srcAttribute.itemSize ), srcAttribute.itemSize, false ); if ( ! srcAttribute.normalized && ! srcAttribute.isInterleavedBufferAttribute ) { diff --git a/examples/jsm/loaders/VTKLoader.js b/examples/jsm/loaders/VTKLoader.js index af74d3f7da3eeb..d7f249e0d56926 100644 --- a/examples/jsm/loaders/VTKLoader.js +++ b/examples/jsm/loaders/VTKLoader.js @@ -112,8 +112,20 @@ class VTKLoader extends Loader { // pattern for detecting the end of a number sequence const patWord = /^[^\d.\s-]+/; - // pattern for reading vertices, 3 floats or integers - const pat3Floats = /(\-?\d+\.?[\d\-\+e]*)\s+(\-?\d+\.?[\d\-\+e]*)\s+(\-?\d+\.?[\d\-\+e]*)/g; + function parseFloats( line ) { + + const result = []; + const parts = line.split( /\s+/ ); + + for ( let i = 0; i < parts.length; i ++ ) { + + if ( parts[ i ] !== '' ) result.push( parseFloat( parts[ i ] ) ); + + } + + return result; + + } // pattern for connectivity, an integer followed by any number of ints // the first integer is the number of polygon nodes @@ -165,14 +177,15 @@ class VTKLoader extends Loader { } else if ( inPointsSection ) { // get the vertices - while ( ( result = pat3Floats.exec( line ) ) !== null ) { + if ( patWord.exec( line ) === null ) { - if ( patWord.exec( line ) !== null ) break; + const values = parseFloats( line ); - const x = parseFloat( result[ 1 ] ); - const y = parseFloat( result[ 2 ] ); - const z = parseFloat( result[ 3 ] ); - positions.push( x, y, z ); + for ( let k = 0; k + 2 < values.length; k += 3 ) { + + positions.push( values[ k ], values[ k + 1 ], values[ k + 2 ] ); + + } } @@ -243,17 +256,16 @@ class VTKLoader extends Loader { // Get the colors - while ( ( result = pat3Floats.exec( line ) ) !== null ) { + if ( patWord.exec( line ) === null ) { - if ( patWord.exec( line ) !== null ) break; + const values = parseFloats( line ); - const r = parseFloat( result[ 1 ] ); - const g = parseFloat( result[ 2 ] ); - const b = parseFloat( result[ 3 ] ); + for ( let k = 0; k + 2 < values.length; k += 3 ) { - color.setRGB( r, g, b, SRGBColorSpace ); + color.setRGB( values[ k ], values[ k + 1 ], values[ k + 2 ], SRGBColorSpace ); + colors.push( color.r, color.g, color.b ); - colors.push( color.r, color.g, color.b ); + } } @@ -261,14 +273,15 @@ class VTKLoader extends Loader { // Get the normal vectors - while ( ( result = pat3Floats.exec( line ) ) !== null ) { + if ( patWord.exec( line ) === null ) { - if ( patWord.exec( line ) !== null ) break; + const values = parseFloats( line ); - const nx = parseFloat( result[ 1 ] ); - const ny = parseFloat( result[ 2 ] ); - const nz = parseFloat( result[ 3 ] ); - normals.push( nx, ny, nz ); + for ( let k = 0; k + 2 < values.length; k += 3 ) { + + normals.push( values[ k ], values[ k + 1 ], values[ k + 2 ] ); + + } } @@ -400,7 +413,7 @@ class VTKLoader extends Loader { let index = start; let c = buffer[ index ]; const s = []; - while ( c !== 10 ) { + while ( c !== 10 && index < buffer.length ) { s.push( String.fromCharCode( c ) ); index ++; diff --git a/src/renderers/WebGLRenderer.js b/src/renderers/WebGLRenderer.js index 760eaeec1889b0..ea16241325b482 100644 --- a/src/renderers/WebGLRenderer.js +++ b/src/renderers/WebGLRenderer.js @@ -2988,6 +2988,10 @@ class WebGLRenderer { const textureFormat = texture.format; const textureType = texture.type; + // when using MRT, select the correct color buffer for the subsequent read command + + if ( renderTarget.textures.length > 1 ) _gl.readBuffer( _gl.COLOR_ATTACHMENT0 + textureIndex ); + if ( ! capabilities.textureFormatReadable( textureFormat ) ) { error( 'WebGLRenderer.readRenderTargetPixels: renderTarget is not in RGBA or implementation defined format.' ); @@ -3006,10 +3010,6 @@ class WebGLRenderer { if ( ( x >= 0 && x <= ( renderTarget.width - width ) ) && ( y >= 0 && y <= ( renderTarget.height - height ) ) ) { - // when using MRT, select the correct color buffer for the subsequent read command - - if ( renderTarget.textures.length > 1 ) _gl.readBuffer( _gl.COLOR_ATTACHMENT0 + textureIndex ); - _gl.readPixels( x, y, width, height, utils.convert( textureFormat ), utils.convert( textureType ), buffer ); } @@ -3070,6 +3070,11 @@ class WebGLRenderer { const textureFormat = texture.format; const textureType = texture.type; + // when using MRT, select the correct color buffer for the subsequent read command + + if ( renderTarget.textures.length > 1 ) _gl.readBuffer( _gl.COLOR_ATTACHMENT0 + textureIndex ); + + if ( ! capabilities.textureFormatReadable( textureFormat ) ) { throw new Error( 'THREE.WebGLRenderer.readRenderTargetPixelsAsync: renderTarget is not in RGBA or implementation defined format.' ); @@ -3086,10 +3091,6 @@ class WebGLRenderer { _gl.bindBuffer( _gl.PIXEL_PACK_BUFFER, glBuffer ); _gl.bufferData( _gl.PIXEL_PACK_BUFFER, buffer.byteLength, _gl.STREAM_READ ); - // when using MRT, select the correct color buffer for the subsequent read command - - if ( renderTarget.textures.length > 1 ) _gl.readBuffer( _gl.COLOR_ATTACHMENT0 + textureIndex ); - _gl.readPixels( x, y, width, height, utils.convert( textureFormat ), utils.convert( textureType ), 0 ); // reset the frame buffer to the currently set buffer before waiting