webvr_vive_paint.html 11 KB

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