Skip to content

Commit

Permalink
Refactor cloneTo to copyFrom for math library and rename `setValu…
Browse files Browse the repository at this point in the history
…e` to `set` (#844)

* feat: refactor `cloneTo` to `copyFrom` for math library
* feat: rename `setValue` to `set` for math library
  • Loading branch information
GuoLei1990 authored Jun 28, 2022
1 parent 84e16c4 commit 0e26e2c
Show file tree
Hide file tree
Showing 71 changed files with 836 additions and 781 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/2d/dynamic-atlas/DynamicTextAtlas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class DynamicTextAtlas {

const { _width, _height } = this;
const region = DynamicTextAtlas._region;
region.setValue(this._curX / _width, this._curY / _height, width / _width, height / _height);
region.set(this._curX / _width, this._curY / _height, width / _width, height / _height);

// destroy origin texture.
sprite.texture && sprite.texture.destroy();
Expand Down
40 changes: 20 additions & 20 deletions packages/core/src/2d/sprite/Sprite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class Sprite extends RefObject {
set atlasRegion(value: Rect) {
const x = MathUtil.clamp(value.x, 0, 1);
const y = MathUtil.clamp(value.y, 0, 1);
this._atlasRegion.setValue(x, y, MathUtil.clamp(value.width, 0, 1 - x), MathUtil.clamp(value.height, 0, 1 - y));
this._atlasRegion.set(x, y, MathUtil.clamp(value.width, 0, 1 - x), MathUtil.clamp(value.height, 0, 1 - y));
this._setDirtyFlagTrue(DirtyFlag.positions | DirtyFlag.uv);
}

Expand All @@ -99,7 +99,7 @@ export class Sprite extends RefObject {
set atlasRegionOffset(value: Vector4) {
const x = MathUtil.clamp(value.x, 0, 1);
const y = MathUtil.clamp(value.y, 0, 1);
this._atlasRegionOffset.setValue(x, y, MathUtil.clamp(value.z, 0, 1 - x), MathUtil.clamp(value.w, 0, 1 - y));
this._atlasRegionOffset.set(x, y, MathUtil.clamp(value.z, 0, 1 - x), MathUtil.clamp(value.w, 0, 1 - y));
this._setDirtyFlagTrue(DirtyFlag.positions | DirtyFlag.uv);
}

Expand All @@ -114,7 +114,7 @@ export class Sprite extends RefObject {
const pivot = this._pivot;
const { x, y } = value;
if (pivot === value || pivot.x !== x || pivot.y !== y) {
pivot.setValue(x, y);
pivot.set(x, y);
this._setDirtyFlagTrue(DirtyFlag.positions);
}
}
Expand All @@ -130,7 +130,7 @@ export class Sprite extends RefObject {
const region = this._region;
const x = MathUtil.clamp(value.x, 0, 1);
const y = MathUtil.clamp(value.y, 0, 1);
region.setValue(x, y, MathUtil.clamp(value.width, 0, 1 - x), MathUtil.clamp(value.height, 0, 1 - y));
region.set(x, y, MathUtil.clamp(value.width, 0, 1 - x), MathUtil.clamp(value.height, 0, 1 - y));
this._setDirtyFlagTrue(DirtyFlag.positions | DirtyFlag.uv);
}

Expand Down Expand Up @@ -171,8 +171,8 @@ export class Sprite extends RefObject {
this._texture = texture;
this._pixelsPerUnit = pixelsPerUnit;

region && region.cloneTo(this._region);
pivot && pivot.cloneTo(this._pivot);
region && this._region.copyFrom(region);
pivot && this._pivot.copyFrom(pivot);

this._triangles = Sprite._rectangleTriangles;
}
Expand All @@ -192,8 +192,8 @@ export class Sprite extends RefObject {
);
cloneSprite._assetID = this._assetID;
cloneSprite._atlasRotated = this._atlasRotated;
this._atlasRegion.cloneTo(cloneSprite._atlasRegion);
this._atlasRegionOffset.cloneTo(cloneSprite._atlasRegionOffset);
cloneSprite._atlasRegion.copyFrom(this._atlasRegion);
cloneSprite._atlasRegionOffset.copyFrom(this._atlasRegionOffset);
return cloneSprite;
}

Expand Down Expand Up @@ -237,21 +237,21 @@ export class Sprite extends RefObject {
// Assign values ​​to _positions
const positions = this._positions;
// Top-left.
positions[0].setValue(left, top);
positions[0].set(left, top);
// Top-right.
positions[1].setValue(right, top);
positions[1].set(right, top);
// Bottom-right.
positions[2].setValue(right, bottom);
positions[2].set(right, bottom);
// Bottom-left.
positions[3].setValue(left, bottom);
positions[3].set(left, bottom);

// Update bounds.
bounds.min.setValue(left, bottom, 0);
bounds.max.setValue(right, top, 0);
bounds.min.set(left, bottom, 0);
bounds.max.set(right, top, 0);
} else {
// Update bounds.
bounds.min.setValue(0, 0, 0);
bounds.max.setValue(0, 0, 0);
bounds.min.set(0, 0, 0);
bounds.max.set(0, 0, 0);
}
}

Expand All @@ -278,13 +278,13 @@ export class Sprite extends RefObject {
const right = atlasRegionW + atlasRegionX - Math.max(regionRight - offsetRight, 0) * realWidth;
const bottom = atlasRegionH + atlasRegionY - Math.max(regionY - offsetBottom, 0) * realHeight;
// Top-left.
uv[0].setValue(left, top);
uv[0].set(left, top);
// Top-right.
uv[1].setValue(right, top);
uv[1].set(right, top);
// Bottom-right.
uv[2].setValue(right, bottom);
uv[2].set(right, bottom);
// Bottom-left.
uv[3].setValue(left, bottom);
uv[3].set(left, bottom);
}
this._setDirtyFlagFalse(DirtyFlag.all);
}
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/2d/sprite/SpriteMask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Renderer } from "../../Renderer";
import { SpriteMaskElement } from "../../RenderPipeline/SpriteMaskElement";
import { Shader } from "../../shader/Shader";
import { ShaderProperty } from "../../shader/ShaderProperty";
import { UpdateFlag } from "../../UpdateFlag";
import { SpriteMaskLayer } from "../enums/SpriteMaskLayer";
import { Sprite } from "./Sprite";

