webvr_paint.html 9.7 KB

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