webvr_paint.html 12 KB

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