2
0
Эх сурвалжийг харах

Fixed vertex colors initialization for BufferGeometry in CTMLoader.

Also some small refactoring of BufferGeometry handling in WebGLRenderer.

Fixes #2087
alteredq 13 жил өмнө
parent
commit
ddbf4e1c49

+ 3 - 3
build/Three.js

@@ -351,9 +351,9 @@ k.deleteRenderbuffer(a.__webglRenderbuffer)}}};this.deallocateMaterial=function(
 b,c){if(a.hasPositions&&!a.__webglVertexBuffer)a.__webglVertexBuffer=k.createBuffer();if(a.hasNormals&&!a.__webglNormalBuffer)a.__webglNormalBuffer=k.createBuffer();if(a.hasUvs&&!a.__webglUvBuffer)a.__webglUvBuffer=k.createBuffer();if(a.hasColors&&!a.__webglColorBuffer)a.__webglColorBuffer=k.createBuffer();if(a.hasPositions){k.bindBuffer(k.ARRAY_BUFFER,a.__webglVertexBuffer);k.bufferData(k.ARRAY_BUFFER,a.positionArray,k.DYNAMIC_DRAW);k.enableVertexAttribArray(b.attributes.position);k.vertexAttribPointer(b.attributes.position,
 3,k.FLOAT,false,0,0)}if(a.hasNormals){k.bindBuffer(k.ARRAY_BUFFER,a.__webglNormalBuffer);if(c.shading===THREE.FlatShading){var d,e,f,g,h,i,j,l,m,n,o,p=a.count*3;for(o=0;o<p;o=o+9){n=a.normalArray;d=n[o];e=n[o+1];f=n[o+2];g=n[o+3];i=n[o+4];l=n[o+5];h=n[o+6];j=n[o+7];m=n[o+8];d=(d+g+h)/3;e=(e+i+j)/3;f=(f+l+m)/3;n[o]=d;n[o+1]=e;n[o+2]=f;n[o+3]=d;n[o+4]=e;n[o+5]=f;n[o+6]=d;n[o+7]=e;n[o+8]=f}}k.bufferData(k.ARRAY_BUFFER,a.normalArray,k.DYNAMIC_DRAW);k.enableVertexAttribArray(b.attributes.normal);k.vertexAttribPointer(b.attributes.normal,
 3,k.FLOAT,false,0,0)}if(a.hasUvs&&c.map){k.bindBuffer(k.ARRAY_BUFFER,a.__webglUvBuffer);k.bufferData(k.ARRAY_BUFFER,a.uvArray,k.DYNAMIC_DRAW);k.enableVertexAttribArray(b.attributes.uv);k.vertexAttribPointer(b.attributes.uv,2,k.FLOAT,false,0,0)}if(a.hasColors&&c.vertexColors!==THREE.NoColors){k.bindBuffer(k.ARRAY_BUFFER,a.__webglColorBuffer);k.bufferData(k.ARRAY_BUFFER,a.colorArray,k.DYNAMIC_DRAW);k.enableVertexAttribArray(b.attributes.color);k.vertexAttribPointer(b.attributes.color,3,k.FLOAT,false,
-0,0)}k.drawArrays(k.TRIANGLES,0,a.count);a.count=0};this.renderBufferDirect=function(a,b,c,d,e,f){if(d.visible!==false){c=q(a,b,c,d,f);a=c.attributes;b=false;d=e.id*16777215+c.id*2+(d.wireframe?1:0);if(d!==ca){ca=d;b=true}if(f instanceof THREE.Mesh){f=e.offsets;d=0;for(c=f.length;d<c;++d){if(b){k.bindBuffer(k.ARRAY_BUFFER,e.vertexPositionBuffer);k.vertexAttribPointer(a.position,e.vertexPositionBuffer.itemSize,k.FLOAT,false,0,f[d].index*12);if(a.normal>=0&&e.vertexNormalBuffer){k.bindBuffer(k.ARRAY_BUFFER,
-e.vertexNormalBuffer);k.vertexAttribPointer(a.normal,e.vertexNormalBuffer.itemSize,k.FLOAT,false,0,f[d].index*12)}if(a.uv>=0&&e.vertexUvBuffer)if(e.vertexUvBuffer){k.bindBuffer(k.ARRAY_BUFFER,e.vertexUvBuffer);k.vertexAttribPointer(a.uv,e.vertexUvBuffer.itemSize,k.FLOAT,false,0,f[d].index*8);k.enableVertexAttribArray(a.uv)}else k.disableVertexAttribArray(a.uv);if(a.color>=0&&e.vertexColorBuffer){k.bindBuffer(k.ARRAY_BUFFER,e.vertexColorBuffer);k.vertexAttribPointer(a.color,e.vertexColorBuffer.itemSize,
-k.FLOAT,false,0,f[d].index*16)}k.bindBuffer(k.ELEMENT_ARRAY_BUFFER,e.vertexIndexBuffer)}k.drawElements(k.TRIANGLES,f[d].count,k.UNSIGNED_SHORT,f[d].start*2);D.info.render.calls++;D.info.render.vertices=D.info.render.vertices+f[d].count;D.info.render.faces=D.info.render.faces+f[d].count/3}}}};this.renderBuffer=function(a,b,c,d,e,f){if(d.visible!==false){var g,i,c=q(a,b,c,d,f),b=c.attributes,a=false,c=e.id*16777215+c.id*2+(d.wireframe?1:0);if(c!==ca){ca=c;a=true}if(!d.morphTargets&&b.position>=0){if(a){k.bindBuffer(k.ARRAY_BUFFER,
+0,0)}k.drawArrays(k.TRIANGLES,0,a.count);a.count=0};this.renderBufferDirect=function(a,b,c,d,e,f){if(d.visible!==false){c=q(a,b,c,d,f);a=c.attributes;b=false;d=e.id*16777215+c.id*2+(d.wireframe?1:0);if(d!==ca){ca=d;b=true}if(f instanceof THREE.Mesh){f=e.offsets;d=0;for(c=f.length;d<c;++d){if(b){var g=e.vertexPositionBuffer.itemSize;k.bindBuffer(k.ARRAY_BUFFER,e.vertexPositionBuffer);k.vertexAttribPointer(a.position,g,k.FLOAT,false,0,f[d].index*g*4);if(a.normal>=0&&e.vertexNormalBuffer){g=e.vertexNormalBuffer.itemSize;
+k.bindBuffer(k.ARRAY_BUFFER,e.vertexNormalBuffer);k.vertexAttribPointer(a.normal,g,k.FLOAT,false,0,f[d].index*g*4)}if(a.uv>=0&&e.vertexUvBuffer)if(e.vertexUvBuffer){g=e.vertexUvBuffer.itemSize;k.bindBuffer(k.ARRAY_BUFFER,e.vertexUvBuffer);k.vertexAttribPointer(a.uv,g,k.FLOAT,false,0,f[d].index*g*4);k.enableVertexAttribArray(a.uv)}else k.disableVertexAttribArray(a.uv);if(a.color>=0&&e.vertexColorBuffer){g=e.vertexColorBuffer.itemSize;k.bindBuffer(k.ARRAY_BUFFER,e.vertexColorBuffer);k.vertexAttribPointer(a.color,
+g,k.FLOAT,false,0,f[d].index*g*4)}k.bindBuffer(k.ELEMENT_ARRAY_BUFFER,e.vertexIndexBuffer)}k.drawElements(k.TRIANGLES,f[d].count,k.UNSIGNED_SHORT,f[d].start*2);D.info.render.calls++;D.info.render.vertices=D.info.render.vertices+f[d].count;D.info.render.faces=D.info.render.faces+f[d].count/3}}}};this.renderBuffer=function(a,b,c,d,e,f){if(d.visible!==false){var g,i,c=q(a,b,c,d,f),b=c.attributes,a=false,c=e.id*16777215+c.id*2+(d.wireframe?1:0);if(c!==ca){ca=c;a=true}if(!d.morphTargets&&b.position>=0){if(a){k.bindBuffer(k.ARRAY_BUFFER,
 e.__webglVertexBuffer);k.vertexAttribPointer(b.position,3,k.FLOAT,false,0,0)}}else if(f.morphTargetBase){c=d.program.attributes;if(f.morphTargetBase!==-1){k.bindBuffer(k.ARRAY_BUFFER,e.__webglMorphTargetsBuffers[f.morphTargetBase]);k.vertexAttribPointer(c.position,3,k.FLOAT,false,0,0)}else if(c.position>=0){k.bindBuffer(k.ARRAY_BUFFER,e.__webglVertexBuffer);k.vertexAttribPointer(c.position,3,k.FLOAT,false,0,0)}if(f.morphTargetForcedOrder.length){var j=0;i=f.morphTargetForcedOrder;for(g=f.morphTargetInfluences;j<
 d.numSupportedMorphTargets&&j<i.length;){k.bindBuffer(k.ARRAY_BUFFER,e.__webglMorphTargetsBuffers[i[j]]);k.vertexAttribPointer(c["morphTarget"+j],3,k.FLOAT,false,0,0);if(d.morphNormals){k.bindBuffer(k.ARRAY_BUFFER,e.__webglMorphNormalsBuffers[i[j]]);k.vertexAttribPointer(c["morphNormal"+j],3,k.FLOAT,false,0,0)}f.__webglMorphTargetInfluences[j]=g[i[j]];j++}}else{i=[];g=f.morphTargetInfluences;var l,m=g.length;for(l=0;l<m;l++){j=g[l];j>0&&i.push([l,j])}if(i.length>d.numSupportedMorphTargets){i.sort(h);
 i.length=d.numSupportedMorphTargets}else i.length>d.numSupportedMorphNormals?i.sort(h):i.length===0&&i.push([0,0]);for(j=0;j<d.numSupportedMorphTargets;){if(i[j]){l=i[j][0];k.bindBuffer(k.ARRAY_BUFFER,e.__webglMorphTargetsBuffers[l]);k.vertexAttribPointer(c["morphTarget"+j],3,k.FLOAT,false,0,0);if(d.morphNormals){k.bindBuffer(k.ARRAY_BUFFER,e.__webglMorphNormalsBuffers[l]);k.vertexAttribPointer(c["morphNormal"+j],3,k.FLOAT,false,0,0)}f.__webglMorphTargetInfluences[j]=g[l]}else{k.vertexAttribPointer(c["morphTarget"+

+ 3 - 3
build/custom/ThreeWebGL.js

@@ -315,9 +315,9 @@ g.deleteRenderbuffer(a.__webglRenderbuffer)}}};this.deallocateMaterial=function(
 b,c){if(a.hasPositions&&!a.__webglVertexBuffer)a.__webglVertexBuffer=g.createBuffer();if(a.hasNormals&&!a.__webglNormalBuffer)a.__webglNormalBuffer=g.createBuffer();if(a.hasUvs&&!a.__webglUvBuffer)a.__webglUvBuffer=g.createBuffer();if(a.hasColors&&!a.__webglColorBuffer)a.__webglColorBuffer=g.createBuffer();if(a.hasPositions){g.bindBuffer(g.ARRAY_BUFFER,a.__webglVertexBuffer);g.bufferData(g.ARRAY_BUFFER,a.positionArray,g.DYNAMIC_DRAW);g.enableVertexAttribArray(b.attributes.position);g.vertexAttribPointer(b.attributes.position,
 3,g.FLOAT,false,0,0)}if(a.hasNormals){g.bindBuffer(g.ARRAY_BUFFER,a.__webglNormalBuffer);if(c.shading===THREE.FlatShading){var d,e,f,h,i,j,l,k,m,n,o,r=a.count*3;for(o=0;o<r;o=o+9){n=a.normalArray;d=n[o];e=n[o+1];f=n[o+2];h=n[o+3];j=n[o+4];k=n[o+5];i=n[o+6];l=n[o+7];m=n[o+8];d=(d+h+i)/3;e=(e+j+l)/3;f=(f+k+m)/3;n[o]=d;n[o+1]=e;n[o+2]=f;n[o+3]=d;n[o+4]=e;n[o+5]=f;n[o+6]=d;n[o+7]=e;n[o+8]=f}}g.bufferData(g.ARRAY_BUFFER,a.normalArray,g.DYNAMIC_DRAW);g.enableVertexAttribArray(b.attributes.normal);g.vertexAttribPointer(b.attributes.normal,
 3,g.FLOAT,false,0,0)}if(a.hasUvs&&c.map){g.bindBuffer(g.ARRAY_BUFFER,a.__webglUvBuffer);g.bufferData(g.ARRAY_BUFFER,a.uvArray,g.DYNAMIC_DRAW);g.enableVertexAttribArray(b.attributes.uv);g.vertexAttribPointer(b.attributes.uv,2,g.FLOAT,false,0,0)}if(a.hasColors&&c.vertexColors!==THREE.NoColors){g.bindBuffer(g.ARRAY_BUFFER,a.__webglColorBuffer);g.bufferData(g.ARRAY_BUFFER,a.colorArray,g.DYNAMIC_DRAW);g.enableVertexAttribArray(b.attributes.color);g.vertexAttribPointer(b.attributes.color,3,g.FLOAT,false,
-0,0)}g.drawArrays(g.TRIANGLES,0,a.count);a.count=0};this.renderBufferDirect=function(a,b,c,d,e,f){if(d.visible!==false){c=q(a,b,c,d,f);a=c.attributes;b=false;d=e.id*16777215+c.id*2+(d.wireframe?1:0);if(d!==ca){ca=d;b=true}if(f instanceof THREE.Mesh){f=e.offsets;d=0;for(c=f.length;d<c;++d){if(b){g.bindBuffer(g.ARRAY_BUFFER,e.vertexPositionBuffer);g.vertexAttribPointer(a.position,e.vertexPositionBuffer.itemSize,g.FLOAT,false,0,f[d].index*12);if(a.normal>=0&&e.vertexNormalBuffer){g.bindBuffer(g.ARRAY_BUFFER,
-e.vertexNormalBuffer);g.vertexAttribPointer(a.normal,e.vertexNormalBuffer.itemSize,g.FLOAT,false,0,f[d].index*12)}if(a.uv>=0&&e.vertexUvBuffer)if(e.vertexUvBuffer){g.bindBuffer(g.ARRAY_BUFFER,e.vertexUvBuffer);g.vertexAttribPointer(a.uv,e.vertexUvBuffer.itemSize,g.FLOAT,false,0,f[d].index*8);g.enableVertexAttribArray(a.uv)}else g.disableVertexAttribArray(a.uv);if(a.color>=0&&e.vertexColorBuffer){g.bindBuffer(g.ARRAY_BUFFER,e.vertexColorBuffer);g.vertexAttribPointer(a.color,e.vertexColorBuffer.itemSize,
-g.FLOAT,false,0,f[d].index*16)}g.bindBuffer(g.ELEMENT_ARRAY_BUFFER,e.vertexIndexBuffer)}g.drawElements(g.TRIANGLES,f[d].count,g.UNSIGNED_SHORT,f[d].start*2);F.info.render.calls++;F.info.render.vertices=F.info.render.vertices+f[d].count;F.info.render.faces=F.info.render.faces+f[d].count/3}}}};this.renderBuffer=function(a,b,c,d,e,f){if(d.visible!==false){var h,i,c=q(a,b,c,d,f),b=c.attributes,a=false,c=e.id*16777215+c.id*2+(d.wireframe?1:0);if(c!==ca){ca=c;a=true}if(!d.morphTargets&&b.position>=0){if(a){g.bindBuffer(g.ARRAY_BUFFER,
+0,0)}g.drawArrays(g.TRIANGLES,0,a.count);a.count=0};this.renderBufferDirect=function(a,b,c,d,e,f){if(d.visible!==false){c=q(a,b,c,d,f);a=c.attributes;b=false;d=e.id*16777215+c.id*2+(d.wireframe?1:0);if(d!==ca){ca=d;b=true}if(f instanceof THREE.Mesh){f=e.offsets;d=0;for(c=f.length;d<c;++d){if(b){var h=e.vertexPositionBuffer.itemSize;g.bindBuffer(g.ARRAY_BUFFER,e.vertexPositionBuffer);g.vertexAttribPointer(a.position,h,g.FLOAT,false,0,f[d].index*h*4);if(a.normal>=0&&e.vertexNormalBuffer){h=e.vertexNormalBuffer.itemSize;
+g.bindBuffer(g.ARRAY_BUFFER,e.vertexNormalBuffer);g.vertexAttribPointer(a.normal,h,g.FLOAT,false,0,f[d].index*h*4)}if(a.uv>=0&&e.vertexUvBuffer)if(e.vertexUvBuffer){h=e.vertexUvBuffer.itemSize;g.bindBuffer(g.ARRAY_BUFFER,e.vertexUvBuffer);g.vertexAttribPointer(a.uv,h,g.FLOAT,false,0,f[d].index*h*4);g.enableVertexAttribArray(a.uv)}else g.disableVertexAttribArray(a.uv);if(a.color>=0&&e.vertexColorBuffer){h=e.vertexColorBuffer.itemSize;g.bindBuffer(g.ARRAY_BUFFER,e.vertexColorBuffer);g.vertexAttribPointer(a.color,
+h,g.FLOAT,false,0,f[d].index*h*4)}g.bindBuffer(g.ELEMENT_ARRAY_BUFFER,e.vertexIndexBuffer)}g.drawElements(g.TRIANGLES,f[d].count,g.UNSIGNED_SHORT,f[d].start*2);F.info.render.calls++;F.info.render.vertices=F.info.render.vertices+f[d].count;F.info.render.faces=F.info.render.faces+f[d].count/3}}}};this.renderBuffer=function(a,b,c,d,e,f){if(d.visible!==false){var h,i,c=q(a,b,c,d,f),b=c.attributes,a=false,c=e.id*16777215+c.id*2+(d.wireframe?1:0);if(c!==ca){ca=c;a=true}if(!d.morphTargets&&b.position>=0){if(a){g.bindBuffer(g.ARRAY_BUFFER,
 e.__webglVertexBuffer);g.vertexAttribPointer(b.position,3,g.FLOAT,false,0,0)}}else if(f.morphTargetBase){c=d.program.attributes;if(f.morphTargetBase!==-1){g.bindBuffer(g.ARRAY_BUFFER,e.__webglMorphTargetsBuffers[f.morphTargetBase]);g.vertexAttribPointer(c.position,3,g.FLOAT,false,0,0)}else if(c.position>=0){g.bindBuffer(g.ARRAY_BUFFER,e.__webglVertexBuffer);g.vertexAttribPointer(c.position,3,g.FLOAT,false,0,0)}if(f.morphTargetForcedOrder.length){var j=0;i=f.morphTargetForcedOrder;for(h=f.morphTargetInfluences;j<
 d.numSupportedMorphTargets&&j<i.length;){g.bindBuffer(g.ARRAY_BUFFER,e.__webglMorphTargetsBuffers[i[j]]);g.vertexAttribPointer(c["morphTarget"+j],3,g.FLOAT,false,0,0);if(d.morphNormals){g.bindBuffer(g.ARRAY_BUFFER,e.__webglMorphNormalsBuffers[i[j]]);g.vertexAttribPointer(c["morphNormal"+j],3,g.FLOAT,false,0,0)}f.__webglMorphTargetInfluences[j]=h[i[j]];j++}}else{i=[];h=f.morphTargetInfluences;var k,m=h.length;for(k=0;k<m;k++){j=h[k];j>0&&i.push([k,j])}if(i.length>d.numSupportedMorphTargets){i.sort(l);
 i.length=d.numSupportedMorphTargets}else i.length>d.numSupportedMorphNormals?i.sort(l):i.length===0&&i.push([0,0]);for(j=0;j<d.numSupportedMorphTargets;){if(i[j]){k=i[j][0];g.bindBuffer(g.ARRAY_BUFFER,e.__webglMorphTargetsBuffers[k]);g.vertexAttribPointer(c["morphTarget"+j],3,g.FLOAT,false,0,0);if(d.morphNormals){g.bindBuffer(g.ARRAY_BUFFER,e.__webglMorphNormalsBuffers[k]);g.vertexAttribPointer(c["morphNormal"+j],3,g.FLOAT,false,0,0)}f.__webglMorphTargetInfluences[j]=h[k]}else{g.vertexAttribPointer(c["morphTarget"+

+ 4 - 4
examples/js/loaders/ctm/CTMLoader.js

@@ -368,10 +368,10 @@ THREE.CTMLoader.prototype.createModelBuffers = function ( file, callback ) {
 
 					if ( vertexColorArray ) {
 
-						newColors[ vertexCounter * 4 ] 	   = vertexNormalArray[ v * 4 ];
-						newColors[ vertexCounter * 4 + 1 ] = vertexNormalArray[ v * 4 + 1 ];
-						newColors[ vertexCounter * 4 + 2 ] = vertexNormalArray[ v * 4 + 2 ];
-						newColors[ vertexCounter * 4 + 3 ] = vertexNormalArray[ v * 4 + 3 ];
+						newColors[ vertexCounter * 4 ] 	   = vertexColorArray[ v * 4 ];
+						newColors[ vertexCounter * 4 + 1 ] = vertexColorArray[ v * 4 + 1 ];
+						newColors[ vertexCounter * 4 + 2 ] = vertexColorArray[ v * 4 + 2 ];
+						newColors[ vertexCounter * 4 + 3 ] = vertexColorArray[ v * 4 + 3 ];
 
 					}
 

+ 13 - 5
src/renderers/WebGLRenderer.js

@@ -3006,15 +3006,19 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 					// vertices
 
+					var positionSize = geometryGroup.vertexPositionBuffer.itemSize;
+
 					_gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.vertexPositionBuffer );
-					_gl.vertexAttribPointer( attributes.position, geometryGroup.vertexPositionBuffer.itemSize, _gl.FLOAT, false, 0, offsets[ i ].index * 4 * 3 );
+					_gl.vertexAttribPointer( attributes.position, positionSize, _gl.FLOAT, false, 0, offsets[ i ].index * positionSize * 4 ); // 4 bytes per Float32
 
 					// normals
 
 					if ( attributes.normal >= 0 && geometryGroup.vertexNormalBuffer ) {
 
+						var normalSize = geometryGroup.vertexNormalBuffer.itemSize;
+
 						_gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.vertexNormalBuffer );
-						_gl.vertexAttribPointer( attributes.normal, geometryGroup.vertexNormalBuffer.itemSize, _gl.FLOAT, false, 0, offsets[ i ].index * 4 * 3 );
+						_gl.vertexAttribPointer( attributes.normal, normalSize, _gl.FLOAT, false, 0, offsets[ i ].index * normalSize * 4 );
 
 					}
 
@@ -3024,8 +3028,10 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 						if ( geometryGroup.vertexUvBuffer ) {
 
+							var uvSize = geometryGroup.vertexUvBuffer.itemSize;
+
 							_gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.vertexUvBuffer );
-							_gl.vertexAttribPointer(  attributes.uv, geometryGroup.vertexUvBuffer.itemSize, _gl.FLOAT, false, 0, offsets[ i ].index * 4 * 2 );
+							_gl.vertexAttribPointer(  attributes.uv, uvSize, _gl.FLOAT, false, 0, offsets[ i ].index * uvSize * 4 );
 
 							_gl.enableVertexAttribArray( attributes.uv );
 
@@ -3041,8 +3047,10 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 					if ( attributes.color >= 0 && geometryGroup.vertexColorBuffer ) {
 
+						var colorSize = geometryGroup.vertexColorBuffer.itemSize;
+
 						_gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.vertexColorBuffer );
-						_gl.vertexAttribPointer( attributes.color, geometryGroup.vertexColorBuffer.itemSize, _gl.FLOAT, false, 0, offsets[ i ].index * 4 * 4 );
+						_gl.vertexAttribPointer( attributes.color, colorSize, _gl.FLOAT, false, 0, offsets[ i ].index * colorSize * 4 );
 
 
 					}
@@ -3053,7 +3061,7 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 				// render indexed triangles
 
-				_gl.drawElements( _gl.TRIANGLES, offsets[ i ].count, _gl.UNSIGNED_SHORT, offsets[ i ].start * 2 ); // 2 = Uint16
+				_gl.drawElements( _gl.TRIANGLES, offsets[ i ].count, _gl.UNSIGNED_SHORT, offsets[ i ].start * 2 ); // 2 bytes per Uint16
 
 				_this.info.render.calls ++;
 				_this.info.render.vertices += offsets[ i ].count; // not really true, here vertices can be shared