-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawMetaball.html
391 lines (381 loc) · 13.9 KB
/
drawMetaball.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - marching cubes</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
</head>
<body>
<div id="container"></div>
<div id="info">
<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> -
marching cubes<br/>
based on greggman's <a href="https://webglsamples.org/blob/blob.html">blob</a>, original code by Henrik Rydgård<br />
<div id="coord"></div>
</div>
<script type="module">
import * as THREE from './Utils/js/three.module.js';
import Stats from './Utils/js/mod/stats.module.js';
import { GUI } from './Utils/js/mod/dat.gui.module.js';
import { OrbitControls } from './Utils/js/mod/OrbitControls.js';
import { MarchingCubes } from './Utils/js/mod/MarchingCubes.js';
import { ToonShader1, ToonShader2, ToonShaderHatching, ToonShaderDotted } from './Utils/js/mod/ToonShader.js';
var container, stats;
var camera, scene, renderer;
var materials, current_material;
var light, pointLight, ambientLight;
var effect, resolution;
var effectController;
var time = 0;
var clock = new THREE.Clock();
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();
var geometry = new THREE.PlaneBufferGeometry( 2000, 2000 );
geometry.rotateX( - Math.PI / 2 );
var plane = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { visible: false } ) );
plane.name = 'workingplane';
var metaball = [];
var dimension = 500;
var infohtml = '';
/**/
init();
animate();
function init() {
container = document.getElementById( 'container' );
// CAMERA
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
//camera.position.set( - 500, 500, 1500 );
camera.position.set(0, 1000, 0 );
camera.lookAt( new THREE.Vector3() );
// SCENE
scene = new THREE.Scene();
scene.background = new THREE.Color( 0x050505 );
// LIGHTS
light = new THREE.DirectionalLight( 0xffffff );
light.position.set( 0.5, 0.5, 1 );
scene.add( light );
pointLight = new THREE.PointLight( 0xff3300 );
pointLight.position.set( 0, 0, 100 );
scene.add( pointLight );
ambientLight = new THREE.AmbientLight( 0x080808 );
scene.add( ambientLight );
// MATERIALS
materials = generateMaterials();
current_material = "shiny";
// MARCHING CUBES
resolution = 28;
effect = new MarchingCubes( resolution, materials[ current_material ].m, true, true );
effect.position.set( 0, 0, 0 );
effect.scale.set( dimension, dimension, dimension );
effect.enableUvs = false;
effect.enableColors = false;
effect.addPlaneY( 2, 12 );
scene.add( effect );
scene.add( plane );
// RENDERER
renderer = new THREE.WebGLRenderer();
renderer.gammaOutput = true;
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.domElement.style.position = "absolute";
renderer.domElement.style.top = "0px";
renderer.domElement.style.left = "0px";
container.appendChild( renderer.domElement );
// CONTROLS
//var controls = new OrbitControls( camera, renderer.domElement );
// STATS
stats = new Stats();
container.appendChild( stats.dom );
// GUI
setupGui();
// EVENTS
window.addEventListener( 'resize', onWindowResize, false );
document.addEventListener( 'mousedown', onDocumentMouseDown.bind(this), false );
infohtml = document.getElementById('coord');
}
//
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function onDocumentMouseDown( event ) {
var x = event.clientX;
var y = event.clientY;
mousedown(event, x,y);
}
function mousedown( event, x, y,me ) {
var maxX = x,
minX = x,
maxY = y,
minY = y;
mouse.set( ( x / window.innerWidth ) * 2 - 1, - ( y / window.innerHeight ) * 2 + 1 );
raycaster.setFromCamera( mouse, camera );
var intersects = raycaster.intersectObjects( scene.children, true );
if ( intersects.length > 0 ) {
intersects.map(
function(e){
//info2.innerHTML += e.object.name;
}
);
if(intersects[ 0 ].object.name.indexOf('workingplane') !== -1){
//me.draw_mode = true
let intersect = intersects[ 0 ];
//let X = 0.5-(intersect.point.x/1000);
//let Y = 0.5-(intersect.point.y/1000);
//let Z = 0.5-(intersect.point.z/1000);
let X = 0.5-(intersect.point.x/1000);
let Y = 0.5-(intersect.point.y/1000);
let Z = 0.5-(intersect.point.z/1000);
addCubes(effect,X,Y,Z);
/*var voxel = me.Sphere(0xffff00);
voxel.name = 'f3d_sphere_' + me.number_of_f3d_spheres;
me.number_of_f3d_spheres += 1;
me.setOldCoord(intersect.point.x,intersect.point.z);
me.setLastSphereCenter(intersect.point.x,intersect.point.z);
voxel.position.copy( intersect.point ).add( intersect.face.normal );
//voxel.position.divideScalar( 50 ).floor().multiplyScalar( 50 ).addScalar( 25 );
me.scene.add( voxel );
me.f3d_scene[0].push(me.scene.children.length-1);*/
}
} else {
console.log('nothing here');
}
//me.render();
}
function generateMaterials() {
// environment map
var path = "textures/cube/SwedishRoyalCastle/";
var format = '.jpg';
var urls = [
path + 'px' + format, path + 'nx' + format,
path + 'py' + format, path + 'ny' + format,
path + 'pz' + format, path + 'nz' + format
];
var cubeTextureLoader = new THREE.CubeTextureLoader();
var reflectionCube = cubeTextureLoader.load( urls );
var refractionCube = cubeTextureLoader.load( urls );
refractionCube.mapping = THREE.CubeRefractionMapping;
// toons
var toonMaterial1 = createShaderMaterial( ToonShader1, light, ambientLight );
var toonMaterial2 = createShaderMaterial( ToonShader2, light, ambientLight );
var hatchingMaterial = createShaderMaterial( ToonShaderHatching, light, ambientLight );
var dottedMaterial = createShaderMaterial( ToonShaderDotted, light, ambientLight );
var texture = new THREE.TextureLoader().load( "textures/uv_grid_opengl.jpg" );
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
var materials = {
"chrome": {
m: new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: reflectionCube } ),
h: 0, s: 0, l: 1
},
"liquid": {
m: new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: refractionCube, refractionRatio: 0.85 } ),
h: 0, s: 0, l: 1
},
"shiny": {
m: new THREE.MeshStandardMaterial( { color: 0x550000, envMap: reflectionCube, roughness: 0.1, metalness: 1.0 } ),
h: 0, s: 0.8, l: 0.2
},
"matte": {
m: new THREE.MeshPhongMaterial( { color: 0x000000, specular: 0x111111, shininess: 1 } ),
h: 0, s: 0, l: 1
},
"flat": {
m: new THREE.MeshLambertMaterial( { color: 0x000000, flatShading: true } ),
h: 0, s: 0, l: 1
},
"textured": {
m: new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, shininess: 1, map: texture } ),
h: 0, s: 0, l: 1
},
"colors": {
m: new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0xffffff, shininess: 2, vertexColors: THREE.VertexColors } ),
h: 0, s: 0, l: 1
},
"multiColors": {
m: new THREE.MeshPhongMaterial( { shininess: 2, vertexColors: THREE.VertexColors } ),
h: 0, s: 0, l: 1
},
"plastic": {
m: new THREE.MeshPhongMaterial( { color: 0x000000, specular: 0x888888, shininess: 250 } ),
h: 0.6, s: 0.8, l: 0.1
},
"toon1": {
m: toonMaterial1,
h: 0.2, s: 1, l: 0.75
},
"toon2": {
m: toonMaterial2,
h: 0.4, s: 1, l: 0.75
},
"hatching": {
m: hatchingMaterial,
h: 0.2, s: 1, l: 0.9
},
"dotted": {
m: dottedMaterial,
h: 0.2, s: 1, l: 0.9
}
};
return materials;
}
function createShaderMaterial( shader, light, ambientLight ) {
var u = THREE.UniformsUtils.clone( shader.uniforms );
var vs = shader.vertexShader;
var fs = shader.fragmentShader;
var material = new THREE.ShaderMaterial( { uniforms: u, vertexShader: vs, fragmentShader: fs } );
material.uniforms[ "uDirLightPos" ].value = light.position;
material.uniforms[ "uDirLightColor" ].value = light.color;
material.uniforms[ "uAmbientLightColor" ].value = ambientLight.color;
return material;
}
//
function setupGui() {
var createHandler = function ( id ) {
return function () {
var mat_old = materials[ current_material ];
mat_old.h = m_h.getValue();
mat_old.s = m_s.getValue();
mat_old.l = m_l.getValue();
current_material = id;
var mat = materials[ id ];
effect.material = mat.m;
m_h.setValue( mat.h );
m_s.setValue( mat.s );
m_l.setValue( mat.l );
effect.enableUvs = ( current_material === "textured" ) ? true : false;
effect.enableColors = ( current_material === "colors" || current_material === "multiColors" ) ? true : false;
};
};
effectController = {
material: "shiny",
speed: 1.0,
numBlobs: 10,
resolution: 28,
isolation: 80,
floor: true,
wallx: false,
wallz: false,
hue: 0.0,
saturation: 0.8,
lightness: 0.1,
lhue: 0.04,
lsaturation: 1.0,
llightness: 0.5,
lx: 0.5,
ly: 0.5,
lz: 1.0,
dummy: function () {}
};
var h, m_h, m_s, m_l;
var gui = new GUI();
// material (type)
h = gui.addFolder( "Materials" );
for ( var m in materials ) {
effectController[ m ] = createHandler( m );
h.add( effectController, m ).name( m );
}
// material (color)
h = gui.addFolder( "Material color" );
m_h = h.add( effectController, "hue", 0.0, 1.0, 0.025 );
m_s = h.add( effectController, "saturation", 0.0, 1.0, 0.025 );
m_l = h.add( effectController, "lightness", 0.0, 1.0, 0.025 );
// light (point)
h = gui.addFolder( "Point light color" );
h.add( effectController, "lhue", 0.0, 1.0, 0.025 ).name( "hue" );
h.add( effectController, "lsaturation", 0.0, 1.0, 0.025 ).name( "saturation" );
h.add( effectController, "llightness", 0.0, 1.0, 0.025 ).name( "lightness" );
// light (directional)
h = gui.addFolder( "Directional light orientation" );
h.add( effectController, "lx", - 1.0, 1.0, 0.025 ).name( "x" );
h.add( effectController, "ly", - 1.0, 1.0, 0.025 ).name( "y" );
h.add( effectController, "lz", - 1.0, 1.0, 0.025 ).name( "z" );
// simulation
h = gui.addFolder( "Simulation" );
h.add( effectController, "speed", 0.1, 8.0, 0.05 );
h.add( effectController, "numBlobs", 1, 50, 1 );
h.add( effectController, "resolution", 14, 100, 1 );
h.add( effectController, "isolation", 10, 300, 1 );
h.add( effectController, "floor" );
h.add( effectController, "wallx" );
h.add( effectController, "wallz" );
}
// this controls content of marching cubes voxel field
function updateCubes( object, time, numblobs, floor, wallx, wallz ) {
object.reset();
// fill the field with some metaballs
var i, ballx, bally, ballz, subtract, strength;
var rainbow = [
new THREE.Color( 0xff0000 ),
new THREE.Color( 0xff7f00 ),
new THREE.Color( 0xffff00 ),
new THREE.Color( 0x00ff00 ),
new THREE.Color( 0x0000ff ),
new THREE.Color( 0x4b0082 ),
new THREE.Color( 0x9400d3 )
];
subtract = 12;
strength = 1.2 / ( ( Math.sqrt( numblobs ) - 1 ) / 4 + 1 );
numblobs = metaball.length;
for ( i = 0; i < numblobs; i ++ ) {
ballx = Math.sin( i + 1.26 * time * ( 1.03 + 0.5 * Math.cos( 0.21 * i ) ) ) * 0.27 + 0.5;
bally = Math.abs( Math.cos( i + 1.12 * time * Math.cos( 1.22 + 0.1424 * i ) ) ) * 0.77; // dip into the floor
ballz = Math.cos( i + 1.32 * time * 0.1 * Math.sin( ( 0.92 + 0.53 * i ) ) ) * 0.27 + 0.5;
if ( current_material === 'multiColors' ) {
object.addBall( metaball[i]._X, metaball[i]._Y, metaball[i]._Z, strength, subtract, rainbow[ i % 7 ] );
} else {
object.addBall( metaball[i]._X, metaball[i]._Y, metaball[i]._Z, strength, subtract );
}
/*
if ( current_material === 'multiColors' ) {
object.addBall( ballx, bally, ballz, strength, subtract, rainbow[ i % 7 ] );
} else {
object.addBall( ballx, bally, ballz, strength, subtract );
}
*/
}
if ( floor ) object.addPlaneY( 2, 12 );
if ( wallz ) object.addPlaneZ( 2, 12 );
if ( wallx ) object.addPlaneX( 2, 12 );
}
//addCubes( effect, time, effectController.numBlobs, effectController.floor, effectController.wallx, effectController.wallz );
function addCubes( object, x, y, z ) {
infohtml.innerText = 'x: ' + (1-x) + ', y: ' + y + ', z: ' + (1-z);
metaball.push({_X:(1-x),_Y:y,_Z:(1-z)})
}
//
function animate() {
requestAnimationFrame( animate );
render();
stats.update();
}
function render() {
var delta = clock.getDelta();
time += delta * effectController.speed * 0.5;
// marching cubes
if ( effectController.resolution !== resolution ) {
resolution = effectController.resolution;
effect.init( Math.floor( resolution ) );
}
if ( effectController.isolation !== effect.isolation ) {
effect.isolation = effectController.isolation;
}
updateCubes( effect, time, effectController.numBlobs, effectController.floor, effectController.wallx, effectController.wallz );
// materials
if ( effect.material instanceof THREE.ShaderMaterial ) {
effect.material.uniforms[ "uBaseColor" ].value.setHSL( effectController.hue, effectController.saturation, effectController.lightness );
} else {
effect.material.color.setHSL( effectController.hue, effectController.saturation, effectController.lightness );
}
// lights
light.position.set( effectController.lx, effectController.ly, effectController.lz );
light.position.normalize();
pointLight.color.setHSL( effectController.lhue, effectController.lsaturation, effectController.llightness );
// render
renderer.render( scene, camera );
}
</script>
</body>
</html>