webxr_vr_sculpt.html 6.1 KB

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