Skip to content

Commit

Permalink
Merged in feature/three_perf (pull request #13)
Browse files Browse the repository at this point in the history
Cache Three materials

Approved-by: Davy Hélard
  • Loading branch information
Alexandre committed Apr 27, 2023
2 parents f1300d6 + d96a618 commit 789fd48
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
11 changes: 3 additions & 8 deletions Extensions/3D/Cube3DRuntimeObjectPixiRenderer.ts
Expand Up @@ -72,14 +72,9 @@ namespace gdjs {
if (!this._object.isFaceAtIndexVisible(faceIndex))
return getTransparentMaterial();

return new THREE.MeshBasicMaterial({
map: this._runtimeGame
.getImageManager()
.getThreeTexture(this._object.getFaceAtIndexResourceName(faceIndex)),
// TODO (3D) - optimization: use FrontSide instead of DoubleSide if no transparent textures and all face are shown.
side: THREE.DoubleSide,
transparent: true,
});
return this._runtimeGame
.getImageManager()
.getThreeMaterial(this._object.getFaceAtIndexResourceName(faceIndex));
}

updateFace(faceIndex: integer) {
Expand Down
20 changes: 20 additions & 0 deletions GDJS/Runtime/pixi-renderers/pixi-image-manager.ts
Expand Up @@ -71,6 +71,7 @@ namespace gdjs {
* Map associating a resource name to the loaded Three.js texture.
*/
private _loadedThreeTextures: Hashtable<THREE.Texture>;
private _loadedThreeMaterials: Hashtable<THREE.Material>;

private _resourcesLoader: RuntimeGameResourcesLoader;

Expand All @@ -89,6 +90,7 @@ namespace gdjs {
);
this._loadedTextures = new Hashtable();
this._loadedThreeTextures = new Hashtable();
this._loadedThreeMaterials = new Hashtable();
}

/**
Expand Down Expand Up @@ -206,6 +208,24 @@ namespace gdjs {
return threeTexture;
}

/**
* Return the three.js material associated to the specified resource name.
* @param resourceName The name of the resource
* @returns The requested material.
*/
getThreeMaterial(resourceName: string) {
const loadedThreeMaterial = this._loadedThreeMaterials.get(resourceName);
if (loadedThreeMaterial) return loadedThreeMaterial;

const material = new THREE.MeshBasicMaterial({
map: this.getThreeTexture(resourceName),
side: THREE.FrontSide,
});

this._loadedThreeMaterials.put(resourceName, material);
return material;
}

/**
* Return the PIXI video texture associated to the specified resource name.
* Returns a placeholder texture if not found.
Expand Down

0 comments on commit 789fd48

Please sign in to comment.