webvr_vive_sculpt.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #101010;
  11. color: #fff;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. a {
  16. color: #f00;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <script src="../build/three.js"></script>
  22. <script src="js/WebVR.js"></script>
  23. <script src="js/effects/VREffect.js"></script>
  24. <script src="js/controls/VRControls.js"></script>
  25. <script src="js/ViveController.js"></script>
  26. <script src="js/loaders/OBJLoader.js"></script>
  27. <script src="js/MarchingCubes.js"></script>
  28. <script>
  29. if ( WEBVR.isLatestAvailable() === false ) {
  30. document.body.appendChild( WEBVR.getMessage() );
  31. }
  32. //
  33. var container;
  34. var camera, scene, renderer;
  35. var effect, controls;
  36. var controller1, controller2;
  37. var blob, vector;
  38. var points = [];
  39. init();
  40. initBlob();
  41. animate();
  42. function init() {
  43. container = document.createElement( 'div' );
  44. document.body.appendChild( container );
  45. var info = document.createElement( 'div' );
  46. info.style.position = 'absolute';
  47. info.style.top = '10px';
  48. info.style.width = '100%';
  49. info.style.textAlign = 'center';
  50. info.innerHTML = '<a href="http://threejs.org" target="_blank">three.js</a> webgl - htc vive - sculpt';
  51. container.appendChild( info );
  52. scene = new THREE.Scene();
  53. scene.background = new THREE.Color( 0x222222 );
  54. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 50 );
  55. scene.add( camera );
  56. var geometry = new THREE.BoxGeometry( 0.5, 0.8, 0.5 );
  57. var material = new THREE.MeshStandardMaterial( { color: 0x444444, roughness: 1.0, metalness: 0.0 } );
  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.PlaneGeometry( 4, 4 );
  65. var material = new THREE.MeshStandardMaterial( { color: 0x222222, roughness: 1.0, metalness: 0.0 } );
  66. var floor = new THREE.Mesh( geometry, material );
  67. floor.rotation.x = - Math.PI / 2;
  68. floor.receiveShadow = true;
  69. scene.add( floor );
  70. scene.add( new THREE.GridHelper( 10, 40, 0x111111, 0x111111 ) );
  71. scene.add( new THREE.HemisphereLight( 0x888877, 0x777788 ) );
  72. var light = new THREE.DirectionalLight( 0xffffff );
  73. light.position.set( 0, 6, 0 );
  74. light.castShadow = true;
  75. light.shadow.camera.top = 2;
  76. light.shadow.camera.bottom = -2;
  77. light.shadow.camera.right = 2;
  78. light.shadow.camera.left = -2;
  79. light.shadow.mapSize.set( 4096, 4096 );
  80. scene.add( light );
  81. // scene.add( new THREE.DirectionalLightHelper( light ) );
  82. // scene.add( new THREE.CameraHelper( light.shadow.camera ) );
  83. //
  84. renderer = new THREE.WebGLRenderer( { antialias: true } );
  85. renderer.setPixelRatio( window.devicePixelRatio );
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. renderer.domElement.style.width = ( window.innerWidth * 2 ) + 'px';
  88. renderer.sortObjects = false;
  89. renderer.shadowMap.enabled = true;
  90. renderer.gammaInput = true;
  91. renderer.gammaOutput = true;
  92. container.appendChild( renderer.domElement );
  93. controls = new THREE.VRControls( camera );
  94. controls.standing = true;
  95. // controllers
  96. controller1 = new THREE.ViveController( 0 );
  97. controller1.standingMatrix = controls.getStandingMatrix();
  98. scene.add( controller1 );
  99. controller2 = new THREE.ViveController( 1 );
  100. controller2.standingMatrix = controls.getStandingMatrix();
  101. scene.add( controller2 );
  102. var loader = new THREE.OBJLoader();
  103. loader.setPath( 'models/obj/vive-controller/' );
  104. loader.load( 'vr_controller_vive_1_5.obj', function ( object ) {
  105. var loader = new THREE.TextureLoader();
  106. loader.setPath( 'models/obj/vive-controller/' );
  107. var controller = object.children[ 0 ];
  108. controller.material.map = loader.load( 'onepointfive_texture.png' );
  109. controller.material.specularMap = loader.load( 'onepointfive_spec.png' );
  110. controller.castShadow = true;
  111. controller.receiveShadow = true;
  112. // var pivot = new THREE.Mesh( new THREE.BoxGeometry( 0.01, 0.01, 0.01 ) );
  113. var pivot = new THREE.Mesh( new THREE.IcosahedronGeometry( 0.002, 2 ), blob.material );
  114. pivot.name = 'pivot';
  115. pivot.position.y = -0.016;
  116. pivot.position.z = -0.043;
  117. pivot.rotation.x = Math.PI / 5.5;
  118. controller.add( pivot );
  119. var range = new THREE.Mesh( new THREE.IcosahedronGeometry( 0.03, 3 ), new THREE.MeshBasicMaterial( { opacity: 0.25, transparent: true } ) );
  120. pivot.add( range );
  121. controller1.add( controller.clone() );
  122. controller2.add( controller.clone() );
  123. } );
  124. effect = new THREE.VREffect( renderer );
  125. if ( WEBVR.isAvailable() === true ) {
  126. document.body.appendChild( WEBVR.getButton( effect ) );
  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. effect.setSize( window.innerWidth, window.innerHeight );
  163. }
  164. //
  165. function animate() {
  166. effect.requestAnimationFrame( animate );
  167. render();
  168. }
  169. function transformPoint( vector ) {
  170. vector.x = ( vector.x + 1.0 ) / 2.0;
  171. vector.y = ( vector.y / 2.0 );
  172. vector.z = ( vector.z + 1.0 ) / 2.0;
  173. }
  174. function handleController( controller, id ) {
  175. var gamepad = controller.getGamepad();
  176. var pivot = controller.getObjectByName( 'pivot' );
  177. if ( gamepad && pivot ) {
  178. var matrix = pivot.matrixWorld;
  179. points[ id ].position.setFromMatrixPosition( matrix );
  180. transformPoint( points[ id ].position );
  181. if ( gamepad.buttons[ 0 ].pressed ) {
  182. blob.material.color.setHex( Math.random() * 0xffffff );
  183. }
  184. if ( gamepad.buttons[ 1 ].pressed ) {
  185. var strength = points[ id ].strength / 2;
  186. var vector = new THREE.Vector3().setFromMatrixPosition( matrix );
  187. transformPoint( vector );
  188. points.push( { position: vector, strength: strength, subtract: 10 } );
  189. }
  190. if ( gamepad.buttons[ 2 ].pressed && id === 0 ) {
  191. if ( points.length > 2 ) {
  192. points.shift();
  193. points.shift();
  194. updateBlob();
  195. var geometry = blob.generateGeometry();
  196. var mesh = new THREE.Mesh( geometry, blob.material.clone() );
  197. mesh.position.y = 1;
  198. mesh.castShadow = true;
  199. mesh.receiveShadow = true;
  200. scene.add( mesh );
  201. initPoints();
  202. }
  203. }
  204. if ( gamepad.buttons[ 2 ].pressed && id === 1 ) {
  205. points[ id ].strength = ( Math.sin( performance.now() / 1000 ) + 1.5 ) / 20.0;
  206. }
  207. }
  208. }
  209. function updateBlob() {
  210. blob.reset();
  211. for ( var i = 0; i < points.length; i ++ ) {
  212. var point = points[ i ];
  213. var position = point.position;
  214. blob.addBall( position.x, position.y, position.z, point.strength, point.subtract );
  215. }
  216. }
  217. function render() {
  218. handleController( controller1, 0 );
  219. handleController( controller2, 1 );
  220. updateBlob();
  221. controls.update();
  222. effect.render( scene, camera );
  223. }
  224. </script>
  225. </body>
  226. </html>