webvr_vive_paint.html 11 KB

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