webgl_marchingcubes.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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. <style>
  8. body {
  9. color: #fff;
  10. font-family: Monospace;
  11. font-size: 13px;
  12. text-align: center;
  13. background-color: #000;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #info {
  18. color: #ffffff;
  19. position: absolute;
  20. top: 0px;
  21. width: 100%;
  22. padding: 5px;
  23. }
  24. a {
  25. color: gold;
  26. }
  27. #oldie {
  28. font-family: monospace;
  29. font-size: 13px;
  30. text-align: center;
  31. background: rgb(0, 0, 50);
  32. color: #fff;
  33. padding: 1em;
  34. width: 475px;
  35. margin: 5em auto 0;
  36. display: none;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <div id="container"></div>
  42. <div id="info">
  43. <a href="http://threejs.org" target="_blank">three.js</a> -
  44. marching cubes -
  45. [based on greggman's <a href="http://webglsamples.googlecode.com/hg/blob/blob.html">blob</a>, original code by Henrik Rydgård]
  46. </div>
  47. <script src="../build/three.js"></script>
  48. <script src="js/controls/OrbitControls.js"></script>
  49. <script src="js/shaders/CopyShader.js"></script>
  50. <script src="js/shaders/FXAAShader.js"></script>
  51. <script src="js/shaders/HorizontalTiltShiftShader.js"></script>
  52. <script src="js/shaders/VerticalTiltShiftShader.js"></script>
  53. <script src="js/postprocessing/EffectComposer.js"></script>
  54. <script src="js/postprocessing/RenderPass.js"></script>
  55. <script src="js/postprocessing/BloomPass.js"></script>
  56. <script src="js/postprocessing/ShaderPass.js"></script>
  57. <script src="js/postprocessing/MaskPass.js"></script>
  58. <script src="js/postprocessing/SavePass.js"></script>
  59. <script src="js/MarchingCubes.js"></script>
  60. <script src="js/ShaderToon.js"></script>
  61. <script src="js/Detector.js"></script>
  62. <script src="js/libs/stats.min.js"></script>
  63. <script src="js/libs/dat.gui.min.js"></script>
  64. <script>
  65. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  66. var MARGIN = 0;
  67. var SCREEN_WIDTH = window.innerWidth;
  68. var SCREEN_HEIGHT = window.innerHeight - 2 * MARGIN;
  69. var container, stats;
  70. var camera, scene, renderer;
  71. var mesh, texture, geometry, materials, material, current_material;
  72. var light, pointLight, ambientLight;
  73. var effect, resolution, numBlobs;
  74. var composer, effectFXAA, hblur, vblur;
  75. var effectController;
  76. var time = 0;
  77. var clock = new THREE.Clock();
  78. init();
  79. animate();
  80. function init() {
  81. container = document.getElementById( 'container' );
  82. // CAMERA
  83. camera = new THREE.PerspectiveCamera( 45, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000 );
  84. camera.position.set( -500, 500, 1500 );
  85. // SCENE
  86. scene = new THREE.Scene();
  87. // LIGHTS
  88. light = new THREE.DirectionalLight( 0xffffff );
  89. light.position.set( 0.5, 0.5, 1 );
  90. scene.add( light );
  91. pointLight = new THREE.PointLight( 0xff3300 );
  92. pointLight.position.set( 0, 0, 100 );
  93. scene.add( pointLight );
  94. ambientLight = new THREE.AmbientLight( 0x080808 );
  95. scene.add( ambientLight );
  96. // MATERIALS
  97. materials = generateMaterials();
  98. current_material = "shiny";
  99. // MARCHING CUBES
  100. resolution = 28;
  101. numBlobs = 10;
  102. effect = new THREE.MarchingCubes( resolution, materials[ current_material ].m, true, true );
  103. effect.position.set( 0, 0, 0 );
  104. effect.scale.set( 700, 700, 700 );
  105. effect.enableUvs = false;
  106. effect.enableColors = false;
  107. scene.add( effect );
  108. // RENDERER
  109. renderer = new THREE.WebGLRenderer();
  110. renderer.setClearColor( 0x050505 );
  111. renderer.setPixelRatio( window.devicePixelRatio );
  112. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  113. renderer.domElement.style.position = "absolute";
  114. renderer.domElement.style.top = MARGIN + "px";
  115. renderer.domElement.style.left = "0px";
  116. container.appendChild( renderer.domElement );
  117. //
  118. renderer.gammaInput = true;
  119. renderer.gammaOutput = true;
  120. // CONTROLS
  121. controls = new THREE.OrbitControls( camera, renderer.domElement );
  122. // STATS
  123. stats = new Stats();
  124. container.appendChild( stats.dom );
  125. // COMPOSER
  126. renderer.autoClear = false;
  127. var renderTargetParameters = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat, stencilBuffer: false };
  128. renderTarget = new THREE.WebGLRenderTarget( SCREEN_WIDTH, SCREEN_HEIGHT, renderTargetParameters );
  129. effectFXAA = new THREE.ShaderPass( THREE.FXAAShader );
  130. hblur = new THREE.ShaderPass( THREE.HorizontalTiltShiftShader );
  131. vblur = new THREE.ShaderPass( THREE.VerticalTiltShiftShader );
  132. var bluriness = 8;
  133. hblur.uniforms[ 'h' ].value = bluriness / SCREEN_WIDTH;
  134. vblur.uniforms[ 'v' ].value = bluriness / SCREEN_HEIGHT;
  135. hblur.uniforms[ 'r' ].value = vblur.uniforms[ 'r' ].value = 0.5;
  136. effectFXAA.uniforms[ 'resolution' ].value.set( 1 / SCREEN_WIDTH, 1 / SCREEN_HEIGHT );
  137. composer = new THREE.EffectComposer( renderer, renderTarget );
  138. var renderModel = new THREE.RenderPass( scene, camera );
  139. vblur.renderToScreen = true;
  140. //effectFXAA.renderToScreen = true;
  141. composer = new THREE.EffectComposer( renderer, renderTarget );
  142. composer.addPass( renderModel );
  143. composer.addPass( effectFXAA );
  144. composer.addPass( hblur );
  145. composer.addPass( vblur );
  146. // GUI
  147. setupGui();
  148. // EVENTS
  149. window.addEventListener( 'resize', onWindowResize, false );
  150. }
  151. //
  152. function onWindowResize( event ) {
  153. SCREEN_WIDTH = window.innerWidth;
  154. SCREEN_HEIGHT = window.innerHeight - 2 * MARGIN;
  155. camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  156. camera.updateProjectionMatrix();
  157. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  158. composer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  159. hblur.uniforms[ 'h' ].value = 4 / SCREEN_WIDTH;
  160. vblur.uniforms[ 'v' ].value = 4 / SCREEN_HEIGHT;
  161. effectFXAA.uniforms[ 'resolution' ].value.set( 1 / SCREEN_WIDTH, 1 / SCREEN_HEIGHT );
  162. }
  163. function generateMaterials() {
  164. // environment map
  165. var path = "textures/cube/SwedishRoyalCastle/";
  166. var format = '.jpg';
  167. var urls = [
  168. path + 'px' + format, path + 'nx' + format,
  169. path + 'py' + format, path + 'ny' + format,
  170. path + 'pz' + format, path + 'nz' + format
  171. ];
  172. var cubeTextureLoader = new THREE.CubeTextureLoader();
  173. var reflectionCube = cubeTextureLoader.load( urls );
  174. reflectionCube.format = THREE.RGBFormat;
  175. var refractionCube = cubeTextureLoader.load( urls );
  176. reflectionCube.format = THREE.RGBFormat;
  177. refractionCube.mapping = THREE.CubeRefractionMapping;
  178. // toons
  179. var toonMaterial1 = createShaderMaterial( "toon1", light, ambientLight ),
  180. toonMaterial2 = createShaderMaterial( "toon2", light, ambientLight ),
  181. hatchingMaterial = createShaderMaterial( "hatching", light, ambientLight ),
  182. hatchingMaterial2 = createShaderMaterial( "hatching", light, ambientLight ),
  183. dottedMaterial = createShaderMaterial( "dotted", light, ambientLight ),
  184. dottedMaterial2 = createShaderMaterial( "dotted", light, ambientLight );
  185. hatchingMaterial2.uniforms.uBaseColor.value.setRGB( 0, 0, 0 );
  186. hatchingMaterial2.uniforms.uLineColor1.value.setHSL( 0, 0.8, 0.5 );
  187. hatchingMaterial2.uniforms.uLineColor2.value.setHSL( 0, 0.8, 0.5 );
  188. hatchingMaterial2.uniforms.uLineColor3.value.setHSL( 0, 0.8, 0.5 );
  189. hatchingMaterial2.uniforms.uLineColor4.value.setHSL( 0.1, 0.8, 0.5 );
  190. dottedMaterial2.uniforms.uBaseColor.value.setRGB( 0, 0, 0 );
  191. dottedMaterial2.uniforms.uLineColor1.value.setHSL( 0.05, 1.0, 0.5 );
  192. var texture = new THREE.TextureLoader().load( "textures/UV_Grid_Sm.jpg" );
  193. texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
  194. var materials = {
  195. "chrome" :
  196. {
  197. m: new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: reflectionCube } ),
  198. h: 0, s: 0, l: 1
  199. },
  200. "liquid" :
  201. {
  202. m: new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: refractionCube, refractionRatio: 0.85 } ),
  203. h: 0, s: 0, l: 1
  204. },
  205. "shiny" :
  206. {
  207. m: new THREE.MeshStandardMaterial( { color: 0x550000, envMap: reflectionCube, roughness: 0.1, metalness: 1.0 } ),
  208. h: 0, s: 0.8, l: 0.2
  209. },
  210. "matte" :
  211. {
  212. m: new THREE.MeshPhongMaterial( { color: 0x000000, specular: 0x111111, shininess: 1 } ),
  213. h: 0, s: 0, l: 1
  214. },
  215. "flat" :
  216. {
  217. m: new THREE.MeshPhongMaterial( { color: 0x000000, specular: 0x111111, shininess: 1, shading: THREE.FlatShading } ),
  218. h: 0, s: 0, l: 1
  219. },
  220. "textured" :
  221. {
  222. m: new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, shininess: 1, map: texture } ),
  223. h: 0, s: 0, l: 1
  224. },
  225. "colors" :
  226. {
  227. m: new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0xffffff, shininess: 2, vertexColors: THREE.VertexColors } ),
  228. h: 0, s: 0, l: 1
  229. },
  230. "plastic" :
  231. {
  232. m: new THREE.MeshPhongMaterial( { color: 0x000000, specular: 0x888888, shininess: 250 } ),
  233. h: 0.6, s: 0.8, l: 0.1
  234. },
  235. "toon1" :
  236. {
  237. m: toonMaterial1,
  238. h: 0.2, s: 1, l: 0.75
  239. },
  240. "toon2" :
  241. {
  242. m: toonMaterial2,
  243. h: 0.4, s: 1, l: 0.75
  244. },
  245. "hatching" :
  246. {
  247. m: hatchingMaterial,
  248. h: 0.2, s: 1, l: 0.9
  249. },
  250. "hatching2" :
  251. {
  252. m: hatchingMaterial2,
  253. h: 0.0, s: 0.8, l: 0.5
  254. },
  255. "dotted" :
  256. {
  257. m: dottedMaterial,
  258. h: 0.2, s: 1, l: 0.9
  259. },
  260. "dotted2" :
  261. {
  262. m: dottedMaterial2,
  263. h: 0.1, s: 1, l: 0.5
  264. }
  265. };
  266. return materials;
  267. }
  268. function createShaderMaterial( id, light, ambientLight ) {
  269. var shader = THREE.ShaderToon[ id ];
  270. var u = THREE.UniformsUtils.clone( shader.uniforms );
  271. var vs = shader.vertexShader;
  272. var fs = shader.fragmentShader;
  273. var material = new THREE.ShaderMaterial( { uniforms: u, vertexShader: vs, fragmentShader: fs } );
  274. material.uniforms.uDirLightPos.value = light.position;
  275. material.uniforms.uDirLightColor.value = light.color;
  276. material.uniforms.uAmbientLightColor.value = ambientLight.color;
  277. return material;
  278. }
  279. //
  280. function setupGui() {
  281. var createHandler = function( id ) {
  282. return function() {
  283. var mat_old = materials[ current_material ];
  284. mat_old.h = m_h.getValue();
  285. mat_old.s = m_s.getValue();
  286. mat_old.l = m_l.getValue();
  287. current_material = id;
  288. var mat = materials[ id ];
  289. effect.material = mat.m;
  290. m_h.setValue( mat.h );
  291. m_s.setValue( mat.s );
  292. m_l.setValue( mat.l );
  293. if ( current_material === "textured" ) {
  294. effect.enableUvs = true;
  295. } else {
  296. effect.enableUvs = false;
  297. }
  298. if ( current_material === "colors" ) {
  299. effect.enableColors = true;
  300. } else {
  301. effect.enableColors = false;
  302. }
  303. };
  304. };
  305. effectController = {
  306. material: "shiny",
  307. speed: 1.0,
  308. numBlobs: 10,
  309. resolution: 28,
  310. isolation: 80,
  311. floor: true,
  312. wallx: false,
  313. wallz: false,
  314. hue: 0.0,
  315. saturation: 0.8,
  316. lightness: 0.1,
  317. lhue: 0.04,
  318. lsaturation: 1.0,
  319. llightness: 0.5,
  320. lx: 0.5,
  321. ly: 0.5,
  322. lz: 1.0,
  323. postprocessing: false,
  324. dummy: function() {
  325. }
  326. };
  327. var h, m_h, m_s, m_l;
  328. var gui = new dat.GUI();
  329. // material (type)
  330. h = gui.addFolder( "Materials" );
  331. for ( var m in materials ) {
  332. effectController[ m ] = createHandler( m );
  333. h.add( effectController, m ).name( m );
  334. }
  335. // material (color)
  336. h = gui.addFolder( "Material color" );
  337. m_h = h.add( effectController, "hue", 0.0, 1.0, 0.025 );
  338. m_s = h.add( effectController, "saturation", 0.0, 1.0, 0.025 );
  339. m_l = h.add( effectController, "lightness", 0.0, 1.0, 0.025 );
  340. // light (point)
  341. h = gui.addFolder( "Point light color" );
  342. h.add( effectController, "lhue", 0.0, 1.0, 0.025 ).name("hue");
  343. h.add( effectController, "lsaturation", 0.0, 1.0, 0.025 ).name("saturation");
  344. h.add( effectController, "llightness", 0.0, 1.0, 0.025 ).name("lightness");
  345. // light (directional)
  346. h = gui.addFolder( "Directional light orientation" );
  347. h.add( effectController, "lx", -1.0, 1.0, 0.025 ).name("x");
  348. h.add( effectController, "ly", -1.0, 1.0, 0.025 ).name("y");
  349. h.add( effectController, "lz", -1.0, 1.0, 0.025 ).name("z");
  350. // simulation
  351. h = gui.addFolder( "Simulation" );
  352. h.add( effectController, "speed", 0.1, 8.0, 0.05 );
  353. h.add( effectController, "numBlobs", 1, 50, 1 );
  354. h.add( effectController, "resolution", 14, 100, 1 );
  355. h.add( effectController, "isolation", 10, 300, 1 );
  356. h.add( effectController, "floor" );
  357. h.add( effectController, "wallx" );
  358. h.add( effectController, "wallz" );
  359. // rendering
  360. h = gui.addFolder( "Rendering" );
  361. h.add( effectController, "postprocessing" );
  362. }
  363. // this controls content of marching cubes voxel field
  364. function updateCubes( object, time, numblobs, floor, wallx, wallz ) {
  365. object.reset();
  366. // fill the field with some metaballs
  367. var i, ballx, bally, ballz, subtract, strength;
  368. subtract = 12;
  369. strength = 1.2 / ( ( Math.sqrt( numblobs ) - 1 ) / 4 + 1 );
  370. for ( i = 0; i < numblobs; i ++ ) {
  371. ballx = Math.sin( i + 1.26 * time * ( 1.03 + 0.5 * Math.cos( 0.21 * i ) ) ) * 0.27 + 0.5;
  372. bally = Math.abs( Math.cos( i + 1.12 * time * Math.cos( 1.22 + 0.1424 * i ) ) ) * 0.77; // dip into the floor
  373. ballz = Math.cos( i + 1.32 * time * 0.1 * Math.sin( ( 0.92 + 0.53 * i ) ) ) * 0.27 + 0.5;
  374. object.addBall(ballx, bally, ballz, strength, subtract);
  375. }
  376. if ( floor ) object.addPlaneY( 2, 12 );
  377. if ( wallz ) object.addPlaneZ( 2, 12 );
  378. if ( wallx ) object.addPlaneX( 2, 12 );
  379. }
  380. //
  381. function animate() {
  382. requestAnimationFrame( animate );
  383. render();
  384. stats.update();
  385. }
  386. function render() {
  387. var delta = clock.getDelta();
  388. time += delta * effectController.speed * 0.5;
  389. controls.update( delta );
  390. // marching cubes
  391. if ( effectController.resolution !== resolution ) {
  392. resolution = effectController.resolution;
  393. effect.init( Math.floor( resolution ) );
  394. }
  395. if ( effectController.isolation !== effect.isolation ) {
  396. effect.isolation = effectController.isolation;
  397. }
  398. updateCubes( effect, time, effectController.numBlobs, effectController.floor, effectController.wallx, effectController.wallz );
  399. // materials
  400. if ( effect.material instanceof THREE.ShaderMaterial ) {
  401. if ( current_material === "dotted2" ) {
  402. effect.material.uniforms.uLineColor1.value.setHSL( effectController.hue, effectController.saturation, effectController.lightness );
  403. } else if ( current_material === "hatching2" ) {
  404. u = effect.material.uniforms;
  405. u.uLineColor1.value.setHSL( effectController.hue, effectController.saturation, effectController.lightness );
  406. u.uLineColor2.value.setHSL( effectController.hue, effectController.saturation, effectController.lightness );
  407. u.uLineColor3.value.setHSL( effectController.hue, effectController.saturation, effectController.lightness );
  408. u.uLineColor4.value.setHSL( ( effectController.hue + 0.2 % 1.0 ), effectController.saturation, effectController.lightness );
  409. } else {
  410. effect.material.uniforms.uBaseColor.value.setHSL( effectController.hue, effectController.saturation, effectController.lightness );
  411. }
  412. } else {
  413. effect.material.color.setHSL( effectController.hue, effectController.saturation, effectController.lightness );
  414. }
  415. // lights
  416. light.position.set( effectController.lx, effectController.ly, effectController.lz );
  417. light.position.normalize();
  418. pointLight.color.setHSL( effectController.lhue, effectController.lsaturation, effectController.llightness );
  419. // render
  420. if ( effectController.postprocessing ) {
  421. composer.render( delta );
  422. } else {
  423. renderer.clear();
  424. renderer.render( scene, camera );
  425. }
  426. }
  427. </script>
  428. </body>
  429. </html>