webvr_vive_sculpt.html 8.9 KB

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