Skip to content

Commit 67a4386

Browse files
authored
Merge pull request #15940 from gkjohnson/marching-cubes-convenience-functions
Add setCell, getCell, blur function to MarchingCubes script
2 parents edeefba + 8933cc7 commit 67a4386

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

examples/js/MarchingCubes.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,79 @@ THREE.MarchingCubes = function ( resolution, material, enableUvs, enableColors )
772772
// Updates
773773
/////////////////////////////////////
774774

775+
this.setCell = function ( x, y, z, value ) {
776+
777+
var index = this.size2 * z + this.size * y + x;
778+
this.field[ index ] = value;
779+
780+
};
781+
782+
this.getCell = function ( x, y, z ) {
783+
784+
var index = this.size2 * z + this.size * y + x;
785+
return this.field[ index ];
786+
787+
};
788+
789+
this.blur = function ( intensity ) {
790+
791+
if ( intensity === undefined ) {
792+
793+
intensity = 1;
794+
795+
}
796+
797+
var field = this.field;
798+
var fieldCopy = field.slice();
799+
var size = this.size;
800+
var size2 = this.size2;
801+
for ( var x = 0; x < size; x ++ ) {
802+
803+
for ( var y = 0; y < size; y ++ ) {
804+
805+
for ( var z = 0; z < size; z ++ ) {
806+
807+
var index = size2 * z + size * y + x;
808+
var val = fieldCopy[ index ];
809+
var count = 1;
810+
811+
for ( var x2 = - 1; x2 <= 1; x2 += 2 ) {
812+
813+
var x3 = x2 + x;
814+
if ( x3 < 0 || x3 >= size ) continue;
815+
816+
for ( var y2 = - 1; y2 <= 1; y2 += 2 ) {
817+
818+
var y3 = y2 + y;
819+
if ( y3 < 0 || y3 >= size ) continue;
820+
821+
for ( var z2 = - 1; z2 <= 1; z2 += 2 ) {
822+
823+
var z3 = z2 + z;
824+
if ( z3 < 0 || z3 >= size ) continue;
825+
826+
var index2 = size2 * z3 + size * y3 + x3;
827+
var val2 = fieldCopy[ index2 ];
828+
829+
count ++;
830+
val += intensity * ( val2 - val ) / count;
831+
832+
}
833+
834+
}
835+
836+
}
837+
838+
field[ index ] = val;
839+
840+
}
841+
842+
}
843+
844+
}
845+
846+
};
847+
775848
this.reset = function () {
776849

777850
var i;

0 commit comments

Comments
 (0)