webvr_vive_sculpt.html 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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/vr/ViveController.js"></script>
  23. <script src="js/vr/WebVR.js"></script>
  24. <script src="js/loaders/OBJLoader.js"></script>
  25. <script src="js/MarchingCubes.js"></script>
  26. <script>
  27. WEBVR.checkAvailability().catch( function( message ) {
  28. document.body.appendChild( WEBVR.getMessageContainer( message ) );
  29. } );
  30. //
  31. var container;
  32. var camera, scene, renderer;
  33. var controller1, controller2;
  34. var blob, vector;
  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. scene.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.shadowMap.enabled = true;
  93. renderer.gammaInput = true;
  94. renderer.gammaOutput = true;
  95. container.appendChild( renderer.domElement );
  96. renderer.vr.enabled = true;
  97. renderer.vr.standing = true;
  98. // controllers
  99. controller1 = new THREE.ViveController( 0 );
  100. controller1.standingMatrix = renderer.vr.getStandingMatrix();
  101. scene.add( controller1 );
  102. controller2 = new THREE.ViveController( 1 );
  103. controller2.standingMatrix = renderer.vr.getStandingMatrix();
  104. scene.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. WEBVR.getVRDisplay( function ( display ) {
  128. renderer.vr.setDevice( display );
  129. document.body.appendChild( WEBVR.getButton( display, renderer.domElement ) );
  130. } );
  131. //
  132. window.addEventListener( 'resize', onWindowResize, false );
  133. }
  134. function initBlob() {
  135. /*
  136. var path = "textures/cube/SwedishRoyalCastle/";
  137. var format = '.jpg';
  138. var urls = [
  139. path + 'px' + format, path + 'nx' + format,
  140. path + 'py' + format, path + 'ny' + format,
  141. path + 'pz' + format, path + 'nz' + format
  142. ];
  143. var reflectionCube = new THREE.CubeTextureLoader().load( urls );
  144. */
  145. var material = new THREE.MeshStandardMaterial( {
  146. color: 0xffffff,
  147. // envMap: reflectionCube,
  148. roughness: 0.9,
  149. metalness: 0.0
  150. } );
  151. blob = new THREE.MarchingCubes( 64, material, true );
  152. blob.position.y = 1;
  153. scene.add( blob );
  154. initPoints();
  155. }
  156. function initPoints() {
  157. points = [
  158. { position: new THREE.Vector3(), strength: - 0.08, subtract: 10 },
  159. { position: new THREE.Vector3(), strength: 0.04, subtract: 10 }
  160. ];
  161. }
  162. function onWindowResize() {
  163. camera.aspect = window.innerWidth / window.innerHeight;
  164. camera.updateProjectionMatrix();
  165. renderer.setSize( window.innerWidth, window.innerHeight );
  166. }
  167. //
  168. function animate() {
  169. renderer.animate( render );
  170. }
  171. function transformPoint( vector ) {
  172. vector.x = ( vector.x + 1.0 ) / 2.0;
  173. vector.y = ( vector.y / 2.0 );
  174. vector.z = ( vector.z + 1.0 ) / 2.0;
  175. }
  176. function handleController( controller, id ) {
  177. controller.update();
  178. var pivot = controller.getObjectByName( 'pivot' );
  179. if ( pivot ) {
  180. var matrix = pivot.matrixWorld;
  181. points[ id ].position.setFromMatrixPosition( matrix );
  182. transformPoint( points[ id ].position );
  183. if ( controller.getButtonState( 'thumbpad' ) ) {
  184. blob.material.color.setHex( Math.random() * 0xffffff );
  185. }
  186. if ( controller.getButtonState( 'trigger' ) ) {
  187. var strength = points[ id ].strength / 2;
  188. var vector = new THREE.Vector3().setFromMatrixPosition( matrix );
  189. transformPoint( vector );
  190. points.push( { position: vector, strength: strength, subtract: 10 } );
  191. }
  192. if ( id === 0 && controller.getButtonState( 'grips' ) ) {
  193. if ( points.length > 2 ) {
  194. points.shift();
  195. points.shift();
  196. updateBlob();
  197. var geometry = blob.generateGeometry();
  198. var mesh = new THREE.Mesh( geometry, blob.material.clone() );
  199. mesh.position.y = 1;
  200. mesh.castShadow = true;
  201. mesh.receiveShadow = true;
  202. scene.add( mesh );
  203. initPoints();
  204. }
  205. }
  206. if ( id === 1 && controller.getButtonState( 'grips' ) ) {
  207. points[ id ].strength = ( Math.sin( performance.now() / 1000 ) + 1.5 ) / 20.0;
  208. }
  209. }
  210. }
  211. function updateBlob() {
  212. blob.reset();
  213. for ( var i = 0; i < points.length; i ++ ) {
  214. var point = points[ i ];
  215. var position = point.position;
  216. blob.addBall( position.x, position.y, position.z, point.strength, point.subtract );
  217. }
  218. }
  219. function render() {
  220. handleController( controller1, 0 );
  221. handleController( controller2, 1 );
  222. updateBlob();
  223. renderer.render( scene, camera );
  224. }
  225. </script>
  226. </body>
  227. </html>