Skip to content

Commit

Permalink
Enable mat2/3/4 in shader
Browse files Browse the repository at this point in the history
  • Loading branch information
takahirox committed Aug 15, 2021
1 parent d57bbd9 commit b4e4588
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 30 deletions.
16 changes: 8 additions & 8 deletions src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

}

Expand All @@ -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 );

}

Expand All @@ -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 );

}

Expand All @@ -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 );

}

Expand Down
72 changes: 51 additions & 21 deletions src/renderers/webgl/WebGLBindingStates.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@

const programAttribute = programAttributes[ name ];

if ( programAttribute >= 0 ) {
if ( programAttribute.location >= 0 ) {

const geometryAttribute = geometryAttributes[ name ];

Expand All @@ -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 ) {

Expand All @@ -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 ) {

Expand All @@ -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 );

}

Expand All @@ -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' ) {

Expand All @@ -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 ) {

Expand All @@ -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 );

}

Expand Down
11 changes: 10 additions & 1 deletion src/renderers/webgl/WebGLProgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

}

Expand Down
3 changes: 3 additions & 0 deletions utils/build/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b4e4588

Please sign in to comment.