webxr_xr_sculpt.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js xr - 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> xr - 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 { XRButton } from 'three/addons/webxr/XRButton.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 grid = new THREE.GridHelper( 4, 1, 0x111111, 0x111111 );
  48. scene.add( grid );
  49. scene.add( new THREE.HemisphereLight( 0x888877, 0x777788 ) );
  50. const light = new THREE.DirectionalLight( 0xffffff );
  51. light.position.set( 0, 6, 0 );
  52. scene.add( light );
  53. //
  54. renderer = new THREE.WebGLRenderer( { antialias: true } );
  55. renderer.setPixelRatio( window.devicePixelRatio );
  56. renderer.setSize( window.innerWidth, window.innerHeight );
  57. renderer.xr.enabled = true;
  58. container.appendChild( renderer.domElement );
  59. document.body.appendChild( XRButton.createButton( renderer ) );
  60. // controllers
  61. function onSelectStart() {
  62. this.userData.isSelecting = true;
  63. }
  64. function onSelectEnd() {
  65. this.userData.isSelecting = false;
  66. }
  67. controller1 = renderer.xr.getController( 0 );
  68. controller1.addEventListener( 'selectstart', onSelectStart );
  69. controller1.addEventListener( 'selectend', onSelectEnd );
  70. controller1.userData.id = 0;
  71. scene.add( controller1 );
  72. controller2 = renderer.xr.getController( 1 );
  73. controller2.addEventListener( 'selectstart', onSelectStart );
  74. controller2.addEventListener( 'selectend', onSelectEnd );
  75. controller2.userData.id = 1;
  76. scene.add( controller2 );
  77. //
  78. const pivot = new THREE.Mesh( new THREE.IcosahedronGeometry( 0.01, 3 ) );
  79. pivot.name = 'pivot';
  80. pivot.position.z = - 0.05;
  81. const group = new THREE.Group();
  82. group.add( pivot );
  83. controller1.add( group.clone() );
  84. controller2.add( group.clone() );
  85. //
  86. window.addEventListener( 'resize', onWindowResize );
  87. }
  88. function initBlob() {
  89. /*
  90. let path = "textures/cube/SwedishRoyalCastle/";
  91. let format = '.jpg';
  92. let urls = [
  93. path + 'px' + format, path + 'nx' + format,
  94. path + 'py' + format, path + 'ny' + format,
  95. path + 'pz' + format, path + 'nz' + format
  96. ];
  97. let reflectionCube = new CubeTextureLoader().load( urls );
  98. */
  99. const material = new THREE.MeshStandardMaterial( {
  100. color: 0xffffff,
  101. // envMap: reflectionCube,
  102. roughness: 0.9,
  103. metalness: 0.0,
  104. transparent: true
  105. } );
  106. blob = new MarchingCubes( 64, material, false, false, 500000 );
  107. blob.position.y = 1;
  108. scene.add( blob );
  109. initPoints();
  110. }
  111. function initPoints() {
  112. points = [
  113. { position: new THREE.Vector3(), strength: 0.04, subtract: 10 },
  114. { position: new THREE.Vector3(), strength: - 0.08, subtract: 10 }
  115. ];
  116. }
  117. function onWindowResize() {
  118. camera.aspect = window.innerWidth / window.innerHeight;
  119. camera.updateProjectionMatrix();
  120. renderer.setSize( window.innerWidth, window.innerHeight );
  121. }
  122. //
  123. function animate() {
  124. renderer.setAnimationLoop( render );
  125. }
  126. function transformPoint( vector ) {
  127. vector.x = ( vector.x + 1.0 ) / 2.0;
  128. vector.y = ( vector.y / 2.0 );
  129. vector.z = ( vector.z + 1.0 ) / 2.0;
  130. }
  131. function handleController( controller ) {
  132. const pivot = controller.getObjectByName( 'pivot' );
  133. if ( pivot ) {
  134. const id = controller.userData.id;
  135. const matrix = pivot.matrixWorld;
  136. points[ id ].position.setFromMatrixPosition( matrix );
  137. transformPoint( points[ id ].position );
  138. if ( controller.userData.isSelecting ) {
  139. const strength = points[ id ].strength / 2;
  140. const vector = new THREE.Vector3().setFromMatrixPosition( matrix );
  141. transformPoint( vector );
  142. points.push( { position: vector, strength: strength, subtract: 10 } );
  143. }
  144. }
  145. }
  146. function updateBlob() {
  147. blob.reset();
  148. for ( let i = 0; i < points.length; i ++ ) {
  149. const point = points[ i ];
  150. const position = point.position;
  151. blob.addBall( position.x, position.y, position.z, point.strength, point.subtract );
  152. }
  153. }
  154. function render() {
  155. handleController( controller1 );
  156. handleController( controller2 );
  157. updateBlob();
  158. renderer.render( scene, camera );
  159. }
  160. </script>
  161. </body>
  162. </html>