webgl_marchingcubes.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - marching cubes</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="container"></div>
  11. <div id="info">
  12. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> -
  13. marching cubes<br/>
  14. based on greggman's <a href="https://webglsamples.org/blob/blob.html">blob</a>, original code by Henrik Rydgård
  15. </div>
  16. <script src="../build/three.js"></script>
  17. <script src="js/controls/OrbitControls.js"></script>
  18. <script src="js/MarchingCubes.js"></script>
  19. <script src="js/ShaderToon.js"></script>
  20. <script src="js/WebGL.js"></script>
  21. <script src="js/libs/stats.min.js"></script>
  22. <script src="js/libs/dat.gui.min.js"></script>
  23. <script>
  24. if ( WEBGL.isWebGLAvailable() === false ) {
  25. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  26. }
  27. var container, stats;
  28. var camera, scene, renderer;
  29. var materials, current_material;
  30. var light, pointLight, ambientLight;
  31. var effect, resolution;
  32. var effectController;
  33. var time = 0;
  34. var clock = new THREE.Clock();
  35. init();
  36. animate();
  37. function init() {
  38. container = document.getElementById( 'container' );
  39. // CAMERA
  40. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
  41. camera.position.set( - 500, 500, 1500 );
  42. // SCENE
  43. scene = new THREE.Scene();
  44. scene.background = new THREE.Color( 0x050505 );
  45. // LIGHTS
  46. light = new THREE.DirectionalLight( 0xffffff );
  47. light.position.set( 0.5, 0.5, 1 );
  48. scene.add( light );
  49. pointLight = new THREE.PointLight( 0xff3300 );
  50. pointLight.position.set( 0, 0, 100 );
  51. scene.add( pointLight );
  52. ambientLight = new THREE.AmbientLight( 0x080808 );
  53. scene.add( ambientLight );
  54. // MATERIALS
  55. materials = generateMaterials();
  56. current_material = "shiny";
  57. // MARCHING CUBES
  58. resolution = 28;
  59. effect = new THREE.MarchingCubes( resolution, materials[ current_material ].m, true, true );
  60. effect.position.set( 0, 0, 0 );
  61. effect.scale.set( 700, 700, 700 );
  62. effect.enableUvs = false;
  63. effect.enableColors = false;
  64. scene.add( effect );
  65. // RENDERER
  66. renderer = new THREE.WebGLRenderer();
  67. renderer.gammaOutput = true;
  68. renderer.setPixelRatio( window.devicePixelRatio );
  69. renderer.setSize( window.innerWidth, window.innerHeight );
  70. renderer.domElement.style.position = "absolute";
  71. renderer.domElement.style.top = "0px";
  72. renderer.domElement.style.left = "0px";
  73. container.appendChild( renderer.domElement );
  74. // CONTROLS
  75. var controls = new THREE.OrbitControls( camera, renderer.domElement );
  76. // STATS
  77. stats = new Stats();
  78. container.appendChild( stats.dom );
  79. // GUI
  80. setupGui();
  81. // EVENTS
  82. window.addEventListener( 'resize', onWindowResize, false );
  83. }
  84. //
  85. function onWindowResize() {
  86. camera.aspect = window.innerWidth / window.innerHeight;
  87. camera.updateProjectionMatrix();
  88. renderer.setSize( window.innerWidth, window.innerHeight );
  89. }
  90. function generateMaterials() {
  91. // environment map
  92. var path = "textures/cube/SwedishRoyalCastle/";
  93. var format = '.jpg';
  94. var urls = [
  95. path + 'px' + format, path + 'nx' + format,
  96. path + 'py' + format, path + 'ny' + format,
  97. path + 'pz' + format, path + 'nz' + format
  98. ];
  99. var cubeTextureLoader = new THREE.CubeTextureLoader();
  100. var reflectionCube = cubeTextureLoader.load( urls );
  101. var refractionCube = cubeTextureLoader.load( urls );
  102. refractionCube.mapping = THREE.CubeRefractionMapping;
  103. // toons
  104. var toonMaterial1 = createShaderMaterial( "toon1", light, ambientLight );
  105. var toonMaterial2 = createShaderMaterial( "toon2", light, ambientLight );
  106. var hatchingMaterial = createShaderMaterial( "hatching", light, ambientLight );
  107. var dottedMaterial = createShaderMaterial( "dotted", light, ambientLight );
  108. var texture = new THREE.TextureLoader().load( "textures/UV_Grid_Sm.jpg" );
  109. texture.wrapS = THREE.RepeatWrapping;
  110. texture.wrapT = THREE.RepeatWrapping;
  111. var materials = {
  112. "chrome": {
  113. m: new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: reflectionCube } ),
  114. h: 0, s: 0, l: 1
  115. },
  116. "liquid": {
  117. m: new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: refractionCube, refractionRatio: 0.85 } ),
  118. h: 0, s: 0, l: 1
  119. },
  120. "shiny": {
  121. m: new THREE.MeshStandardMaterial( { color: 0x550000, envMap: reflectionCube, roughness: 0.1, metalness: 1.0 } ),
  122. h: 0, s: 0.8, l: 0.2
  123. },
  124. "matte": {
  125. m: new THREE.MeshPhongMaterial( { color: 0x000000, specular: 0x111111, shininess: 1 } ),
  126. h: 0, s: 0, l: 1
  127. },
  128. "flat": {
  129. m: new THREE.MeshLambertMaterial( { color: 0x000000, flatShading: true } ),
  130. h: 0, s: 0, l: 1
  131. },
  132. "textured": {
  133. m: new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, shininess: 1, map: texture } ),
  134. h: 0, s: 0, l: 1
  135. },
  136. "colors": {
  137. m: new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0xffffff, shininess: 2, vertexColors: THREE.VertexColors } ),
  138. h: 0, s: 0, l: 1
  139. },
  140. "multiColors": {
  141. m: new THREE.MeshPhongMaterial( { shininess: 2, vertexColors: THREE.VertexColors } ),
  142. h: 0, s: 0, l: 1
  143. },
  144. "plastic": {
  145. m: new THREE.MeshPhongMaterial( { color: 0x000000, specular: 0x888888, shininess: 250 } ),
  146. h: 0.6, s: 0.8, l: 0.1
  147. },
  148. "toon1": {
  149. m: toonMaterial1,
  150. h: 0.2, s: 1, l: 0.75
  151. },
  152. "toon2": {
  153. m: toonMaterial2,
  154. h: 0.4, s: 1, l: 0.75
  155. },
  156. "hatching": {
  157. m: hatchingMaterial,
  158. h: 0.2, s: 1, l: 0.9
  159. },
  160. "dotted": {
  161. m: dottedMaterial,
  162. h: 0.2, s: 1, l: 0.9
  163. }
  164. };
  165. return materials;
  166. }
  167. function createShaderMaterial( id, light, ambientLight ) {
  168. var shader = THREE.ShaderToon[ id ];
  169. var u = THREE.UniformsUtils.clone( shader.uniforms );
  170. var vs = shader.vertexShader;
  171. var fs = shader.fragmentShader;
  172. var material = new THREE.ShaderMaterial( { uniforms: u, vertexShader: vs, fragmentShader: fs } );
  173. material.uniforms[ "uDirLightPos" ].value = light.position;
  174. material.uniforms[ "uDirLightColor" ].value = light.color;
  175. material.uniforms[ "uAmbientLightColor" ].value = ambientLight.color;
  176. return material;
  177. }
  178. //
  179. function setupGui() {
  180. var createHandler = function ( id ) {
  181. return function () {
  182. var mat_old = materials[ current_material ];
  183. mat_old.h = m_h.getValue();
  184. mat_old.s = m_s.getValue();
  185. mat_old.l = m_l.getValue();
  186. current_material = id;
  187. var mat = materials[ id ];
  188. effect.material = mat.m;
  189. m_h.setValue( mat.h );
  190. m_s.setValue( mat.s );
  191. m_l.setValue( mat.l );
  192. effect.enableUvs = ( current_material === "textured" ) ? true : false;
  193. effect.enableColors = ( current_material === "colors" || current_material === "multiColors" ) ? true : false;
  194. };
  195. };
  196. effectController = {
  197. material: "shiny",
  198. speed: 1.0,
  199. numBlobs: 10,
  200. resolution: 28,
  201. isolation: 80,
  202. floor: true,
  203. wallx: false,
  204. wallz: false,
  205. hue: 0.0,
  206. saturation: 0.8,
  207. lightness: 0.1,
  208. lhue: 0.04,
  209. lsaturation: 1.0,
  210. llightness: 0.5,
  211. lx: 0.5,
  212. ly: 0.5,
  213. lz: 1.0,
  214. dummy: function () {}
  215. };
  216. var h, m_h, m_s, m_l;
  217. var gui = new dat.GUI();
  218. // material (type)
  219. h = gui.addFolder( "Materials" );
  220. for ( var m in materials ) {
  221. effectController[ m ] = createHandler( m );
  222. h.add( effectController, m ).name( m );
  223. }
  224. // material (color)
  225. h = gui.addFolder( "Material color" );
  226. m_h = h.add( effectController, "hue", 0.0, 1.0, 0.025 );
  227. m_s = h.add( effectController, "saturation", 0.0, 1.0, 0.025 );
  228. m_l = h.add( effectController, "lightness", 0.0, 1.0, 0.025 );
  229. // light (point)
  230. h = gui.addFolder( "Point light color" );
  231. h.add( effectController, "lhue", 0.0, 1.0, 0.025 ).name( "hue" );
  232. h.add( effectController, "lsaturation", 0.0, 1.0, 0.025 ).name( "saturation" );
  233. h.add( effectController, "llightness", 0.0, 1.0, 0.025 ).name( "lightness" );
  234. // light (directional)
  235. h = gui.addFolder( "Directional light orientation" );
  236. h.add( effectController, "lx", - 1.0, 1.0, 0.025 ).name( "x" );
  237. h.add( effectController, "ly", - 1.0, 1.0, 0.025 ).name( "y" );
  238. h.add( effectController, "lz", - 1.0, 1.0, 0.025 ).name( "z" );
  239. // simulation
  240. h = gui.addFolder( "Simulation" );
  241. h.add( effectController, "speed", 0.1, 8.0, 0.05 );
  242. h.add( effectController, "numBlobs", 1, 50, 1 );
  243. h.add( effectController, "resolution", 14, 100, 1 );
  244. h.add( effectController, "isolation", 10, 300, 1 );
  245. h.add( effectController, "floor" );
  246. h.add( effectController, "wallx" );
  247. h.add( effectController, "wallz" );
  248. }
  249. // this controls content of marching cubes voxel field
  250. function updateCubes( object, time, numblobs, floor, wallx, wallz ) {
  251. object.reset();
  252. // fill the field with some metaballs
  253. var i, ballx, bally, ballz, subtract, strength;
  254. var rainbow = [
  255. new THREE.Color(0xff0000),
  256. new THREE.Color(0xff7f00),
  257. new THREE.Color(0xffff00),
  258. new THREE.Color(0x00ff00),
  259. new THREE.Color(0x0000ff),
  260. new THREE.Color(0x4b0082),
  261. new THREE.Color(0x9400d3)
  262. ];
  263. subtract = 12;
  264. strength = 1.2 / ( ( Math.sqrt( numblobs ) - 1 ) / 4 + 1 );
  265. for ( i = 0; i < numblobs; i ++ ) {
  266. ballx = Math.sin( i + 1.26 * time * ( 1.03 + 0.5 * Math.cos( 0.21 * i ) ) ) * 0.27 + 0.5;
  267. bally = Math.abs( Math.cos( i + 1.12 * time * Math.cos( 1.22 + 0.1424 * i ) ) ) * 0.77; // dip into the floor
  268. ballz = Math.cos( i + 1.32 * time * 0.1 * Math.sin( ( 0.92 + 0.53 * i ) ) ) * 0.27 + 0.5;
  269. if ( current_material === 'multiColors' ) {
  270. object.addBall( ballx, bally, ballz, strength, subtract, rainbow[i % 7] );
  271. } else {
  272. object.addBall( ballx, bally, ballz, strength, subtract );
  273. }
  274. }
  275. if ( floor ) object.addPlaneY( 2, 12 );
  276. if ( wallz ) object.addPlaneZ( 2, 12 );
  277. if ( wallx ) object.addPlaneX( 2, 12 );
  278. }
  279. //
  280. function animate() {
  281. requestAnimationFrame( animate );
  282. render();
  283. stats.update();
  284. }
  285. function render() {
  286. var delta = clock.getDelta();
  287. time += delta * effectController.speed * 0.5;
  288. // marching cubes
  289. if ( effectController.resolution !== resolution ) {
  290. resolution = effectController.resolution;
  291. effect.init( Math.floor( resolution ) );
  292. }
  293. if ( effectController.isolation !== effect.isolation ) {
  294. effect.isolation = effectController.isolation;
  295. }
  296. updateCubes( effect, time, effectController.numBlobs, effectController.floor, effectController.wallx, effectController.wallz );
  297. // materials
  298. if ( effect.material instanceof THREE.ShaderMaterial ) {
  299. effect.material.uniforms[ "uBaseColor" ].value.setHSL( effectController.hue, effectController.saturation, effectController.lightness );
  300. } else {
  301. effect.material.color.setHSL( effectController.hue, effectController.saturation, effectController.lightness );
  302. }
  303. // lights
  304. light.position.set( effectController.lx, effectController.ly, effectController.lz );
  305. light.position.normalize();
  306. pointLight.color.setHSL( effectController.lhue, effectController.lsaturation, effectController.llightness );
  307. // render
  308. renderer.render( scene, camera );
  309. }
  310. </script>
  311. </body>
  312. </html>