From 7f6247fa5e0e2a30b00fdf32a26d29cf1c7059f7 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 29 Nov 2021 16:28:57 +0800 Subject: [PATCH 01/17] fix: `worldToViewportPoint()` bug --- packages/core/src/Camera.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/core/src/Camera.ts b/packages/core/src/Camera.ts index 5bff6416e3..d4f501e68d 100644 --- a/packages/core/src/Camera.ts +++ b/packages/core/src/Camera.ts @@ -314,7 +314,7 @@ export class Camera extends Component { // Transform of coordinate axis. out.x = (nx + 1.0) * 0.5; out.y = (1.0 - ny) * 0.5; - out.z = nz; + out.z = (nz + 1.0) * 0.5; out.w = w; return out; } @@ -480,10 +480,9 @@ export class Camera extends Component { private _innerViewportToWorldPoint(point: Vector3, invViewProjMat: Matrix, out: Vector3): Vector3 { // Depth is a normalized value, 0 is nearPlane, 1 is farClipPlane. - const depth = point.z * 2 - 1; // Transform to clipping space matrix const clipPoint = MathTemp.tempVec4; - clipPoint.setValue(point.x * 2 - 1, 1 - point.y * 2, depth, 1); + clipPoint.setValue(point.x * 2 - 1, 1 - point.y * 2, point.z * 2 - 1, 1); Vector4.transform(clipPoint, invViewProjMat, clipPoint); const invW = 1.0 / clipPoint.w; out.x = clipPoint.x * invW; From 1abb051dd596fa9e25b4e51f4f139f22abbbc791 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 29 Nov 2021 17:52:53 +0800 Subject: [PATCH 02/17] fix: `screenToViewportPoint()` bug --- packages/core/src/Camera.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/core/src/Camera.ts b/packages/core/src/Camera.ts index d4f501e68d..a14ec4832b 100644 --- a/packages/core/src/Camera.ts +++ b/packages/core/src/Camera.ts @@ -361,6 +361,7 @@ export class Camera extends Component { const viewport = this.viewport; out.x = (point.x / canvas.width - viewport.x) / viewport.z; out.y = (point.y / canvas.height - viewport.y) / viewport.w; + (point).z !== undefined && ((out).z = (point).z); return out; } From 58c91b4a062e851f62df2ecaca0327ff4556aa38 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 29 Nov 2021 19:49:46 +0800 Subject: [PATCH 03/17] refactor: opt `viewportToWorldPoint` and `screenToWorldPoint` --- packages/core/src/Camera.ts | 37 ++++++++++++++++++++----------------- packages/math/src/Matrix.ts | 6 +++--- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/packages/core/src/Camera.ts b/packages/core/src/Camera.ts index a14ec4832b..6af7a7365d 100644 --- a/packages/core/src/Camera.ts +++ b/packages/core/src/Camera.ts @@ -18,6 +18,7 @@ import { Transform } from "./Transform"; import { UpdateFlag } from "./UpdateFlag"; class MathTemp { + static tempRay = new Ray(); static tempMat4 = new Matrix(); static tempVec4 = new Vector4(); static tempVec3 = new Vector3(); @@ -321,32 +322,33 @@ export class Camera extends Component { /** * Transform a point from viewport space to world space. - * @param point - Point in viewport space, X and Y are the viewport space coordinates, Z is the viewport depth. The near clipping plane is 0, and the far clipping plane is 1 + * @param point - Point in viewport space, X and Y are the camera viewport space coordinates, Z is in world units from the camera * @param out - Point in world space * @returns Point in world space */ viewportToWorldPoint(point: Vector3, out: Vector3): Vector3 { - const invViewProjMat = this.invViewProjMat; - return this._innerViewportToWorldPoint(point, invViewProjMat, out); + const { tempVec2: pointXY, tempRay: ray } = MathTemp; + pointXY.setValue(point.x, point.y); + this.viewportPointToRay(pointXY, ray); + Vector3.scale(ray.direction, point.z, out); + Vector3.add(ray.origin, out, out); + return out; } /** * Generate a ray by a point in viewport. - * @param point - Point in viewport space, which is represented by normalization + * @param point - Point in viewport space, X and Y are the camera viewport space coordinates * @param out - Ray * @returns Ray */ viewportPointToRay(point: Vector2, out: Ray): Ray { - const clipPoint = MathTemp.tempVec3; + const invViewProjMat = this.invViewProjMat; // Use the intersection of the near clipping plane as the origin point. - clipPoint.setValue(point.x, point.y, 0); - const origin = this.viewportToWorldPoint(clipPoint, out.origin); + const origin = this._innerViewportToWorldPoint(point, 0.0, invViewProjMat, out.origin); // Use the intersection of the far clipping plane as the origin point. - clipPoint.z = 1.0; - const farPoint: Vector3 = this._innerViewportToWorldPoint(clipPoint, this._invViewProjMat, clipPoint); - Vector3.subtract(farPoint, origin, out.direction); - out.direction.normalize(); - + const direction: Vector3 = this._innerViewportToWorldPoint(point, 1.0, invViewProjMat, out.direction); + Vector3.subtract(direction, origin, direction); + direction.normalize(); return out; } @@ -391,8 +393,9 @@ export class Camera extends Component { } /** - * Transform a point from screen space to world space. - * @param point - Screen space point + * Transform a point from screen space to world space.= + * + * @param point - Screen space point, the top-left of the screen is (0,0), the right-bottom is (pixelWidth,pixelHeight), The z position is in world units from the camera * @param out - Point in world space * @returns Point in world space */ @@ -403,7 +406,7 @@ export class Camera extends Component { /** * Generate a ray by a point in screen. - * @param point - Point in screen space, the unit is pixel + * @param point - Point in screen space, the top-left of the screen is (0,0), the right-bottom is (pixelWidth,pixelHeight) * @param out - Ray * @returns Ray */ @@ -479,11 +482,11 @@ export class Camera extends Component { this._isInvViewProjDirty.flag = true; } - private _innerViewportToWorldPoint(point: Vector3, invViewProjMat: Matrix, out: Vector3): Vector3 { + private _innerViewportToWorldPoint(pointXY: Vector2, pointZ: number, invViewProjMat: Matrix, out: Vector3): Vector3 { // Depth is a normalized value, 0 is nearPlane, 1 is farClipPlane. // Transform to clipping space matrix const clipPoint = MathTemp.tempVec4; - clipPoint.setValue(point.x * 2 - 1, 1 - point.y * 2, point.z * 2 - 1, 1); + clipPoint.setValue(pointXY.x * 2 - 1, 1 - pointXY.y * 2, pointZ * 2 - 1, 1); Vector4.transform(clipPoint, invViewProjMat, clipPoint); const invW = 1.0 / clipPoint.w; out.x = clipPoint.x * invW; diff --git a/packages/math/src/Matrix.ts b/packages/math/src/Matrix.ts index f4e9b88db7..0ae87e3559 100644 --- a/packages/math/src/Matrix.ts +++ b/packages/math/src/Matrix.ts @@ -517,15 +517,15 @@ export class Matrix implements IClone { /** * Calculate a perspective projection matrix. - * @param fovy - Field of view in the y direction, in radians + * @param fovY - Field of view in the y direction, in radians * @param aspect - Aspect ratio, defined as view space width divided by height * @param near - The depth of the near plane * @param far - The depth of the far plane * @param out - The calculated perspective projection matrix */ - static perspective(fovy: number, aspect: number, near: number, far: number, out: Matrix): void { + static perspective(fovY: number, aspect: number, near: number, far: number, out: Matrix): void { const oe = out.elements; - const f = 1.0 / Math.tan(fovy / 2); + const f = 1.0 / Math.tan(fovY / 2); const nf = 1 / (near - far); oe[0] = f / aspect; From c0eeb8ee9a528568c152029d1edeeeb7f48c6617 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 29 Nov 2021 19:51:40 +0800 Subject: [PATCH 04/17] fix: comments --- packages/core/src/Camera.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/Camera.ts b/packages/core/src/Camera.ts index 6af7a7365d..04363baca0 100644 --- a/packages/core/src/Camera.ts +++ b/packages/core/src/Camera.ts @@ -393,7 +393,7 @@ export class Camera extends Component { } /** - * Transform a point from screen space to world space.= + * Transform a point from screen space to world space. * * @param point - Screen space point, the top-left of the screen is (0,0), the right-bottom is (pixelWidth,pixelHeight), The z position is in world units from the camera * @param out - Point in world space From e2ad46f658b70b127cb9d9d8c7506b97c3f2632e Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 29 Nov 2021 19:55:04 +0800 Subject: [PATCH 05/17] fix: opt code --- packages/core/src/Camera.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/core/src/Camera.ts b/packages/core/src/Camera.ts index 04363baca0..6c184eabd9 100644 --- a/packages/core/src/Camera.ts +++ b/packages/core/src/Camera.ts @@ -378,6 +378,7 @@ export class Camera extends Component { const viewport = this.viewport; out.x = (viewport.x + point.x * viewport.z) * canvas.width; out.y = (viewport.y + point.y * viewport.w) * canvas.height; + (point).z !== undefined && ((out).z = (point).z); return out; } From c248b9f1d79fe9cc08d820759bd14083cd92db55 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 29 Nov 2021 20:00:46 +0800 Subject: [PATCH 06/17] refactor: opt code --- packages/core/src/Camera.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/core/src/Camera.ts b/packages/core/src/Camera.ts index 6c184eabd9..a31cc25369 100644 --- a/packages/core/src/Camera.ts +++ b/packages/core/src/Camera.ts @@ -21,7 +21,6 @@ class MathTemp { static tempRay = new Ray(); static tempMat4 = new Matrix(); static tempVec4 = new Vector4(); - static tempVec3 = new Vector3(); static tempVec2 = new Vector2(); } From 81ad193811080c7c91c2cd8447f19abb22f756d3 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Tue, 30 Nov 2021 14:15:43 +0800 Subject: [PATCH 07/17] Merge branch 'main' of github.com:oasis-engine/engine --- packages/core/src/Camera.ts | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/packages/core/src/Camera.ts b/packages/core/src/Camera.ts index a31cc25369..b629cb022a 100644 --- a/packages/core/src/Camera.ts +++ b/packages/core/src/Camera.ts @@ -341,7 +341,7 @@ export class Camera extends Component { * @returns Ray */ viewportPointToRay(point: Vector2, out: Ray): Ray { - const invViewProjMat = this.invViewProjMat; + const invViewProjMat = this._getInvViewProjMat(); // Use the intersection of the near clipping plane as the origin point. const origin = this._innerViewportToWorldPoint(point, 0.0, invViewProjMat, out.origin); // Use the intersection of the far clipping plane as the origin point. @@ -495,33 +495,31 @@ export class Camera extends Component { return out; } - private _updateShaderData(context: RenderContext) { + private _updateShaderData(context: RenderContext): void { const shaderData = this.shaderData; shaderData.setMatrix(Camera._viewMatrixProperty, this.viewMatrix); shaderData.setMatrix(Camera._projectionMatrixProperty, this.projectionMatrix); shaderData.setMatrix(Camera._vpMatrixProperty, context._viewProjectMatrix); shaderData.setMatrix(Camera._inverseViewMatrixProperty, this._transform.worldMatrix); - shaderData.setMatrix(Camera._inverseProjectionMatrixProperty, this.inverseProjectionMatrix); + shaderData.setMatrix(Camera._inverseProjectionMatrixProperty, this._getInverseProjectionMatrix()); shaderData.setVector3(Camera._cameraPositionProperty, this._transform.worldPosition); } /** - * @private * The inverse matrix of view projection matrix. - */ - get invViewProjMat(): Matrix { + */ ƒ; + private _getInvViewProjMat(): Matrix { if (this._isInvViewProjDirty.flag) { this._isInvViewProjDirty.flag = false; - Matrix.multiply(this._transform.worldMatrix, this.inverseProjectionMatrix, this._invViewProjMat); + Matrix.multiply(this._transform.worldMatrix, this._getInverseProjectionMatrix(), this._invViewProjMat); } return this._invViewProjMat; } /** - * @private * The inverse of the projection matrix. */ - get inverseProjectionMatrix(): Readonly { + private _getInverseProjectionMatrix(): Readonly { if (this._isInvProjMatDirty) { this._isInvProjMatDirty = false; Matrix.invert(this.projectionMatrix, this._inverseProjectionMatrix); From d2d9a8453e43579247b5bed64b0190cf25caedeb Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Tue, 30 Nov 2021 14:16:06 +0800 Subject: [PATCH 08/17] refactor: opt code --- packages/core/src/Camera.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/Camera.ts b/packages/core/src/Camera.ts index b629cb022a..ca5fb4d0b6 100644 --- a/packages/core/src/Camera.ts +++ b/packages/core/src/Camera.ts @@ -507,7 +507,7 @@ export class Camera extends Component { /** * The inverse matrix of view projection matrix. - */ ƒ; + */ private _getInvViewProjMat(): Matrix { if (this._isInvViewProjDirty.flag) { this._isInvViewProjDirty.flag = false; From 6d68052d3e122ecea33e81251b0e723a350f0f5a Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 6 Dec 2021 17:06:22 +0800 Subject: [PATCH 09/17] fix: front face bug --- .../src/RenderPipeline/BasicRenderPipeline.ts | 4 ++-- packages/core/src/RenderPipeline/RenderQueue.ts | 3 ++- .../core/src/RenderPipeline/SpriteBatcher.ts | 2 +- .../src/RenderPipeline/SpriteMaskBatcher.ts | 2 +- packages/core/src/Transform.ts | 11 +++++++++++ packages/core/src/shader/state/RasterState.ts | 17 ++++++++++++++--- packages/core/src/shader/state/RenderState.ts | 8 +++++--- 7 files changed, 36 insertions(+), 11 deletions(-) diff --git a/packages/core/src/RenderPipeline/BasicRenderPipeline.ts b/packages/core/src/RenderPipeline/BasicRenderPipeline.ts index 345c14740f..1e54db6080 100644 --- a/packages/core/src/RenderPipeline/BasicRenderPipeline.ts +++ b/packages/core/src/RenderPipeline/BasicRenderPipeline.ts @@ -229,7 +229,7 @@ export class BasicRenderPipeline { program.uploadAll(program.materialUniformBlock, _backgroundTextureMaterial.shaderData); program.uploadUngroupTextures(); - _backgroundTextureMaterial.renderState._apply(engine); + _backgroundTextureMaterial.renderState._apply(engine, true); rhi.drawPrimitive(mesh, mesh.subMesh, program); } @@ -263,7 +263,7 @@ export class BasicRenderPipeline { program.uploadAll(program.materialUniformBlock, shaderData); program.uploadUngroupTextures(); - renderState._apply(engine); + renderState._apply(engine, true); rhi.drawPrimitive(mesh, mesh.subMesh, program); } } diff --git a/packages/core/src/RenderPipeline/RenderQueue.ts b/packages/core/src/RenderPipeline/RenderQueue.ts index f07ac02cbd..fedfadf825 100644 --- a/packages/core/src/RenderPipeline/RenderQueue.ts +++ b/packages/core/src/RenderPipeline/RenderQueue.ts @@ -137,7 +137,8 @@ export class RenderQueue { program.uploadUngroupTextures(); } } - material.renderState._apply(camera.engine); + material.renderState._apply(camera.engine,element.component.entity.transform._isFrontFaceInvert()); + rhi.drawPrimitive(element.mesh, element.subMesh, program); } else { const spriteElement = item; diff --git a/packages/core/src/RenderPipeline/SpriteBatcher.ts b/packages/core/src/RenderPipeline/SpriteBatcher.ts index 828ecc8657..e4a76ecb43 100644 --- a/packages/core/src/RenderPipeline/SpriteBatcher.ts +++ b/packages/core/src/RenderPipeline/SpriteBatcher.ts @@ -113,7 +113,7 @@ export class SpriteBatcher extends Basic2DBatcher { program.uploadAll(program.rendererUniformBlock, renderer.shaderData); program.uploadAll(program.materialUniformBlock, material.shaderData); - material.renderState._apply(engine); + material.renderState._apply(engine,true); engine._hardwareRenderer.drawPrimitive(mesh, subMesh, program); diff --git a/packages/core/src/RenderPipeline/SpriteMaskBatcher.ts b/packages/core/src/RenderPipeline/SpriteMaskBatcher.ts index 2c95387d82..f41ab540e2 100644 --- a/packages/core/src/RenderPipeline/SpriteMaskBatcher.ts +++ b/packages/core/src/RenderPipeline/SpriteMaskBatcher.ts @@ -93,7 +93,7 @@ export class SpriteMaskBatcher extends Basic2DBatcher { program.uploadAll(program.rendererUniformBlock, renderer.shaderData); program.uploadAll(program.materialUniformBlock, material.shaderData); - material.renderState._apply(engine); + material.renderState._apply(engine, true); engine._hardwareRenderer.drawPrimitive(mesh, subMesh, program); } diff --git a/packages/core/src/Transform.ts b/packages/core/src/Transform.ts index 8ed7b43983..757a4fa0dc 100644 --- a/packages/core/src/Transform.ts +++ b/packages/core/src/Transform.ts @@ -518,6 +518,17 @@ export class Transform extends Component { this._updateAllWorldFlag(); } + /** + * @internal + */ + _isFrontFaceInvert(): boolean { + const scale = this.lossyWorldScale; + let isInvert = scale.x < 0; + scale.y < 0 && (isInvert = !isInvert); + scale.z < 0 && (isInvert = !isInvert); + return isInvert; + } + /** * Get worldMatrix: Will trigger the worldMatrix update of itself and all parent entities. * Get worldPosition: Will trigger the worldMatrix, local position update of itself and the worldMatrix update of all parent entities. diff --git a/packages/core/src/shader/state/RasterState.ts b/packages/core/src/shader/state/RasterState.ts index b17345459e..4c53588a2e 100644 --- a/packages/core/src/shader/state/RasterState.ts +++ b/packages/core/src/shader/state/RasterState.ts @@ -15,15 +15,17 @@ export class RasterState { /** @internal */ _cullFaceEnable: boolean = true; + /** @internal */ + _frontFaceInvert: boolean = false; /** * @internal */ - _apply(hardwareRenderer: IHardwareRenderer, lastRenderState: RenderState): void { - this._platformApply(hardwareRenderer, lastRenderState.rasterState); + _apply(hardwareRenderer: IHardwareRenderer, lastRenderState: RenderState, frontFaceInvert: boolean): void { + this._platformApply(hardwareRenderer, lastRenderState.rasterState, frontFaceInvert); } - private _platformApply(rhi: IHardwareRenderer, lastState: RasterState): void { + private _platformApply(rhi: IHardwareRenderer, lastState: RasterState, frontFaceInvert: boolean): void { const gl = rhi.gl; const { cullMode, depthBias, slopeScaledDepthBias } = this; @@ -49,6 +51,15 @@ export class RasterState { } } + if (frontFaceInvert !== lastState._frontFaceInvert) { + if (frontFaceInvert) { + gl.frontFace(gl.CW); + } else { + gl.frontFace(gl.CCW); + } + lastState._frontFaceInvert == frontFaceInvert; + } + // apply polygonOffset. if (depthBias !== lastState.depthBias || slopeScaledDepthBias !== lastState.slopeScaledDepthBias) { if (depthBias !== 0 || slopeScaledDepthBias !== 0) { diff --git a/packages/core/src/shader/state/RenderState.ts b/packages/core/src/shader/state/RenderState.ts index 00044705e0..127d25235a 100644 --- a/packages/core/src/shader/state/RenderState.ts +++ b/packages/core/src/shader/state/RenderState.ts @@ -17,13 +17,15 @@ export class RenderState { /** Raster state. */ readonly rasterState: RasterState = new RasterState(); - /** @internal */ - _apply(engine: Engine): void { + /** + * @internal + */ + _apply(engine: Engine, frontFaceInvert: boolean): void { const hardwareRenderer = engine._hardwareRenderer; const lastRenderState = engine._lastRenderState; this.blendState._apply(hardwareRenderer, lastRenderState); this.depthState._apply(hardwareRenderer, lastRenderState); this.stencilState._apply(hardwareRenderer, lastRenderState); - this.rasterState._apply(hardwareRenderer, lastRenderState); + this.rasterState._apply(hardwareRenderer, lastRenderState, frontFaceInvert); } } From 2b888ca5db3225bad960231796ebbf3f19a8c81f Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 6 Dec 2021 17:25:18 +0800 Subject: [PATCH 10/17] refactor: fix code --- packages/core/src/RenderPipeline/BasicRenderPipeline.ts | 4 ++-- packages/core/src/RenderPipeline/SpriteBatcher.ts | 2 +- packages/core/src/RenderPipeline/SpriteMaskBatcher.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/core/src/RenderPipeline/BasicRenderPipeline.ts b/packages/core/src/RenderPipeline/BasicRenderPipeline.ts index 1e54db6080..b58ded5e26 100644 --- a/packages/core/src/RenderPipeline/BasicRenderPipeline.ts +++ b/packages/core/src/RenderPipeline/BasicRenderPipeline.ts @@ -229,7 +229,7 @@ export class BasicRenderPipeline { program.uploadAll(program.materialUniformBlock, _backgroundTextureMaterial.shaderData); program.uploadUngroupTextures(); - _backgroundTextureMaterial.renderState._apply(engine, true); + _backgroundTextureMaterial.renderState._apply(engine, false); rhi.drawPrimitive(mesh, mesh.subMesh, program); } @@ -263,7 +263,7 @@ export class BasicRenderPipeline { program.uploadAll(program.materialUniformBlock, shaderData); program.uploadUngroupTextures(); - renderState._apply(engine, true); + renderState._apply(engine, false); rhi.drawPrimitive(mesh, mesh.subMesh, program); } } diff --git a/packages/core/src/RenderPipeline/SpriteBatcher.ts b/packages/core/src/RenderPipeline/SpriteBatcher.ts index e4a76ecb43..c382bfbca0 100644 --- a/packages/core/src/RenderPipeline/SpriteBatcher.ts +++ b/packages/core/src/RenderPipeline/SpriteBatcher.ts @@ -113,7 +113,7 @@ export class SpriteBatcher extends Basic2DBatcher { program.uploadAll(program.rendererUniformBlock, renderer.shaderData); program.uploadAll(program.materialUniformBlock, material.shaderData); - material.renderState._apply(engine,true); + material.renderState._apply(engine,false); engine._hardwareRenderer.drawPrimitive(mesh, subMesh, program); diff --git a/packages/core/src/RenderPipeline/SpriteMaskBatcher.ts b/packages/core/src/RenderPipeline/SpriteMaskBatcher.ts index f41ab540e2..4236e6533c 100644 --- a/packages/core/src/RenderPipeline/SpriteMaskBatcher.ts +++ b/packages/core/src/RenderPipeline/SpriteMaskBatcher.ts @@ -93,7 +93,7 @@ export class SpriteMaskBatcher extends Basic2DBatcher { program.uploadAll(program.rendererUniformBlock, renderer.shaderData); program.uploadAll(program.materialUniformBlock, material.shaderData); - material.renderState._apply(engine, true); + material.renderState._apply(engine, false); engine._hardwareRenderer.drawPrimitive(mesh, subMesh, program); } From 0e9423f1d9e4dec53741687c539df57ac6c38f6e Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Tue, 7 Dec 2021 17:34:00 +0800 Subject: [PATCH 11/17] Merge branch 'main' of github.com:oasis-engine/engine --- packages/core/src/RenderPipeline/RenderQueue.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/RenderPipeline/RenderQueue.ts b/packages/core/src/RenderPipeline/RenderQueue.ts index fedfadf825..8c23ba65c8 100644 --- a/packages/core/src/RenderPipeline/RenderQueue.ts +++ b/packages/core/src/RenderPipeline/RenderQueue.ts @@ -137,7 +137,7 @@ export class RenderQueue { program.uploadUngroupTextures(); } } - material.renderState._apply(camera.engine,element.component.entity.transform._isFrontFaceInvert()); + material.renderState._apply(camera.engine, renderer.entity.transform._isFrontFaceInvert()); rhi.drawPrimitive(element.mesh, element.subMesh, program); } else { From 6cf4ec36428898c0f6e6aafc9ef9fffaf852f88e Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Tue, 7 Dec 2021 18:13:43 +0800 Subject: [PATCH 12/17] fix: front face bug --- packages/core/src/shader/state/RasterState.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/shader/state/RasterState.ts b/packages/core/src/shader/state/RasterState.ts index 4c53588a2e..04672267f4 100644 --- a/packages/core/src/shader/state/RasterState.ts +++ b/packages/core/src/shader/state/RasterState.ts @@ -57,7 +57,7 @@ export class RasterState { } else { gl.frontFace(gl.CCW); } - lastState._frontFaceInvert == frontFaceInvert; + lastState._frontFaceInvert = frontFaceInvert; } // apply polygonOffset. From 21f9f4df9b49283e62bf2c265f97f54fcb9562d3 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Wed, 29 Dec 2021 16:00:55 +0800 Subject: [PATCH 13/17] fix: asset destroy bug --- packages/core/src/Engine.ts | 3 +-- .../src/RenderPipeline/BasicRenderPipeline.ts | 4 ++-- .../core/src/RenderPipeline/RenderQueue.ts | 8 +++---- packages/core/src/Scene.ts | 9 -------- packages/core/src/asset/RefObject.ts | 12 ++--------- packages/core/src/asset/ResourceManager.ts | 15 +++++++------ packages/core/src/base/EngineObject.ts | 21 +++++++++++++++++++ packages/core/src/base/EventDispatcher.ts | 5 ++--- packages/core/src/shader/ShaderProgram.ts | 2 +- packages/loader/src/gltf/GLTFResource.ts | 2 +- packages/loader/src/scene-loader/Oasis.ts | 6 ++++-- 11 files changed, 45 insertions(+), 42 deletions(-) diff --git a/packages/core/src/Engine.ts b/packages/core/src/Engine.ts index e6925f881e..61bd5d8b05 100644 --- a/packages/core/src/Engine.ts +++ b/packages/core/src/Engine.ts @@ -11,7 +11,6 @@ import { FeatureManager } from "./FeatureManager"; import { InputManager } from "./input/InputManager"; import { RenderQueueType } from "./material/enums/RenderQueueType"; import { Material } from "./material/Material"; -import { ModelMesh, PrimitiveMesh } from "./mesh"; import { PhysicsManager } from "./physics"; import { IHardwareRenderer } from "./renderingHardwareInterface/IHardwareRenderer"; import { ClassPool } from "./RenderPipeline/ClassPool"; @@ -179,7 +178,7 @@ export class Engine extends EventDispatcher { * @param physics - native physics Engine */ constructor(canvas: Canvas, hardwareRenderer: IHardwareRenderer, physics?: IPhysics, settings?: EngineSettings) { - super(null); + super(); this._hardwareRenderer = hardwareRenderer; this._hardwareRenderer.init(canvas); if (physics) { diff --git a/packages/core/src/RenderPipeline/BasicRenderPipeline.ts b/packages/core/src/RenderPipeline/BasicRenderPipeline.ts index b58ded5e26..327ec60758 100644 --- a/packages/core/src/RenderPipeline/BasicRenderPipeline.ts +++ b/packages/core/src/RenderPipeline/BasicRenderPipeline.ts @@ -227,7 +227,7 @@ export class BasicRenderPipeline { const program = _backgroundTextureMaterial.shader._getShaderProgram(engine, Shader._compileMacros); program.bind(); program.uploadAll(program.materialUniformBlock, _backgroundTextureMaterial.shaderData); - program.uploadUngroupTextures(); + program.uploadUnGroupTextures(); _backgroundTextureMaterial.renderState._apply(engine, false); rhi.drawPrimitive(mesh, mesh.subMesh, program); @@ -261,7 +261,7 @@ export class BasicRenderPipeline { program.bind(); program.groupingOtherUniformBlock(); program.uploadAll(program.materialUniformBlock, shaderData); - program.uploadUngroupTextures(); + program.uploadUnGroupTextures(); renderState._apply(engine, false); rhi.drawPrimitive(mesh, mesh.subMesh, program); diff --git a/packages/core/src/RenderPipeline/RenderQueue.ts b/packages/core/src/RenderPipeline/RenderQueue.ts index 8c23ba65c8..beb204a213 100644 --- a/packages/core/src/RenderPipeline/RenderQueue.ts +++ b/packages/core/src/RenderPipeline/RenderQueue.ts @@ -104,8 +104,8 @@ export class RenderQueue { program.uploadAll(program.cameraUniformBlock, cameraData); program.uploadAll(program.rendererUniformBlock, rendererData); program.uploadAll(program.materialUniformBlock, materialData); - // Ungroup textures should upload default value, texture uint maybe change by logic of texture bind. - program.uploadUngroupTextures(); + // UnGroup textures should upload default value, texture uint maybe change by logic of texture bind. + program.uploadUnGroupTextures(); program._uploadCamera = camera; program._uploadRenderer = renderer; program._uploadMaterial = material; @@ -132,9 +132,9 @@ export class RenderQueue { program.uploadTextures(program.materialUniformBlock, materialData); } - // We only consider switchProgram case, because ungroup texture's value is always default. + // We only consider switchProgram case, because UnGroup texture's value is always default. if (switchProgram) { - program.uploadUngroupTextures(); + program.uploadUnGroupTextures(); } } material.renderState._apply(camera.engine, renderer.entity.transform._isFrontFaceInvert()); diff --git a/packages/core/src/Scene.ts b/packages/core/src/Scene.ts index ab9e072410..2fc7e09b58 100644 --- a/packages/core/src/Scene.ts +++ b/packages/core/src/Scene.ts @@ -33,7 +33,6 @@ export class Scene extends EngineObject { /** @internal */ _globalShaderMacro: ShaderMacroCollection = new ShaderMacroCollection(); - private _destroyed: boolean = false; private _rootEntities: Entity[] = []; private _ambientLight: AmbientLight; @@ -72,13 +71,6 @@ export class Scene extends EngineObject { return this._rootEntities; } - /** - * Whether it's destroyed. - */ - get destroyed(): boolean { - return this._destroyed; - } - /** * Create scene. * @param engine - Engine @@ -218,7 +210,6 @@ export class Scene extends EngineObject { this._activeCameras.length = 0; (Scene.sceneFeatureManager as any)._objects = []; this.shaderData._addRefCount(-1); - this._destroyed = true; } /** diff --git a/packages/core/src/asset/RefObject.ts b/packages/core/src/asset/RefObject.ts index 0b8ab6319d..1b721f3da8 100644 --- a/packages/core/src/asset/RefObject.ts +++ b/packages/core/src/asset/RefObject.ts @@ -10,7 +10,6 @@ export abstract class RefObject extends EngineObject implements IRefObject { isGCIgnored: boolean = false; private _refCount: number = 0; - private _destroyed: boolean = false; /** * Counted by valid references. @@ -19,13 +18,6 @@ export abstract class RefObject extends EngineObject implements IRefObject { return this._refCount; } - /** - * Whether it has been destroyed. - */ - get destroyed(): boolean { - return this._destroyed; - } - protected constructor(engine: Engine) { super(engine); engine.resourceManager._addRefObject(this.instanceId, this); @@ -43,7 +35,7 @@ export abstract class RefObject extends EngineObject implements IRefObject { // resourceManager maybe null,because engine has destroyed. // TODO:the right way to fix this is to ensure destroy all when call engine.destroy,thus don't need to add this project. if (resourceManager) { - resourceManager._deleteAsset(this); + super.destroy(); resourceManager._deleteRefObject(this.instanceId); } @@ -53,7 +45,7 @@ export abstract class RefObject extends EngineObject implements IRefObject { } this._engine = null; this._onDestroy(); - this._destroyed = true; + return true; } diff --git a/packages/core/src/asset/ResourceManager.ts b/packages/core/src/asset/ResourceManager.ts index d2fc8bb984..28d812c18e 100644 --- a/packages/core/src/asset/ResourceManager.ts +++ b/packages/core/src/asset/ResourceManager.ts @@ -1,10 +1,9 @@ +import { Engine, EngineObject } from ".."; +import { ObjectValues } from "../base/Util"; import { AssetPromise } from "./AssetPromise"; +import { Loader } from "./Loader"; import { LoadItem } from "./LoadItem"; import { RefObject } from "./RefObject"; -import { Engine } from ".."; -import { Loader } from "./Loader"; -import { AssetType } from "./AssetType"; -import { ObjectValues } from "../base/Util"; /** * ResourceManager @@ -17,10 +16,10 @@ export class ResourceManager { /** * @internal */ - static _addLoader(type: string, loader: Loader, extnames: string[]) { + static _addLoader(type: string, loader: Loader, extNames: string[]) { this._loaders[type] = loader; - for (let i = 0, len = extnames.length; i < len; i++) { - this._extTypeMapping[extnames[i]] = type; + for (let i = 0, len = extNames.length; i < len; i++) { + this._extTypeMapping[extNames[i]] = type; } } @@ -153,7 +152,7 @@ export class ResourceManager { /** * @internal */ - _deleteAsset(asset: RefObject): void { + _deleteAsset(asset: EngineObject): void { const id = asset.instanceId; const path = this._assetPool[id]; if (path) { diff --git a/packages/core/src/base/EngineObject.ts b/packages/core/src/base/EngineObject.ts index 44891784fc..b503df8e02 100644 --- a/packages/core/src/base/EngineObject.ts +++ b/packages/core/src/base/EngineObject.ts @@ -15,6 +15,8 @@ export abstract class EngineObject { @ignoreClone protected _engine: Engine; + protected _destroyed: boolean = false; + /** * Get the engine which the object belongs. */ @@ -22,7 +24,26 @@ export abstract class EngineObject { return this._engine; } + /** + * Whether it has been destroyed. + */ + get destroyed(): boolean { + return this._destroyed; + } + constructor(engine: Engine) { this._engine = engine; } + + /** + * Destroy self. + */ + destroy(): void { + if (this._destroyed) return; + + const resourceManager = this._engine.resourceManager; + resourceManager?._deleteAsset(this); + + this._destroyed = true; + } } diff --git a/packages/core/src/base/EventDispatcher.ts b/packages/core/src/base/EventDispatcher.ts index 0cc14a3482..e8329e7f9c 100644 --- a/packages/core/src/base/EventDispatcher.ts +++ b/packages/core/src/base/EventDispatcher.ts @@ -1,11 +1,10 @@ -import { EngineObject } from "./EngineObject"; -import { Event } from "./Event"; import { ignoreClone } from "../clone/CloneManager"; +import { Event } from "./Event"; /** * EventDispatcher, which can be inherited as a base class. */ -export class EventDispatcher extends EngineObject { +export class EventDispatcher { @ignoreClone private _evts = Object.create(null); private _evtCount = 0; diff --git a/packages/core/src/shader/ShaderProgram.ts b/packages/core/src/shader/ShaderProgram.ts index 0b6f06f210..df644d65a2 100644 --- a/packages/core/src/shader/ShaderProgram.ts +++ b/packages/core/src/shader/ShaderProgram.ts @@ -136,7 +136,7 @@ export class ShaderProgram { /** * Upload ungroup texture shader data in shader uniform block. */ - uploadUngroupTextures(): void { + uploadUnGroupTextures(): void { const textureUniforms = this.otherUniformBlock.textureUniforms; // textureUniforms property maybe null if ShaderUniformBlock not contain any texture. if (textureUniforms) { diff --git a/packages/loader/src/gltf/GLTFResource.ts b/packages/loader/src/gltf/GLTFResource.ts index 97de45519b..38f70eb6aa 100644 --- a/packages/loader/src/gltf/GLTFResource.ts +++ b/packages/loader/src/gltf/GLTFResource.ts @@ -13,7 +13,7 @@ import { import { IGLTF } from "./Schema"; /** - * Product after GLTF parser, usually, `defaultSceneRoot` is only needed to use. + * Product after GLTF parser, usually, `defaultSceneRoot` is only needed tso use. */ export class GLTFResource extends EngineObject { /** GLTF file url. */ diff --git a/packages/loader/src/scene-loader/Oasis.ts b/packages/loader/src/scene-loader/Oasis.ts index ea4d120f09..0a8d3a9953 100644 --- a/packages/loader/src/scene-loader/Oasis.ts +++ b/packages/loader/src/scene-loader/Oasis.ts @@ -1,4 +1,4 @@ -import { EventDispatcher, ObjectValues } from "@oasis-engine/core"; +import { Engine, EventDispatcher, ObjectValues } from "@oasis-engine/core"; import { AbilityManager } from "./AbilityManager"; import { NodeManager } from "./NodeManager"; import { SceneManager } from "./SceneManager"; @@ -15,9 +15,11 @@ export class Oasis extends EventDispatcher { private schema: Schema; public timeout: number; private oasis = this; + public engine:Engine; private constructor(private _options: Options, public readonly pluginManager: PluginManager) { - super(_options.engine); + super(); + this.engine=_options.engine; this.schema = _options.config; this.timeout = _options.timeout; _options.scripts = _options.scripts ?? {}; From edc4dbe0d0f2c5d134c911a99b682164a029effb Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Wed, 29 Dec 2021 16:17:18 +0800 Subject: [PATCH 14/17] refactor: opt code --- packages/core/src/asset/RefObject.ts | 2 -- packages/core/src/asset/ResourceManager.ts | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/core/src/asset/RefObject.ts b/packages/core/src/asset/RefObject.ts index 1b721f3da8..4d370930cf 100644 --- a/packages/core/src/asset/RefObject.ts +++ b/packages/core/src/asset/RefObject.ts @@ -58,7 +58,6 @@ export abstract class RefObject extends EngineObject implements IRefObject { /** * @internal - * Add reference resource. */ _addRefCount(value: number): void { this._refCount += value; @@ -66,7 +65,6 @@ export abstract class RefObject extends EngineObject implements IRefObject { /** * @internal - * Remove reference resource. */ _addToResourceManager(path: string): void { this._engine.resourceManager._addAsset(path, this); diff --git a/packages/core/src/asset/ResourceManager.ts b/packages/core/src/asset/ResourceManager.ts index 28d812c18e..ad0b2f78b2 100644 --- a/packages/core/src/asset/ResourceManager.ts +++ b/packages/core/src/asset/ResourceManager.ts @@ -144,7 +144,7 @@ export class ResourceManager { /** * @internal */ - _addAsset(path: string, asset: RefObject): void { + _addAsset(path: string, asset: EngineObject): void { this._assetPool[asset.instanceId] = path; this._assetUrlPool[path] = asset; } From 99b55d7ba4a742d0b0c8da59d54dd24a3862fbaa Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Wed, 29 Dec 2021 16:22:42 +0800 Subject: [PATCH 15/17] refactor: opt code --- packages/loader/src/gltf/GLTFResource.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/loader/src/gltf/GLTFResource.ts b/packages/loader/src/gltf/GLTFResource.ts index 38f70eb6aa..97de45519b 100644 --- a/packages/loader/src/gltf/GLTFResource.ts +++ b/packages/loader/src/gltf/GLTFResource.ts @@ -13,7 +13,7 @@ import { import { IGLTF } from "./Schema"; /** - * Product after GLTF parser, usually, `defaultSceneRoot` is only needed tso use. + * Product after GLTF parser, usually, `defaultSceneRoot` is only needed to use. */ export class GLTFResource extends EngineObject { /** GLTF file url. */ From 4f2b9e8691ec4999a55b2efab453182512b989f5 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Thu, 30 Dec 2021 12:39:38 +0800 Subject: [PATCH 16/17] refactor: opt code --- packages/core/src/base/EngineObject.ts | 4 +--- packages/loader/src/scene-loader/Oasis.ts | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/core/src/base/EngineObject.ts b/packages/core/src/base/EngineObject.ts index b503df8e02..19dfba9c1f 100644 --- a/packages/core/src/base/EngineObject.ts +++ b/packages/core/src/base/EngineObject.ts @@ -41,9 +41,7 @@ export abstract class EngineObject { destroy(): void { if (this._destroyed) return; - const resourceManager = this._engine.resourceManager; - resourceManager?._deleteAsset(this); - + this._engine.resourceManager?._deleteAsset(this); this._destroyed = true; } } diff --git a/packages/loader/src/scene-loader/Oasis.ts b/packages/loader/src/scene-loader/Oasis.ts index 0a8d3a9953..61c306c668 100644 --- a/packages/loader/src/scene-loader/Oasis.ts +++ b/packages/loader/src/scene-loader/Oasis.ts @@ -15,11 +15,11 @@ export class Oasis extends EventDispatcher { private schema: Schema; public timeout: number; private oasis = this; - public engine:Engine; + public engine: Engine; private constructor(private _options: Options, public readonly pluginManager: PluginManager) { super(); - this.engine=_options.engine; + this.engine = _options.engine; this.schema = _options.config; this.timeout = _options.timeout; _options.scripts = _options.scripts ?? {}; From b98620ac3390d19b62857dda5f94841d3410e2dc Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Thu, 30 Dec 2021 12:45:19 +0800 Subject: [PATCH 17/17] refactor: opt code --- packages/core/src/base/EngineObject.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/core/src/base/EngineObject.ts b/packages/core/src/base/EngineObject.ts index 19dfba9c1f..7736f010e6 100644 --- a/packages/core/src/base/EngineObject.ts +++ b/packages/core/src/base/EngineObject.ts @@ -11,10 +11,8 @@ export abstract class EngineObject { @ignoreClone readonly instanceId: number = ++EngineObject._instanceIdCounter; - /** Engine to which the object belongs. */ @ignoreClone protected _engine: Engine; - protected _destroyed: boolean = false; /**