diff --git a/.eslintrc.js b/.eslintrc.js old mode 100755 new mode 100644 diff --git a/.gitignore b/.gitignore old mode 100755 new mode 100644 diff --git a/package.json b/package.json old mode 100755 new mode 100644 diff --git a/public/data/pic0.png b/public/data/pic0.png old mode 100755 new mode 100644 diff --git a/public/data/pic1.jpg b/public/data/pic1.jpg old mode 100755 new mode 100644 diff --git a/public/data/test.txt b/public/data/test.txt old mode 100755 new mode 100644 diff --git a/public/data/textureUV.jpg b/public/data/textureUV.jpg old mode 100755 new mode 100644 diff --git a/public/favicon.ico b/public/favicon.ico old mode 100755 new mode 100644 diff --git a/public/index.html b/public/index.html old mode 100755 new mode 100644 diff --git a/public/logo192.png b/public/logo192.png old mode 100755 new mode 100644 diff --git a/public/logo512.png b/public/logo512.png old mode 100755 new mode 100644 diff --git a/public/manifest.json b/public/manifest.json old mode 100755 new mode 100644 diff --git a/public/pic0.png b/public/pic0.png old mode 100755 new mode 100644 diff --git a/public/pic1.jpg b/public/pic1.jpg old mode 100755 new mode 100644 diff --git a/public/robots.txt b/public/robots.txt old mode 100755 new mode 100644 diff --git a/src/App.css b/src/App.css old mode 100755 new mode 100644 diff --git a/src/App.tsx b/src/App.tsx old mode 100755 new mode 100644 diff --git a/src/components/Render.tsx b/src/components/Render.tsx index 6e4551ca..12d1b43d 100644 --- a/src/components/Render.tsx +++ b/src/components/Render.tsx @@ -1,39 +1,24 @@ -import React from "react"; -import { HttpRequest } from "../renderi/commons/HttpRequest"; +import React, { useEffect } from "react"; +import { BasicWebGLApplication } from "../renderi/webgl/GLRenderingContext"; interface RenderProps { name: string } -class Render extends React.Component { +const Render: React.FC = (prop) => { - //eslint-disable-next-line - constructor(prop) { - super(prop); - } - - componentDidMount() { + useEffect(() => { let canvas: HTMLCanvasElement | null = document.getElementById('render') as HTMLCanvasElement; - console.log(canvas) - - let ret = HttpRequest.loadImg("https://3dresource.aihomee.com/uploads/image/2021-04-09/original/202104091701593415.jpg"); - - ret.then((data) => { - let path:string = URL.createObjectURL(data); - document.querySelector('#ig')?.setAttribute("src",path); - }); - - } - - render() { - return <> -
- {this.props.name} - - test -
- ; - } + let app: BasicWebGLApplication = new BasicWebGLApplication(canvas); + app.run(); + }, []); + + + return
+ {prop.name} + +
+ } //lightgray diff --git a/src/examples/Demo1.ts b/src/examples/Demo1.ts old mode 100755 new mode 100644 diff --git a/src/examples/Test.ts b/src/examples/Test.ts old mode 100755 new mode 100644 diff --git a/src/index.css b/src/index.css old mode 100755 new mode 100644 diff --git a/src/index.tsx b/src/index.tsx old mode 100755 new mode 100644 diff --git a/src/react-app-env.d.ts b/src/react-app-env.d.ts old mode 100755 new mode 100644 diff --git a/src/render/common.ts b/src/render/common.ts old mode 100755 new mode 100644 diff --git a/src/render/common/DrawHelper.ts b/src/render/common/DrawHelper.ts old mode 100755 new mode 100644 diff --git a/src/render/common/HttpRequest.ts b/src/render/common/HttpRequest.ts old mode 100755 new mode 100644 diff --git a/src/render/common/ImageInfo.ts b/src/render/common/ImageInfo.ts old mode 100755 new mode 100644 diff --git a/src/render/core/Application.ts b/src/render/core/Application.ts old mode 100755 new mode 100644 diff --git a/src/render/core/Camera.ts b/src/render/core/Camera.ts old mode 100755 new mode 100644 diff --git a/src/render/core/CameraApplication.ts b/src/render/core/CameraApplication.ts old mode 100755 new mode 100644 diff --git a/src/render/core/Event.ts b/src/render/core/Event.ts old mode 100755 new mode 100644 diff --git a/src/render/core/Frustum.ts b/src/render/core/Frustum.ts old mode 100755 new mode 100644 diff --git a/src/render/core/Primitives.ts b/src/render/core/Primitives.ts old mode 100755 new mode 100644 diff --git a/src/render/core/Timer.ts b/src/render/core/Timer.ts old mode 100755 new mode 100644 diff --git a/src/render/ds/AdapterBase.ts b/src/render/ds/AdapterBase.ts old mode 100755 new mode 100644 diff --git a/src/render/ds/Dictionary.ts b/src/render/ds/Dictionary.ts old mode 100755 new mode 100644 diff --git a/src/render/ds/IAdapter.ts b/src/render/ds/IAdapter.ts old mode 100755 new mode 100644 diff --git a/src/render/ds/IEnumerator.ts b/src/render/ds/IEnumerator.ts old mode 100755 new mode 100644 diff --git a/src/render/ds/List.ts b/src/render/ds/List.ts old mode 100755 new mode 100644 diff --git a/src/render/ds/ListNode.ts b/src/render/ds/ListNode.ts old mode 100755 new mode 100644 diff --git a/src/render/ds/NodeB2TEnumberator.ts b/src/render/ds/NodeB2TEnumberator.ts old mode 100755 new mode 100644 diff --git a/src/render/ds/NodeEnumeratorFactory.ts b/src/render/ds/NodeEnumeratorFactory.ts old mode 100755 new mode 100644 diff --git a/src/render/ds/NodeT2BEnumerator.ts b/src/render/ds/NodeT2BEnumerator.ts old mode 100755 new mode 100644 diff --git a/src/render/ds/Queue.ts b/src/render/ds/Queue.ts old mode 100755 new mode 100644 diff --git a/src/render/ds/Stack.ts b/src/render/ds/Stack.ts old mode 100755 new mode 100644 diff --git a/src/render/ds/TreeNode.ts b/src/render/ds/TreeNode.ts old mode 100755 new mode 100644 diff --git a/src/render/ds/TypedArrayList.ts b/src/render/ds/TypedArrayList.ts old mode 100755 new mode 100644 diff --git a/src/render/ds/index.ts b/src/render/ds/index.ts old mode 100755 new mode 100644 diff --git a/src/render/math/Mat4.ts b/src/render/math/Mat4.ts old mode 100755 new mode 100644 diff --git a/src/render/math/Quat.ts b/src/render/math/Quat.ts old mode 100755 new mode 100644 diff --git a/src/render/math/Util.ts b/src/render/math/Util.ts old mode 100755 new mode 100644 diff --git a/src/render/math/Vec2.ts b/src/render/math/Vec2.ts old mode 100755 new mode 100644 diff --git a/src/render/math/Vec3.ts b/src/render/math/Vec3.ts old mode 100755 new mode 100644 diff --git a/src/render/math/Vec4.ts b/src/render/math/Vec4.ts old mode 100755 new mode 100644 diff --git a/src/render/math/index.ts b/src/render/math/index.ts old mode 100755 new mode 100644 diff --git a/src/render/webgl/WebGLApplication.ts b/src/render/webgl/WebGLApplication.ts old mode 100755 new mode 100644 diff --git a/src/render/webgl/WebGLAttribState.ts b/src/render/webgl/WebGLAttribState.ts old mode 100755 new mode 100644 diff --git a/src/render/webgl/WebGLHepler.ts b/src/render/webgl/WebGLHepler.ts old mode 100755 new mode 100644 diff --git a/src/render/webgl/WebGLMatrixStack.ts b/src/render/webgl/WebGLMatrixStack.ts old mode 100755 new mode 100644 diff --git a/src/render/webgl/WebGLMesh.ts b/src/render/webgl/WebGLMesh.ts old mode 100755 new mode 100644 diff --git a/src/render/webgl/WebGLProgram.ts b/src/render/webgl/WebGLProgram.ts old mode 100755 new mode 100644 diff --git a/src/render/webgl/WebGLProgramCache.ts b/src/render/webgl/WebGLProgramCache.ts old mode 100755 new mode 100644 diff --git a/src/render/webgl/WebGLShaderSource.ts b/src/render/webgl/WebGLShaderSource.ts old mode 100755 new mode 100644 diff --git a/src/render/webgl/WebGLTexture.ts b/src/render/webgl/WebGLTexture.ts old mode 100755 new mode 100644 diff --git a/src/render/webgl/WebGLTextureCache.ts b/src/render/webgl/WebGLTextureCache.ts old mode 100755 new mode 100644 diff --git a/src/render/webgl/index.ts b/src/render/webgl/index.ts old mode 100755 new mode 100644 diff --git a/src/renderi/commons/HttpRequest.ts b/src/renderi/commons/HttpRequest.ts old mode 100755 new mode 100644 diff --git a/src/renderi/commons/Util.ts b/src/renderi/commons/Util.ts old mode 100755 new mode 100644 diff --git a/src/renderi/commons/types.ts b/src/renderi/commons/types.ts old mode 100755 new mode 100644 diff --git a/src/renderi/core/Application.ts b/src/renderi/core/Application.ts old mode 100755 new mode 100644 diff --git a/src/renderi/core/Event.ts b/src/renderi/core/Event.ts old mode 100755 new mode 100644 diff --git a/src/renderi/core/ImageInfo.ts b/src/renderi/core/ImageInfo.ts old mode 100755 new mode 100644 diff --git a/src/renderi/core/Timer.ts b/src/renderi/core/Timer.ts old mode 100755 new mode 100644 diff --git a/src/renderi/lib/AsyncLoadTestApplication.ts b/src/renderi/lib/AsyncLoadTestApplication.ts old mode 100755 new mode 100644 index 1eec9643..d2d0bcf8 --- a/src/renderi/lib/AsyncLoadTestApplication.ts +++ b/src/renderi/lib/AsyncLoadTestApplication.ts @@ -1,3 +1,4 @@ +import { resolve } from "node:dns"; import { HttpRequest } from "../commons/HttpRequest"; import { Application } from "../core/Application"; @@ -22,16 +23,20 @@ export class AsyncLoadTestApplication extends Application { /** * 并行加载图片 */ - public loadImageParallel(): void { - let _promises: Promise[] = []; - for (let i = 0; i < this._urls.length; i++) { - _promises.push(HttpRequest.loadImageAsync(this._urls[i])); - } - Promise.all(_promises).then((images: HTMLImageElement[]) => { - for (let i: number = 0; i < images.length; i++) { - console.log("Load image parallel", images[i]); + public loadImageParallel(): Promise { + return new Promise((resolve, reject): void => { + let _promises: Promise[] = []; + for (let i = 0; i < this._urls.length; i++) { + _promises.push(HttpRequest.loadImageAsync(this._urls[i])); } + Promise.all(_promises).then((images: HTMLImageElement[]) => { + for (let i: number = 0; i < images.length; i++) { + console.log("Load image parallel", images[i]); + } + resolve(); + }); }); + } /** @@ -44,7 +49,35 @@ export class AsyncLoadTestApplication extends Application { public async run(): Promise { await this.loadImageSequence(); - await this.loadTextFile(); - this.loadImageParallel(); + await this.loadTextFile(); + await this.loadImageParallel(); + + // HttpRequest.loadImg("https://3dresource.aihomee.com/uploads/image/2021-04-09/original/202104091701593415.jpg"); + + // HttpRequest.loadImg("https://3dresource.aihomee.com/uploads/image/2021-06-21/original/202106210918315593.jpg"); + + // let urls: string[] = [ + // "https://3dresource.aihomee.com/uploads/image/2021-06-19/original/202106191144276197.jpg", + // "https://3dresource.aihomee.com/uploads/image/2021-04-22/original/202104221743486390.jpg", + // "https://3dresource.aihomee.com/uploads/image/2021-04-09/original/20210409170159930.jpg", + // "https://3dresource.aihomee.com/uploads/image/2021-04-09/original/202104092012357318.jpg", + // "https://3dresource.aihomee.com/uploads/image/2021-06-21/original/202106210916006390.jpg", + // "https://3dresource.aihomee.com/uploads/image/2021-06-21/original/202106210918315593.jpg", + // "https://3dresource.aihomee.com/uploads/image/2021-06-21/original/202106211159238419.jpg" + // ]; + + // for(let i: number = 0; i < urls.length; i++){ + // HttpRequest.loadImg(urls[i]); + // } + + + //console.log(ret); + + // await ret.then((data) => { + // let path:string = URL.createObjectURL(data); + // console.log("load img"); + // }); + + console.log("finished"); } } \ No newline at end of file diff --git a/src/renderi/lib/CameraApplication.ts b/src/renderi/lib/CameraApplication.ts old mode 100755 new mode 100644 diff --git a/src/renderi/math/Mat4.ts b/src/renderi/math/Mat4.ts old mode 100755 new mode 100644 index 3b788763..70d9af6f --- a/src/renderi/math/Mat4.ts +++ b/src/renderi/math/Mat4.ts @@ -18,6 +18,13 @@ export class mat4 { } } + public print(): void { + console.log(this.values[0] + " " + this.values[1] + " " + this.values[2] + " " + this.values[3]); + console.log(this.values[4] + " " + this.values[5] + " " + this.values[6] + " " + this.values[6]); + console.log(this.values[8] + " " + this.values[9] + " " + this.values[10] + " " + this.values[11]); + console.log(this.values[12] + " " + this.values[13] + " " + this.values[14] + " " + this.values[15]); + } + public set(values: number[]): mat4 { for (let i = 0; i < 16; i++) { this.values[i] = values[i]; @@ -608,25 +615,10 @@ export class mat4 { let y = vec3.cross(z, x).normalize(); return new mat4([ - x.x, - y.x, - z.x, - 0, - - x.y, - y.y, - z.y, - 0, - - x.z, - y.z, - z.z, - 0, - - -vec3.dot(x, position), - -vec3.dot(y, position), - -vec3.dot(z, position), - 1 + x.x, y.x, z.x, 0, + x.y, y.y, z.y, 0, + x.z, y.z, z.z, 0, + -vec3.dot(x, position), -vec3.dot(y, position), -vec3.dot(z, position), 1 ]); } diff --git a/src/renderi/math/Matrix4.ts b/src/renderi/math/Matrix4.ts new file mode 100644 index 00000000..f0010236 --- /dev/null +++ b/src/renderi/math/Matrix4.ts @@ -0,0 +1,199 @@ +import { EPSILON } from "../commons/types"; +import { vec3, vec4 } from "."; + + +export class Matrix4 { + + + /** + * @var 矩阵数据 + */ + public values = new Float32Array(16); + + + public constructor(values: number[] | null = null) { + if (values) { + this.setAll(values); + } + else { + this.setIdentity(); + } + } + + /** + * 通过数组去设置矩阵 + * @param values Array + * @returns + */ + public setAll(values: number[]): Matrix4 { + for (let i = 0; i < 16; i++) { + this.values[i] = values[i]; + } + + return this; + } + + /** + * 通过索引去设置矩阵 + * @param index + * @param value + * @returns + */ + public setIndex(index: number, value: number): Matrix4 { + if (index < 0 || index > 15) { + throw new Error("Index out of bounds"); + } + this.values[index] = value; + return this; + } + + /** + * 通过行和列去设置矩阵 + * @param row + * @param col + * @param value + * @returns + */ + public set(row: number, col: number, value: number): Matrix4 { + let index: number = row * 4 + col; + if (index < 0 || index > 15) { + throw new Error("Index out of bounds"); + } + this.setIndex(index, value); + return this; + } + + /** + * 通过索引来获取矩阵元素的值 + * @param index + * @returns + */ + public getIndex(index: number): number { + if (index < 0 || index > 15) { + throw new Error("Index out of bounds"); + } + return this.values[index]; + } + + /** + * 获取矩阵的一行 + * @param index + * @returns + */ + public getRow(index: number): vec4 { + let ret: vec4 = new vec4(); + ret.r = this.get(index, 0); + ret.g = this.get(index, 1); + ret.b = this.get(index, 2); + ret.a = this.get(index, 3); + return ret; + } + + /** + * 获取矩阵的一列 + * @param index + * @returns + */ + public getCol(index: number): vec4 { + let ret: vec4 = new vec4(); + ret.r = this.get(0, index); + ret.g = this.get(1, index); + ret.b = this.get(2, index); + ret.a = this.get(3, index); + return ret; + } + + /** + * 通过行列号来获取矩阵元素的值 + * @param row + * @param col + * @returns + */ + public get(row: number, col: number): number { + let index: number = row * 4 + col; + return this.getIndex(index); + } + + /** + * 打印 + */ + public print(): void { + console.log(this.values[0] + " " + this.values[1] + " " + this.values[2] + " " + this.values[3]); + console.log(this.values[4] + " " + this.values[5] + " " + this.values[6] + " " + this.values[6]); + console.log(this.values[8] + " " + this.values[9] + " " + this.values[10] + " " + this.values[11]); + console.log(this.values[12] + " " + this.values[13] + " " + this.values[14] + " " + this.values[15]); + } + /** + * 设置成单位矩阵 + * @returns + */ + public setIdentity(): Matrix4 { + this.values[0] = 1; + this.values[1] = 0; + this.values[2] = 0; + this.values[3] = 0; + this.values[4] = 0; + this.values[5] = 1; + this.values[6] = 0; + this.values[7] = 0; + this.values[8] = 0; + this.values[9] = 0; + this.values[10] = 1; + this.values[11] = 0; + this.values[12] = 0; + this.values[13] = 0; + this.values[14] = 0; + this.values[15] = 1; + + return this; + } + + + /** + * 矩阵乘 + * @param matrix + * @returns + */ + public multiply(matrix: Matrix4): Matrix4 { + + for (let i = 0; i < 4; i++) { + for(let j = 0; j < 4; j++){ + let v1: vec4 = this.getRow() + this.set(i,j, ) + } + } + return this; + } + + + /** + * 生成一个投影矩阵 + * @param left + * @param right + * @param bottom + * @param top + * @param near + * @param far + * @returns + */ + public static orthographic(left: number, right: number, bottom: number, top: number, near: number, far: number): Matrix4 { + let ret: Matrix4 = new Matrix4(); + + let tm: Matrix4 = new Matrix4(); + tm.set(0, 3, -(left + right) / 2); + tm.set(1, 3, -(bottom + top) / 2); + tm.set(2, 3, -(near + far) / 2); + + let sm: Matrix4 = new Matrix4(); + sm.set(0, 0, 2 / (right - left)); + sm.set(1, 2, 2 / (top - bottom)); + sm.set(2, 2, 2 / (far - near)); + + let tzm: Matrix4 = new Matrix4(); + tzm.set(2, 2, -1); + + + + return ret; + } +} \ No newline at end of file diff --git a/src/renderi/math/Quat.ts b/src/renderi/math/Quat.ts old mode 100755 new mode 100644 diff --git a/src/renderi/math/Vec2.ts b/src/renderi/math/Vec2.ts old mode 100755 new mode 100644 diff --git a/src/renderi/math/Vec3.ts b/src/renderi/math/Vec3.ts old mode 100755 new mode 100644 diff --git a/src/renderi/math/Vec4.ts b/src/renderi/math/Vec4.ts old mode 100755 new mode 100644 diff --git a/src/renderi/math/index.ts b/src/renderi/math/index.ts old mode 100755 new mode 100644 index a144eb24..f3a0c1f4 --- a/src/renderi/math/index.ts +++ b/src/renderi/math/index.ts @@ -3,4 +3,5 @@ export * from './Vec3'; export * from './Vec4'; export * from './Mat4'; export * from './Quat'; -export * from '../commons/Util'; \ No newline at end of file +export * from '../commons/Util'; +export * from './Matrix4'; \ No newline at end of file diff --git a/src/renderi/webgl/GLApplication.ts b/src/renderi/webgl/GLApplication.ts old mode 100755 new mode 100644 diff --git a/src/renderi/webgl/GLHelper.ts b/src/renderi/webgl/GLHelper.ts new file mode 100644 index 00000000..409a02b2 --- /dev/null +++ b/src/renderi/webgl/GLHelper.ts @@ -0,0 +1,30 @@ +export class GLHelper { + /** + * 打印webgl当前状态 + */ + public static printStates(gl: WebGLRenderingContext | null): void { + if (gl === null) { + return; + } + console.log("1. isBlendEnable = " + gl.isEnabled(gl.BLEND)); + console.log("2. isCullFaceEnable = " + gl.isEnabled(gl.CULL_FACE)); + console.log("3. isDepthTestEnable = " + gl.isEnabled(gl.DEPTH_TEST)); + console.log("4. isDitherEnable = " + gl.isEnabled(gl.DITHER)); + console.log("5. isPolygonOffsetFillEnable = " + gl.isEnabled(gl.POLYGON_OFFSET_FILL)); + console.log("6. isSampleAlphtToCoverageEnable = " + gl.isEnabled(gl.SAMPLE_ALPHA_TO_COVERAGE)); + console.log("7. isSampleCoverageEnable = " + gl.isEnabled(gl.SAMPLE_COVERAGE)); + console.log("8. isScissorEnable = " + gl.isEnabled(gl.SCISSOR_TEST)); + console.log("9. isStencilEnable = " + gl.isEnabled(gl.STENCIL_TEST)); + } + + /** + * 触发上下文丢失事件 + * @param gl + */ + public static triggerContextLostEvent(gl: WebGLRenderingContext): void { + let ret: WEBGL_lose_context | null = gl.getExtension('WEBGL_lose_context'); + if(ret !== null){ + ret.loseContext(); + } + } +} \ No newline at end of file diff --git a/src/renderi/webgl/GLRenderingContext.ts b/src/renderi/webgl/GLRenderingContext.ts new file mode 100644 index 00000000..a7057dfa --- /dev/null +++ b/src/renderi/webgl/GLRenderingContext.ts @@ -0,0 +1,71 @@ +import { Application } from "../core/Application"; +import { mat4, Util, vec3, Matrix4 } from "../math"; +import { GLHelper } from "./GLHelper" + +export class BasicWebGLApplication extends Application { + + /** + * @var 渲染上下文 + */ + public gl: WebGLRenderingContext; + + /** + * @var 投影矩阵 + */ + public projectMatrix: mat4; + + /** + * @var 视图矩阵 + */ + public viewMatrix: mat4; + + /** + * @var 视图投影矩阵 + */ + public viewProjectMatrix: mat4; + + + public constructor(canvas: HTMLCanvasElement) { + + super(canvas); + + //创建渲染上下文 + let contextAttribs: WebGLContextAttributes = { + depth: true, //创建深度缓存区 + stencil: true, //创建蒙板缓冲区 + alpha: true, //颜色缓冲区是否开始透明通道 + premultipliedAlpha: true, //预乘???? + antialias: true,//是否使用抗锯齿 + preserveDrawingBuffer: false //是否保留上一帧的内容,这里设置乘false,所以不保留,所以不需要clear + } + let ctx: WebGLRenderingContext | null = this.canvas.getContext("webgl", contextAttribs); + if (ctx === null) { + throw new Error("getContext Error!"); + } + this.gl = ctx; + + //渲染上下文丢失事件处理,如果上下文丢失,则会重新创建一个上下文对象 + let that = this; + this.canvas.addEventListener('webglcontextlost', function (e) { + console.log(JSON.stringify(e)); + let ctx: WebGLRenderingContext | null = that.canvas.getContext("webgl", contextAttribs); + if (ctx === null) { + throw new Error("getContext Error!"); + } + that.gl = ctx; + }, false); + + //初始化视图和投影矩阵 + this.projectMatrix = mat4.perspective(Util.toRadian(45), this.canvas.width/this.canvas.height, 0.1, 100); + this.viewMatrix = mat4.lookAt(new vec3([0,0,5]), new vec3([0,0,0]), new vec3([0,1,0])); + this.viewProjectMatrix = mat4.product(this.projectMatrix, this.viewMatrix); + } + + + public run(): void { + let m:Matrix4 = Matrix4.orthographic(-50, 50, -25, 25, -25, 25); + + //GLHelper.printStates(this.gl); + //console.log(this.gl.getParameter(this.gl.DEPTH_TEST)); + } +} \ No newline at end of file diff --git a/src/reportWebVitals.ts b/src/reportWebVitals.ts old mode 100755 new mode 100644 diff --git a/src/setupTests.ts b/src/setupTests.ts old mode 100755 new mode 100644 diff --git a/tsconfig.json b/tsconfig.json old mode 100755 new mode 100644