webvr_paint.html 9.4 KB

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