Compare commits

..

1 Commits

Author SHA1 Message Date
AncTe
fead02b9f3
Merge 91c7ee455c into 820dc5bf4c 2025-11-01 10:15:34 +00:00
4 changed files with 29 additions and 56 deletions

View File

@ -1,6 +1,3 @@
{ {
"name": "@motajs/render-assets", "name": "@motajs/render-assets"
"dependencies": {
"@motajs/client-base": "workspace:*"
}
} }

View File

@ -14,9 +14,9 @@ import {
} from './types'; } from './types';
import vert from './shader/pack.vert?raw'; import vert from './shader/pack.vert?raw';
import frag from './shader/pack.frag?raw'; import frag from './shader/pack.frag?raw';
import { compileGLWith } from './utils';
import { logger } from '@motajs/common'; import { logger } from '@motajs/common';
import { isNil } from 'lodash-es'; import { isNil } from 'lodash-es';
import { compileProgramWith } from 'packages/client-base/src/glUtils';
interface IndexMarkedComposedData { interface IndexMarkedComposedData {
/** 组合数据 */ /** 组合数据 */
@ -229,7 +229,7 @@ export class TextureMaxRectsWebGL2Composer
this.canvas.width = maxWidth; this.canvas.width = maxWidth;
this.canvas.height = maxHeight; this.canvas.height = maxHeight;
this.gl = this.canvas.getContext('webgl2')!; this.gl = this.canvas.getContext('webgl2')!;
const program = compileProgramWith(this.gl, vert, frag)!; const program = compileGLWith(this.gl, vert, frag)!;
this.program = program; this.program = program;
// 初始化画布数据 // 初始化画布数据

View File

@ -5,3 +5,4 @@ export * from './store';
export * from './streamComposer'; export * from './streamComposer';
export * from './texture'; export * from './texture';
export * from './types'; export * from './types';
export * from './utils';

View File

@ -1,45 +1,18 @@
import { logger } from '@motajs/common'; import { logger } from '@motajs/common';
/** export function compileGLWith(
*
* @param gl WebGL2
* @param type
* @param source
*/
export function compileShader(
gl: WebGL2RenderingContext, gl: WebGL2RenderingContext,
type: number, vert: string,
source: string frag: string
): WebGLShader | null { ): WebGLProgram | null {
const shader = gl.createShader(type); const vsShader = compileShader(gl, gl.VERTEX_SHADER, vert);
if (!shader) return null; const fsShader = compileShader(gl, gl.FRAGMENT_SHADER, frag);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { if (!vsShader || !fsShader) return null;
const info = gl.getShaderInfoLog(shader);
const typeStr = type === gl.VERTEX_SHADER ? 'vertex' : 'fragment';
logger.error(10, typeStr, info ?? '');
return null;
}
return shader;
}
/**
*
* @param gl WebGL2
* @param vs
* @param fs
*/
export function compileProgram(
gl: WebGL2RenderingContext,
vs: WebGLShader,
fs: WebGLShader
) {
const program = gl.createProgram(); const program = gl.createProgram();
gl.attachShader(program, vs); gl.attachShader(program, vsShader);
gl.attachShader(program, fs); gl.attachShader(program, fsShader);
gl.linkProgram(program); gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) { if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
@ -51,21 +24,23 @@ export function compileProgram(
return program; return program;
} }
/** function compileShader(
* 使
* @param gl WebGL2
* @param vs
* @param fs
*/
export function compileProgramWith(
gl: WebGL2RenderingContext, gl: WebGL2RenderingContext,
vs: string, type: number,
fs: string source: string
): WebGLProgram | null { ): WebGLShader | null {
const vsShader = compileShader(gl, gl.VERTEX_SHADER, vs); const shader = gl.createShader(type);
const fsShader = compileShader(gl, gl.FRAGMENT_SHADER, fs); if (!shader) return null;
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!vsShader || !fsShader) return null; // 如果编译失败
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
return compileProgram(gl, vsShader, fsShader); const info = gl.getShaderInfoLog(shader);
const typeStr = type === gl.VERTEX_SHADER ? 'vertex' : 'fragment';
logger.error(10, typeStr, info ?? '');
return null;
}
return shader;
} }