webvr_paint.html 13 KB

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