123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- /**
- *
- * @param {webgl上下文:gl} gl
- * @param {json数据,里面包括了所有的模型文件} directory
- * @param {json数据,里面包括了sky的所有信息} skyInfo
- * //{"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]}
- */
- import { Texture } from '../textures/Texture.js';
- function FdageSky(gl, directory, skyInfo) {
- this.gl = gl;
- var d = directory.extract("sky.dat") || directory.extract("sky.png");
- var c,b;
- if (void 0 !== d) {
- this.specularTexture = new Texture(gl,{
- width: 256,
- height: 2048,
- clamp: !0
- });
- c = d.data;
- for (var d = d.data.length, e = d / 4, f = new Uint8Array(d), g = 0, h = 0; g < d; ++h)
- f[g++] = c[h + 2 * e],
- f[g++] = c[h + e],
- f[g++] = c[h],
- f[g++] = c[h + 3 * e];
- this.specularTexture.loadArray(f)
- }
- this.diffuseCoefficients = new Float32Array(skyInfo.diffuseCoefficients);
- this.backgroundMode = skyInfo.backgroundMode || 0;
- this.backgroundBrightness = skyInfo.backgroundBrightness || 1;
- this.backgroundColor = new Float32Array(skyInfo.backgroundColor);
- if (1 <= this.backgroundMode)
- if (this.backgroundShader = gl.shaderCache.fromURLs("skyvert.glsl", 3 == this.backgroundMode ? "skySH.glsl" : "sky.glsl", ["#define SKYMODE " + this.backgroundMode]),
- this.vertexBuffer = gl.createBuffer(),
- gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer),
- b = 1 / 256,
- c = 0.5 / 256,
- d = 2.8 * c,
- e = 0.5 * c,
- 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]),
- gl.bufferData(gl.ARRAY_BUFFER, b, gl.STATIC_DRAW),
- gl.bindBuffer(gl.ARRAY_BUFFER, null),
- this.indexBuffer = gl.createBuffer(),
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer),
- 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]),
- this.skyIndexCount = b.length,
- gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, b, gl.STATIC_DRAW),
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null),
- 3 == this.backgroundMode)
- for (this.backgroundCoefficients = new Float32Array(this.diffuseCoefficients),
- g = 0; g < this.backgroundCoefficients.length; ++g)
- this.backgroundCoefficients[g] *= this.backgroundBrightness;
- else {
- this.backgroundTexture = new Texture(gl,{
- width: 256,
- height: 256,
- clamp: !0
- });
- b = !1;
- var k;
- gl.ext.textureHalf && gl.ext.textureHalfLinear && (this.backgroundTexture.loadArray(null, gl.RGB, gl.ext.textureHalf.HALF_FLOAT_OES),
- k = new Framebuffer(gl,{
- color0: this.backgroundTexture
- }),
- b = k.valid);
- !b && gl.ext.textureFloat && gl.ext.textureFloatLinear && !gl.hints.mobile && (this.backgroundTexture.loadArray(null, gl.RGB, gl.FLOAT),
- k = new Framebuffer(gl,{
- color0: this.backgroundTexture
- }),
- b = k.valid);
- b || (this.backgroundTexture.loadArray(),
- k = new Framebuffer(gl,{
- color0: this.backgroundTexture
- }));
- k.bind();
- k = new Shader(gl);
- 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);}");
- k.bind();
- gl.uniform1f(k.params.b, 7 * Math.sqrt(this.backgroundBrightness));
- this.specularTexture.bind(k.samplers.tex);
- b = gl.createBuffer();
- gl.bindBuffer(gl.ARRAY_BUFFER, b);
- b = new Float32Array([-1, -1, 0.5, 1, 3, -1, 0.5, 1, -1, 3, 0.5, 1]);
- gl.bufferData(gl.ARRAY_BUFFER, b, gl.STATIC_DRAW);
- gl.enableVertexAttribArray(k.attribs.p);
- gl.vertexAttribPointer(k.attribs.p, 4, gl.FLOAT, !1, 0, 0);
- gl.drawArrays(gl.TRIANGLES, 0, 3);
- gl.disableVertexAttribArray(k.attribs.p)
- }
- }
- FdageSky.prototype.setClearColor = function() {
- if (marmoset.transparentBackground)
- this.gl.clearColor(0, 0, 0, 0);
- else if (1 > this.backgroundMode) {
- var a = this.backgroundColor;
- this.gl.clearColor(a[0], a[1], a[2], 1)
- } else
- this.gl.clearColor(0.0582, 0.06772, 0.07805, 1)
- };
- FdageSky.prototype.draw = function(a) {
- if (1 > this.backgroundMode || marmoset.transparentBackground)
- return !1;
- if (this.complete()) {
- var c = this.gl
- , b = this.backgroundShader
- , d = a.view
- , e = a.lights.invMatrix;
- b.bind();
- c.uniformMatrix4fv(b.params.uInverseSkyMatrix, !1, e);
- c.uniformMatrix4fv(b.params.uViewProjection, !1, d.viewProjectionMatrix);
- 3 == this.backgroundMode ? c.uniform4fv(b.params.uSkyCoefficients, this.backgroundCoefficients) : this.backgroundTexture.bind(b.samplers.tSkyTexture);
- a = 0.07 + 0.94 * (1 - a.stripData.activeFade());
- c.uniform1f(b.params.uAlpha, a);
- c.bindBuffer(c.ARRAY_BUFFER, this.vertexBuffer);
- c.enableVertexAttribArray(b.attribs.vPosition);
- c.vertexAttribPointer(b.attribs.vPosition, 3, c.FLOAT, !1, 20, 0);
- c.enableVertexAttribArray(b.attribs.vTexCoord);
- c.vertexAttribPointer(b.attribs.vTexCoord, 2, c.FLOAT, !1, 20, 12);
- c.bindBuffer(c.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
- 1 > a && (c.enable(c.BLEND),
- c.blendFunc(c.SRC_ALPHA, c.ONE_MINUS_SRC_ALPHA));
- c.depthMask(!1);
- c.disable(c.DEPTH_TEST);
- c.drawElements(c.TRIANGLES, this.skyIndexCount, c.UNSIGNED_SHORT, 0);
- c.enable(c.DEPTH_TEST);
- c.depthMask(!0);
- 1 > a && c.disable(c.BLEND);
- c.disableVertexAttribArray(b.attribs.vPosition);
- c.disableVertexAttribArray(b.attribs.vTexCoord)
- }
- };
- FdageSky.prototype.complete = function() {
- return this.backgroundShader && !this.backgroundShader.complete() ? !1 : this.specularTexture.complete()
- };
- export { FdageSky };
|