webvr_vive_paint.html 12 KB

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