webgl_geometry_csg.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js geometry - csg</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. <style>
  9. body {
  10. background-color: #eeeeee;
  11. color: #333;
  12. }
  13. a {
  14. color: #009688;
  15. text-decoration: underline;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div id="info">
  21. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> bvh csg - <a href="https://github.com/gkjohnson/three-bvh-csg" target="_blank" rel="noopener">three-bvh-csg</a><br/>
  22. See <a href="https://github.com/gkjohnson/three-bvh-csg" target="_blank" rel="noopener">main project repository</a> for more information and examples on constructive solid geometry.
  23. </div>
  24. <!-- Import maps polyfill -->
  25. <!-- Remove this when import maps will be widely supported -->
  26. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  27. <script type="importmap">
  28. {
  29. "imports": {
  30. "three": "../build/three.module.js",
  31. "three/addons/": "./jsm/",
  32. "three-mesh-bvh": "https://unpkg.com/three-mesh-bvh@^0.5.22/build/index.module.js",
  33. "three-bvh-csg": "https://unpkg.com/three-bvh-csg@^0.0.4/build/index.module.js"
  34. }
  35. }
  36. </script>
  37. <script type="module">
  38. import * as THREE from 'three';
  39. import Stats from 'three/addons/libs/stats.module.js';
  40. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  41. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  42. import { SUBTRACTION, INTERSECTION, ADDITION, Brush, Evaluator } from 'three-bvh-csg';
  43. let stats;
  44. let camera, scene, renderer;
  45. let baseBrush, brush;
  46. let core;
  47. let result, evaluator, wireframe;
  48. const params = {
  49. operation: SUBTRACTION,
  50. useGroups: true,
  51. wireframe: false,
  52. };
  53. init();
  54. animate();
  55. function init() {
  56. // environment
  57. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 100 );
  58. camera.position.set( - 1, 1, 1 ).normalize().multiplyScalar( 10 );
  59. scene = new THREE.Scene();
  60. scene.background = new THREE.Color( 0xfce4ec );
  61. // lights
  62. const ambient = new THREE.HemisphereLight( 0xffffff, 0xbfd4d2, 0.9 );
  63. scene.add( ambient );
  64. const directionalLight = new THREE.DirectionalLight( 0xffffff, 0.1 );
  65. directionalLight.position.set( 1, 4, 3 ).multiplyScalar( 3 );
  66. directionalLight.castShadow = true;
  67. directionalLight.shadow.mapSize.setScalar( 2048 );
  68. directionalLight.shadow.bias = - 1e-4;
  69. directionalLight.shadow.normalBias = 1e-4;
  70. scene.add( directionalLight );
  71. // renderer
  72. renderer = new THREE.WebGLRenderer( { antialias: true } );
  73. renderer.setPixelRatio( window.devicePixelRatio );
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. renderer.shadowMap.enabled = true;
  76. renderer.shadowMap.type = THREE.PCFSoftShadow;
  77. document.body.appendChild( renderer.domElement );
  78. stats = new Stats();
  79. document.body.appendChild( stats.dom );
  80. // add shadow plane
  81. const plane = new THREE.Mesh(
  82. new THREE.PlaneGeometry(),
  83. new THREE.ShadowMaterial( {
  84. color: 0xd81b60,
  85. transparent: true,
  86. opacity: 0.075,
  87. side: THREE.DoubleSide,
  88. } ),
  89. );
  90. plane.position.y = - 3;
  91. plane.rotation.x = - Math.PI / 2;
  92. plane.scale.setScalar( 10 );
  93. plane.receiveShadow = true;
  94. scene.add( plane );
  95. // create brushes
  96. evaluator = new Evaluator();
  97. baseBrush = new Brush(
  98. new THREE.IcosahedronGeometry( 2, 3 ),
  99. new THREE.MeshStandardMaterial( {
  100. flatShading: true,
  101. polygonOffset: true,
  102. polygonOffsetUnits: 1,
  103. polygonOffsetFactor: 1,
  104. } ),
  105. );
  106. brush = new Brush(
  107. new THREE.CylinderGeometry( 1, 1, 5, 45 ),
  108. new THREE.MeshStandardMaterial( {
  109. color: 0x80cbc4,
  110. polygonOffset: true,
  111. polygonOffsetUnits: 1,
  112. polygonOffsetFactor: 1,
  113. } ),
  114. );
  115. core = new Brush(
  116. new THREE.IcosahedronGeometry( 0.15, 1 ),
  117. new THREE.MeshStandardMaterial( {
  118. flatShading: true,
  119. color: 0xff9800,
  120. emissive: 0xff9800,
  121. emissiveIntensity: 0.35,
  122. polygonOffset: true,
  123. polygonOffsetUnits: 1,
  124. polygonOffsetFactor: 1,
  125. } ),
  126. );
  127. core.castShadow = true;
  128. scene.add( core );
  129. // create wireframe
  130. wireframe = new THREE.Mesh(
  131. undefined,
  132. new THREE.MeshBasicMaterial( { color: 0x009688, wireframe: true } ),
  133. );
  134. scene.add( wireframe );
  135. // controls
  136. const controls = new OrbitControls( camera, renderer.domElement );
  137. controls.minDistance = 5;
  138. controls.maxDistance = 75;
  139. // set up gui
  140. const gui = new GUI();
  141. gui.add( params, 'operation', { SUBTRACTION, INTERSECTION, ADDITION } );
  142. gui.add( params, 'wireframe' );
  143. gui.add( params, 'useGroups' );
  144. window.addEventListener( 'resize', onWindowResize );
  145. onWindowResize();
  146. }
  147. function updateCSG() {
  148. evaluator.useGroups = params.useGroups;
  149. result = evaluator.evaluate( baseBrush, brush, params.operation, result );
  150. result.castShadow = true;
  151. result.receiveShadow = true;
  152. scene.add( result );
  153. }
  154. function onWindowResize() {
  155. camera.aspect = window.innerWidth / window.innerHeight;
  156. camera.updateProjectionMatrix();
  157. renderer.setSize( window.innerWidth, window.innerHeight );
  158. }
  159. function animate() {
  160. requestAnimationFrame( animate );
  161. // update the transforms
  162. const t = window.performance.now() + 9000;
  163. baseBrush.rotation.x = t * 0.0001;
  164. baseBrush.rotation.y = t * 0.00025;
  165. baseBrush.rotation.z = t * 0.0005;
  166. baseBrush.updateMatrixWorld();
  167. brush.rotation.x = t * - 0.0002;
  168. brush.rotation.y = t * - 0.0005;
  169. brush.rotation.z = t * - 0.001;
  170. const s = 0.5 + 0.5 * ( 1 + Math.sin( t * 0.001 ) );
  171. brush.scale.set( s, 1, s );
  172. brush.updateMatrixWorld();
  173. // update the csg
  174. updateCSG();
  175. wireframe.geometry = result.geometry;
  176. wireframe.visible = params.wireframe;
  177. renderer.render( scene, camera );
  178. stats.update();
  179. }
  180. </script>
  181. </body>
  182. </html>