Skip to content

Commit 7f8a2e9

Browse files
committed
WebGLRenderer: Moved material logic out of renderBufferImmediate().
1 parent 7d09eeb commit 7f8a2e9

File tree

3 files changed

+43
-53
lines changed

3 files changed

+43
-53
lines changed

examples/js/MarchingCubes.js

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -342,31 +342,54 @@ THREE.MarchingCubes = function ( resolution, material, enableUvs, enableColors )
342342

343343
// normals
344344

345-
scope.normalArray[ c + 0 ] = norm[ o1 ];
346-
scope.normalArray[ c + 1 ] = norm[ o1 + 1 ];
347-
scope.normalArray[ c + 2 ] = norm[ o1 + 2 ];
345+
if ( scope.material.flatShading === true ) {
348346

349-
scope.normalArray[ c + 3 ] = norm[ o2 ];
350-
scope.normalArray[ c + 4 ] = norm[ o2 + 1 ];
351-
scope.normalArray[ c + 5 ] = norm[ o2 + 2 ];
347+
var nx = ( norm[ o1 + 0 ] + norm[ o2 + 0 ] + norm[ o3 + 0 ] ) / 3;
348+
var ny = ( norm[ o1 + 1 ] + norm[ o2 + 1 ] + norm[ o3 + 1 ] ) / 3;
349+
var nz = ( norm[ o1 + 2 ] + norm[ o2 + 2 ] + norm[ o3 + 2 ] ) / 3;
352350

353-
scope.normalArray[ c + 6 ] = norm[ o3 ];
354-
scope.normalArray[ c + 7 ] = norm[ o3 + 1 ];
355-
scope.normalArray[ c + 8 ] = norm[ o3 + 2 ];
351+
scope.normalArray[ c + 0 ] = nx;
352+
scope.normalArray[ c + 1 ] = ny;
353+
scope.normalArray[ c + 2 ] = nz;
354+
355+
scope.normalArray[ c + 3 ] = nx;
356+
scope.normalArray[ c + 4 ] = ny;
357+
scope.normalArray[ c + 5 ] = nz;
358+
359+
scope.normalArray[ c + 6 ] = nx;
360+
scope.normalArray[ c + 7 ] = ny;
361+
scope.normalArray[ c + 8 ] = nz;
362+
363+
364+
} else {
365+
366+
scope.normalArray[ c + 0 ] = norm[ o1 + 0 ];
367+
scope.normalArray[ c + 1 ] = norm[ o1 + 1 ];
368+
scope.normalArray[ c + 2 ] = norm[ o1 + 2 ];
369+
370+
scope.normalArray[ c + 3 ] = norm[ o2 + 0 ];
371+
scope.normalArray[ c + 4 ] = norm[ o2 + 1 ];
372+
scope.normalArray[ c + 5 ] = norm[ o2 + 2 ];
373+
374+
scope.normalArray[ c + 6 ] = norm[ o3 + 0 ];
375+
scope.normalArray[ c + 7 ] = norm[ o3 + 1 ];
376+
scope.normalArray[ c + 8 ] = norm[ o3 + 2 ];
377+
378+
}
356379

357380
// uvs
358381

359382
if ( scope.enableUvs ) {
360383

361384
var d = scope.count * 2;
362385

363-
scope.uvArray[ d + 0 ] = pos[ o1 ];
386+
scope.uvArray[ d + 0 ] = pos[ o1 + 0 ];
364387
scope.uvArray[ d + 1 ] = pos[ o1 + 2 ];
365388

366-
scope.uvArray[ d + 2 ] = pos[ o2 ];
389+
scope.uvArray[ d + 2 ] = pos[ o2 + 0 ];
367390
scope.uvArray[ d + 3 ] = pos[ o2 + 2 ];
368391

369-
scope.uvArray[ d + 4 ] = pos[ o3 ];
392+
scope.uvArray[ d + 4 ] = pos[ o3 + 0 ];
370393
scope.uvArray[ d + 5 ] = pos[ o3 + 2 ];
371394

372395
}
@@ -375,15 +398,15 @@ THREE.MarchingCubes = function ( resolution, material, enableUvs, enableColors )
375398

