webvr_vive_paint.html 11 KB

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