webvr_vive_paint.html 11 KB

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