webvr_vive_sculpt.html 8.1 KB

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