monument-app.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. if (typeof AFRAME === 'undefined') {
  2. throw new Error('Component attempted to register before AFRAME was available.');
  3. }
  4. AFRAME.registerComponent('envmap', {
  5. /**
  6. * Creates a new THREE.ShaderMaterial using the two shaders defined
  7. * in vertex.glsl and fragment.glsl.
  8. */
  9. init: function () {
  10. const data = this.data;
  11. //this.applyToMesh();
  12. this.el.addEventListener('model-loaded', () => this.applyToMesh());
  13. },
  14. /**
  15. * Update the ShaderMaterial when component data changes.
  16. */
  17. update: function () {
  18. },
  19. getEnvMap: function () {
  20. var path = './assets/textures/skybox2/';
  21. var format = '.jpg';
  22. var urls = [
  23. path + 'px' + format, path + 'nx' + format,
  24. path + 'py' + format, path + 'ny' + format,
  25. path + 'pz' + format, path + 'nz' + format
  26. ];
  27. envMap = new THREE.CubeTextureLoader().load(urls);
  28. envMap.format = THREE.RGBFormat;
  29. return envMap;
  30. },
  31. /**
  32. * Apply the material to the current entity.
  33. */
  34. applyToMesh: function () {
  35. const mesh = this.el.getObject3D('mesh');
  36. //var scene = mesh;
  37. var envMap = this.getEnvMap();
  38. mesh.traverse(function (node) {
  39. if (node.material) {
  40. node.material.side = THREE.BackSide;
  41. node.material.needsUpdate = true;
  42. //side = THREE.DoubleSide; break;
  43. }
  44. });
  45. mesh.traverse(function (node) {
  46. if (node.material && (node.material.isMeshStandardMaterial ||
  47. (node.material.isShaderMaterial && node.material.envMap !== undefined))) {
  48. node.material.envMap = envMap;
  49. node.material.needsUpdate = true;
  50. }
  51. });
  52. // const mesh = this.el.getObject3D('mesh');
  53. // if (mesh) {
  54. // mesh.material = this.material;
  55. // }
  56. },
  57. /**
  58. * On each frame, update the 'time' uniform in the shaders.
  59. */
  60. tick: function (t) {
  61. }
  62. })
  63. //https://threejs.org/examples/webgl_shaders_sky.html
  64. AFRAME.registerComponent('skyshader', {
  65. init: function () {
  66. let sunSphereEl = document.querySelector('a-scene').querySelector('#sun');
  67. this.sunSphere = sunSphereEl.object3D;
  68. this.sky = new THREE.Sky();
  69. let scene = this.el.sceneEl;
  70. let effectController = {
  71. turbidity: 5,
  72. rayleigh: 2,
  73. mieCoefficient: 0.005,
  74. mieDirectionalG: 0.8,
  75. luminance: 1,
  76. inclination: 0, // elevation / inclination
  77. azimuth: 0.25, // Facing front,
  78. sun: ! true
  79. };
  80. let uniforms = this.sky.uniforms;
  81. uniforms.turbidity.value = effectController.turbidity;
  82. uniforms.rayleigh.value = effectController.rayleigh;
  83. uniforms.luminance.value = effectController.luminance;
  84. uniforms.mieCoefficient.value = effectController.mieCoefficient;
  85. uniforms.mieDirectionalG.value = effectController.mieDirectionalG;
  86. this.el.setObject3D('mesh', this.sky.mesh);
  87. let distance = 400000;
  88. var theta = Math.PI * (effectController.inclination - 0.5);
  89. var phi = 2 * Math.PI * (effectController.azimuth - 0.5);
  90. this.sunSphere.position.x = distance * Math.cos(phi);
  91. this.sunSphere.position.y = distance * Math.sin(phi) * Math.sin(theta);
  92. this.sunSphere.position.z = distance * Math.sin(phi) * Math.cos(theta);
  93. this.sunSphere.visible = effectController.sun;
  94. this.sky.uniforms.sunPosition.value.copy(this.sunSphere.position);
  95. },
  96. update: function () {
  97. },
  98. tick: function (t) {
  99. }
  100. })
  101. AFRAME.registerComponent('sun', {
  102. init: function () {
  103. this.sunSphere = new THREE.Mesh(
  104. new THREE.SphereBufferGeometry(20000, 16, 8),
  105. new THREE.MeshBasicMaterial({ color: 0xffffff })
  106. );
  107. this.sunSphere.position.y = - 700000;
  108. this.sunSphere.visible = true;
  109. this.el.setObject3D('mesh', this.sunSphere);
  110. },
  111. update: function () {
  112. },
  113. tick: function (t) {
  114. }
  115. })
  116. AFRAME.registerComponent('avatarbvh', {
  117. /**
  118. * Creates a new THREE.ShaderMaterial using the two shaders defined
  119. * in vertex.glsl and fragment.glsl.
  120. */
  121. init: function () {
  122. let skeletonHelper;
  123. let self = this;
  124. this.clock = new THREE.Clock();
  125. let loader = new THREE.BVHLoader();
  126. loader.load( "./assets/walk.bvh", function( result ) {
  127. skeletonHelper = new THREE.SkeletonHelper( result.skeleton.bones[ 0 ] );
  128. skeletonHelper.skeleton = result.skeleton; // allow animation mixer to bind to SkeletonHelper directly
  129. //var material = new THREE.LineBasicMaterial( { vertexColors: THREE.VertexColors, depthTest: false, depthWrite: false, transparent: true } );
  130. skeletonHelper.material.depthTest = true;
  131. skeletonHelper.material.lineWidth = 10.0;
  132. skeletonHelper.material.depthWrite = true;
  133. skeletonHelper.material.transparent = false;
  134. skeletonHelper.material.flatShading = true;
  135. var boneContainer = new THREE.Group();
  136. boneContainer.add( result.skeleton.bones[ 0 ] );
  137. self.el.setObject3D('skeletonHelper', skeletonHelper);
  138. self.el.setObject3D('mesh', boneContainer);
  139. //scene.add( skeletonHelper );
  140. // scene.add( boneContainer );
  141. // play animation
  142. self.mixer = new THREE.AnimationMixer( skeletonHelper );
  143. self.mixer.clipAction( result.clip ).setEffectiveWeight( 1.0 ).play();
  144. } );
  145. },
  146. /**
  147. * Update the ShaderMaterial when component data changes.
  148. */
  149. update: function () {
  150. },
  151. tick: function (t) {
  152. var delta = this.clock.getDelta();
  153. if ( this.mixer ) this.mixer.update( delta );
  154. }
  155. })