2
0

webxr_vr_sculpt.html 6.4 KB

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