FdageSky.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. *
  3. * @param {webgl上下文:gl} gl
  4. * @param {json数据,里面包括了所有的模型文件} directory
  5. * @param {json数据,里面包括了sky的所有信息} skyInfo
  6. * //{"imageURL":"sky.dat","diffuseCoefficients":[0.168186,0.160808,0.121161,0,0.0668349,0.0631655,0.0531622,0,0.0703842,0.0722825,0.0526607,0,-0.0652816,-0.0583796,-0.0402046,0,-0.0322897,-0.0272824,-0.0174457,0,0.0388359,0.035343,0.0235444,0,0.011817,0.0121625,0.00825249,0,-0.105062,-0.110114,-0.0931767,0,0.0460938,0.04754,0.0361852,0],"backgroundMode":0,"backgroundBrightness":1,"backgroundColor":[0.035338,0.035338,0.035338,1]}
  7. */
  8. import { Texture } from '../textures/Texture.js';
  9. function FdageSky(gl, directory, skyInfo) {
  10. this.gl = gl;
  11. var d = directory.extract("sky.dat") || directory.extract("sky.png");
  12. var c,b;
  13. if (void 0 !== d) {
  14. this.specularTexture = new Texture(gl,{
  15. width: 256,
  16. height: 2048,
  17. clamp: !0
  18. });
  19. c = d.data;
  20. for (var d = d.data.length, e = d / 4, f = new Uint8Array(d), g = 0, h = 0; g < d; ++h)
  21. f[g++] = c[h + 2 * e],
  22. f[g++] = c[h + e],
  23. f[g++] = c[h],
  24. f[g++] = c[h + 3 * e];
  25. this.specularTexture.loadArray(f)
  26. }
  27. this.diffuseCoefficients = new Float32Array(skyInfo.diffuseCoefficients);
  28. this.backgroundMode = skyInfo.backgroundMode || 0;
  29. this.backgroundBrightness = skyInfo.backgroundBrightness || 1;
  30. this.backgroundColor = new Float32Array(skyInfo.backgroundColor);
  31. if (1 <= this.backgroundMode)
  32. if (this.backgroundShader = gl.shaderCache.fromURLs("skyvert.glsl", 3 == this.backgroundMode ? "skySH.glsl" : "sky.glsl", ["#define SKYMODE " + this.backgroundMode]),
  33. this.vertexBuffer = gl.createBuffer(),
  34. gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer),
  35. b = 1 / 256,
  36. c = 0.5 / 256,
  37. d = 2.8 * c,
  38. e = 0.5 * c,
  39. b = new Float32Array([0, 1, 0, 0.49609375 + b, 0.49609375 + b, 1, 0, 0, 0.9921875 + b, 0.49609375 + b, 0, 0, 1, 0.49609375 + b, 0.9921875 + b, -1, 0, 0, 0 + b, 0.49609375 + b, 0, 0, -1, 0.49609375 + b, 0 + b, 0, -1, 0, 0.9921875 + b, 0 + b, 0, -1, 0, 0.9921875 + b, 0.9921875 + b, 0, -1, 0, 0 + b, 0.9921875 + b, 0, -1, 0, 0 + b, 0 + b, d, 1 - d, -d, 0.5 + c, 0.5 - c, d, 1 - d, d, 0.5 + c, 0.5 + c, -d, 1 - d, d, 0.5 - c, 0.5 + c, -d, 1 - d, -d, 0.5 - c, 0.5 - c, -d, 0, -1 + d, 0.5 - c, 0 + b + c, d, 0, -1 + d, 0.5 + c, 0 + b + c, 1 - d, 0, -d, 0.9921875 + b - c, 0.5 - c, 1 - d, 0, d, 0.9921875 + b - c, 0.5 + c, d, 0, 1 - d, 0.5 + c, 0.9921875 + b - c, -d, 0, 1 - d, 0.5 - c, 0.9921875 + b - c, -1 + d, 0, d, 0 + b + c, 0.5 + c, -1 + d, 0, -d, 0 + b + c, 0.5 - c, 1, 0, 0, 0.9921875 + b - e, 0.49609375 + b, 0, 0, 1, 0.49609375 + b, 0.9921875 + b - e, -1, 0, 0, 0 + b + e, 0.49609375 + b, 0, 0, -1, 0.49609375 + b, 0 + b + e, 0, 1, 0, 0.49609375 + b - e, 0.49609375 + b, 0, 1, 0, 0.49609375 + b, 0.49609375 + b - e, 0, 1, 0, 0.49609375 + b + e, 0.49609375 + b, 0, 1, 0, 0.49609375 + b, 0.49609375 + b + e]),
  40. gl.bufferData(gl.ARRAY_BUFFER, b, gl.STATIC_DRAW),
  41. gl.bindBuffer(gl.ARRAY_BUFFER, null),
  42. this.indexBuffer = gl.createBuffer(),
  43. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer),
  44. b = new Uint16Array([2, 1, 6, 3, 2, 7, 8, 4, 3, 4, 5, 1, 9, 14, 15, 17, 10, 16, 18, 19, 11, 20, 13, 12, 28, 12, 13, 13, 24, 28, 28, 24, 9, 9, 24, 14, 25, 9, 15, 25, 15, 21, 10, 25, 21, 10, 21, 16, 22, 26, 10, 22, 10, 17, 18, 11, 26, 22, 18, 26, 19, 23, 27, 19, 27, 11, 23, 20, 27, 27, 20, 12]),
  45. this.skyIndexCount = b.length,
  46. gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, b, gl.STATIC_DRAW),
  47. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null),
  48. 3 == this.backgroundMode)
  49. for (this.backgroundCoefficients = new Float32Array(this.diffuseCoefficients),
  50. g = 0; g < this.backgroundCoefficients.length; ++g)
  51. this.backgroundCoefficients[g] *= this.backgroundBrightness;
  52. else {
  53. this.backgroundTexture = new Texture(gl,{
  54. width: 256,
  55. height: 256,
  56. clamp: !0
  57. });
  58. b = !1;
  59. var k;
  60. gl.ext.textureHalf && gl.ext.textureHalfLinear && (this.backgroundTexture.loadArray(null, gl.RGB, gl.ext.textureHalf.HALF_FLOAT_OES),
  61. k = new Framebuffer(gl,{
  62. color0: this.backgroundTexture
  63. }),
  64. b = k.valid);
  65. !b && gl.ext.textureFloat && gl.ext.textureFloatLinear && !gl.hints.mobile && (this.backgroundTexture.loadArray(null, gl.RGB, gl.FLOAT),
  66. k = new Framebuffer(gl,{
  67. color0: this.backgroundTexture
  68. }),
  69. b = k.valid);
  70. b || (this.backgroundTexture.loadArray(),
  71. k = new Framebuffer(gl,{
  72. color0: this.backgroundTexture
  73. }));
  74. k.bind();
  75. k = new Shader(gl);
  76. k.build("precision highp float; varying vec2 tc; attribute vec4 p; void main(){ gl_Position=p; tc=vec2(0.5,0.5/8.0)*p.xy+vec2(0.5,6.5/8.0); }", "precision highp float; varying vec2 tc; uniform sampler2D tex; uniform float b; void main(){vec4 s=texture2D(tex,tc); gl_FragColor.xyz=s.xyz*(b*s.w);}");
  77. k.bind();
  78. gl.uniform1f(k.params.b, 7 * Math.sqrt(this.backgroundBrightness));
  79. this.specularTexture.bind(k.samplers.tex);
  80. b = gl.createBuffer();
  81. gl.bindBuffer(gl.ARRAY_BUFFER, b);
  82. b = new Float32Array([-1, -1, 0.5, 1, 3, -1, 0.5, 1, -1, 3, 0.5, 1]);
  83. gl.bufferData(gl.ARRAY_BUFFER, b, gl.STATIC_DRAW);
  84. gl.enableVertexAttribArray(k.attribs.p);
  85. gl.vertexAttribPointer(k.attribs.p, 4, gl.FLOAT, !1, 0, 0);
  86. gl.drawArrays(gl.TRIANGLES, 0, 3);
  87. gl.disableVertexAttribArray(k.attribs.p)
  88. }
  89. }
  90. FdageSky.prototype.setClearColor = function() {
  91. if (marmoset.transparentBackground)
  92. this.gl.clearColor(0, 0, 0, 0);
  93. else if (1 > this.backgroundMode) {
  94. var a = this.backgroundColor;
  95. this.gl.clearColor(a[0], a[1], a[2], 1)
  96. } else
  97. this.gl.clearColor(0.0582, 0.06772, 0.07805, 1)
  98. };
  99. FdageSky.prototype.draw = function(a) {
  100. if (1 > this.backgroundMode || marmoset.transparentBackground)
  101. return !1;
  102. if (this.complete()) {
  103. var c = this.gl
  104. , b = this.backgroundShader
  105. , d = a.view
  106. , e = a.lights.invMatrix;
  107. b.bind();
  108. c.uniformMatrix4fv(b.params.uInverseSkyMatrix, !1, e);
  109. c.uniformMatrix4fv(b.params.uViewProjection, !1, d.viewProjectionMatrix);
  110. 3 == this.backgroundMode ? c.uniform4fv(b.params.uSkyCoefficients, this.backgroundCoefficients) : this.backgroundTexture.bind(b.samplers.tSkyTexture);
  111. a = 0.07 + 0.94 * (1 - a.stripData.activeFade());
  112. c.uniform1f(b.params.uAlpha, a);
  113. c.bindBuffer(c.ARRAY_BUFFER, this.vertexBuffer);
  114. c.enableVertexAttribArray(b.attribs.vPosition);
  115. c.vertexAttribPointer(b.attribs.vPosition, 3, c.FLOAT, !1, 20, 0);
  116. c.enableVertexAttribArray(b.attribs.vTexCoord);
  117. c.vertexAttribPointer(b.attribs.vTexCoord, 2, c.FLOAT, !1, 20, 12);
  118. c.bindBuffer(c.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
  119. 1 > a && (c.enable(c.BLEND),
  120. c.blendFunc(c.SRC_ALPHA, c.ONE_MINUS_SRC_ALPHA));
  121. c.depthMask(!1);
  122. c.disable(c.DEPTH_TEST);
  123. c.drawElements(c.TRIANGLES, this.skyIndexCount, c.UNSIGNED_SHORT, 0);
  124. c.enable(c.DEPTH_TEST);
  125. c.depthMask(!0);
  126. 1 > a && c.disable(c.BLEND);
  127. c.disableVertexAttribArray(b.attribs.vPosition);
  128. c.disableVertexAttribArray(b.attribs.vTexCoord)
  129. }
  130. };
  131. FdageSky.prototype.complete = function() {
  132. return this.backgroundShader && !this.backgroundShader.complete() ? !1 : this.specularTexture.complete()
  133. };
  134. export { FdageSky };