@smoud/tiny
Version:
Fast and tiny JavaScript library for HTML5 game and playable ads creation.
81 lines (64 loc) • 2 kB
JavaScript
var THREE = require("three"),
LoadingManager = THREE.LoadingManager,
Texture = THREE.Texture,
sRGBEncoding = THREE.sRGBEncoding;
var gltfLoader = null;
Tiny.Cache.gltf = {};
Tiny.Cache.texture3d = {};
Tiny.Cache.mesh3d = {};
Tiny.Cache.animation3d = {};
Tiny.Loader.prototype.gltf = function (key, json, splitObjects, cb) {
this.list.push({
key: key,
src: json,
type: "gltf",
split: splitObjects,
cb: cb
});
};
Tiny.Loader.prototype.texture3d = function (key, src, cb) {
this.list.push({
key: key,
src: src,
type: "texture3d",
cb: cb
});
};
Tiny.Loader.gltf = function (resource, cb) {
var key = resource.key;
if (gltfLoader == null) {
var manager = new LoadingManager();
gltfLoader = new window.GLTFLoader(manager);
}
function loaded(gltf) {
Tiny.Cache.gltf[key] = gltf;
if (resource.split) {
gltf.scene.traverse(function (obj) {
if (obj.isMesh) Tiny.Cache.mesh3d[key + "." + obj.name] = obj;
});
for (var i = 0; i < gltf.animations.length; i++) {
var obj = gltf.animations[i];
Tiny.Cache.animation3d[key + "." + obj.name] = obj;
}
}
if (resource.cb) resource.cb(gltf);
cb();
}
if (resource.src.length > 200) {
gltfLoader.parse(resource.src, "", loaded);
} else {
gltfLoader.load(resource.src, loaded);
}
};
Tiny.Loader.texture3d = function (resource, cb) {
var key = resource.key;
Tiny.Loader.image(resource, function (resource, image) {
var texture = new Texture(image);
texture.encoding = sRGBEncoding;
texture.flipY = false;
texture.needsUpdate = true;
texture.key = key;
Tiny.Cache.texture3d[resource.key] = texture;
cb();
});
};