1
0

webvr_vive_paint.html 11 KB

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