Expand Down Expand Up @@ -119,7 +118,7 @@ export class SpriteMask extends Renderer implements ICustomClone {

for (let i = 0, n = positions.length; i < n; i++) {
const curVertexPos = localPositions[i];
localVertexPos.setValue(curVertexPos.x, curVertexPos.y, 0);
localVertexPos.set(curVertexPos.x, curVertexPos.y, 0);
Vector3.transformToVec3(localVertexPos, worldMatrix, positions[i]);
}

Expand Down
11 changes: 5 additions & 6 deletions packages/core/src/2d/sprite/SpriteRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Renderer } from "../../Renderer";
import { CompareFunction } from "../../shader/enums/CompareFunction";
import { Shader } from "../../shader/Shader";
import { ShaderProperty } from "../../shader/ShaderProperty";
import { UpdateFlag } from "../../UpdateFlag";
import { SpriteMaskInteraction } from "../enums/SpriteMaskInteraction";
import { SpriteMaskLayer } from "../enums/SpriteMaskLayer";
import { Sprite } from "./Sprite";
Expand All @@ -19,7 +18,7 @@ import { Sprite } from "./Sprite";
export class SpriteRenderer extends Renderer implements ICustomClone {
/** @internal */
static _textureProperty: ShaderProperty = Shader.getPropertyByName("u_spriteTexture");

private static _tempVec3: Vector3 = new Vector3();

/** @internal temp solution. */
Expand Down Expand Up @@ -80,7 +79,7 @@ export class SpriteRenderer extends Renderer implements ICustomClone {

set color(value: Color) {
if (this._color !== value) {
value.cloneTo(this._color);
this._color.copyFrom(value);
}
}

Expand Down Expand Up @@ -173,7 +172,7 @@ export class SpriteRenderer extends Renderer implements ICustomClone {

for (let i = 0, n = _positions.length; i < n; i++) {
const curVertexPos = localPositions[i];
localVertexPos.setValue(flipX ? -curVertexPos.x : curVertexPos.x, flipY ? -curVertexPos.y : curVertexPos.y, 0);
localVertexPos.set(flipX ? -curVertexPos.x : curVertexPos.x, flipY ? -curVertexPos.y : curVertexPos.y, 0);
Vector3.transformToVec3(localVertexPos, worldMatrix, _positions[i]);
}

Expand Down Expand Up @@ -264,8 +263,8 @@ export class SpriteRenderer extends Renderer implements ICustomClone {
BoundingBox.transform(localBounds, worldMatrix, worldBounds);
}
} else {
worldBounds.min.setValue(0, 0, 0);
worldBounds.max.setValue(0, 0, 0);
worldBounds.min.set(0, 0, 0);
worldBounds.max.set(0, 0, 0);
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/2d/text/TextRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class TextRenderer extends Renderer {

set color(value: Color) {
if (this._color !== value) {
value.cloneTo(this._color);
this._color.copyFrom(value);
}
}

Expand Down Expand Up @@ -446,7 +446,7 @@ export class TextRenderer extends Renderer {
const { _positions } = this;
for (let i = 0, n = _positions.length; i < n; i++) {
const curVertexPos = localPositions[i];
localVertexPos.setValue(curVertexPos.x, curVertexPos.y, 0);
localVertexPos.set(curVertexPos.x, curVertexPos.y, 0);
Vector3.transformToVec3(localVertexPos, worldMatrix, _positions[i]);
}
}
Expand Down
24 changes: 12 additions & 12 deletions packages/core/src/Background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,24 @@ export class Background {

switch (this._textureFillMode) {
case BackgroundTextureFillMode.Fill:
positions[0].setValue(-1, -1, 1);
positions[1].setValue(1, -1, 1);
positions[2].setValue(-1, 1, 1);
positions[3].setValue(1, 1, 1);
positions[0].set(-1, -1, 1);
positions[1].set(1, -1, 1);
positions[2].set(-1, 1, 1);
positions[3].set(1, 1, 1);
break;
case BackgroundTextureFillMode.AspectFitWidth:
const fitWidthScale = (this._texture.height * width) / this.texture.width / height;
positions[0].setValue(-1, -fitWidthScale, 1);
positions[1].setValue(1, -fitWidthScale, 1);
positions[2].setValue(-1, fitWidthScale, 1);
positions[3].setValue(1, fitWidthScale, 1);
positions[0].set(-1, -fitWidthScale, 1);
positions[1].set(1, -fitWidthScale, 1);
positions[2].set(-1, fitWidthScale, 1);
positions[3].set(1, fitWidthScale, 1);
break;
case BackgroundTextureFillMode.AspectFitHeight:
const fitHeightScale = (this._texture.width * height) / this.texture.height / width;
positions[0].setValue(-fitHeightScale, -1, 1);
positions[1].setValue(fitHeightScale, -1, 1);
positions[2].setValue(-fitHeightScale, 1, 1);
positions[3].setValue(fitHeightScale, 1, 1);
positions[0].set(-fitHeightScale, -1, 1);
positions[1].set(fitHeightScale, -1, 1);
positions[2].set(-fitHeightScale, 1, 1);
positions[3].set(fitHeightScale, 1, 1);
break;
}
_backgroundTextureMesh.setPositions(positions);
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/Camera.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BoundingFrustum, MathUtil, Matrix, Quaternion, Ray, Vector2, Vector3, Vector4 } from "@oasis-engine/math";
import { BoundingFrustum, MathUtil, Matrix, Ray, Vector2, Vector3, Vector4 } from "@oasis-engine/math";
import { Logger } from "./base";
import { BoolUpdateFlag } from "./BoolUpdateFlag";
import { deepClone, ignoreClone } from "./clone/CloneManager";
Expand Down Expand Up @@ -159,7 +159,7 @@ export class Camera extends Component {

set viewport(value: Vector4) {
if (value !== this._viewport) {
value.cloneTo(this._viewport);
this._viewport.copyFrom(value);
}
this._projMatChange();
}
Expand Down Expand Up @@ -313,7 +313,7 @@ export class Camera extends Component {
Vector3.transformToVec4(cameraPoint, this.projectionMatrix, viewportPoint);

const w = viewportPoint.w;
out.setValue((viewportPoint.x / w + 1.0) * 0.5, (1.0 - viewportPoint.y / w) * 0.5, -cameraPoint.z);
out.set((viewportPoint.x / w + 1.0) * 0.5, (1.0 - viewportPoint.y / w) * 0.5, -cameraPoint.z);
return out;
}

Expand Down Expand Up @@ -494,7 +494,7 @@ export class Camera extends Component {
// Depth is a normalized value, 0 is nearPlane, 1 is farClipPlane.
// Transform to clipping space matrix
const clipPoint = MathTemp.tempVec3;
clipPoint.setValue(x * 2 - 1, 1 - y * 2, z * 2 - 1);
clipPoint.set(x * 2 - 1, 1 - y * 2, z * 2 - 1);
Vector3.transformCoordinate(clipPoint, invViewProjMat, out);
return out;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/RenderPipeline/BasicRenderPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export class BasicRenderPipeline {
(this._lastCanvasSize.x !== canvas.width || this._lastCanvasSize.y !== canvas.height) &&
background._textureFillMode !== BackgroundTextureFillMode.Fill
) {
this._lastCanvasSize.setValue(canvas.width, canvas.height);
this._lastCanvasSize.set(canvas.width, canvas.height);
background._resizeBackgroundTexture();
}

Expand Down Expand Up @@ -255,7 +255,7 @@ export class BasicRenderPipeline {
ShaderMacroCollection.unionCollection(camera._globalShaderMacro, shaderData._macroCollection, compileMacros);

const { viewMatrix, projectionMatrix } = camera;
viewMatrix.cloneTo(_matrix);
_matrix.copyFrom(viewMatrix);
const e = _matrix.elements;
e[12] = e[13] = e[14] = 0;
Matrix.multiply(projectionMatrix, _matrix, _matrix);
Expand Down
Loading

0 comments on commit 0e26e2c

Please sign in to comment.