babylonjs-editor-tools 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/guards.js +20 -0
- package/build/index.js +19 -0
- package/build/loader.js +40 -0
- package/build/texture.js +81 -0
- package/declaration/guards.d.ts +12 -0
- package/declaration/index.d.ts +2 -0
- package/declaration/loader.d.ts +3 -0
- package/declaration/texture.d.ts +2 -0
- package/package.json +17 -0
package/build/guards.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isTexture = exports.isMesh = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Returns wether or not the given object is a Mesh.
|
|
6
|
+
* @param object defines the reference to the object to test its class name.
|
|
7
|
+
*/
|
|
8
|
+
function isMesh(object) {
|
|
9
|
+
return object.getClassName?.() === "Mesh";
|
|
10
|
+
}
|
|
11
|
+
exports.isMesh = isMesh;
|
|
12
|
+
/**
|
|
13
|
+
* Returns wether or not the given object is a Texture.
|
|
14
|
+
* @param object defines the reference to the object to test its class name.
|
|
15
|
+
*/
|
|
16
|
+
function isTexture(object) {
|
|
17
|
+
return object?.getClassName?.() === "Texture";
|
|
18
|
+
}
|
|
19
|
+
exports.isTexture = isTexture;
|
|
20
|
+
//# sourceMappingURL=guards.js.map
|
package/build/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./loader"), exports);
|
|
18
|
+
__exportStar(require("./texture"), exports);
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
package/build/loader.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.loadScriptsFor = exports.loadScene = void 0;
|
|
4
|
+
const sceneLoader_1 = require("@babylonjs/core/Loading/sceneLoader");
|
|
5
|
+
async function loadScene(rootUrl, sceneFilename, scene) {
|
|
6
|
+
await sceneLoader_1.SceneLoader.AppendAsync(rootUrl, sceneFilename, scene);
|
|
7
|
+
loadScriptsFor(scene, scene);
|
|
8
|
+
scene.transformNodes.forEach((transformNode) => loadScriptsFor(scene, transformNode));
|
|
9
|
+
scene.meshes.forEach((mesh) => loadScriptsFor(scene, mesh));
|
|
10
|
+
scene.lights.forEach((light) => loadScriptsFor(scene, light));
|
|
11
|
+
scene.cameras.forEach((camera) => loadScriptsFor(scene, camera));
|
|
12
|
+
}
|
|
13
|
+
exports.loadScene = loadScene;
|
|
14
|
+
function loadScriptsFor(scene, object) {
|
|
15
|
+
if (!object.metadata) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const map = require("@/scripts").scriptsMap;
|
|
19
|
+
object.metadata.scripts?.forEach((script) => {
|
|
20
|
+
if (!script.enabled) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const exports = map[script.key];
|
|
24
|
+
if (!exports) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (exports.default) {
|
|
28
|
+
const instance = new exports.default(object);
|
|
29
|
+
scene.onBeforeRenderObservable.addOnce(() => {
|
|
30
|
+
instance.onStart?.();
|
|
31
|
+
});
|
|
32
|
+
if (instance.onUpdate) {
|
|
33
|
+
scene.onBeforeRenderObservable.add(() => instance.onUpdate());
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
object.metadata.scripts = undefined;
|
|
38
|
+
}
|
|
39
|
+
exports.loadScriptsFor = loadScriptsFor;
|
|
40
|
+
//# sourceMappingURL=loader.js.map
|
package/build/texture.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.startTextureOptimizer = void 0;
|
|
4
|
+
const guards_1 = require("./guards");
|
|
5
|
+
const sceneMap = new Map();
|
|
6
|
+
function startTextureOptimizer(scene) {
|
|
7
|
+
const engine = scene.getEngine();
|
|
8
|
+
const existingRenderLoop = sceneMap.get(scene);
|
|
9
|
+
if (existingRenderLoop) {
|
|
10
|
+
engine.stopRenderLoop(existingRenderLoop);
|
|
11
|
+
}
|
|
12
|
+
let time = 0;
|
|
13
|
+
const renderLoop = () => {
|
|
14
|
+
const camera = scene.activeCamera;
|
|
15
|
+
time += engine.getDeltaTime();
|
|
16
|
+
if (!camera || time < 1000) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
scene.meshes.forEach((mesh) => {
|
|
20
|
+
if (!(0, guards_1.isMesh)(mesh) || !mesh.material) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
handleMesh(camera, mesh);
|
|
24
|
+
});
|
|
25
|
+
time = 0;
|
|
26
|
+
};
|
|
27
|
+
sceneMap.set(scene, renderLoop);
|
|
28
|
+
engine.runRenderLoop(renderLoop);
|
|
29
|
+
}
|
|
30
|
+
exports.startTextureOptimizer = startTextureOptimizer;
|
|
31
|
+
function handleMesh(camera, mesh) {
|
|
32
|
+
const material = mesh.material;
|
|
33
|
+
const textures = material.getActiveTextures();
|
|
34
|
+
if (!textures.length) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
let isInFrustrum = false;
|
|
38
|
+
if (mesh.instances.length) {
|
|
39
|
+
isInFrustrum = [mesh, ...mesh.instances].find((instance) => camera.isInFrustum(instance)) ? true : false;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
isInFrustrum = camera.isInFrustum(mesh);
|
|
43
|
+
}
|
|
44
|
+
textures.forEach((texture) => {
|
|
45
|
+
if (!(0, guards_1.isTexture)(texture) || !texture.getInternalTexture() || !texture.isReady()) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
texture.metadata ??= {};
|
|
49
|
+
texture.metadata.editor ??= {
|
|
50
|
+
ratio: 1,
|
|
51
|
+
url: texture.getInternalTexture()?.url,
|
|
52
|
+
};
|
|
53
|
+
const url = texture.metadata.editor.url;
|
|
54
|
+
if (!url) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
let ratio = texture.metadata.editor.ratio;
|
|
58
|
+
if (isInFrustrum && ratio === 1) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
let split = url.split("/");
|
|
62
|
+
const dirname = split.slice(0, -1).join("/");
|
|
63
|
+
const basename = split[split.length - 1];
|
|
64
|
+
split = basename.split(".");
|
|
65
|
+
ratio *= (isInFrustrum ? 2 : 0.5);
|
|
66
|
+
const width = texture.getBaseSize().width * ratio;
|
|
67
|
+
const height = texture.getBaseSize().height * ratio;
|
|
68
|
+
if (!isInFrustrum && (width <= 8 || height <= 8)) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (ratio === 1 && texture.getInternalTexture().url !== url) {
|
|
72
|
+
texture.updateURL(url);
|
|
73
|
+
}
|
|
74
|
+
else if (ratio < 1) {
|
|
75
|
+
const newUrl = `${dirname}/${split[0]}_${width}_${height}.${split[1]}`;
|
|
76
|
+
texture.updateURL(newUrl);
|
|
77
|
+
}
|
|
78
|
+
texture.metadata.editor.ratio = ratio;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=texture.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Mesh } from "@babylonjs/core/Meshes/mesh";
|
|
2
|
+
import { Texture } from "@babylonjs/core/Materials/Textures/texture";
|
|
3
|
+
/**
|
|
4
|
+
* Returns wether or not the given object is a Mesh.
|
|
5
|
+
* @param object defines the reference to the object to test its class name.
|
|
6
|
+
*/
|
|
7
|
+
export declare function isMesh(object: any): object is Mesh;
|
|
8
|
+
/**
|
|
9
|
+
* Returns wether or not the given object is a Texture.
|
|
10
|
+
* @param object defines the reference to the object to test its class name.
|
|
11
|
+
*/
|
|
12
|
+
export declare function isTexture(object: any): object is Texture;
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "babylonjs-editor-tools",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Babylon.js Editor Tools is a set of tools to help you create, edit and manage your Babylon.js scenes made using the Babylon.JS Editor",
|
|
5
|
+
"productName": "Babylon.js Editor Tools",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"watch": "tsc -p . --watch"
|
|
8
|
+
},
|
|
9
|
+
"main": "build/index.js",
|
|
10
|
+
"typings": "declaration/index.ts",
|
|
11
|
+
"license": "(Apache-2.0)",
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"@babylonjs/core": "7.0.0",
|
|
14
|
+
"typescript": "5.3.3"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {}
|
|
17
|
+
}
|