376399
if ( scope.enableColors ) {
377400

378-
scope.colorArray[ c + 0 ] = pos[ o1 ];
401+
scope.colorArray[ c + 0 ] = pos[ o1 + 0 ];
379402
scope.colorArray[ c + 1 ] = pos[ o1 + 1 ];
380403
scope.colorArray[ c + 2 ] = pos[ o1 + 2 ];
381404

382-
scope.colorArray[ c + 3 ] = pos[ o2 ];
405+
scope.colorArray[ c + 3 ] = pos[ o2 + 0 ];
383406
scope.colorArray[ c + 4 ] = pos[ o2 + 1 ];
384407
scope.colorArray[ c + 5 ] = pos[ o2 + 2 ];
385408

386-
scope.colorArray[ c + 6 ] = pos[ o3 ];
409+
scope.colorArray[ c + 6 ] = pos[ o3 + 0 ];
387410
scope.colorArray[ c + 7 ] = pos[ o3 + 1 ];
388411
scope.colorArray[ c + 8 ] = pos[ o3 + 2 ];
389412

@@ -438,13 +461,13 @@ THREE.MarchingCubes = function ( resolution, material, enableUvs, enableColors )
438461
this.hasPositions = true;
439462
this.hasNormals = true;
440463

441-
if ( this.enableUvs ) {
464+
if ( this.enableUvs && this.material.map ) {
442465

443466
this.hasUvs = true;
444467

445468
}
446469

447-
if ( this.enableColors ) {
470+
if ( this.enableColors && this.material.vertexColors !== THREE.NoColors ) {
448471

449472
this.hasColors = true;
450473

examples/webgl_marchingcubes.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@
313313

314314
"flat" :
315315
{
316-
m: new THREE.MeshPhongMaterial( { color: 0x000000, specular: 0x111111, shininess: 1, flatShading: true } ),
316+
m: new THREE.MeshLambertMaterial( { color: 0x000000, flatShading: true } ),
317317
h: 0, s: 0, l: 1
318318
},
319319

src/renderers/WebGLRenderer.js

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -602,62 +602,29 @@ function WebGLRenderer( parameters ) {
602602
if ( object.hasNormals ) {
603603

604604
_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.normal );
605-
606-
if ( ! material.isMeshPhongMaterial &&
607-
! material.isMeshStandardMaterial &&
608-
! material.isMeshNormalMaterial &&
609-
material.flatShading === true ) {
610-
611-
for ( var i = 0, l = object.count * 3; i < l; i += 9 ) {
612-
613-
var array = object.normalArray;
614-
615-
var nx = ( array[ i + 0 ] + array[ i + 3 ] + array[ i + 6 ] ) / 3;
616-
var ny = ( array[ i + 1 ] + array[ i + 4 ] + array[ i + 7 ] ) / 3;
617-
var nz = ( array[ i + 2 ] + array[ i + 5 ] + array[ i + 8 ] ) / 3;
618-
619-
array[ i + 0 ] = nx;
620-
array[ i + 1 ] = ny;
621-
array[ i + 2 ] = nz;
622-
623-
array[ i + 3 ] = nx;
624-
array[ i + 4 ] = ny;
625-
array[ i + 5 ] = nz;
626-
627-
array[ i + 6 ] = nx;
628-
array[ i + 7 ] = ny;
629-
array[ i + 8 ] = nz;
630-
631-
}
632-
633-
}
634-
635605
_gl.bufferData( _gl.ARRAY_BUFFER, object.normalArray, _gl.DYNAMIC_DRAW );
636606

637607
state.enableAttribute( programAttributes.normal );
638-
639608
_gl.vertexAttribPointer( programAttributes.normal, 3, _gl.FLOAT, false, 0, 0 );
640609

641610
}
642611

643-
if ( object.hasUvs && material.map ) {
612+
if ( object.hasUvs ) {
644613

645614
_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.uv );
646615
_gl.bufferData( _gl.ARRAY_BUFFER, object.uvArray, _gl.DYNAMIC_DRAW );
647616

648617
state.enableAttribute( programAttributes.uv );
649-
650618
_gl.vertexAttribPointer( programAttributes.uv, 2, _gl.FLOAT, false, 0, 0 );
651619

652620
}
653621

654-
if ( object.hasColors && material.vertexColors !== NoColors ) {
622+
if ( object.hasColors ) {
655623

656624
_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.color );
657625
_gl.bufferData( _gl.ARRAY_BUFFER, object.colorArray, _gl.DYNAMIC_DRAW );
658626

659627
state.enableAttribute( programAttributes.color );
660-
661628
_gl.vertexAttribPointer( programAttributes.color, 3, _gl.FLOAT, false, 0, 0 );
662629

663630
}

0 commit comments

Comments
 (0)