webxr_vr_sculpt.html 6.1 KB

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