Skip to content

Commit

Permalink
Refactor valuetypes (#4798)
Browse files Browse the repository at this point in the history
* Some value-types change

* value-type change

* Refactor valuetypes
  • Loading branch information
pandamicro authored and jerrywwl committed Jul 3, 2019
1 parent 0e3b425 commit ec9e639
Show file tree
Hide file tree
Showing 26 changed files with 262 additions and 621 deletions.
2 changes: 1 addition & 1 deletion cocos/3d/framework/particle/animator/color-overtime.ts
Expand Up @@ -36,7 +36,7 @@ export default class ColorOvertimeModule {

public animate (particle: Particle) {
if (this.enable) {
Color.mul(particle.color, particle.startColor, this.color.evaluate(1.0 - particle.remainingLifetime / particle.startLifetime, pseudoRandom(particle.randomSeed + COLOR_OVERTIME_RAND_OFFSET)));
particle.startColor.multiply(particle.color, this.color.evaluate(1.0 - particle.remainingLifetime / particle.startLifetime, pseudoRandom(particle.randomSeed + COLOR_OVERTIME_RAND_OFFSET)));
}
}
}
Expand Down
1 change: 0 additions & 1 deletion cocos/3d/framework/particle/animator/velocity-overtime.ts
Expand Up @@ -3,7 +3,6 @@
* @category particle
*/

import { CCClass } from '../../../../core/data';
import { ccclass, property } from '../../../../core/data/class-decorator';
import { mat4, pseudoRandom, quat, vec3 } from '../../../../core/vmath';
import { Space } from '../enum';
Expand Down
3 changes: 1 addition & 2 deletions cocos/3d/framework/particle/emitter/shape-module.ts
Expand Up @@ -3,9 +3,8 @@
* @category particle
*/

import { CCClass } from '../../../../core/data';
import { ccclass, property } from '../../../../core/data/class-decorator';
import { Enum, Vec3 } from '../../../../core/value-types';
import { Vec3 } from '../../../../core/value-types';
import { clamp, mat4, pingPong, quat, random, randomRange, repeat, toDegree, toRadian, vec2, vec3 } from '../../../../core/vmath';
import CurveRange from '../animator/curve-range';
import { fixedAngleUnitVector2, particleEmitZAxis, randomPointBetweenCircleAtFixedAngle, randomPointBetweenSphere, randomPointInCube, randomSign, randomSortArray, randomUnitVector } from '../particle-general-function';
Expand Down
4 changes: 2 additions & 2 deletions cocos/3d/framework/particle/renderer/trail.ts
Expand Up @@ -541,7 +541,7 @@ export default class TrailModule {
private _updateTrailElement (module: any, trailEle: ITrailElement, p: Particle, dt: number): boolean {
trailEle.lifetime += dt;
if (module.colorFromParticle) {
Color.mul(trailEle.color, p.color, module.colorOvertime.evaluate(1.0 - p.remainingLifetime / p.startLifetime, 1));
p.color.multiply(trailEle.color, module.colorOvertime.evaluate(1.0 - p.remainingLifetime / p.startLifetime, 1));
} else {
trailEle.color.set(module.colorOvertime.evaluate(1.0 - p.remainingLifetime / p.startLifetime, 1));
}
Expand All @@ -564,7 +564,7 @@ export default class TrailModule {
this._vbF32[this.vbOffset++] = trailSeg.velocity.x;
this._vbF32[this.vbOffset++] = trailSeg.velocity.y;
this._vbF32[this.vbOffset++] = trailSeg.velocity.z;
Color.mul(_temp_color, trailSeg.color, colorModifer);
trailSeg.color.multiply(_temp_color, colorModifer);
this._vbUint32[this.vbOffset++] = _temp_color._val;
this._vbF32[this.vbOffset++] = trailSeg.position.x;
this._vbF32[this.vbOffset++] = trailSeg.position.y;
Expand Down
4 changes: 2 additions & 2 deletions cocos/3d/framework/physics/collider/collider-component.ts
Expand Up @@ -91,7 +91,7 @@ export class ColliderComponent extends PhysicsBasedComponent implements IEventTa
if (!CC_EDITOR) {
const rigidBody = this.sharedBody.rigidBody as RigidBodyComponent | null;
if (rigidBody != null) {
vec3.sub(offset, this.node.worldPosition, rigidBody.node.worldPosition);
vec3.subtract(offset, this.node.worldPosition, rigidBody.node.worldPosition);
vec3.add(offset, offset, this._center);
this._shapeBase.setCenter(offset);
} else {
Expand Down Expand Up @@ -207,7 +207,7 @@ export class ColliderComponent extends PhysicsBasedComponent implements IEventTa

const rigidBody = this.sharedBody.rigidBody as RigidBodyComponent | null;
if (rigidBody != null) {
vec3.sub(offset, this.node.worldPosition, rigidBody.node.worldPosition);
vec3.subtract(offset, this.node.worldPosition, rigidBody.node.worldPosition);
vec3.add(offset, offset, this._center);
this.sharedBody.body.addShape(this._shapeBase!, offset);
} else {
Expand Down
2 changes: 1 addition & 1 deletion cocos/3d/geom-utils/distance.ts
Expand Up @@ -90,7 +90,7 @@ export function pt_point_obb (out: vec3, point: vec3, obb_: obb): vec3 {
e[1] = obb_.halfExtents.y;
e[2] = obb_.halfExtents.z;

vec3.sub(d, point, obb_.center);
vec3.subtract(d, point, obb_.center);

// Start result at center of obb; make steps from there
vec3.set(out, obb_.center.x, obb_.center.y, obb_.center.z);
Expand Down
17 changes: 10 additions & 7 deletions cocos/3d/ui/assembler/graphics/webgl/graphics-assembler.ts
Expand Up @@ -27,7 +27,7 @@
* @category ui-assembler
*/

import { Color, Vec3 } from '../../../../../core/value-types';
import { Color, Vec2, Vec3 } from '../../../../../core/value-types';
import { color4 } from '../../../../../core/vmath';
import { GFXPrimitiveMode } from '../../../../../gfx';
import { Model } from '../../../../../renderer';
Expand Down Expand Up @@ -340,8 +340,9 @@ export const graphicsAssembler: IAssembler = {

if (!loop) {
// Add cap
const dPos = p1.sub(p0);
dPos.normalizeSelf();
const dPos = new Point(0, 0);
p1.subtract(dPos, p0);
dPos.normalize(dPos);

const dx = dPos.x;
const dy = dPos.y;
Expand Down Expand Up @@ -385,8 +386,9 @@ export const graphicsAssembler: IAssembler = {
meshbuffer.vertexStart++;
} else {
// Add cap
const dPos = p1.sub(p0);
dPos.normalizeSelf();
const dPos = new Point(0, 0);
p1.subtract(dPos, p0);
dPos.normalize(dPos);

const dx = dPos.x;
const dy = dPos.y;
Expand Down Expand Up @@ -596,10 +598,11 @@ export const graphicsAssembler: IAssembler = {

for (let j = 0, size = pts.length; j < size; j++) {
// Calculate segment direction and length
const dPos = p1.sub(p0);
const dPos = new Point(0, 0);
p1.subtract(dPos, p0);
p0.len = dPos.mag();
if (dPos.x || dPos.y) {
dPos.normalizeSelf();
dPos.normalize(dPos);
}
p0.dx = dPos.x;
p0.dy = dPos.y;
Expand Down
2 changes: 1 addition & 1 deletion cocos/3d/ui/components/button-component.ts
Expand Up @@ -538,7 +538,7 @@ export class ButtonComponent extends Component {
}

if (this._transition === Transition.COLOR) {
Color.lerp(renderComp.color, this._fromColor, this._toColor, ratio);
Color.lerp(renderComp._color, this._fromColor, this._toColor, ratio);
} else if (this.transition === Transition.SCALE) {
target.getScale(this._targetScale);
this._targetScale.x = lerp(this._fromScale.x, this._toScale.x, ratio);
Expand Down
2 changes: 1 addition & 1 deletion cocos/3d/ui/components/layout-component.ts
Expand Up @@ -873,7 +873,7 @@ export class LayoutComponent extends Component {
if (!allChildrenBoundingBox) {
allChildrenBoundingBox = childTransform.getBoundingBoxToWorld();
} else {
allChildrenBoundingBox.union(allChildrenBoundingBox!, childTransform.getBoundingBoxToWorld());
allChildrenBoundingBox.union(allChildrenBoundingBox, childTransform.getBoundingBoxToWorld());
}
}
}
Expand Down
54 changes: 33 additions & 21 deletions cocos/3d/ui/components/scroll-view-component.ts
Expand Up @@ -892,7 +892,8 @@ export class ScrollViewComponent extends ViewGroupComponent {
return;
}

const deltaMove = touch.getUILocation().sub(touch.getStartLocation());
const deltaMove = new Vec2();
touch.getUILocation().subtract(deltaMove, touch.getStartLocation());
// FIXME: touch move delta should be calculated by DPI.
if (deltaMove.mag() > 7) {
if (!this._touchMoved && event.target !== this.node) {
Expand Down Expand Up @@ -993,7 +994,8 @@ export class ScrollViewComponent extends ViewGroupComponent {
}

private _startInertiaScroll (touchMoveVelocity: Vec3) {
const inertiaTotalMovement = touchMoveVelocity.mul(MOVEMENT_FACTOR);
const inertiaTotalMovement = new Vec3();
touchMoveVelocity.multiply(inertiaTotalMovement, MOVEMENT_FACTOR);
this._startAttenuatingAutoScroll(inertiaTotalMovement, touchMoveVelocity);
}

Expand All @@ -1009,7 +1011,8 @@ export class ScrollViewComponent extends ViewGroupComponent {
private _startAttenuatingAutoScroll (deltaMove: Vec3, initialVelocity: Vec3) {
let time = this._calculateAutoScrollTimeByInitalSpeed(initialVelocity.mag());

let targetDelta = deltaMove.normalize();
let targetDelta = new Vec3();
deltaMove.normalize(targetDelta);
const contentSize = this._content!.getContentSize();
const scrollviewSize = this.node.getContentSize();

Expand All @@ -1019,18 +1022,19 @@ export class ScrollViewComponent extends ViewGroupComponent {
const attenuatedFactorX = this._calculateAttenuatedFactor(totalMoveWidth);
const attenuatedFactorY = this._calculateAttenuatedFactor(totalMoveHeight);

targetDelta = new Vec3(
targetDelta.x * totalMoveWidth * (1 - this.brake) * attenuatedFactorX,
targetDelta.y * totalMoveHeight * attenuatedFactorY * (1 - this.brake),
0);
targetDelta.x = targetDelta.x * totalMoveWidth * (1 - this.brake) * attenuatedFactorX;
targetDelta.y = targetDelta.y * totalMoveHeight * attenuatedFactorY * (1 - this.brake);
targetDelta.z = 0;

const originalMoveLength = deltaMove.mag();
let factor = targetDelta.mag() / originalMoveLength;
targetDelta = targetDelta.add(deltaMove);
targetDelta.add(targetDelta, deltaMove);

if (this.brake > 0 && factor > 7) {
factor = Math.sqrt(factor);
targetDelta = deltaMove.mul(factor).add(deltaMove);
let a = new Vec3();
deltaMove.multiply(a, factor);
a.add(targetDelta, deltaMove);
}

if (this.brake > 0 && factor > 3) {
Expand Down Expand Up @@ -1080,7 +1084,8 @@ export class ScrollViewComponent extends ViewGroupComponent {

let totalMovement = new Vec3();
totalMovement = this._touchMoveDisplacements.reduce((a, b) => {
return a.add(b);
a.add(a, b);
return a;
}, totalMovement);

return new Vec3(totalMovement.x * (1 - this.brake) / totalTime,
Expand All @@ -1096,7 +1101,7 @@ export class ScrollViewComponent extends ViewGroupComponent {

private _moveContent (deltaMove: Vec3, canStartBounceBack?: boolean) {
const adjustedMove = this._flattenVectorByDirection(deltaMove);
this.getContentPosition().add(adjustedMove, _tempPos);
this.getContentPosition().add(_tempPos, adjustedMove);

this.setContentPosition(_tempPos);

Expand Down Expand Up @@ -1215,7 +1220,8 @@ export class ScrollViewComponent extends ViewGroupComponent {
this._outOfBoundaryAmountDirty = true;
if (this._isOutOfBoundary()) {
const outOfBoundary = this._getHowMuchOutOfBoundary(new Vec3());
const newPosition = this.getContentPosition().add(outOfBoundary);
let newPosition = new Vec3();
this.getContentPosition().add(newPosition, outOfBoundary);
if (this._content) {
this._content.setPosition(newPosition);
// this._updateScrollBar(0);
Expand Down Expand Up @@ -1275,7 +1281,7 @@ export class ScrollViewComponent extends ViewGroupComponent {

if (!this.elastic) {
outOfBoundary = this._getHowMuchOutOfBoundary(realMove);
realMove = realMove.add(outOfBoundary);
realMove.add(realMove, outOfBoundary);
}

let scrollEventType = '';
Expand Down Expand Up @@ -1454,7 +1460,10 @@ export class ScrollViewComponent extends ViewGroupComponent {
percentage = quintEaseOut(percentage);
}

let newPosition = this._autoScrollStartPosition.add(this._autoScrollTargetDelta.mul(percentage));
let a = new Vec3();
this._autoScrollTargetDelta.multiply(a, percentage);
let newPosition = new Vec3();
this._autoScrollStartPosition.add(newPosition, a);
let reachedEnd = Math.abs(percentage - 1) <= EPSILON;

const fireEvent = Math.abs(percentage - 1) <= this.getScrollEndedEventTiming();
Expand All @@ -1464,16 +1473,18 @@ export class ScrollViewComponent extends ViewGroupComponent {
}

if (this.elastic) {
let brakeOffsetPosition = newPosition.sub(this._autoScrollBrakingStartPosition);
let brakeOffsetPosition = new Vec3();
newPosition.subtract(brakeOffsetPosition, this._autoScrollBrakingStartPosition);
if (isAutoScrollBrake) {
brakeOffsetPosition = brakeOffsetPosition.mul(brakingFactor);
brakeOffsetPosition.multiply(brakeOffsetPosition, brakingFactor);
}
newPosition = this._autoScrollBrakingStartPosition.add(brakeOffsetPosition);
this._autoScrollBrakingStartPosition.add(newPosition, brakeOffsetPosition);
} else {
const moveDelta = newPosition.sub(this.getContentPosition());
const moveDelta = new Vec3();
newPosition.subtract(moveDelta, this.getContentPosition());
const outOfBoundary = this._getHowMuchOutOfBoundary(moveDelta);
if (!outOfBoundary.fuzzyEquals(ZERO, EPSILON)) {
newPosition = newPosition.add(outOfBoundary);
newPosition.add(newPosition, outOfBoundary);
reachedEnd = true;
}
}
Expand All @@ -1482,7 +1493,8 @@ export class ScrollViewComponent extends ViewGroupComponent {
this._autoScrolling = false;
}

const deltaMove = newPosition.sub(this.getContentPosition());
const deltaMove = new Vec3();
newPosition.subtract(deltaMove, this.getContentPosition());
this._moveContent(this._clampDelta(deltaMove), reachedEnd);
this._dispatchEvent('scrolling');

Expand Down Expand Up @@ -1520,7 +1532,7 @@ export class ScrollViewComponent extends ViewGroupComponent {
const applyToVertical = options.applyToVertical;
this._calculateBoundary();

anchor = anchor.clampf(new Vec2(0, 0), new Vec2(1, 1));
anchor.clampf(new Vec2(0, 0), new Vec2(1, 1));

const scrollSize = this.node.getContentSize();
const contentSize = this._content!.getContentSize();
Expand Down
3 changes: 2 additions & 1 deletion cocos/3d/ui/components/ui-transfrom-component.ts
Expand Up @@ -412,7 +412,8 @@ export class UITransformComponent extends Component {
-this._anchorPoint.y * height,
width,
height);
return rect.transformMat4(rect, _matrix);
rect.transformMat4(rect, _matrix);
return rect;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion cocos/3d/ui/components/widget-manager.ts
Expand Up @@ -373,7 +373,8 @@ function adjustWidgetToAllowMovingInEditor (this: WidgetComponent, eventType: Sy
const self = this;
const newPos = self.node.getPosition();
const oldPos = this._lastPos;
const delta = newPos.sub(oldPos);
const delta = new Vec3();
newPos.subtract(delta, oldPos);
oldPos.set(newPos);

let target = self.node.parent!;
Expand Down
3 changes: 2 additions & 1 deletion cocos/animation/animation-curve.ts
Expand Up @@ -327,7 +327,8 @@ const selectLerpFx = (() => {
function makeValueTypeLerpFx<T extends ValueType> (constructor: Constructor<T>) {
const tempValue = new constructor();
return (from: T, to: T, ratio: number) => {
return constructor.lerp(from, to, ratio, tempValue);
constructor.lerp(tempValue, from, to, ratio);
return tempValue;
};
}

Expand Down
18 changes: 12 additions & 6 deletions cocos/animation/motion-path-helper.ts
Expand Up @@ -5,7 +5,7 @@
import { binarySearchEpsilon as binarySearch } from '../core/data/utils/binary-search';
import { errorID } from '../core/platform/CCDebug';
import { Vec2 } from '../core/value-types';
import { AnimCurve, computeRatioByType, CurveType, CurveValue, CurveTarget } from './animation-curve';
import { AnimCurve, computeRatioByType, CurveType, CurveValue } from './animation-curve';
import { bezier } from './bezier';

// tslint:disable:no-shadowed-variable
Expand Down Expand Up @@ -339,18 +339,24 @@ export function sampleMotionPaths (motionPaths: Array<(MotionPath | undefined)>,

finalProgress = computeRatioByType(finalProgress, type);

let pos: Vec2;
let pos = new Vec2();

if (finalProgress < 0) {
const bezier = curve.beziers[0];
const length = (0 - finalProgress) * bezier.getLength();
const normal = bezier.start.sub(bezier.endCtrlPoint).normalize();
pos = bezier.start.add(normal.mul(length));
const normal = new Vec2();
bezier.start.subtract(normal, bezier.endCtrlPoint);
normal.normalize(normal);
normal.multiply(normal, length);
pos.add(bezier.start, normal);
} else if (finalProgress > 1) {
const bezier = curve.beziers[curve.beziers.length - 1];
const length = (finalProgress - 1) * bezier.getLength();
const normal = bezier.end.sub(bezier.startCtrlPoint).normalize();
pos = bezier.end.add(normal.mul(length));
const normal = new Vec2();
bezier.end.subtract(normal, bezier.startCtrlPoint);
normal.normalize(normal);
normal.multiply(normal, length);
pos.add(bezier.end, normal);
} else {
let bezierIndex = binarySearch(progresses, finalProgress);
if (bezierIndex < 0) { bezierIndex = ~bezierIndex; }
Expand Down
4 changes: 2 additions & 2 deletions cocos/core/platform/event-manager/CCTouch.ts
Expand Up @@ -177,7 +177,7 @@ export default class Touch {
out = new Vec2();
}

this._point.sub(this._prevPoint, out);
this._point.subtract(out, this._prevPoint);
return out;
}

Expand All @@ -190,7 +190,7 @@ export default class Touch {
out = new Vec2();
}

this._point.sub(this._prevPoint, _vec2);
this._point.subtract(_vec2, this._prevPoint);
vec2.set(out, cc.view.getScaleX(), cc.view.getScaleY());
vec2.divide(out, _vec2, out);
return out;
Expand Down

0 comments on commit ec9e639

Please sign in to comment.