monument-app.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. })