2
0

webvr_vive_paint.html 12 KB

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