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