webxr_vr_sculpt.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js vr - sculpt</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> vr - sculpt
  12. </div>
  13. <!-- Import maps polyfill -->
  14. <!-- Remove this when import maps will be widely supported -->
  15. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  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 { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  27. import { MarchingCubes } from 'three/addons/objects/MarchingCubes.js';
  28. import { VRButton } from 'three/addons/webxr/VRButton.js';
  29. let container;
  30. let camera, scene, renderer;
  31. let controller1, controller2;
  32. let controls, blob;
  33. let points = [];
  34. init();
  35. initBlob();
  36. animate();
  37. function init() {
  38. container = document.createElement( 'div' );
  39. document.body.appendChild( container );
  40. scene = new THREE.Scene();
  41. scene.background = new THREE.Color( 0x222222 );
  42. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 50 );
  43. camera.position.set( 0, 1.6, 3 );
  44. controls = new OrbitControls( camera, container );
  45. controls.target.set( 0, 1.6, 0 );
  46. controls.update();
  47. const tableGeometry = new THREE.BoxGeometry( 0.5, 0.8, 0.5 );
  48. const tableMaterial = new THREE.MeshStandardMaterial( {
  49. color: 0x444444,
  50. roughness: 1.0,
  51. metalness: 0.0
  52. } );
  53. const table = new THREE.Mesh( tableGeometry, tableMaterial );
  54. table.position.y = 0.35;
  55. table.position.z = 0.85;
  56. scene.add( table );
  57. const floorGometry = new THREE.PlaneGeometry( 4, 4 );
  58. const floorMaterial = new THREE.MeshStandardMaterial( {
  59. color: 0x222222,
  60. roughness: 1.0,
  61. metalness: 0.0
  62. } );
  63. const floor = new THREE.Mesh( floorGometry, floorMaterial );
  64. floor.rotation.x = - Math.PI / 2;
  65. scene.add( floor );
  66. const grid = new THREE.GridHelper( 10, 20, 0x111111, 0x111111 );
  67. // grid.material.depthTest = false; // avoid z-fighting
  68. scene.add( grid );
  69. scene.add( new THREE.HemisphereLight( 0x888877, 0x777788 ) );
  70. const light = new THREE.DirectionalLight( 0xffffff );
  71. light.position.set( 0, 6, 0 );
  72. scene.add( light );
  73. //
  74. renderer = new THREE.WebGLRenderer( { antialias: true } );
  75. renderer.setPixelRatio( window.devicePixelRatio );
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. renderer.outputEncoding = THREE.sRGBEncoding;
  78. renderer.xr.enabled = true;
  79. container.appendChild( renderer.domElement );
  80. document.body.appendChild( VRButton.createButton( renderer ) );
  81. // controllers
  82. function onSelectStart() {
  83. this.userData.isSelecting = true;
  84. }
  85. function onSelectEnd() {
  86. this.userData.isSelecting = false;
  87. }
  88. controller1 = renderer.xr.getController( 0 );
  89. controller1.addEventListener( 'selectstart', onSelectStart );
  90. controller1.addEventListener( 'selectend', onSelectEnd );
  91. controller1.userData.id = 0;
  92. scene.add( controller1 );
  93. controller2 = renderer.xr.getController( 1 );
  94. controller2.addEventListener( 'selectstart', onSelectStart );
  95. controller2.addEventListener( 'selectend', onSelectEnd );
  96. controller2.userData.id = 1;
  97. scene.add( controller2 );
  98. //
  99. const geometry = new THREE.CylinderGeometry( 0.01, 0.02, 0.08, 5 );
  100. geometry.rotateX( - Math.PI / 2 );
  101. const material = new THREE.MeshStandardMaterial( { flatShading: true } );
  102. const mesh = new THREE.Mesh( geometry, material );
  103. const pivot = new THREE.Mesh( new THREE.IcosahedronGeometry( 0.01, 3 ) );
  104. pivot.name = 'pivot';
  105. pivot.position.z = - 0.05;
  106. mesh.add( pivot );
  107. controller1.add( mesh.clone() );
  108. controller2.add( mesh.clone() );
  109. //
  110. window.addEventListener( 'resize', onWindowResize );
  111. }
  112. function initBlob() {
  113. /*
  114. let path = "textures/cube/SwedishRoyalCastle/";
  115. let format = '.jpg';
  116. let urls = [
  117. path + 'px' + format, path + 'nx' + format,
  118. path + 'py' + format, path + 'ny' + format,
  119. path + 'pz' + format, path + 'nz' + format
  120. ];
  121. let reflectionCube = new CubeTextureLoader().load( urls );
  122. */
  123. const material = new THREE.MeshStandardMaterial( {
  124. color: 0xffffff,
  125. // envMap: reflectionCube,
  126. roughness: 0.9,
  127. metalness: 0.0,
  128. transparent: true
  129. } );
  130. blob = new MarchingCubes( 64, material, false, false, 500000 );
  131. blob.position.y = 1;
  132. scene.add( blob );
  133. initPoints();
  134. }
  135. function initPoints() {
  136. points = [
  137. { position: new THREE.Vector3(), strength: 0.04, subtract: 10 },
  138. { position: new THREE.Vector3(), strength: - 0.08, subtract: 10 }
  139. ];
  140. }
  141. function onWindowResize() {
  142. camera.aspect = window.innerWidth / window.innerHeight;
  143. camera.updateProjectionMatrix();
  144. renderer.setSize( window.innerWidth, window.innerHeight );
  145. }
  146. //
  147. function animate() {
  148. renderer.setAnimationLoop( render );
  149. }
  150. function transformPoint( vector ) {
  151. vector.x = ( vector.x + 1.0 ) / 2.0;
  152. vector.y = ( vector.y / 2.0 );
  153. vector.z = ( vector.z + 1.0 ) / 2.0;
  154. }
  155. function handleController( controller ) {
  156. const pivot = controller.getObjectByName( 'pivot' );
  157. if ( pivot ) {
  158. const id = controller.userData.id;
  159. const matrix = pivot.matrixWorld;
  160. points[ id ].position.setFromMatrixPosition( matrix );
  161. transformPoint( points[ id ].position );
  162. if ( controller.userData.isSelecting ) {
  163. const strength = points[ id ].strength / 2;
  164. const vector = new THREE.Vector3().setFromMatrixPosition( matrix );
  165. transformPoint( vector );
  166. points.push( { position: vector, strength: strength, subtract: 10 } );
  167. }
  168. }
  169. }
  170. function updateBlob() {
  171. blob.reset();
  172. for ( let i = 0; i < points.length; i ++ ) {
  173. const point = points[ i ];
  174. const position = point.position;
  175. blob.addBall( position.x, position.y, position.z, point.strength, point.subtract );
  176. }
  177. }
  178. function render() {
  179. handleController( controller1 );
  180. handleController( controller2 );
  181. updateBlob();
  182. renderer.render( scene, camera );
  183. }
  184. </script>
  185. </body>
  186. </html>