webvr_vive_sculpt.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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( {
  58. color: 0x444444,
  59. roughness: 1.0,
  60. metalness: 0.0
  61. } );
  62. var table = new THREE.Mesh( geometry, material );
  63. table.position.y = 0.35;
  64. table.position.z = 0.85;
  65. table.castShadow = true;
  66. table.receiveShadow = true;
  67. scene.add( table );
  68. var geometry = new THREE.PlaneGeometry( 4, 4 );
  69. var material = new THREE.MeshStandardMaterial( {
  70. color: 0x222222,
  71. roughness: 1.0,
  72. metalness: 0.0
  73. } );
  74. var floor = new THREE.Mesh( geometry, material );
  75. floor.rotation.x = - Math.PI / 2;
  76. floor.receiveShadow = true;
  77. scene.add( floor );
  78. scene.add( new THREE.GridHelper( 10, 40, 0x111111, 0x111111 ) );
  79. scene.add( new THREE.HemisphereLight( 0x888877, 0x777788 ) );
  80. var light = new THREE.DirectionalLight( 0xffffff );
  81. light.position.set( 0, 6, 0 );
  82. light.castShadow = true;
  83. light.shadow.camera.top = 2;
  84. light.shadow.camera.bottom = -2;
  85. light.shadow.camera.right = 2;
  86. light.shadow.camera.left = -2;
  87. light.shadow.mapSize.set( 4096, 4096 );
  88. scene.add( light );
  89. // scene.add( new THREE.DirectionalLightHelper( light ) );
  90. // scene.add( new THREE.CameraHelper( light.shadow.camera ) );
  91. //
  92. renderer = new THREE.WebGLRenderer( { antialias: true } );
  93. renderer.setPixelRatio( window.devicePixelRatio );
  94. renderer.setSize( window.innerWidth, window.innerHeight );
  95. renderer.domElement.style.width = ( window.innerWidth * 2 ) + 'px';
  96. renderer.sortObjects = false;
  97. renderer.shadowMap.enabled = true;
  98. renderer.gammaInput = true;
  99. renderer.gammaOutput = true;
  100. container.appendChild( renderer.domElement );
  101. controls = new THREE.VRControls( camera );
  102. controls.standing = true;
  103. // controllers
  104. controller1 = new THREE.ViveController( 0 );
  105. controller1.standingMatrix = controls.getStandingMatrix();
  106. scene.add( controller1 );
  107. controller2 = new THREE.ViveController( 1 );
  108. controller2.standingMatrix = controls.getStandingMatrix();
  109. scene.add( controller2 );
  110. var loader = new THREE.OBJLoader();
  111. loader.setPath( 'models/obj/vive-controller/' );
  112. loader.load( 'vr_controller_vive_1_5.obj', function ( object ) {
  113. var loader = new THREE.TextureLoader();
  114. loader.setPath( 'models/obj/vive-controller/' );
  115. var controller = object.children[ 0 ];
  116. controller.material.map = loader.load( 'onepointfive_texture.png' );
  117. controller.material.specularMap = loader.load( 'onepointfive_spec.png' );
  118. controller.castShadow = true;
  119. controller.receiveShadow = true;
  120. // var pivot = new THREE.Mesh( new THREE.BoxGeometry( 0.01, 0.01, 0.01 ) );
  121. var pivot = new THREE.Mesh( new THREE.IcosahedronGeometry( 0.002, 2 ), blob.material );
  122. pivot.name = 'pivot';
  123. pivot.position.y = -0.016;
  124. pivot.position.z = -0.043;
  125. pivot.rotation.x = Math.PI / 5.5;
  126. controller.add( pivot );
  127. var range = new THREE.Mesh( new THREE.IcosahedronGeometry( 0.03, 3 ), new THREE.MeshBasicMaterial( { opacity: 0.25, transparent: true } ) );
  128. pivot.add( range );
  129. controller1.add( controller.clone() );
  130. controller2.add( controller.clone() );
  131. } );
  132. effect = new THREE.VREffect( renderer );
  133. if ( WEBVR.isAvailable() === true ) {
  134. document.body.appendChild( WEBVR.getButton( effect ) );
  135. }
  136. //
  137. window.addEventListener( 'resize', onWindowResize, false );
  138. }
  139. function initBlob() {
  140. /*
  141. var path = "textures/cube/SwedishRoyalCastle/";
  142. var format = '.jpg';
  143. var urls = [
  144. path + 'px' + format, path + 'nx' + format,
  145. path + 'py' + format, path + 'ny' + format,
  146. path + 'pz' + format, path + 'nz' + format
  147. ];
  148. var reflectionCube = new THREE.CubeTextureLoader().load( urls );
  149. */
  150. var material = new THREE.MeshStandardMaterial( {
  151. color: 0xffffff,
  152. // envMap: reflectionCube,
  153. roughness: 0.9,
  154. metalness: 0.0
  155. } );
  156. blob = new THREE.MarchingCubes( 64, material, true );
  157. blob.position.y = 1;
  158. scene.add( blob );
  159. initPoints();
  160. }
  161. function initPoints() {
  162. points = [
  163. { position: new THREE.Vector3(), strength: - 0.08, subtract: 10 },
  164. { position: new THREE.Vector3(), strength: 0.04, subtract: 10 }
  165. ];
  166. }
  167. function onWindowResize() {
  168. camera.aspect = window.innerWidth / window.innerHeight;
  169. camera.updateProjectionMatrix();
  170. effect.setSize( window.innerWidth, window.innerHeight );
  171. }
  172. //
  173. function animate() {
  174. effect.requestAnimationFrame( animate );
  175. render();
  176. }
  177. function transformPoint( vector ) {
  178. vector.x = ( vector.x + 1.0 ) / 2.0;
  179. vector.y = ( vector.y / 2.0 );
  180. vector.z = ( vector.z + 1.0 ) / 2.0;
  181. }
  182. function handleController( controller, id ) {
  183. controller.update();
  184. var pivot = controller.getObjectByName( 'pivot' );
  185. if ( pivot ) {
  186. var matrix = pivot.matrixWorld;
  187. points[ id ].position.setFromMatrixPosition( matrix );
  188. transformPoint( points[ id ].position );
  189. if ( controller.getButtonState( 'thumbpad' ) ) {
  190. blob.material.color.setHex( Math.random() * 0xffffff );
  191. }
  192. if ( controller.getButtonState( 'trigger' ) ) {
  193. var strength = points[ id ].strength / 2;
  194. var vector = new THREE.Vector3().setFromMatrixPosition( matrix );
  195. transformPoint( vector );
  196. points.push( { position: vector, strength: strength, subtract: 10 } );
  197. }
  198. if ( id === 0 && controller.getButtonState( 'grips' ) ) {
  199. if ( points.length > 2 ) {
  200. points.shift();
  201. points.shift();
  202. updateBlob();
  203. var geometry = blob.generateGeometry();
  204. var mesh = new THREE.Mesh( geometry, blob.material.clone() );
  205. mesh.position.y = 1;
  206. mesh.castShadow = true;
  207. mesh.receiveShadow = true;
  208. scene.add( mesh );
  209. initPoints();
  210. }
  211. }
  212. if ( id === 1 && controller.getButtonState( 'grips' ) ) {
  213. points[ id ].strength = ( Math.sin( performance.now() / 1000 ) + 1.5 ) / 20.0;
  214. }
  215. }
  216. }
  217. function updateBlob() {
  218. blob.reset();
  219. for ( var i = 0; i < points.length; i ++ ) {
  220. var point = points[ i ];
  221. var position = point.position;
  222. blob.addBall( position.x, position.y, position.z, point.strength, point.subtract );
  223. }
  224. }
  225. function render() {
  226. handleController( controller1, 0 );
  227. handleController( controller2, 1 );
  228. updateBlob();
  229. controls.update();
  230. effect.render( scene, camera );
  231. }
  232. </script>
  233. </body>
  234. </html>