import { toFixedValue } from '@gedit/canvas-draw'; export const reMarsPlayer = () => { if (!window.ge) { return; } Object.defineProperty(window.ge.Player.prototype, 'doTick', { value: function (dt: number, forceRender: boolean): void { const { renderErrors } = this.renderer.engine; if (renderErrors.size > 0) { this.emit('rendererror', renderErrors.values().next().value); // 有渲染错误时暂停播放 this.ticker?.pause(); } const comps: ge.Composition[] = this.compositions; let skipRender = false; dt = Math.max(dt, toFixedValue(1000 / 60, 6)); dt = toFixedValue(Math.min(dt, 33), 6); comps.sort((a, b) => a.getIndex() - b.getIndex()); const currentCompositions = []; for (let i = 0; i < comps.length; i++) { const composition = comps[i]; // @ts-ignore const elementId = composition.url.elementId; const item = this.animElemMap.get(elementId); if (!item) { continue; } const interval = toFixedValue(1000 / item.fps, 6); if (composition.textureOffloaded) { skipRender = true; window.ge.logger.error( `Composition ${composition.name} texture offloaded, skip render.` ); currentCompositions.push(composition); continue; } if (!composition.isDestroyed && composition.renderer) { composition.setSpeed(item.speed); if (item.speed === -1 && composition.time <= 0) { const duration = composition.getDuration(); composition.gotoAndPlay(duration); } const p = (composition.prevTime || 0) + dt; if (p >= interval) { composition.update(p); composition.prevTime = 0; composition.runRenderer = true; } else { composition.prevTime = p; composition.runRenderer = false; } } if (!composition.isDestroyed) { currentCompositions.push(composition); } } this.compositions = currentCompositions; this.baseCompositionIndex = this.compositions.length; if (skipRender) { this.emit('rendererror', new Error('Play when texture offloaded.')); return this.ticker?.pause(); } if (!this.paused || forceRender) { const { level } = this.gpuCapability; const { gl } = (this.renderer as ge.GLRenderer).context; const time = level === 2 && this.reportGPUTime ? window.ge.gpuTimer(gl as WebGL2RenderingContext) : undefined; time?.begin(); if ( this.compositions.length || this.compositions.length < comps.length || forceRender ) { for (let i = 0; i < comps.length; i++) { const composition = comps[i]; if (!composition.runRenderer) { continue; } // @ts-ignore const elementId = composition.url.elementId; const item = this.animElemMap.get(elementId); if (!item) { continue; } this.canvas.style.width = `${item.width}px`; this.canvas.style.height = `${item.height}px`; this.canvas.width = item.width * item.pixelRatio; //* Math.min(window.devicePixelRatio, 2); this.canvas.height = item.height * item.pixelRatio; // * Math.min(window.devicePixelRatio, 2); this.renderer.resize(this.canvas.width, this.canvas.height); // console.log(this.canvas.width, window.devicePixelRatio); composition.camera.aspect = this.canvas.width / this.canvas.height; this.renderer.setFramebuffer(null); this.renderer.clear({ stencilAction: window.ge.TextureLoadAction.clear, clearStencil: 0, depthAction: window.ge.TextureLoadAction.clear, clearDepth: 1, colorAction: window.ge.TextureLoadAction.clear, clearColor: [0, 0, 0, 0], }); !composition.renderFrame.isDestroyed && this.renderer.renderRenderFrame(composition.renderFrame); // 单个渲染器渲染 // const gl = this.renderer.context.gl as WebGL2RenderingContext; // const pixels = new Uint8Array(gl.drawingBufferWidth * gl.drawingBufferHeight * 4); // gl.readPixels(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight, gl.RGBA, gl.UNSIGNED_BYTE, pixels); // const imageData = new ImageData(new Uint8ClampedArray(pixels), gl.drawingBufferWidth, gl.drawingBufferHeight); const ctx = item.context as CanvasRenderingContext2D; ctx.restore(); ctx.clearRect(0, 0, item.width, item.height); ctx.save(); const scale = 1 / item.pixelRatio; ctx.scale(scale, scale); // console.log(imageData); // ctx.putImageData(imageData, 0, 0); ctx.drawImage(this.canvas, 0, 0); } } time?.end(); time?.getTime().then(t => this.reportGPUTime?.(t ?? 0)).catch; if (this.autoPlaying) { this.emit('update', { player: this, playing: true, }); } } }, }); };