webgl_marchingcubes.html 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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="https://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 type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import Stats from 'three/addons/libs/stats.module.js';
  27. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. import { MarchingCubes } from 'three/addons/objects/MarchingCubes.js';
  30. import { ToonShader1, ToonShader2, ToonShaderHatching, ToonShaderDotted } from 'three/addons/shaders/ToonShader.js';
  31. let container, stats;
  32. let camera, scene, renderer;
  33. let materials, current_material;
  34. let light, pointLight, ambientLight;
  35. let effect, resolution;
  36. let effectController;
  37. let time = 0;
  38. const clock = new THREE.Clock();
  39. init();
  40. function init() {
  41. container = document.getElementById( 'container' );
  42. // CAMERA
  43. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
  44. camera.position.set( - 500, 500, 1500 );
  45. // SCENE
  46. scene = new THREE.Scene();
  47. scene.background = new THREE.Color( 0x050505 );
  48. // LIGHTS
  49. light = new THREE.DirectionalLight( 0xffffff, 3 );
  50. light.position.set( 0.5, 0.5, 1 );
  51. scene.add( light );
  52. pointLight = new THREE.PointLight( 0xff7c00, 3, 0, 0 );
  53. pointLight.position.set( 0, 0, 100 );
  54. scene.add( pointLight );
  55. ambientLight = new THREE.AmbientLight( 0x323232, 3 );
  56. scene.add( ambientLight );
  57. // MATERIALS
  58. materials = generateMaterials();
  59. current_material = 'shiny';
  60. // MARCHING CUBES
  61. resolution = 28;
  62. effect = new MarchingCubes( resolution, materials[ current_material ], true, true, 100000 );
  63. effect.position.set( 0, 0, 0 );
  64. effect.scale.set( 700, 700, 700 );
  65. effect.enableUvs = false;
  66. effect.enableColors = false;
  67. scene.add( effect );
  68. // RENDERER
  69. renderer = new THREE.WebGLRenderer();
  70. renderer.setPixelRatio( window.devicePixelRatio );
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. renderer.setAnimationLoop( animate );
  73. container.appendChild( renderer.domElement );
  74. // CONTROLS
  75. const controls = new OrbitControls( camera, renderer.domElement );
  76. controls.minDistance = 500;
  77. controls.maxDistance = 5000;
  78. // STATS
  79. stats = new Stats();
  80. container.appendChild( stats.dom );
  81. // GUI
  82. setupGui();
  83. // EVENTS
  84. window.addEventListener( 'resize', onWindowResize );
  85. }
  86. //
  87. function onWindowResize() {
  88. camera.aspect = window.innerWidth / window.innerHeight;
  89. camera.updateProjectionMatrix();
  90. renderer.setSize( window.innerWidth, window.innerHeight );
  91. }
  92. function generateMaterials() {
  93. // environment map
  94. const path = 'textures/cube/SwedishRoyalCastle/';
  95. const format = '.jpg';
  96. const urls = [
  97. path + 'px' + format, path + 'nx' + format,
  98. path + 'py' + format, path + 'ny' + format,
  99. path + 'pz' + format, path + 'nz' + format
  100. ];
  101. const cubeTextureLoader = new THREE.CubeTextureLoader();
  102. const reflectionCube = cubeTextureLoader.load( urls );
  103. const refractionCube = cubeTextureLoader.load( urls );
  104. refractionCube.mapping = THREE.CubeRefractionMapping;
  105. // toons
  106. const toonMaterial1 = createShaderMaterial( ToonShader1, light, ambientLight );
  107. const toonMaterial2 = createShaderMaterial( ToonShader2, light, ambientLight );
  108. const hatchingMaterial = createShaderMaterial( ToonShaderHatching, light, ambientLight );
  109. const dottedMaterial = createShaderMaterial( ToonShaderDotted, light, ambientLight );
  110. const texture = new THREE.TextureLoader().load( 'textures/uv_grid_opengl.jpg' );
  111. texture.wrapS = THREE.RepeatWrapping;
  112. texture.wrapT = THREE.RepeatWrapping;
  113. texture.colorSpace = THREE.SRGBColorSpace;
  114. const materials = {
  115. 'shiny': new THREE.MeshStandardMaterial( { color: 0x9c0000, envMap: reflectionCube, roughness: 0.1, metalness: 1.0 } ),
  116. 'chrome': new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: reflectionCube } ),
  117. 'liquid': new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: refractionCube, refractionRatio: 0.85 } ),
  118. 'matte': new THREE.MeshPhongMaterial( { specular: 0x494949, shininess: 1 } ),
  119. 'flat': new THREE.MeshLambertMaterial( { /*TODO flatShading: true */ } ),
  120. 'textured': new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, shininess: 1, map: texture } ),
  121. 'colors': new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0xffffff, shininess: 2, vertexColors: true } ),
  122. 'multiColors': new THREE.MeshPhongMaterial( { shininess: 2, vertexColors: true } ),
  123. 'plastic': new THREE.MeshPhongMaterial( { specular: 0xc1c1c1, shininess: 250 } ),
  124. 'toon1': toonMaterial1,
  125. 'toon2': toonMaterial2,
  126. 'hatching': hatchingMaterial,
  127. 'dotted': dottedMaterial
  128. };
  129. return materials;
  130. }
  131. function createShaderMaterial( shader, light, ambientLight ) {
  132. const u = THREE.UniformsUtils.clone( shader.uniforms );
  133. const vs = shader.vertexShader;
  134. const fs = shader.fragmentShader;
  135. const material = new THREE.ShaderMaterial( { uniforms: u, vertexShader: vs, fragmentShader: fs } );
  136. material.uniforms[ 'uDirLightPos' ].value = light.position;
  137. material.uniforms[ 'uDirLightColor' ].value = light.color;
  138. material.uniforms[ 'uAmbientLightColor' ].value = ambientLight.color;
  139. return material;
  140. }
  141. //
  142. function setupGui() {
  143. const createHandler = function ( id ) {
  144. return function () {
  145. current_material = id;
  146. effect.material = materials[ id ];
  147. effect.enableUvs = ( current_material === 'textured' ) ? true : false;
  148. effect.enableColors = ( current_material === 'colors' || current_material === 'multiColors' ) ? true : false;
  149. };
  150. };
  151. effectController = {
  152. material: 'shiny',
  153. speed: 1.0,
  154. numBlobs: 10,
  155. resolution: 28,
  156. isolation: 80,
  157. floor: true,
  158. wallx: false,
  159. wallz: false,
  160. dummy: function () {}
  161. };
  162. let h;
  163. const gui = new GUI();
  164. // material (type)
  165. h = gui.addFolder( 'Materials' );
  166. for ( const m in materials ) {
  167. effectController[ m ] = createHandler( m );
  168. h.add( effectController, m ).name( m );
  169. }
  170. // simulation
  171. h = gui.addFolder( 'Simulation' );
  172. h.add( effectController, 'speed', 0.1, 8.0, 0.05 );
  173. h.add( effectController, 'numBlobs', 1, 50, 1 );
  174. h.add( effectController, 'resolution', 14, 100, 1 );
  175. h.add( effectController, 'isolation', 10, 300, 1 );
  176. h.add( effectController, 'floor' );
  177. h.add( effectController, 'wallx' );
  178. h.add( effectController, 'wallz' );
  179. }
  180. // this controls content of marching cubes voxel field
  181. function updateCubes( object, time, numblobs, floor, wallx, wallz ) {
  182. object.reset();
  183. // fill the field with some metaballs
  184. const rainbow = [
  185. new THREE.Color( 0xff0000 ),
  186. new THREE.Color( 0xffbb00 ),
  187. new THREE.Color( 0xffff00 ),
  188. new THREE.Color( 0x00ff00 ),
  189. new THREE.Color( 0x0000ff ),
  190. new THREE.Color( 0x9400bd ),
  191. new THREE.Color( 0xc800eb )
  192. ];
  193. const subtract = 12;
  194. const strength = 1.2 / ( ( Math.sqrt( numblobs ) - 1 ) / 4 + 1 );
  195. for ( let i = 0; i < numblobs; i ++ ) {
  196. const ballx = Math.sin( i + 1.26 * time * ( 1.03 + 0.5 * Math.cos( 0.21 * i ) ) ) * 0.27 + 0.5;
  197. const bally = Math.abs( Math.cos( i + 1.12 * time * Math.cos( 1.22 + 0.1424 * i ) ) ) * 0.77; // dip into the floor
  198. const ballz = Math.cos( i + 1.32 * time * 0.1 * Math.sin( ( 0.92 + 0.53 * i ) ) ) * 0.27 + 0.5;
  199. if ( current_material === 'multiColors' ) {
  200. object.addBall( ballx, bally, ballz, strength, subtract, rainbow[ i % 7 ] );
  201. } else {
  202. object.addBall( ballx, bally, ballz, strength, subtract );
  203. }
  204. }
  205. if ( floor ) object.addPlaneY( 2, 12 );
  206. if ( wallz ) object.addPlaneZ( 2, 12 );
  207. if ( wallx ) object.addPlaneX( 2, 12 );
  208. object.update();
  209. }
  210. //
  211. function animate() {
  212. render();
  213. stats.update();
  214. }
  215. function render() {
  216. const delta = clock.getDelta();
  217. time += delta * effectController.speed * 0.5;
  218. // marching cubes
  219. if ( effectController.resolution !== resolution ) {
  220. resolution = effectController.resolution;
  221. effect.init( Math.floor( resolution ) );
  222. }
  223. if ( effectController.isolation !== effect.isolation ) {
  224. effect.isolation = effectController.isolation;
  225. }
  226. updateCubes( effect, time, effectController.numBlobs, effectController.floor, effectController.wallx, effectController.wallz );
  227. // render
  228. renderer.render( scene, camera );
  229. }
  230. </script>
  231. </body>
  232. </html>