Skip to content

Commit

Permalink
Merge pull request #923 from actnwit/refactor/effekseer
Browse files Browse the repository at this point in the history
refactor: add type annotations of Effekseer
  • Loading branch information
Yuki Shimada authored Nov 28, 2021
2 parents b7a5059 + 0186098 commit 0012d45
Show file tree
Hide file tree
Showing 3 changed files with 465 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ VERSION-FILE
samples/**/main.js*
samples/**/types.js*
docs/api/
!vendor/effekseer.d.ts

# VSCode
!**/.vscode/tasks.json
Expand Down
64 changes: 42 additions & 22 deletions src/effekseer/EffekseerComponent.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference path="../../vendor/effekseer.d.ts" />
import Component from '../foundation/core/Component';
import EntityRepository from '../foundation/core/EntityRepository';
import SceneGraphComponent from '../foundation/components/SceneGraphComponent';
Expand All @@ -18,8 +19,6 @@ import MutableMatrix44 from '../foundation/math/MutableMatrix44';
import {Is} from '../foundation/misc/Is';
import {IVector3} from '../foundation/math/IVector';

declare let effekseer: any;

export default class EffekseerComponent extends Component {
public uri?: string;
public playJustAfterLoaded = false;
Expand All @@ -28,16 +27,18 @@ export default class EffekseerComponent extends Component {
public static wasmModuleUri = undefined;
public randomSeed = -1;
public isImageLoadWithCredential = false;
private __effect?: any;
private __context: any;
private __handle?: any;
private __effect?: effekseer.EffekseerEffect;
private __context?: effekseer.EffekseerContext;
private __handle?: effekseer.EffekseerHandle;
private __speed = 1;
private __timer?: unknown;
private __sceneGraphComponent?: SceneGraphComponent;
private __transformComponent?: TransformComponent;
private static __isInitialized = false;
private static __tmp_identityMatrix_0: MutableMatrix44 = MutableMatrix44.identity();
private static __tmp_identityMatrix_1: MutableMatrix44 = MutableMatrix44.identity();
private static __tmp_identityMatrix_0: MutableMatrix44 =
MutableMatrix44.identity();
private static __tmp_identityMatrix_1: MutableMatrix44 =
MutableMatrix44.identity();

private isLoadEffect = false;

Expand Down Expand Up @@ -67,15 +68,15 @@ export default class EffekseerComponent extends Component {
console.warn('No Effekseer context yet');
return false;
}
if (Is.not.exist(this.__context)) {
if (Is.not.exist(this.__effect)) {
console.warn('No Effekseer effect yet');
return false;
}

this.stop();
this.isPause = false;

this.__handle = this.__context?.play(this.__effect);
this.__handle = this.__context?.play(this.__effect, 0, 0, 0);
if (this.randomSeed > 0) {
this.__handle?.setRandomSeed(this.randomSeed);
}
Expand Down Expand Up @@ -114,6 +115,9 @@ export default class EffekseerComponent extends Component {
if (!this.play()) {
return false;
}
if (Is.not.exist(this.__context)) {
return false;
}

let time = 0;
const oneTime = 0.0166;
Expand Down Expand Up @@ -180,11 +184,16 @@ export default class EffekseerComponent extends Component {
}

private __createEffekseerContext() {
if (Is.not.exist(this.uri)) {
console.error('Effekseer file not found.');
return;
}
effekseer.setImageCrossOrigin(
this.isImageLoadWithCredential ? 'use-credentials' : ''
);
this.__context = effekseer.createContext();
const webGLResourceRepository = CGAPIResourceRepository.getWebGLResourceRepository();
const webGLResourceRepository =
CGAPIResourceRepository.getWebGLResourceRepository();
const glw = webGLResourceRepository.currentWebGLContextWrapper;
EffekseerComponent.__isInitialized = true;
const gl = glw!.getRawContext();
Expand All @@ -210,10 +219,16 @@ export default class EffekseerComponent extends Component {
if (Is.not.exist(this.__context) && Is.not.exist(this.__effect)) {
const useWASM = Is.exist(EffekseerComponent.wasmModuleUri);
if (useWASM) {
effekseer.initRuntime(EffekseerComponent.wasmModuleUri, () => {
this.__createEffekseerContext();
this.moveStageTo(ProcessStage.Logic);
});
effekseer.initRuntime(
EffekseerComponent.wasmModuleUri!,
() => {
this.__createEffekseerContext();
this.moveStageTo(ProcessStage.Logic);
},
() => {
console.error('Failed to initialize Effekseer');
}
);
} else {
this.__createEffekseerContext();
this.moveStageTo(ProcessStage.Logic);
Expand All @@ -224,18 +239,21 @@ export default class EffekseerComponent extends Component {
$logic() {
if (!this.isPause) {
// Playing ...
this.__context.update();
if (Is.exist(this.__context)) {
this.__context.update();
}
}

if (this.__handle != null) {
const worldMatrix = EffekseerComponent.__tmp_identityMatrix_0.copyComponents(
this.__sceneGraphComponent!.worldMatrixInner
);
const worldMatrix =
EffekseerComponent.__tmp_identityMatrix_0.copyComponents(
this.__sceneGraphComponent!.worldMatrixInner
);
this.__handle.setMatrix(worldMatrix._v);
this.__handle.setSpeed(this.__speed);
}

if(this.isPause) {
if (this.isPause) {
// If Pause mode...
this.moveStageTo(ProcessStage.Render);
return;
Expand Down Expand Up @@ -265,9 +283,11 @@ export default class EffekseerComponent extends Component {
viewMatrix.identity();
projectionMatrix.identity();
}
this.__context.setProjectionMatrix(projectionMatrix._v);
this.__context.setCameraMatrix(viewMatrix._v);
this.__context.draw();
if (Is.exist(this.__context)) {
this.__context.setProjectionMatrix(projectionMatrix._v);
this.__context.setCameraMatrix(viewMatrix._v);
this.__context.draw();
}

this.moveStageTo(ProcessStage.Logic);
}
Expand Down
Loading

0 comments on commit 0012d45

Please sign in to comment.