webvr_paint.html 12 KB

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