From b4e45888e8e4deb5753721af8774f86328494adc Mon Sep 17 00:00:00 2001 From: Takahiro Date: Sun, 15 Aug 2021 15:47:50 -0700 Subject: [PATCH] Enable mat2/3/4 in shader --- src/renderers/WebGLRenderer.js | 16 ++--- src/renderers/webgl/WebGLBindingStates.js | 72 ++++++++++++++++------- src/renderers/webgl/WebGLProgram.js | 11 +++- utils/build/rollup.config.js | 3 + 4 files changed, 72 insertions(+), 30 deletions(-) diff --git a/src/renderers/WebGLRenderer.js b/src/renderers/WebGLRenderer.js index a35d36419c53a8..28de5a01d59407 100644 --- a/src/renderers/WebGLRenderer.js +++ b/src/renderers/WebGLRenderer.js @@ -687,8 +687,8 @@ function WebGLRenderer( parameters = {} ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.position ); _gl.bufferData( _gl.ARRAY_BUFFER, object.positionArray, _gl.DYNAMIC_DRAW ); - bindingStates.enableAttribute( programAttributes.position ); - _gl.vertexAttribPointer( programAttributes.position, 3, _gl.FLOAT, false, 0, 0 ); + bindingStates.enableAttribute( programAttributes.position.location ); + _gl.vertexAttribPointer( programAttributes.position.location, 3, _gl.FLOAT, false, 0, 0 ); } @@ -697,8 +697,8 @@ function WebGLRenderer( parameters = {} ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.normal ); _gl.bufferData( _gl.ARRAY_BUFFER, object.normalArray, _gl.DYNAMIC_DRAW ); - bindingStates.enableAttribute( programAttributes.normal ); - _gl.vertexAttribPointer( programAttributes.normal, 3, _gl.FLOAT, false, 0, 0 ); + bindingStates.enableAttribute( programAttributes.normal.location ); + _gl.vertexAttribPointer( programAttributes.normal.location, 3, _gl.FLOAT, false, 0, 0 ); } @@ -707,8 +707,8 @@ function WebGLRenderer( parameters = {} ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.uv ); _gl.bufferData( _gl.ARRAY_BUFFER, object.uvArray, _gl.DYNAMIC_DRAW ); - bindingStates.enableAttribute( programAttributes.uv ); - _gl.vertexAttribPointer( programAttributes.uv, 2, _gl.FLOAT, false, 0, 0 ); + bindingStates.enableAttribute( programAttributes.uv.location ); + _gl.vertexAttribPointer( programAttributes.uv.location, 2, _gl.FLOAT, false, 0, 0 ); } @@ -717,8 +717,8 @@ function WebGLRenderer( parameters = {} ) { _gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.color ); _gl.bufferData( _gl.ARRAY_BUFFER, object.colorArray, _gl.DYNAMIC_DRAW ); - bindingStates.enableAttribute( programAttributes.color ); - _gl.vertexAttribPointer( programAttributes.color, 3, _gl.FLOAT, false, 0, 0 ); + bindingStates.enableAttribute( programAttributes.color.location ); + _gl.vertexAttribPointer( programAttributes.color.location, 3, _gl.FLOAT, false, 0, 0 ); } diff --git a/src/renderers/webgl/WebGLBindingStates.js b/src/renderers/webgl/WebGLBindingStates.js index dcaa3859571224..5a34385abb1bbc 100644 --- a/src/renderers/webgl/WebGLBindingStates.js +++ b/src/renderers/webgl/WebGLBindingStates.js @@ -322,7 +322,7 @@ const programAttribute = programAttributes[ name ]; - if ( programAttribute >= 0 ) { + if ( programAttribute.location >= 0 ) { const geometryAttribute = geometryAttributes[ name ]; @@ -349,7 +349,11 @@ if ( data && data.isInstancedInterleavedBuffer ) { - enableAttributeAndDivisor( programAttribute, data.meshPerAttribute ); + for ( let i = 0; i < programAttribute.locationSize; i ++ ) { + + enableAttributeAndDivisor( programAttribute.location + i, data.meshPerAttribute ); + + } if ( geometry._maxInstanceCount === undefined ) { @@ -359,18 +363,38 @@ } else { - enableAttribute( programAttribute ); + for ( let i = 0; i < programAttribute.locationSize; i ++ ) { + + enableAttribute( programAttribute.location + i ); + + } } gl.bindBuffer( gl.ARRAY_BUFFER, buffer ); - vertexAttribPointer( programAttribute, size, type, normalized, stride * bytesPerElement, offset * bytesPerElement ); + + for ( let i = 0; i < programAttribute.locationSize; i ++ ) { + + vertexAttribPointer( + programAttribute.location + i, + size / programAttribute.locationSize, + type, + normalized, + stride * bytesPerElement, + ( offset + ( size / programAttribute.locationSize ) * i ) * bytesPerElement + ); + + } } else { if ( geometryAttribute.isInstancedBufferAttribute ) { - enableAttributeAndDivisor( programAttribute, geometryAttribute.meshPerAttribute ); + for ( let i = 0; i < programAttribute.locationSize; i ++ ) { + + enableAttributeAndDivisor( programAttribute.location + i, geometryAttribute.meshPerAttribute ); + + } if ( geometry._maxInstanceCount === undefined ) { @@ -380,12 +404,16 @@ } else { - enableAttribute( programAttribute ); + for ( let i = 0; i < programAttribute.locationSize; i ++ ) { + + enableAttribute( programAttribute.location + i ); + + } } gl.bindBuffer( gl.ARRAY_BUFFER, buffer ); - vertexAttribPointer( programAttribute, size, type, normalized, 0, 0 ); + vertexAttribPointer( programAttribute.location, size, type, normalized, 0, 0 ); } @@ -400,17 +428,19 @@ const buffer = attribute.buffer; const type = attribute.type; - enableAttributeAndDivisor( programAttribute + 0, 1 ); - enableAttributeAndDivisor( programAttribute + 1, 1 ); - enableAttributeAndDivisor( programAttribute + 2, 1 ); - enableAttributeAndDivisor( programAttribute + 3, 1 ); + for ( let i = 0; i < programAttribute.locationSize; i ++ ) { + + enableAttributeAndDivisor( programAttribute.location + i, 1 ); + + } gl.bindBuffer( gl.ARRAY_BUFFER, buffer ); - gl.vertexAttribPointer( programAttribute + 0, 4, type, false, 64, 0 ); - gl.vertexAttribPointer( programAttribute + 1, 4, type, false, 64, 16 ); - gl.vertexAttribPointer( programAttribute + 2, 4, type, false, 64, 32 ); - gl.vertexAttribPointer( programAttribute + 3, 4, type, false, 64, 48 ); + for ( let i = 0; i < programAttribute.locationSize; i ++ ) { + + gl.vertexAttribPointer( programAttribute.location + i, 4, type, false, 64, 16 * i ); + + } } else if ( name === 'instanceColor' ) { @@ -423,11 +453,11 @@ const buffer = attribute.buffer; const type = attribute.type; - enableAttributeAndDivisor( programAttribute, 1 ); + enableAttributeAndDivisor( programAttribute.location, 1 ); gl.bindBuffer( gl.ARRAY_BUFFER, buffer ); - gl.vertexAttribPointer( programAttribute, 3, type, false, 12, 0 ); + gl.vertexAttribPointer( programAttribute.location, 3, type, false, 12, 0 ); } else if ( materialDefaultAttributeValues !== undefined ) { @@ -438,19 +468,19 @@ switch ( value.length ) { case 2: - gl.vertexAttrib2fv( programAttribute, value ); + gl.vertexAttrib2fv( programAttribute.location, value ); break; case 3: - gl.vertexAttrib3fv( programAttribute, value ); + gl.vertexAttrib3fv( programAttribute.location, value ); break; case 4: - gl.vertexAttrib4fv( programAttribute, value ); + gl.vertexAttrib4fv( programAttribute.location, value ); break; default: - gl.vertexAttrib1fv( programAttribute, value ); + gl.vertexAttrib1fv( programAttribute.location, value ); } diff --git a/src/renderers/webgl/WebGLProgram.js b/src/renderers/webgl/WebGLProgram.js index 3541da91796a21..54233526ac765e 100644 --- a/src/renderers/webgl/WebGLProgram.js +++ b/src/renderers/webgl/WebGLProgram.js @@ -153,9 +153,18 @@ function fetchAttributeLocations( gl, program ) { const info = gl.getActiveAttrib( program, i ); const name = info.name; + let locationSize = 1; + if ( info.type === gl.FLOAT_MAT2 ) locationSize = 2; + if ( info.type === gl.FLOAT_MAT3 ) locationSize = 3; + if ( info.type === gl.FLOAT_MAT4 ) locationSize = 4; + // console.log( 'THREE.WebGLProgram: ACTIVE VERTEX ATTRIBUTE:', name, i ); - attributes[ name ] = gl.getAttribLocation( program, name ); + attributes[ name ] = { + type: info.type, + location: gl.getAttribLocation( program, name ), + locationSize: locationSize + }; } diff --git a/utils/build/rollup.config.js b/utils/build/rollup.config.js index d833629c9c0571..14e4500c330c3d 100644 --- a/utils/build/rollup.config.js +++ b/utils/build/rollup.config.js @@ -125,6 +125,9 @@ export function glconstants() { FRAGMENT_SHADER: 35632, MAX_VERTEX_TEXTURE_IMAGE_UNITS: 35660, MAX_COMBINED_TEXTURE_IMAGE_UNITS: 35661, + FLOAT_MAT2: 35674, + FLOAT_MAT3: 35675, + FLOAT_MAT4: 35676, COMPILE_STATUS: 35713, LINK_STATUS: 35714, VALIDATE_STATUS: 35715,