webvr_vive_paint.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. <!-- Origin Trial Token, feature = WebVR (For Chrome M62+), origin = https://threejs.org, expires = 2018-08-30 -->
  8. <meta http-equiv="origin-trial" data-feature="WebVR (For Chrome M62+)" data-expires="2018-08-30" content="Ag80lPFLAvRyqP2W5I5XBzACxrTAQTWa3cXebXzq+WzW66nlQa6lvejGg1gdAMrzYbY6jUWp8g08kEnzb6svVgcAAABQeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJWUjEuMU02MiIsImV4cGlyeSI6MTUzNTYzNjYxOX0=">
  9. <!-- Origin Trial Token, feature = WebXR Device API, origin = https://threejs.org, expires = 2018-08-28 -->
  10. <meta http-equiv="origin-trial" data-feature="WebXR Device API" data-expires="2018-08-28" content="Ag7nJS0Q6nBKfmRY1XLKHslnz73amLhaf8RoFpYz36MpMq0oa30AETLXer74BIwa3t8uDXlR0n4W9f/o674Rqw4AAABQeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJYUkRldmljZSIsImV4cGlyeSI6MTUzNTQxNDQwMH0=">
  11. <!-- Origin Trial Token, feature = WebXR Gamepad Support, origin = https://threejs.org, expires = 2018-08-28 -->
  12. <meta http-equiv="origin-trial" data-feature="WebXR Gamepad Support" data-expires="2018-08-28" content="AsflqqNG2L/Eepy8xSwCYwWH5U7w3nN7Ak137jGxMeBFz9lqQVcMBqMTcMw6ZkxThB7ZM2Cn7hgPqX++ZlgC9wMAAABYeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJYUkdhbWVwYWRTdXBwb3J0IiwiZXhwaXJ5IjoxNTM1NDE0NDAwfQ==">
  13. <style>
  14. body {
  15. font-family: Monospace;
  16. background-color: #101010;
  17. color: #fff;
  18. margin: 0px;
  19. overflow: hidden;
  20. }
  21. a {
  22. color: #f00;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <script src="../build/three.js"></script>
  28. <script src="js/vr/ViveController.js"></script>
  29. <script src="js/vr/PaintViveController.js"></script>
  30. <script src="js/vr/WebVR.js"></script>
  31. <script src="js/loaders/OBJLoader.js"></script>
  32. <script>
  33. var container;
  34. var camera, scene, renderer;
  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 point4 = new THREE.Vector3();
  45. var point5 = new THREE.Vector3();
  46. init();
  47. initGeometry();
  48. animate();
  49. function init() {
  50. container = document.createElement( 'div' );
  51. document.body.appendChild( container );
  52. var info = document.createElement( 'div' );
  53. info.style.position = 'absolute';
  54. info.style.top = '10px';
  55. info.style.width = '100%';
  56. info.style.textAlign = 'center';
  57. info.innerHTML = '<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - htc vive - paint';
  58. container.appendChild( info );
  59. scene = new THREE.Scene();
  60. scene.background = new THREE.Color( 0x222222 );
  61. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 50 );
  62. var geometry = new THREE.BoxBufferGeometry( 0.5, 0.8, 0.5 );
  63. var material = new THREE.MeshStandardMaterial( {
  64. color: 0x444444,
  65. roughness: 1.0,
  66. metalness: 0.0
  67. } );
  68. var table = new THREE.Mesh( geometry, material );
  69. table.position.y = 0.35;
  70. table.position.z = 0.85;
  71. table.castShadow = true;
  72. table.receiveShadow = true;
  73. scene.add( table );
  74. /*
  75. var table = new THREE.Mesh( geometry, material );
  76. table.position.y = 0.35;
  77. table.position.z = -0.85;
  78. table.castShadow = true;
  79. table.receiveShadow = true;
  80. scene.add( table );
  81. */
  82. var geometry = new THREE.PlaneBufferGeometry( 4, 4 );
  83. var material = new THREE.MeshStandardMaterial( {
  84. color: 0x222222,
  85. roughness: 1.0,
  86. metalness: 0.0
  87. } );
  88. var floor = new THREE.Mesh( geometry, material );
  89. floor.rotation.x = - Math.PI / 2;
  90. floor.receiveShadow = true;
  91. scene.add( floor );
  92. scene.add( new THREE.GridHelper( 20, 40, 0x111111, 0x111111 ) );
  93. scene.add( new THREE.HemisphereLight( 0x888877, 0x777788 ) );
  94. var light = new THREE.DirectionalLight( 0xffffff );
  95. light.position.set( 0, 6, 0 );
  96. light.castShadow = true;
  97. light.shadow.camera.top = 2;
  98. light.shadow.camera.bottom = -2;
  99. light.shadow.camera.right = 2;
  100. light.shadow.camera.left = -2;
  101. light.shadow.mapSize.set( 4096, 4096 );
  102. scene.add( light );
  103. // scene.add( new THREE.DirectionalLightHelper( light ) );
  104. // scene.add( new THREE.CameraHelper( light.shadow.camera ) );
  105. //
  106. renderer = new THREE.WebGLRenderer( { antialias: true } );
  107. renderer.setPixelRatio( window.devicePixelRatio );
  108. renderer.setSize( window.innerWidth, window.innerHeight );
  109. renderer.gammaInput = true;
  110. renderer.gammaOutput = true;
  111. renderer.shadowMap.enabled = true;
  112. renderer.vr.enabled = true;
  113. container.appendChild( renderer.domElement );
  114. document.body.appendChild( WEBVR.createButton( renderer ) );
  115. // controllers
  116. controller1 = new THREE.PaintViveController( 0 );
  117. controller1.standingMatrix = renderer.vr.getStandingMatrix();
  118. controller1.userData.points = [ new THREE.Vector3(), new THREE.Vector3() ];
  119. controller1.userData.matrices = [ new THREE.Matrix4(), new THREE.Matrix4() ];
  120. scene.add( controller1 );
  121. controller2 = new THREE.PaintViveController( 1 );
  122. controller2.standingMatrix = renderer.vr.getStandingMatrix();
  123. controller2.userData.points = [ new THREE.Vector3(), new THREE.Vector3() ];
  124. controller2.userData.matrices = [ new THREE.Matrix4(), new THREE.Matrix4() ];
  125. scene.add( controller2 );
  126. var loader = new THREE.OBJLoader();
  127. loader.setPath( 'models/obj/vive-controller/' );
  128. loader.load( 'vr_controller_vive_1_5.obj', function ( object ) {
  129. var loader = new THREE.TextureLoader();
  130. loader.setPath( 'models/obj/vive-controller/' );
  131. var controller = object.children[ 0 ];
  132. controller.material.map = loader.load( 'onepointfive_texture.png' );
  133. controller.material.specularMap = loader.load( 'onepointfive_spec.png' );
  134. controller.castShadow = true;
  135. controller.receiveShadow = true;
  136. // var pivot = new THREE.Group();
  137. // var pivot = new THREE.Mesh( new THREE.BoxBufferGeometry( 0.01, 0.01, 0.01 ) );
  138. var pivot = new THREE.Mesh( new THREE.IcosahedronBufferGeometry( 0.01, 2 ) );
  139. pivot.name = 'pivot';
  140. pivot.position.y = -0.016;
  141. pivot.position.z = -0.043;
  142. pivot.rotation.x = Math.PI / 5.5;
  143. controller.add( pivot );
  144. controller1.add( controller.clone() );
  145. pivot.material = pivot.material.clone();
  146. controller2.add( controller.clone() );
  147. } );
  148. //
  149. window.addEventListener( 'resize', onWindowResize, false );
  150. }
  151. function initGeometry() {
  152. var geometry = new THREE.BufferGeometry();
  153. var positions = new THREE.BufferAttribute( new Float32Array( 1000000 * 3 ), 3 );
  154. positions.dynamic = true;
  155. geometry.addAttribute( 'position', positions );
  156. var normals = new THREE.BufferAttribute( new Float32Array( 1000000 * 3 ), 3 );
  157. normals.dynamic = true;
  158. geometry.addAttribute( 'normal', normals );
  159. var colors = new THREE.BufferAttribute( new Float32Array( 1000000 * 3 ), 3 );
  160. colors.dynamic = true;
  161. geometry.addAttribute( 'color', colors );
  162. geometry.drawRange.count = 0;
  163. //
  164. /*
  165. var path = "textures/cube/SwedishRoyalCastle/";
  166. var format = '.jpg';
  167. var urls = [
  168. path + 'px' + format, path + 'nx' + format,
  169. path + 'py' + format, path + 'ny' + format,
  170. path + 'pz' + format, path + 'nz' + format
  171. ];
  172. var reflectionCube = new THREE.CubeTextureLoader().load( urls );
  173. */
  174. var material = new THREE.MeshStandardMaterial( {
  175. roughness: 0.9,
  176. metalness: 0.0,
  177. // envMap: reflectionCube,
  178. vertexColors: THREE.VertexColors,
  179. side: THREE.DoubleSide
  180. } );
  181. line = new THREE.Mesh( geometry, material );
  182. line.frustumCulled = false;
  183. line.castShadow = true;
  184. line.receiveShadow = true;
  185. scene.add( line );
  186. // Shapes
  187. shapes[ 'tube' ] = getTubeShapes( 1.0 );
  188. }
  189. function getTubeShapes( size ) {
  190. var PI2 = Math.PI * 2;
  191. var sides = 10;
  192. var array = [];
  193. var radius = 0.01 * size;
  194. for( var i = 0; i < sides; i ++ ) {
  195. var angle = ( i / sides ) * PI2;
  196. array.push( new THREE.Vector3( Math.sin( angle ) * radius, Math.cos( angle ) * radius, 0 ) );
  197. }
  198. return array;
  199. }
  200. function stroke( controller, point1, point2, matrix1, matrix2 ) {
  201. var color = controller.getColor();
  202. var shapes = getTubeShapes( controller.getSize() );
  203. var geometry = line.geometry;
  204. var attributes = geometry.attributes;
  205. var count = geometry.drawRange.count;
  206. var positions = attributes.position.array;
  207. var normals = attributes.normal.array;
  208. var colors = attributes.color.array;
  209. for ( var j = 0, jl = shapes.length; j < jl; j ++ ) {
  210. var vertex1 = shapes[ j ];
  211. var vertex2 = shapes[ ( j + 1 ) % jl ];
  212. // positions
  213. vector1.copy( vertex1 );
  214. vector1.applyMatrix4( matrix2 );
  215. vector1.add( point2 );
  216. vector2.copy( vertex2 );
  217. vector2.applyMatrix4( matrix2 );
  218. vector2.add( point2 );
  219. vector3.copy( vertex2 );
  220. vector3.applyMatrix4( matrix1 );
  221. vector3.add( point1 );
  222. vector4.copy( vertex1 );
  223. vector4.applyMatrix4( matrix1 );
  224. vector4.add( point1 );
  225. vector1.toArray( positions, ( count + 0 ) * 3 );
  226. vector2.toArray( positions, ( count + 1 ) * 3 );
  227. vector4.toArray( positions, ( count + 2 ) * 3 );
  228. vector2.toArray( positions, ( count + 3 ) * 3 );
  229. vector3.toArray( positions, ( count + 4 ) * 3 );
  230. vector4.toArray( positions, ( count + 5 ) * 3 );
  231. // normals
  232. vector1.copy( vertex1 );
  233. vector1.applyMatrix4( matrix2 );
  234. vector1.normalize();
  235. vector2.copy( vertex2 );
  236. vector2.applyMatrix4( matrix2 );
  237. vector2.normalize();
  238. vector3.copy( vertex2 );
  239. vector3.applyMatrix4( matrix1 );
  240. vector3.normalize();
  241. vector4.copy( vertex1 );
  242. vector4.applyMatrix4( matrix1 );
  243. vector4.normalize();
  244. vector1.toArray( normals, ( count + 0 ) * 3 );
  245. vector2.toArray( normals, ( count + 1 ) * 3 );
  246. vector4.toArray( normals, ( count + 2 ) * 3 );
  247. vector2.toArray( normals, ( count + 3 ) * 3 );
  248. vector3.toArray( normals, ( count + 4 ) * 3 );
  249. vector4.toArray( normals, ( count + 5 ) * 3 );
  250. // colors
  251. color.toArray( colors, ( count + 0 ) * 3 );
  252. color.toArray( colors, ( count + 1 ) * 3 );
  253. color.toArray( colors, ( count + 2 ) * 3 );
  254. color.toArray( colors, ( count + 3 ) * 3 );
  255. color.toArray( colors, ( count + 4 ) * 3 );
  256. color.toArray( colors, ( count + 5 ) * 3 );
  257. count += 6;
  258. }
  259. geometry.drawRange.count = count;
  260. }
  261. function updateGeometry( start, end ) {
  262. if ( start === end ) return;
  263. var offset = start * 3;
  264. var count = ( end - start ) * 3;
  265. var geometry = line.geometry;
  266. var attributes = geometry.attributes;
  267. attributes.position.updateRange.offset = offset;
  268. attributes.position.updateRange.count = count;
  269. attributes.position.needsUpdate = true;
  270. attributes.normal.updateRange.offset = offset;
  271. attributes.normal.updateRange.count = count;
  272. attributes.normal.needsUpdate = true;
  273. attributes.color.updateRange.offset = offset;
  274. attributes.color.updateRange.count = count;
  275. attributes.color.needsUpdate = true;
  276. }
  277. function onWindowResize() {
  278. camera.aspect = window.innerWidth / window.innerHeight;
  279. camera.updateProjectionMatrix();
  280. renderer.setSize( window.innerWidth, window.innerHeight );
  281. }
  282. //
  283. function handleController( controller ) {
  284. controller.update();
  285. var pivot = controller.getObjectByName( 'pivot' );
  286. if ( pivot ) {
  287. pivot.material.color.copy( controller.getColor() );
  288. pivot.scale.setScalar(controller.getSize());
  289. var matrix = pivot.matrixWorld;
  290. var point1 = controller.userData.points[ 0 ];
  291. var point2 = controller.userData.points[ 1 ];
  292. var matrix1 = controller.userData.matrices[ 0 ];
  293. var matrix2 = controller.userData.matrices[ 1 ];
  294. point1.setFromMatrixPosition( matrix );
  295. matrix1.lookAt( point2, point1, up );
  296. if ( controller.getButtonState( 'trigger' ) ) {
  297. stroke( controller, point1, point2, matrix1, matrix2 );
  298. }
  299. point2.copy( point1 );
  300. matrix2.copy( matrix1 );
  301. }
  302. }
  303. function animate() {
  304. renderer.setAnimationLoop( render );
  305. }
  306. function render() {
  307. var count = line.geometry.drawRange.count;
  308. handleController( controller1 );
  309. handleController( controller2 );
  310. updateGeometry( count, line.geometry.drawRange.count );
  311. renderer.render( scene, camera );
  312. }
  313. </script>
  314. </body>
  315. </html>