Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AOT] add type for AOT engine #15527

Merged
merged 9 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cocos/core/data/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const PersistentMask = ~(ToDestroy | Dirty | Destroying | DontDestroy | Deactiva
// all the hideFlags
const AllHideMasks = DontSave | EditorOnly | LockedInEditor | HideInHierarchy;

const objectsToDestroy: any = [];
const objectsToDestroy: CCObject[] = [];
let deferredDestroyTimer: number | null = null;

function compileDestruct (obj, ctor): Function {
Expand Down Expand Up @@ -620,7 +620,7 @@ declare namespace CCObject {
* @return @en Whether it is a CCObject boolean value. @zh 是否为CCObject的布尔值。
* @engineInternal
*/
export function isCCObject (object: any): boolean {
export function isCCObject (object: any): object is CCObject {
minggo marked this conversation as resolved.
Show resolved Hide resolved
return object instanceof CCObject;
}

Expand Down
8 changes: 4 additions & 4 deletions cocos/core/math/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@
* @en convert Color to HEX color string.
* @zh 转换当前颜色为十六进制颜色字符串。
* @param fmt "#rrggbb" or "#rrggbbaa".
* - `'#rrggbbaa'` obtains the hexadecimal value of the Red, Green, Blue, Alpha channels (**two**, high complement 0) and connects them sequentially.

Check warning on line 499 in cocos/core/math/color.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 153. Maximum allowed is 150
* - `'#rrggbb'` is similar to `'#rrggbbaa'` but does not include the Alpha channel.
* @returns the Hex color string
* @example
Expand Down Expand Up @@ -704,31 +704,31 @@
/**
* @deprecated since v3.5.0, this is an engine private interface that will be removed in the future.
*/
public _set_r_unsafe (red): Color {
public _set_r_unsafe (red: number): Color {
this._val = ((this._val & 0xffffff00) | red) >>> 0;
return this;
}

/**
* @deprecated since v3.5.0, this is an engine private interface that will be removed in the future.
*/
public _set_g_unsafe (green): Color {
public _set_g_unsafe (green: number): Color {
this._val = ((this._val & 0xffff00ff) | (green << 8)) >>> 0;
return this;
}

/**
* @deprecated since v3.5.0, this is an engine private interface that will be removed in the future.
*/
public _set_b_unsafe (blue): Color {
public _set_b_unsafe (blue: number): Color {
this._val = ((this._val & 0xff00ffff) | (blue << 16)) >>> 0;
return this;
}

/**
* @deprecated since v3.5.0, this is an engine private interface that will be removed in the future.
*/
public _set_a_unsafe (alpha): Color {
public _set_a_unsafe (alpha: number): Color {
this._val = ((this._val & 0x00ffffff) | (alpha << 24)) >>> 0;
return this;
}
Expand Down
2 changes: 1 addition & 1 deletion cocos/input/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/

import { EDITOR_NOT_IN_PREVIEW, NATIVE } from 'internal:constants';
import { TouchInputSource, MouseInputSource, KeyboardInputSource, AccelerometerInputSource, GamepadInputDevice, HandleInputDevice, HMDInputDevice, HandheldInputDevice } from 'pal/input';

Check warning on line 28 in cocos/input/input.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 186. Maximum allowed is 150
import { touchManager } from '../../pal/input/touch-manager';
import { sys, EventTarget } from '../core';
import { Event, EventAcceleration, EventGamepad, EventHandle, EventHandheld, EventHMD, EventKeyboard, EventMouse, EventTouch, Touch } from './types';
Expand Down Expand Up @@ -62,7 +62,7 @@
}
}

const pointerEventTypeMap = {
const pointerEventTypeMap: Record<string, string> = {
[InputEventType.MOUSE_DOWN]: InputEventType.TOUCH_START,
[InputEventType.MOUSE_MOVE]: InputEventType.TOUCH_MOVE,
[InputEventType.MOUSE_UP]: InputEventType.TOUCH_END,
Expand Down Expand Up @@ -295,8 +295,8 @@
break;
}
} catch (e) {
console.error(`Error occurs in an event listener: ${event.type}`);

Check failure on line 298 in cocos/input/input.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected console statement
console.error(e);

Check failure on line 299 in cocos/input/input.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected console statement
}
}
}
Expand Down Expand Up @@ -474,7 +474,7 @@

/**
* @en
* The singleton of the Input class, this singleton manages all events of input. include: touch, mouse, accelerometer, gamepad, handle, hmd and keyboard.

Check warning on line 477 in cocos/input/input.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 153. Maximum allowed is 150
*
* @zh
* 输入类单例,该单例管理所有的输入事件,包括:触摸、鼠标、加速计、游戏手柄、6DOF手柄、头戴显示器 和 键盘。
Expand Down
5 changes: 3 additions & 2 deletions cocos/particle/models/particle-batch-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { Color } from '../../core';
import { scene } from '../../render-scene';
import { Particle } from '../particle';
import { Material, RenderingSubMesh } from '../../asset/assets';
import type { PVData } from '../renderer/particle-system-renderer-cpu';

const _uvs = [
0, 0, // bottom-left
Expand Down Expand Up @@ -325,7 +326,7 @@ export default class ParticleBatchModel extends scene.Model {
this.setSubModelMaterial(0, mat);
}

public addParticleVertexData (index: number, pvdata: any[]): void {
public addParticleVertexData (index: number, pvdata: PVData): void {
if (!this._useInstance) {
if (!this._mesh) {
let offset: number = index * this._vertAttrsFloatCount;
Expand Down Expand Up @@ -371,7 +372,7 @@ export default class ParticleBatchModel extends scene.Model {
}
}

private addParticleVertexDataIns (index: number, pvdata: any[]): void {
private addParticleVertexDataIns (index: number, pvdata: PVData): void {
let offset: number = index * this._vertAttrsFloatCount;
if (!this._mesh) {
this._vdataF32![offset++] = pvdata[0].x; // position
Expand Down
2 changes: 1 addition & 1 deletion cocos/particle/particle-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@
*/
public pause (): void {
if (this._isStopped) {
console.warn('pause(): particle system is already stopped.');

Check failure on line 984 in cocos/particle/particle-system.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected console statement
return;
}
if (this._isPlaying) {
Expand Down Expand Up @@ -1413,7 +1413,7 @@
}

// internal function
private _emit (dt): void {
private _emit (dt: number): void {
// emit particles.
const startDelay = this.startDelay.evaluate(0, 1)!;
if (this._time > startDelay) {
Expand Down
9 changes: 5 additions & 4 deletions cocos/particle/renderer/particle-system-renderer-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ import { Particle, IParticleModule } from '../particle';
import { RenderMode } from '../enum';
import { cclegacy } from '../../core';
import { Pass } from '../../render-scene';
import type { ParticleSystem } from '../particle-system';

export abstract class ParticleSystemRendererBase {
protected _particleSystem: any = null;
protected _particleSystem: ParticleSystem | null = null;
/**
* @engineInternal
*/
Expand Down Expand Up @@ -62,7 +63,7 @@ export abstract class ParticleSystemRendererBase {
return this._renderInfo!;
}

public onInit (ps: Component): void {
public onInit (ps: ParticleSystem): void {
this._particleSystem = ps;
}

Expand Down Expand Up @@ -93,7 +94,7 @@ export abstract class ParticleSystemRendererBase {
if (this._model.scene) {
this.detachFromScene();
}
this._particleSystem._getRenderScene().addModel(this._model);
this._particleSystem?._getRenderScene().addModel(this._model);
}
}

Expand All @@ -119,7 +120,7 @@ export abstract class ParticleSystemRendererBase {
}

protected _initModel (): void {
if (!this._model) {
if (!this._model && this._particleSystem) {
this._model = cclegacy.director.root.createModel(ParticleBatchModel);
this._model!.setCapacity(this._particleSystem.capacity);
this._model!.visFlags = this._particleSystem.visibility;
Expand Down
76 changes: 42 additions & 34 deletions cocos/particle/renderer/particle-system-renderer-cpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import { EDITOR_NOT_IN_PREVIEW } from 'internal:constants';
import { builtinResMgr } from '../../asset/asset-manager';
import { Material } from '../../asset/assets';
import { Material, Texture2D } from '../../asset/assets';
import { AttributeName, Format, Attribute, FormatInfos } from '../../gfx';
import { Mat4, Vec2, Vec3, Vec4, pseudoRandom, Quat, EPSILON, approx, RecyclePool } from '../../core';
import { MaterialInstance, IMaterialInstanceInfo } from '../../render-scene/core/material-instance';
Expand All @@ -38,6 +38,7 @@
import { ParticleNoise } from '../noise';
import { NoiseModule } from '../animator/noise-module';
import { isCurveTwoValues } from '../particle-general-function';
import type { ParticleSystem } from '../particle-system';

const _tempAttribUV = new Vec3();
const _tempWorldTrans = new Mat4();
Expand Down Expand Up @@ -136,20 +137,24 @@
subModelIdx: 0,
};

// TODO: we should not use this type, should use a uniform array type instead.
// Tracking issue: https://github.com/cocos/cocos-engine/issues/15553
export type PVData = [Vec3, Vec3, Vec3, Vec3, number, Vec3 | null, null];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does it mean?

Copy link
Contributor Author

@PPpro PPpro Jun 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this means that this is an array type, and its element type is uncertain

for example

let a: PVData;
a[0] // Vec3 type
a[4] // number type

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and its element type is uncertain

I think it is defined in the PVData. As PVData[0] should be Vec3, PVData[1] should be Vec3, and so on.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, because it was type of any before, it is very wierd to implement like this

we should not introduce different element types in an array, this issue should be solved here #15553


export default class ParticleSystemRendererCPU extends ParticleSystemRendererBase {
private _defines: MacroRecord;
private _trailDefines: MacroRecord;
private _frameTile_velLenScale: Vec4;
private _tmp_velLenScale: Vec4;
private _defaultMat: Material | null = null;
private _node_scale: Vec4;
private _attrs: any[];
private _particles: RecyclePool | null = null;
private _node_scale: Vec3;
private _attrs: PVData;
private _particles: RecyclePool<Particle> | null = null;
private _defaultTrailMat: Material | null = null;
private _updateList: Map<string, IParticleModule> = new Map<string, IParticleModule>();
private _animateList: Map<string, IParticleModule> = new Map<string, IParticleModule>();
private _runAnimateList: IParticleModule[] = new Array<IParticleModule>();
private _fillDataFunc: any = null;
private _fillDataFunc: ((p: Particle, idx: number, fi: number) => void) | null = null;
private _uScaleHandle = 0;
private _uLenHandle = 0;
private _uNodeRotHandle = 0;
Expand All @@ -165,8 +170,8 @@

this._frameTile_velLenScale = new Vec4(1, 1, 0, 0);
this._tmp_velLenScale = this._frameTile_velLenScale.clone();
this._node_scale = new Vec4();
this._attrs = new Array(7);
this._node_scale = new Vec3();
this._attrs = new Array(7) as PVData;
this._defines = {
CC_USE_WORLD_SPACE: true,
CC_USE_BILLBOARD: true,
Expand All @@ -180,7 +185,7 @@
};
}

public onInit (ps: Component): void {
public onInit (ps: ParticleSystem): void {
super.onInit(ps);

this._particles = new RecyclePool((): Particle => new Particle(this), 16);
Expand All @@ -197,7 +202,7 @@
public clear (): void {
super.clear();
this._particles!.reset();
if (this._particleSystem._trailModule) {
if (this._particleSystem && this._particleSystem._trailModule) {
this._particleSystem._trailModule.clear();
}
this.updateRenderData();
Expand All @@ -217,10 +222,10 @@
}

public getFreeParticle (): Particle | null {
if (this._particles!.length >= this._particleSystem.capacity) {
if (this._particleSystem && this._particles!.length >= this._particleSystem.capacity) {
return null;
}
return this._particles!.add() as Particle;
return this._particles!.add();
}

public getDefaultTrailMaterial (): any {
Expand All @@ -232,6 +237,9 @@

private _initModuleList (): void {
_anim_module.forEach((val): void => {
if (!this._particleSystem) {
return;
}
const pm = this._particleSystem[val];
if (pm && pm.enable) {
if (pm.needUpdate) {
Expand Down Expand Up @@ -293,25 +301,25 @@
}
}

private doUpdateRotation (pass): void {
private doUpdateRotation (pass: Pass): void {
const mode = this._renderInfo!.renderMode;
if (mode !== RenderMode.Mesh && this._alignSpace === AlignmentSpace.View) {
return;
}

if (this._alignSpace === AlignmentSpace.Local) {
this._particleSystem.node.getRotation(_node_rot);
this._particleSystem?.node.getRotation(_node_rot);
} else if (this._alignSpace === AlignmentSpace.World) {
this._particleSystem.node.getWorldRotation(_node_rot);
this._particleSystem?.node.getWorldRotation(_node_rot);
} else if (this._alignSpace === AlignmentSpace.View) {
// Quat.fromEuler(_node_rot, 0.0, 0.0, 0.0);
_node_rot.set(0.0, 0.0, 0.0, 1.0);
const cameraLst: Camera[]|undefined = this._particleSystem.node.scene.renderScene?.cameras;
const cameraLst: Camera[] | undefined = this._particleSystem?.node.scene.renderScene?.cameras;
if (cameraLst !== undefined) {
for (let i = 0; i < cameraLst?.length; ++i) {
const camera: Camera = cameraLst[i];
// eslint-disable-next-line max-len
const checkCamera: boolean = !EDITOR_NOT_IN_PREVIEW ? (camera.visibility & this._particleSystem.node.layer) === this._particleSystem.node.layer : camera.name === 'Editor Camera';
const checkCamera: boolean = !EDITOR_NOT_IN_PREVIEW ? (camera.visibility & this._particleSystem!.node.layer) === this._particleSystem!.node.layer : camera.name === 'Editor Camera';
if (checkCamera) {
Quat.fromViewUp(_node_rot, camera.forward);
break;
Expand All @@ -331,12 +339,12 @@
}

private doUpdateScale (pass): void {
switch (this._particleSystem.scaleSpace) {
switch (this._particleSystem?.scaleSpace) {
case Space.Local:
this._particleSystem.node.getScale(this._node_scale);
this._particleSystem?.node.getScale(this._node_scale);
break;
case Space.World:
this._particleSystem.node.getWorldScale(this._node_scale);
this._particleSystem?.node.getWorldScale(this._node_scale);
break;
default:
break;
Expand All @@ -358,13 +366,13 @@
this.doUpdateRotation(pass);

this._updateList.forEach((value: IParticleModule, key: string): void => {
value.update(ps._simulationSpace, _tempWorldTrans);
value.update(ps.simulationSpace, _tempWorldTrans);
});

const trailModule = ps._trailModule;
const trailEnable = trailModule && trailModule.enable;
if (trailEnable) {
trailModule.update();
trailModule!.update();
}

const useGravity = !ps.gravityModifier.isZero();
Expand All @@ -389,7 +397,7 @@

if (p.remainingLifetime < 0.0) {
if (trailEnable) {
trailModule.removeParticle(p);
trailModule!.removeParticle(p);
}
this._particles!.removeAt(i);
--i;
Expand Down Expand Up @@ -429,7 +437,7 @@

Vec3.scaleAndAdd(p.position, p.position, p.ultimateVelocity, dt); // apply velocity.
if (trailEnable) {
trailModule.animate(p, dt);
trailModule!.animate(p, dt);
}
}

Expand All @@ -453,12 +461,12 @@
for (let i = 0; i < this._particles!.length; ++i) {
const p = this._particles!.data[i];
let fi = 0;
const textureModule = this._particleSystem._textureAnimationModule;
const textureModule = this._particleSystem!._textureAnimationModule;
if (textureModule && textureModule.enable) {
fi = p.frameIndex;
}
idx = i * 4;
this._fillDataFunc(p, idx, fi);
this._fillDataFunc!(p, idx, fi);
}
}

Expand Down Expand Up @@ -487,9 +495,10 @@
if (this._model && index === 0) {
this._model.setSubModelMaterial(0, material);
}
const trailModule = this._particleSystem._trailModule;
if (trailModule && trailModule._trailModel && index === 1) {
trailModule._trailModel.setSubModelMaterial(0, material);
const trailModule = this._particleSystem!._trailModule;
const trailModel = trailModule?.getModel();
if (trailModel && index === 1) {
trailModel.setSubModelMaterial(0, material);
}
}

Expand Down Expand Up @@ -637,8 +646,7 @@
const ps = this._particleSystem;
const shareMaterial = ps.sharedMaterial;
if (shareMaterial != null) {
const effectName = shareMaterial._effectAsset._name;
this._renderInfo!.mainTexture = shareMaterial.getProperty('mainTexture', 0);
this._renderInfo!.mainTexture = shareMaterial.getProperty('mainTexture', 0) as Texture2D;
}

if (ps.sharedMaterial == null && this._defaultMat == null) {
Expand All @@ -653,8 +661,8 @@
this._defaultMat.setProperty('mainTexture', this._renderInfo!.mainTexture);
}
}
const mat: Material = ps.getMaterialInstance(0) || this._defaultMat;
if (ps._simulationSpace === Space.World) {
const mat: Material = ps.getMaterialInstance(0) || this._defaultMat!;
if (ps.simulationSpace === Space.World) {
this._defines[CC_USE_WORLD_SPACE] = true;
} else {
this._defines[CC_USE_WORLD_SPACE] = false;
Expand All @@ -680,7 +688,7 @@
} else if (renderMode === RenderMode.Mesh) {
this._defines[CC_RENDER_MODE] = RENDER_MODE_MESH;
} else {
console.warn(`particle system renderMode ${renderMode} not support.`);

Check failure on line 691 in cocos/particle/renderer/particle-system-renderer-cpu.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected console statement
}
const textureModule = ps._textureAnimationModule;
if (textureModule && textureModule.enable) {
Expand Down Expand Up @@ -715,7 +723,7 @@
} else {
this._trailDefines[CC_USE_WORLD_SPACE] = false;
}
let mat = ps.getMaterialInstance(1);
let mat: Material | null = ps.getMaterialInstance(1);
if (mat === null && this._defaultTrailMat === null) {
_matInsInfo.parent = builtinResMgr.get<Material>('default-trail-material');
_matInsInfo.owner = this._particleSystem;
Expand All @@ -725,7 +733,7 @@
_matInsInfo.owner = null!;
_matInsInfo.subModelIdx = 0;
}
mat = mat || this._defaultTrailMat;
mat = mat || this._defaultTrailMat!;
mat.recompileShaders(this._trailDefines);
trailModule.updateMaterial();
}
Expand Down
Loading
Loading