webvr_vive_paint.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webvr - htc vive - paint</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>
  28. if ( WEBVR.isLatestAvailable() === false ) {
  29. document.body.appendChild( WEBVR.getMessage() );
  30. }
  31. //
  32. var container;
  33. var camera, scene, renderer;
  34. var effect, controls;
  35. var controller1, controller2;
  36. var line;
  37. var shapes = {};
  38. var up = new THREE.Vector3( 0, 1, 0 );
  39. var vector = new THREE.Vector3();
  40. var vector1 = new THREE.Vector3();
  41. var vector2 = new THREE.Vector3();
  42. var vector3 = new THREE.Vector3();
  43. var vector4 = new THREE.Vector3();
  44. var color = new THREE.Color( 0, 0, 0 );
  45. var point4 = new THREE.Vector3();
  46. var point5 = new THREE.Vector3();
  47. init();
  48. initGeometry();
  49. animate();
  50. function init() {
  51. container = document.createElement( 'div' );
  52. document.body.appendChild( container );
  53. var info = document.createElement( 'div' );
  54. info.style.position = 'absolute';
  55. info.style.top = '10px';
  56. info.style.width = '100%';
  57. info.style.textAlign = 'center';
  58. info.innerHTML = '<a href="http://threejs.org" target="_blank">three.js</a> webgl - htc vive - paint';
  59. container.appendChild( info );
  60. scene = new THREE.Scene();
  61. scene.background = new THREE.Color( 0x222222 );
  62. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 50 );
  63. scene.add( camera );
  64. var geometry = new THREE.BoxGeometry( 0.5, 0.8, 0.5 );
  65. var material = new THREE.MeshStandardMaterial( { color: 0x444444, roughness: 1.0, metalness: 0.0 } );
  66. var table = new THREE.Mesh( geometry, material );
  67. table.position.y = 0.35;
  68. table.position.z = 0.85;
  69. table.castShadow = true;
  70. table.receiveShadow = true;
  71. scene.add( table );
  72. /*
  73. var table = new THREE.Mesh( geometry, material );
  74. table.position.y = 0.35;
  75. table.position.z = -0.85;
  76. table.castShadow = true;
  77. table.receiveShadow = true;
  78. scene.add( table );
  79. */
  80. var geometry = new THREE.PlaneGeometry( 4, 4 );
  81. var material = new THREE.MeshStandardMaterial( { color: 0x222222, roughness: 1.0, metalness: 0.0 } );
  82. var floor = new THREE.Mesh( geometry, material );
  83. floor.rotation.x = - Math.PI / 2;
  84. floor.receiveShadow = true;
  85. scene.add( floor );
  86. scene.add( new THREE.GridHelper( 10, 40, 0x111111, 0x111111 ) );
  87. scene.add( new THREE.HemisphereLight( 0x888877, 0x777788 ) );
  88. var light = new THREE.DirectionalLight( 0xffffff );
  89. light.position.set( 0, 6, 0 );
  90. light.castShadow = true;
  91. light.shadow.camera.top = 2;
  92. light.shadow.camera.bottom = -2;
  93. light.shadow.camera.right = 2;
  94. light.shadow.camera.left = -2;
  95. light.shadow.mapSize.set( 4096, 4096 );
  96. scene.add( light );
  97. // scene.add( new THREE.DirectionalLightHelper( light ) );
  98. // scene.add( new THREE.CameraHelper( light.shadow.camera ) );
  99. //
  100. renderer = new THREE.WebGLRenderer( { antialias: true } );
  101. renderer.setPixelRatio( window.devicePixelRatio );
  102. renderer.setSize( window.innerWidth, window.innerHeight );
  103. renderer.domElement.style.width = ( window.innerWidth * 2 ) + 'px';
  104. renderer.sortObjects = false;
  105. renderer.shadowMap.enabled = true;
  106. renderer.gammaInput = true;
  107. renderer.gammaOutput = true;
  108. container.appendChild( renderer.domElement );
  109. controls = new THREE.VRControls( camera );
  110. controls.standing = true;
  111. // controllers
  112. controller1 = new THREE.ViveController( 0 );
  113. controller1.standingMatrix = controls.getStandingMatrix();
  114. controller1.userData.points = [ new THREE.Vector3(), new THREE.Vector3() ];
  115. controller1.userData.matrices = [ new THREE.Matrix4(), new THREE.Matrix4() ];
  116. scene.add( controller1 );
  117. controller2 = new THREE.ViveController( 1 );
  118. controller2.standingMatrix = controls.getStandingMatrix();
  119. controller2.userData.points = [ new THREE.Vector3(), new THREE.Vector3() ];
  120. controller2.userData.matrices = [ new THREE.Matrix4(), new THREE.Matrix4() ];
  121. scene.add( controller2 );
  122. var loader = new THREE.OBJLoader();
  123. loader.setPath( 'models/obj/vive-controller/' );
  124. loader.load( 'vr_controller_vive_1_5.obj', function ( object ) {
  125. var loader = new THREE.TextureLoader();
  126. loader.setPath( 'models/obj/vive-controller/' );
  127. var controller = object.children[ 0 ];
  128. controller.material.map = loader.load( 'onepointfive_texture.png' );
  129. controller.material.specularMap = loader.load( 'onepointfive_spec.png' );
  130. controller.castShadow = true;
  131. controller.receiveShadow = true;
  132. // var pivot = new THREE.Group();
  133. // var pivot = new THREE.Mesh( new THREE.BoxGeometry( 0.01, 0.01, 0.01 ) );
  134. var pivot = new THREE.Mesh( new THREE.IcosahedronGeometry( 0.002, 2 ) );
  135. pivot.name = 'pivot';
  136. pivot.position.y = -0.016;
  137. pivot.position.z = -0.043;
  138. pivot.rotation.x = Math.PI / 5.5;
  139. controller.add( pivot );
  140. var range = new THREE.Mesh( new THREE.IcosahedronGeometry( 0.03, 3 ), new THREE.MeshBasicMaterial( { opacity: 0.25, transparent: true } ) );
  141. pivot.add( range );
  142. controller1.add( controller.clone() );
  143. controller2.add( controller.clone() );
  144. } );
  145. effect = new THREE.VREffect( renderer );
  146. if ( WEBVR.isAvailable() === true ) {
  147. document.body.appendChild( WEBVR.getButton( effect ) );
  148. }
  149. //
  150. window.addEventListener( 'resize', onWindowResize, false );
  151. }
  152. function initGeometry() {
  153. var geometry = new THREE.BufferGeometry();
  154. var positions = new THREE.BufferAttribute( new Float32Array( 1000000 * 3 ), 3 );
  155. geometry.addAttribute( 'position', positions );
  156. var normals = new THREE.BufferAttribute( new Float32Array( 1000000 * 3 ), 3 );
  157. geometry.addAttribute( 'normal', normals );
  158. var colors = new THREE.BufferAttribute( new Float32Array( 1000000 * 3 ), 3 );
  159. geometry.addAttribute( 'color', colors );
  160. geometry.drawRange.count = 0;
  161. //
  162. /*
  163. var path = "textures/cube/SwedishRoyalCastle/";
  164. var format = '.jpg';
  165. var urls = [
  166. path + 'px' + format, path + 'nx' + format,
  167. path + 'py' + format, path + 'ny' + format,
  168. path + 'pz' + format, path + 'nz' + format
  169. ];
  170. var reflectionCube = new THREE.CubeTextureLoader().load( urls );
  171. */
  172. var material = new THREE.MeshStandardMaterial( {
  173. roughness: 0.9,
  174. metalness: 0.0,
  175. // envMap: reflectionCube,
  176. vertexColors: THREE.VertexColors,
  177. side: THREE.DoubleSide
  178. } );
  179. line = new THREE.Mesh( geometry, material );
  180. line.frustumCulled = false;
  181. line.castShadow = true;
  182. line.receiveShadow = true;
  183. // scene.add( line );
  184. // Shapes
  185. var PI2 = Math.PI * 2;
  186. var sides = 10;
  187. var array = [];
  188. for ( var i = 0; i < sides; i ++ ) {
  189. var angle = ( i / sides ) * PI2;
  190. array.push( new THREE.Vector3( Math.sin( angle ) * 0.01, Math.cos( angle ) * 0.01, 0 ) );
  191. }
  192. shapes[ 'tube' ] = array;
  193. }
  194. function stroke( point1, point2, matrix1, matrix2 ) {
  195. var shape = shapes[ 'tube' ];
  196. var geometry = line.geometry;
  197. var attributes = geometry.attributes;
  198. var count = geometry.drawRange.count;
  199. var positions = attributes.position.array;
  200. var normals = attributes.normal.array;
  201. var colors = attributes.color.array;
  202. for ( var j = 0, jl = shape.length; j < jl; j ++ ) {
  203. var vertex1 = shape[ j ];
  204. var vertex2 = shape[ ( j + 1 ) % jl ];
  205. // positions
  206. vector1.copy( vertex1 );
  207. vector1.applyMatrix4( matrix2 );
  208. vector1.add( point2 );
  209. vector2.copy( vertex2 );
  210. vector2.applyMatrix4( matrix2 );
  211. vector2.add( point2 );
  212. vector3.copy( vertex2 );
  213. vector3.applyMatrix4( matrix1 );
  214. vector3.add( point1 );
  215. vector4.copy( vertex1 );
  216. vector4.applyMatrix4( matrix1 );
  217. vector4.add( point1 );
  218. vector1.toArray( positions, ( count + 0 ) * 3 );
  219. vector2.toArray( positions, ( count + 1 ) * 3 );
  220. vector4.toArray( positions, ( count + 2 ) * 3 );
  221. vector2.toArray( positions, ( count + 3 ) * 3 );
  222. vector3.toArray( positions, ( count + 4 ) * 3 );
  223. vector4.toArray( positions, ( count + 5 ) * 3 );
  224. // normals
  225. vector1.copy( vertex1 );
  226. vector1.applyMatrix4( matrix2 );
  227. vector1.normalize();
  228. vector2.copy( vertex2 );
  229. vector2.applyMatrix4( matrix2 );
  230. vector2.normalize();
  231. vector3.copy( vertex2 );
  232. vector3.applyMatrix4( matrix1 );
  233. vector3.normalize();
  234. vector4.copy( vertex1 );
  235. vector4.applyMatrix4( matrix1 );
  236. vector4.normalize();
  237. vector1.toArray( normals, ( count + 0 ) * 3 );
  238. vector2.toArray( normals, ( count + 1 ) * 3 );
  239. vector4.toArray( normals, ( count + 2 ) * 3 );
  240. vector2.toArray( normals, ( count + 3 ) * 3 );
  241. vector3.toArray( normals, ( count + 4 ) * 3 );
  242. vector4.toArray( normals, ( count + 5 ) * 3 );
  243. // colors
  244. color.toArray( colors, ( count + 0 ) * 3 );
  245. color.toArray( colors, ( count + 1 ) * 3 );
  246. color.toArray( colors, ( count + 2 ) * 3 );
  247. color.toArray( colors, ( count + 3 ) * 3 );
  248. color.toArray( colors, ( count + 4 ) * 3 );
  249. color.toArray( colors, ( count + 5 ) * 3 );
  250. count += 6;
  251. }
  252. geometry.drawRange.count = count;
  253. attributes.position.needsUpdate = true;
  254. attributes.normal.needsUpdate = true;
  255. attributes.color.needsUpdate = true;
  256. }
  257. function onWindowResize() {
  258. camera.aspect = window.innerWidth / window.innerHeight;
  259. camera.updateProjectionMatrix();
  260. effect.setSize( window.innerWidth, window.innerHeight );
  261. }
  262. //
  263. function animate() {
  264. effect.requestAnimationFrame( animate );
  265. render();
  266. }
  267. function handleController( controller, id ) {
  268. var gamepad = controller.getGamepad();
  269. if ( gamepad ) {
  270. var matrix = controller.getObjectByName( 'pivot' ).matrixWorld;
  271. var point1 = controller.userData.points[ 0 ];
  272. var point2 = controller.userData.points[ 1 ];
  273. var matrix1 = controller.userData.matrices[ 0 ];
  274. var matrix2 = controller.userData.matrices[ 1 ];
  275. point1.setFromMatrixPosition( matrix );
  276. matrix1.lookAt( point2, point1, up );
  277. if ( gamepad.buttons[ 0 ].pressed ) {
  278. color.setHex( Math.random() * 0xffffff );
  279. }
  280. if ( gamepad.buttons[ 1 ].pressed ) {
  281. if ( line.parent === null ) scene.add( line );
  282. stroke( point1, point2, matrix1, matrix2 );
  283. }
  284. point2.copy( point1 );
  285. matrix2.copy( matrix1 );
  286. }
  287. }
  288. function render() {
  289. controls.update();
  290. handleController( controller1, 0 );
  291. handleController( controller2, 1 );
  292. effect.render( scene, camera );
  293. }
  294. </script>
  295. </body>
  296. </html>