webvr_sculpt.html 5.9 KB

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