webvr_paint.html 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. <!-- WebXR Device API (For Chrome M76+), expires 12/04/2019 -->
  9. <meta http-equiv="origin-trial" content="Aq9LklhCLNUveuCr7QTpGpqwCPG817cYHdVyQuJPOZYk47iRB390lUKa5edVmgS1pZSl8HPspElEC/91Fz55dwIAAABTeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJYUkRldmljZU03NiIsImV4cGlyeSI6MTU3NTQxNzU5OX0=">
  10. </head>
  11. <body>
  12. <div id="info">
  13. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webvr - paint
  14. </div>
  15. <script src="js/vr/HelioWebXRPolyfill.js"></script>
  16. <script type="module">
  17. import * as THREE from '../build/three.module.js';
  18. import { WEBVR } from './jsm/vr/WebVR.js';
  19. var container;
  20. var camera, scene, renderer;
  21. var controller1, controller2;
  22. var line;
  23. var shapes = {};
  24. var up = new THREE.Vector3( 0, 1, 0 );
  25. var vector1 = new THREE.Vector3();
  26. var vector2 = new THREE.Vector3();
  27. var vector3 = new THREE.Vector3();
  28. var vector4 = new THREE.Vector3();
  29. init();
  30. initGeometry();
  31. animate();
  32. function init() {
  33. container = document.createElement( 'div' );
  34. document.body.appendChild( container );
  35. scene = new THREE.Scene();
  36. scene.background = new THREE.Color( 0x222222 );
  37. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 50 );
  38. camera.position.set( 0, 1.6, 0 );
  39. var geometry = new THREE.BoxBufferGeometry( 0.5, 0.8, 0.5 );
  40. var material = new THREE.MeshStandardMaterial( {
  41. color: 0x444444,
  42. roughness: 1.0,
  43. metalness: 0.0
  44. } );
  45. var table = new THREE.Mesh( geometry, material );
  46. table.position.y = 0.35;
  47. table.position.z = 0.85;
  48. scene.add( table );
  49. var geometry = new THREE.PlaneBufferGeometry( 4, 4 );
  50. var material = new THREE.MeshStandardMaterial( {
  51. color: 0x222222,
  52. roughness: 1.0,
  53. metalness: 0.0
  54. } );
  55. var floor = new THREE.Mesh( geometry, material );
  56. floor.rotation.x = - Math.PI / 2;
  57. scene.add( floor );
  58. var grid = new THREE.GridHelper( 10, 20, 0x111111, 0x111111 );
  59. grid.material.depthTest = false; // avoid z-fighting
  60. scene.add( grid );
  61. scene.add( new THREE.HemisphereLight( 0x888877, 0x777788 ) );
  62. var light = new THREE.DirectionalLight( 0xffffff, 0.5 );
  63. light.position.set( 0, 4, 0 );
  64. scene.add( light );
  65. //
  66. renderer = new THREE.WebGLRenderer( { antialias: true } );
  67. renderer.setPixelRatio( window.devicePixelRatio );
  68. renderer.setSize( window.innerWidth, window.innerHeight );
  69. renderer.gammaInput = true;
  70. renderer.gammaOutput = true;
  71. renderer.vr.enabled = true;
  72. container.appendChild( renderer.domElement );
  73. document.body.appendChild( WEBVR.createButton( renderer ) );
  74. // controllers
  75. function onSelectStart() {
  76. this.userData.isSelecting = true;
  77. }
  78. function onSelectEnd() {
  79. this.userData.isSelecting = false;
  80. }
  81. controller1 = renderer.vr.getController( 0 );
  82. controller1.addEventListener( 'selectstart', onSelectStart );
  83. controller1.addEventListener( 'selectend', onSelectEnd );
  84. controller1.userData.points = [ new THREE.Vector3(), new THREE.Vector3() ];
  85. controller1.userData.matrices = [ new THREE.Matrix4(), new THREE.Matrix4() ];
  86. scene.add( controller1 );
  87. controller2 = renderer.vr.getController( 1 );
  88. controller2.addEventListener( 'selectstart', onSelectStart );
  89. controller2.addEventListener( 'selectend', onSelectEnd );
  90. controller2.userData.points = [ new THREE.Vector3(), new THREE.Vector3() ];
  91. controller2.userData.matrices = [ new THREE.Matrix4(), new THREE.Matrix4() ];
  92. scene.add( controller2 );
  93. //
  94. var geometry = new THREE.CylinderBufferGeometry( 0.01, 0.02, 0.08, 5 );
  95. geometry.rotateX( - Math.PI / 2 );
  96. var material = new THREE.MeshStandardMaterial( { flatShading: true } );
  97. var mesh = new THREE.Mesh( geometry, material );
  98. var pivot = new THREE.Mesh( new THREE.IcosahedronBufferGeometry( 0.01, 2 ) );
  99. pivot.name = 'pivot';
  100. pivot.position.z = - 0.05;
  101. mesh.add( pivot );
  102. controller1.add( mesh.clone() );
  103. controller2.add( mesh.clone() );
  104. //
  105. window.addEventListener( 'resize', onWindowResize, false );
  106. }
  107. function initGeometry() {
  108. var geometry = new THREE.BufferGeometry();
  109. var positions = new THREE.BufferAttribute( new Float32Array( 1000000 * 3 ), 3 );
  110. positions.usage = THREE.DynamicDrawUsage;
  111. geometry.setAttribute( 'position', positions );
  112. var normals = new THREE.BufferAttribute( new Float32Array( 1000000 * 3 ), 3 );
  113. normals.usage = THREE.DynamicDrawUsage;
  114. geometry.setAttribute( 'normal', normals );
  115. var colors = new THREE.BufferAttribute( new Float32Array( 1000000 * 3 ), 3 );
  116. colors.usage = THREE.DynamicDrawUsage;
  117. geometry.setAttribute( 'color', colors );
  118. geometry.drawRange.count = 0;
  119. var material = new THREE.MeshStandardMaterial( {
  120. roughness: 0.9,
  121. metalness: 0.0,
  122. vertexColors: THREE.VertexColors
  123. } );
  124. line = new THREE.Mesh( geometry, material );
  125. line.frustumCulled = false;
  126. scene.add( line );
  127. // Shapes
  128. shapes[ 'tube' ] = getTubeShapes( 1.0 );
  129. }
  130. function getTubeShapes( size ) {
  131. var PI2 = Math.PI * 2;
  132. var sides = 10;
  133. var array = [];
  134. var radius = 0.01 * size;
  135. for ( var i = 0; i < sides; i ++ ) {
  136. var angle = ( i / sides ) * PI2;
  137. array.push( new THREE.Vector3( Math.sin( angle ) * radius, Math.cos( angle ) * radius, 0 ) );
  138. }
  139. return array;
  140. }
  141. function stroke( controller, point1, point2, matrix1, matrix2 ) {
  142. var color = new THREE.Color( 0xffffff );
  143. var size = 1;
  144. var shapes = getTubeShapes( size );
  145. var geometry = line.geometry;
  146. var attributes = geometry.attributes;
  147. var count = geometry.drawRange.count;
  148. var positions = attributes.position.array;
  149. var normals = attributes.normal.array;
  150. var colors = attributes.color.array;
  151. for ( var j = 0, jl = shapes.length; j < jl; j ++ ) {
  152. var vertex1 = shapes[ j ];
  153. var vertex2 = shapes[ ( j + 1 ) % jl ];
  154. // positions
  155. vector1.copy( vertex1 );
  156. vector1.applyMatrix4( matrix2 );
  157. vector1.add( point2 );
  158. vector2.copy( vertex2 );
  159. vector2.applyMatrix4( matrix2 );
  160. vector2.add( point2 );
  161. vector3.copy( vertex2 );
  162. vector3.applyMatrix4( matrix1 );
  163. vector3.add( point1 );
  164. vector4.copy( vertex1 );
  165. vector4.applyMatrix4( matrix1 );
  166. vector4.add( point1 );
  167. vector1.toArray( positions, ( count + 0 ) * 3 );
  168. vector2.toArray( positions, ( count + 1 ) * 3 );
  169. vector4.toArray( positions, ( count + 2 ) * 3 );
  170. vector2.toArray( positions, ( count + 3 ) * 3 );
  171. vector3.toArray( positions, ( count + 4 ) * 3 );
  172. vector4.toArray( positions, ( count + 5 ) * 3 );
  173. // normals
  174. vector1.copy( vertex1 );
  175. vector1.applyMatrix4( matrix2 );
  176. vector1.normalize();
  177. vector2.copy( vertex2 );
  178. vector2.applyMatrix4( matrix2 );
  179. vector2.normalize();
  180. vector3.copy( vertex2 );
  181. vector3.applyMatrix4( matrix1 );
  182. vector3.normalize();
  183. vector4.copy( vertex1 );
  184. vector4.applyMatrix4( matrix1 );
  185. vector4.normalize();
  186. vector1.toArray( normals, ( count + 0 ) * 3 );
  187. vector2.toArray( normals, ( count + 1 ) * 3 );
  188. vector4.toArray( normals, ( count + 2 ) * 3 );
  189. vector2.toArray( normals, ( count + 3 ) * 3 );
  190. vector3.toArray( normals, ( count + 4 ) * 3 );
  191. vector4.toArray( normals, ( count + 5 ) * 3 );
  192. // colors
  193. color.toArray( colors, ( count + 0 ) * 3 );
  194. color.toArray( colors, ( count + 1 ) * 3 );
  195. color.toArray( colors, ( count + 2 ) * 3 );
  196. color.toArray( colors, ( count + 3 ) * 3 );
  197. color.toArray( colors, ( count + 4 ) * 3 );
  198. color.toArray( colors, ( count + 5 ) * 3 );
  199. count += 6;
  200. }
  201. geometry.drawRange.count = count;
  202. }
  203. function updateGeometry( start, end ) {
  204. if ( start === end ) return;
  205. var offset = start * 3;
  206. var count = ( end - start ) * 3;
  207. var geometry = line.geometry;
  208. var attributes = geometry.attributes;
  209. attributes.position.updateRange.offset = offset;
  210. attributes.position.updateRange.count = count;
  211. attributes.position.needsUpdate = true;
  212. attributes.normal.updateRange.offset = offset;
  213. attributes.normal.updateRange.count = count;
  214. attributes.normal.needsUpdate = true;
  215. attributes.color.updateRange.offset = offset;
  216. attributes.color.updateRange.count = count;
  217. attributes.color.needsUpdate = true;
  218. }
  219. function onWindowResize() {
  220. camera.aspect = window.innerWidth / window.innerHeight;
  221. camera.updateProjectionMatrix();
  222. renderer.setSize( window.innerWidth, window.innerHeight );
  223. }
  224. //
  225. function handleController( controller ) {
  226. var pivot = controller.getObjectByName( 'pivot' );
  227. if ( pivot ) {
  228. var matrix = pivot.matrixWorld;
  229. var point1 = controller.userData.points[ 0 ];
  230. var point2 = controller.userData.points[ 1 ];
  231. var matrix1 = controller.userData.matrices[ 0 ];
  232. var matrix2 = controller.userData.matrices[ 1 ];
  233. point1.setFromMatrixPosition( matrix );
  234. matrix1.lookAt( point2, point1, up );
  235. if ( controller.userData.isSelecting === true ) {
  236. stroke( controller, point1, point2, matrix1, matrix2 );
  237. }
  238. point2.copy( point1 );
  239. matrix2.copy( matrix1 );
  240. }
  241. }
  242. function animate() {
  243. renderer.setAnimationLoop( render );
  244. }
  245. function render() {
  246. var count = line.geometry.drawRange.count;
  247. handleController( controller1 );
  248. handleController( controller2 );
  249. updateGeometry( count, line.geometry.drawRange.count );
  250. renderer.render( scene, camera );
  251. }
  252. </script>
  253. </body>
  254. </html>