webvr_paint.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. <!-- Origin Trial Token, feature = WebXR Device API (For Chrome M69+), origin = https://threejs.org, expires = 2018-12-02 -->
  10. <meta http-equiv="origin-trial" data-feature="WebXR Device API (For Chrome M69+)" data-expires="2018-12-02" content="Ah46myef4Ax/+fcLtkeotXmIqnvun4lg4bYbHVttuJvbsWiE4oacrvroId7hbCEb4/byxFHIO6+uwq4pwr6RfQkAAABTeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJYUkRldmljZU02OSIsImV4cGlyeSI6MTU0Mzc1ODIyNn0=">
  11. <!-- Origin Trial Token, feature = WebXR Gamepad Support, origin = https://threejs.org, expires = 2018-12-02 -->
  12. <meta http-equiv="origin-trial" data-feature="WebXR Gamepad Support" data-expires="2018-12-02" content="AqI9LIanbGxr/HoTOsYCUNxG82Vy94NZbjhv83R+bF+5NX2jiZOaf7ny+mFoTUP5wrWpYlPjQ6+HeVas9f1lRwYAAABYeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJYUkdhbWVwYWRTdXBwb3J0IiwiZXhwaXJ5IjoxNTQzNzU4MjI2fQ==">
  13. <style>
  14. body {
  15. font-family: Monospace;
  16. background-color: #101010;
  17. color: #fff;
  18. margin: 0px;
  19. overflow: hidden;
  20. }
  21. a {
  22. color: #f00;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <script src="../build/three.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> webvr - 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. function onSelectStart() {
  112. this.userData.isSelecting = true;
  113. }
  114. function onSelectEnd() {
  115. this.userData.isSelecting = false;
  116. }
  117. controller1 = renderer.vr.getController( 0 );
  118. controller1.addEventListener( 'selectstart', onSelectStart );
  119. controller1.addEventListener( 'selectend', onSelectEnd );
  120. controller1.userData.points = [ new THREE.Vector3(), new THREE.Vector3() ];
  121. controller1.userData.matrices = [ new THREE.Matrix4(), new THREE.Matrix4() ];
  122. scene.add( controller1 );
  123. controller2 = renderer.vr.getController( 1 );
  124. controller2.addEventListener( 'selectstart', onSelectStart );
  125. controller2.addEventListener( 'selectend', onSelectEnd );
  126. controller2.userData.points = [ new THREE.Vector3(), new THREE.Vector3() ];
  127. controller2.userData.matrices = [ new THREE.Matrix4(), new THREE.Matrix4() ];
  128. scene.add( controller2 );
  129. var loader = new THREE.OBJLoader();
  130. loader.setPath( 'models/obj/vive-controller/' );
  131. loader.load( 'vr_controller_vive_1_5.obj', function ( object ) {
  132. var loader = new THREE.TextureLoader();
  133. loader.setPath( 'models/obj/vive-controller/' );
  134. var controller = object.children[ 0 ];
  135. controller.material.map = loader.load( 'onepointfive_texture.png' );
  136. controller.material.specularMap = loader.load( 'onepointfive_spec.png' );
  137. controller.castShadow = true;
  138. controller.receiveShadow = true;
  139. // var pivot = new THREE.Group();
  140. // var pivot = new THREE.Mesh( new THREE.BoxBufferGeometry( 0.01, 0.01, 0.01 ) );
  141. var pivot = new THREE.Mesh( new THREE.IcosahedronBufferGeometry( 0.01, 2 ) );
  142. pivot.name = 'pivot';
  143. pivot.position.y = - 0.016;
  144. pivot.position.z = - 0.043;
  145. pivot.rotation.x = Math.PI / 5.5;
  146. controller.add( pivot );
  147. controller1.add( controller.clone() );
  148. pivot.material = pivot.material.clone();
  149. controller2.add( controller.clone() );
  150. } );
  151. //
  152. window.addEventListener( 'resize', onWindowResize, false );
  153. }
  154. function initGeometry() {
  155. var geometry = new THREE.BufferGeometry();
  156. var positions = new THREE.BufferAttribute( new Float32Array( 1000000 * 3 ), 3 );
  157. positions.dynamic = true;
  158. geometry.addAttribute( 'position', positions );
  159. var normals = new THREE.BufferAttribute( new Float32Array( 1000000 * 3 ), 3 );
  160. normals.dynamic = true;
  161. geometry.addAttribute( 'normal', normals );
  162. var colors = new THREE.BufferAttribute( new Float32Array( 1000000 * 3 ), 3 );
  163. colors.dynamic = true;
  164. geometry.addAttribute( 'color', colors );
  165. geometry.drawRange.count = 0;
  166. //
  167. /*
  168. var path = "textures/cube/SwedishRoyalCastle/";
  169. var format = '.jpg';
  170. var urls = [
  171. path + 'px' + format, path + 'nx' + format,
  172. path + 'py' + format, path + 'ny' + format,
  173. path + 'pz' + format, path + 'nz' + format
  174. ];
  175. var reflectionCube = new THREE.CubeTextureLoader().load( urls );
  176. */
  177. var material = new THREE.MeshStandardMaterial( {
  178. roughness: 0.9,
  179. metalness: 0.0,
  180. // envMap: reflectionCube,
  181. vertexColors: THREE.VertexColors,
  182. side: THREE.DoubleSide
  183. } );
  184. line = new THREE.Mesh( geometry, material );
  185. line.frustumCulled = false;
  186. line.castShadow = true;
  187. line.receiveShadow = true;
  188. scene.add( line );
  189. // Shapes
  190. shapes[ 'tube' ] = getTubeShapes( 1.0 );
  191. }
  192. function getTubeShapes( size ) {
  193. var PI2 = Math.PI * 2;
  194. var sides = 10;
  195. var array = [];
  196. var radius = 0.01 * size;
  197. for ( var i = 0; i < sides; i ++ ) {
  198. var angle = ( i / sides ) * PI2;
  199. array.push( new THREE.Vector3( Math.sin( angle ) * radius, Math.cos( angle ) * radius, 0 ) );
  200. }
  201. return array;
  202. }
  203. function stroke( controller, point1, point2, matrix1, matrix2 ) {
  204. var color = new THREE.Color( 0xffffff );
  205. var size = 1;
  206. var shapes = getTubeShapes( size );
  207. var geometry = line.geometry;
  208. var attributes = geometry.attributes;
  209. var count = geometry.drawRange.count;
  210. var positions = attributes.position.array;
  211. var normals = attributes.normal.array;
  212. var colors = attributes.color.array;
  213. for ( var j = 0, jl = shapes.length; j < jl; j ++ ) {
  214. var vertex1 = shapes[ j ];
  215. var vertex2 = shapes[ ( j + 1 ) % jl ];
  216. // positions
  217. vector1.copy( vertex1 );
  218. vector1.applyMatrix4( matrix2 );
  219. vector1.add( point2 );
  220. vector2.copy( vertex2 );
  221. vector2.applyMatrix4( matrix2 );
  222. vector2.add( point2 );
  223. vector3.copy( vertex2 );
  224. vector3.applyMatrix4( matrix1 );
  225. vector3.add( point1 );
  226. vector4.copy( vertex1 );
  227. vector4.applyMatrix4( matrix1 );
  228. vector4.add( point1 );
  229. vector1.toArray( positions, ( count + 0 ) * 3 );
  230. vector2.toArray( positions, ( count + 1 ) * 3 );
  231. vector4.toArray( positions, ( count + 2 ) * 3 );
  232. vector2.toArray( positions, ( count + 3 ) * 3 );
  233. vector3.toArray( positions, ( count + 4 ) * 3 );
  234. vector4.toArray( positions, ( count + 5 ) * 3 );
  235. // normals
  236. vector1.copy( vertex1 );
  237. vector1.applyMatrix4( matrix2 );
  238. vector1.normalize();
  239. vector2.copy( vertex2 );
  240. vector2.applyMatrix4( matrix2 );
  241. vector2.normalize();
  242. vector3.copy( vertex2 );
  243. vector3.applyMatrix4( matrix1 );
  244. vector3.normalize();
  245. vector4.copy( vertex1 );
  246. vector4.applyMatrix4( matrix1 );
  247. vector4.normalize();
  248. vector1.toArray( normals, ( count + 0 ) * 3 );
  249. vector2.toArray( normals, ( count + 1 ) * 3 );
  250. vector4.toArray( normals, ( count + 2 ) * 3 );
  251. vector2.toArray( normals, ( count + 3 ) * 3 );
  252. vector3.toArray( normals, ( count + 4 ) * 3 );
  253. vector4.toArray( normals, ( count + 5 ) * 3 );
  254. // colors
  255. color.toArray( colors, ( count + 0 ) * 3 );
  256. color.toArray( colors, ( count + 1 ) * 3 );
  257. color.toArray( colors, ( count + 2 ) * 3 );
  258. color.toArray( colors, ( count + 3 ) * 3 );
  259. color.toArray( colors, ( count + 4 ) * 3 );
  260. color.toArray( colors, ( count + 5 ) * 3 );
  261. count += 6;
  262. }
  263. geometry.drawRange.count = count;
  264. }
  265. function updateGeometry( start, end ) {
  266. if ( start === end ) return;
  267. var offset = start * 3;
  268. var count = ( end - start ) * 3;
  269. var geometry = line.geometry;
  270. var attributes = geometry.attributes;
  271. attributes.position.updateRange.offset = offset;
  272. attributes.position.updateRange.count = count;
  273. attributes.position.needsUpdate = true;
  274. attributes.normal.updateRange.offset = offset;
  275. attributes.normal.updateRange.count = count;
  276. attributes.normal.needsUpdate = true;
  277. attributes.color.updateRange.offset = offset;
  278. attributes.color.updateRange.count = count;
  279. attributes.color.needsUpdate = true;
  280. }
  281. function onWindowResize() {
  282. camera.aspect = window.innerWidth / window.innerHeight;
  283. camera.updateProjectionMatrix();
  284. renderer.setSize( window.innerWidth, window.innerHeight );
  285. }
  286. //
  287. function handleController( controller ) {
  288. var pivot = controller.getObjectByName( 'pivot' );
  289. if ( pivot ) {
  290. var matrix = pivot.matrixWorld;
  291. var point1 = controller.userData.points[ 0 ];
  292. var point2 = controller.userData.points[ 1 ];
  293. var matrix1 = controller.userData.matrices[ 0 ];
  294. var matrix2 = controller.userData.matrices[ 1 ];
  295. point1.setFromMatrixPosition( matrix );
  296. matrix1.lookAt( point2, point1, up );
  297. if ( controller.userData.isSelecting === true ) {
  298. stroke( controller, point1, point2, matrix1, matrix2 );
  299. }
  300. point2.copy( point1 );
  301. matrix2.copy( matrix1 );
  302. }
  303. }
  304. function animate() {
  305. renderer.setAnimationLoop( render );
  306. }
  307. function render() {
  308. var count = line.geometry.drawRange.count;
  309. handleController( controller1 );
  310. handleController( controller2 );
  311. updateGeometry( count, line.geometry.drawRange.count );
  312. renderer.render( scene, camera );
  313. }
  314. </script>
  315. </body>
  316. </html>