UNPKG

laya-coreui-es

Version:

laya核心库、ui库es版本,剔除了媒体、TTF等功能。基于 LayaAir-release_2.12.0 修改

129 lines (128 loc) 4.85 kB
import { ILaya } from "./../../ILaya"; import { Config } from "./../../Config"; import { LayaGL } from "../layagl/LayaGL"; import { Context } from "../resource/Context"; import { WebGL } from "../webgl/WebGL"; import { WebGLContext } from "../webgl/WebGLContext"; import { BlendMode } from "../webgl/canvas/BlendMode"; import { Shader2D } from "../webgl/shader/d2/Shader2D"; import { ShaderDefines2D } from "../webgl/shader/d2/ShaderDefines2D"; import { Value2D } from "../webgl/shader/d2/value/Value2D"; import { Buffer2D } from "../webgl/utils/Buffer2D"; import { SubmitBase } from "../webgl/submit/SubmitBase"; import { LayaGPU } from "../webgl/LayaGPU"; import { Browser } from "../utils/Browser"; import { RenderInfo } from "./RenderInfo"; export class Render { constructor(width, height, mainCanv) { this._timeId = 0; Render._mainCanvas = mainCanv; let source = Render._mainCanvas.source; source.id = "layaCanvas"; source.width = width; source.height = height; if (Render.isConchApp) { document.body.appendChild(source); } this.initRender(Render._mainCanvas, width, height); window.requestAnimationFrame(loop); function loop(stamp) { ILaya.stage._loop(); window.requestAnimationFrame(loop); } ILaya.stage.on("visibilitychange", this, this._onVisibilitychange); } _onVisibilitychange() { if (!ILaya.stage.isVisibility) { this._timeId = window.setInterval(this._enterFrame, 1000); } else if (this._timeId != 0) { window.clearInterval(this._timeId); } } initRender(canvas, w, h) { function getWebGLContext(canvas) { var gl; var names = ["webgl2", "webgl", "experimental-webgl", "webkit-3d", "moz-webgl"]; if (!Config.useWebGL2 || Browser.onBDMiniGame) { names.shift(); } for (var i = 0; i < names.length; i++) { try { gl = canvas.getContext(names[i], { stencil: Config.isStencil, alpha: Config.isAlpha, antialias: Config.isAntialias, premultipliedAlpha: Config.premultipliedAlpha, preserveDrawingBuffer: Config.preserveDrawingBuffer }); } catch (e) { } if (gl) { (names[i] === 'webgl2') && (WebGL._isWebGL2 = true); new LayaGL(); return gl; } } return null; } var gl = LayaGL.instance = WebGLContext.mainContext = getWebGLContext(Render._mainCanvas.source); if (Config.printWebglOrder) this._replaceWebglcall(gl); if (!gl) return false; LayaGL.instance = gl; LayaGL.layaGPUInstance = new LayaGPU(gl, WebGL._isWebGL2); canvas.size(w, h); Context.__init__(); SubmitBase.__init__(); var ctx = new Context(); ctx.isMain = true; Render._context = ctx; canvas._setContext(ctx); ShaderDefines2D.__init__(); Value2D.__init__(); Shader2D.__init__(); Buffer2D.__int__(gl); BlendMode._init_(gl); return true; } _replaceWebglcall(gl) { var tempgl = {}; for (const key in gl) { if (typeof gl[key] == "function" && key != "getError" && key != "__SPECTOR_Origin_getError" && key != "__proto__") { tempgl[key] = gl[key]; gl[key] = function () { let arr = []; for (let i = 0; i < arguments.length; i++) { arr.push(arguments[i]); } let result = tempgl[key].apply(gl, arr); console.log(RenderInfo.loopCount + ":gl." + key + ":" + arr); let err = gl.getError(); if (err) { console.log(err); debugger; } return result; }; } } } _enterFrame(e = null) { ILaya.stage._loop(); } static get context() { return Render._context; } static get canvas() { return Render._mainCanvas.source; } } Render.supportWebGLPlusAnimation = false; Render.supportWebGLPlusRendering = false; Render.isConchApp = false; { Render.isConchApp = (window.conch != null); if (Render.isConchApp) { Render.supportWebGLPlusRendering = false; } else if (window.qq != null && window.qq.webglPlus != null) { Render.supportWebGLPlusRendering = false; } }