webvr_vive_sculpt.html 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webvr - htc vive - sculpt</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
  7. <!-- Origin Trial Token, feature = WebXR Device API (For Chrome M69+), origin = https://threejs.org, expires = 2019-01-23 -->
  8. <meta http-equiv="origin-trial" data-feature="WebXR Device API (For Chrome M69+)" data-expires="2019-01-23" content="ApibcCZfaeEIkwpe6CSIKZXC8ODly5nYX+QlUSgwhUUlMN1/Ko7dr5XUwZDFl9SiEGtOFFe2csLwnNg33ZBh+QMAAABTeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJYUkRldmljZU02OSIsImV4cGlyeSI6MTU0ODIwMTU5OX0=">
  9. <!-- Origin Trial Token, feature = WebXR Gamepad Support, origin = https://threejs.org, expires = 2019-01-23 -->
  10. <meta http-equiv="origin-trial" data-feature="WebXR Gamepad Support" data-expires="2019-01-23" content="Agt5pp1xG96NMH+7RdcD7jvbkJiJTagqfKtC+28x/TBeQSh4tF7ad7uXL9Clqbts0vivamPTTkRkNdyfJtEezQ4AAABYeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJYUkdhbWVwYWRTdXBwb3J0IiwiZXhwaXJ5IjoxNTQ4MjAxNTk5fQ==">
  11. <style>
  12. body {
  13. font-family: Monospace;
  14. background-color: #101010;
  15. color: #fff;
  16. margin: 0px;
  17. overflow: hidden;
  18. }
  19. a {
  20. color: #f00;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <script src="../build/three.js"></script>
  26. <script src="js/vr/ViveController.js"></script>
  27. <script src="js/vr/WebVR.js"></script>
  28. <script src="js/loaders/OBJLoader.js"></script>
  29. <script src="js/MarchingCubes.js"></script>
  30. <script>
  31. var container;
  32. var camera, scene, renderer;
  33. var controller1, controller2;
  34. var blob;
  35. var points = [];
  36. init();
  37. initBlob();
  38. animate();
  39. function init() {
  40. container = document.createElement( 'div' );
  41. document.body.appendChild( container );
  42. var info = document.createElement( 'div' );
  43. info.style.position = 'absolute';
  44. info.style.top = '10px';
  45. info.style.width = '100%';
  46. info.style.textAlign = 'center';
  47. info.innerHTML = '<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - htc vive - sculpt';
  48. container.appendChild( info );
  49. scene = new THREE.Scene();
  50. scene.background = new THREE.Color( 0x222222 );
  51. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 50 );
  52. var geometry = new THREE.BoxBufferGeometry( 0.5, 0.8, 0.5 );
  53. var material = new THREE.MeshStandardMaterial( {
  54. color: 0x444444,
  55. roughness: 1.0,
  56. metalness: 0.0
  57. } );
  58. var table = new THREE.Mesh( geometry, material );
  59. table.position.y = 0.35;
  60. table.position.z = 0.85;
  61. table.castShadow = true;
  62. table.receiveShadow = true;
  63. scene.add( table );
  64. var geometry = new THREE.PlaneBufferGeometry( 4, 4 );
  65. var material = new THREE.MeshStandardMaterial( {
  66. color: 0x222222,
  67. roughness: 1.0,
  68. metalness: 0.0
  69. } );
  70. var floor = new THREE.Mesh( geometry, material );
  71. floor.rotation.x = - Math.PI / 2;
  72. floor.receiveShadow = true;
  73. scene.add( floor );
  74. scene.add( new THREE.GridHelper( 20, 40, 0x111111, 0x111111 ) );
  75. scene.add( new THREE.HemisphereLight( 0x888877, 0x777788 ) );
  76. var light = new THREE.DirectionalLight( 0xffffff );
  77. light.position.set( 0, 6, 0 );
  78. light.castShadow = true;
  79. light.shadow.camera.top = 2;
  80. light.shadow.camera.bottom = - 2;
  81. light.shadow.camera.right = 2;
  82. light.shadow.camera.left = - 2;
  83. light.shadow.mapSize.set( 4096, 4096 );
  84. scene.add( light );
  85. // scene.add( new THREE.DirectionalLightHelper( light ) );
  86. // scene.add( new THREE.CameraHelper( light.shadow.camera ) );
  87. //
  88. renderer = new THREE.WebGLRenderer( { antialias: true } );
  89. renderer.setPixelRatio( window.devicePixelRatio );
  90. renderer.setSize( window.innerWidth, window.innerHeight );
  91. renderer.gammaInput = true;
  92. renderer.gammaOutput = true;
  93. renderer.shadowMap.enabled = true;
  94. renderer.vr.enabled = true;
  95. container.appendChild( renderer.domElement );
  96. document.body.appendChild( WEBVR.createButton( renderer ) );
  97. // controllers
  98. controller1 = new THREE.ViveController( 0 );
  99. controller1.standingMatrix = renderer.vr.getStandingMatrix();
  100. scene.add( controller1 );
  101. controller2 = new THREE.ViveController( 1 );
  102. controller2.standingMatrix = renderer.vr.getStandingMatrix();
  103. scene.add( controller2 );
  104. var loader = new THREE.OBJLoader();
  105. loader.setPath( 'models/obj/vive-controller/' );
  106. loader.load( 'vr_controller_vive_1_5.obj', function ( object ) {
  107. var loader = new THREE.TextureLoader();
  108. loader.setPath( 'models/obj/vive-controller/' );
  109. var controller = object.children[ 0 ];
  110. controller.material.map = loader.load( 'onepointfive_texture.png' );
  111. controller.material.specularMap = loader.load( 'onepointfive_spec.png' );
  112. controller.castShadow = true;
  113. controller.receiveShadow = true;
  114. // var pivot = new THREE.Mesh( new THREE.BoxBufferGeometry( 0.01, 0.01, 0.01 ) );
  115. var pivot = new THREE.Mesh( new THREE.IcosahedronBufferGeometry( 0.002, 2 ), blob.material );
  116. pivot.name = 'pivot';
  117. pivot.position.y = - 0.016;
  118. pivot.position.z = - 0.043;
  119. pivot.rotation.x = Math.PI / 5.5;
  120. controller.add( pivot );
  121. var range = new THREE.Mesh( new THREE.IcosahedronBufferGeometry( 0.03, 3 ), new THREE.MeshBasicMaterial( { opacity: 0.25, transparent: true } ) );
  122. pivot.add( range );
  123. controller1.add( controller.clone() );
  124. controller2.add( controller.clone() );
  125. } );
  126. //
  127. window.addEventListener( 'resize', onWindowResize, false );
  128. }
  129. function initBlob() {
  130. /*
  131. var path = "textures/cube/SwedishRoyalCastle/";
  132. var format = '.jpg';
  133. var urls = [
  134. path + 'px' + format, path + 'nx' + format,
  135. path + 'py' + format, path + 'ny' + format,
  136. path + 'pz' + format, path + 'nz' + format
  137. ];
  138. var reflectionCube = new THREE.CubeTextureLoader().load( urls );
  139. */
  140. var material = new THREE.MeshStandardMaterial( {
  141. color: 0xffffff,
  142. // envMap: reflectionCube,
  143. roughness: 0.9,
  144. metalness: 0.0
  145. } );
  146. blob = new THREE.MarchingCubes( 64, material, true );
  147. blob.position.y = 1;
  148. scene.add( blob );
  149. initPoints();
  150. }
  151. function initPoints() {
  152. points = [
  153. { position: new THREE.Vector3(), strength: - 0.08, subtract: 10 },
  154. { position: new THREE.Vector3(), strength: 0.04, subtract: 10 }
  155. ];
  156. }
  157. function onWindowResize() {
  158. camera.aspect = window.innerWidth / window.innerHeight;
  159. camera.updateProjectionMatrix();
  160. renderer.setSize( window.innerWidth, window.innerHeight );
  161. }
  162. //
  163. function animate() {
  164. renderer.setAnimationLoop( render );
  165. }
  166. function transformPoint( vector ) {
  167. vector.x = ( vector.x + 1.0 ) / 2.0;
  168. vector.y = ( vector.y / 2.0 );
  169. vector.z = ( vector.z + 1.0 ) / 2.0;
  170. }
  171. function handleController( controller, id ) {
  172. controller.update();
  173. var pivot = controller.getObjectByName( 'pivot' );
  174. if ( pivot ) {
  175. var matrix = pivot.matrixWorld;
  176. points[ id ].position.setFromMatrixPosition( matrix );
  177. transformPoint( points[ id ].position );
  178. if ( controller.getButtonState( 'thumbpad' ) ) {
  179. blob.material.color.setHex( Math.random() * 0xffffff );
  180. }
  181. if ( controller.getButtonState( 'trigger' ) ) {
  182. var strength = points[ id ].strength / 2;
  183. var vector = new THREE.Vector3().setFromMatrixPosition( matrix );
  184. transformPoint( vector );
  185. points.push( { position: vector, strength: strength, subtract: 10 } );
  186. }
  187. if ( id === 0 && controller.getButtonState( 'grips' ) ) {
  188. if ( points.length > 2 ) {
  189. points.shift();
  190. points.shift();
  191. updateBlob();
  192. var geometry = blob.generateBufferGeometry();
  193. var mesh = new THREE.Mesh( geometry, blob.material.clone() );
  194. mesh.position.y = 1;
  195. mesh.castShadow = true;
  196. mesh.receiveShadow = true;
  197. scene.add( mesh );
  198. initPoints();
  199. }
  200. }
  201. if ( id === 1 && controller.getButtonState( 'grips' ) ) {
  202. points[ id ].strength = ( Math.sin( performance.now() / 1000 ) + 1.5 ) / 20.0;
  203. }
  204. }
  205. }
  206. function updateBlob() {
  207. blob.reset();
  208. for ( var i = 0; i < points.length; i ++ ) {
  209. var point = points[ i ];
  210. var position = point.position;
  211. blob.addBall( position.x, position.y, position.z, point.strength, point.subtract );
  212. }
  213. }
  214. function render() {
  215. handleController( controller1, 0 );
  216. handleController( controller2, 1 );
  217. updateBlob();
  218. renderer.render( scene, camera );
  219. }
  220. </script>
  221. </body>
  222. </html>