webvr_paint.html 11 KB

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