mirror of https://gitee.com/cf-fz/WebCAD.git
pull/823/head
parent
91ab25be65
commit
4b581e7153
@ -1,77 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Audio class is used to store audio data as a arraybuffer to be later used by objects with the WebAudio API.
|
||||
*
|
||||
* @class Audio
|
||||
* @extends {Resource}
|
||||
* @constructor
|
||||
* @module Resources
|
||||
* @param {String} url URL to Audio file
|
||||
*/
|
||||
function Audio(url)
|
||||
{
|
||||
Resource.call(this, "audio", "Audio");
|
||||
|
||||
if (url !== undefined) {
|
||||
//Arraybuffer data
|
||||
if (url instanceof window.ArrayBuffer) {
|
||||
this.data = url;
|
||||
this.encoding = "";
|
||||
this.format = "arraybuffer";
|
||||
}
|
||||
//URL
|
||||
else {
|
||||
this.data = FileSystem.readFileArrayBuffer(url);
|
||||
this.encoding = FileSystem.getFileExtension(url);
|
||||
this.format = "arraybuffer";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Audio.prototype = Object.create(Resource.prototype);
|
||||
|
||||
/**
|
||||
* Check if a file name refers to a supported audio file.
|
||||
*
|
||||
* @method fileIsAudio
|
||||
* @static
|
||||
* @param {File} file
|
||||
* @return {boolean} True if the file refers to a supported audio format.
|
||||
*/
|
||||
Audio.fileIsAudio = function (file)
|
||||
{
|
||||
if (file !== undefined) {
|
||||
if (file.type.startsWith("audio")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Serialize audio data as json.
|
||||
*
|
||||
* Audio data is serialized in Base64.
|
||||
*
|
||||
* @method toJSON
|
||||
* @param {meta} meta
|
||||
* @return {Object} data
|
||||
*/
|
||||
Audio.prototype.toJSON = function (meta)
|
||||
{
|
||||
var data = Resource.prototype.toJSON.call(this, meta);
|
||||
|
||||
if (meta.audio[this.uuid] !== undefined) {
|
||||
return meta.audio[this.uuid];
|
||||
}
|
||||
|
||||
data.encoding = this.encoding;
|
||||
data.data = this.data;
|
||||
data.format = this.format;
|
||||
|
||||
meta.audio[this.uuid] = data;
|
||||
|
||||
return data;
|
||||
};
|
@ -1,296 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Font class stores font data, font data can be stored as an opentype json or as a TTF file (stored in Base64).
|
||||
*
|
||||
* Font objects are used for booth 2D and 3D text.
|
||||
*
|
||||
* @class Font
|
||||
* @constructor
|
||||
* @extends {Resource}
|
||||
* @module Resources
|
||||
* @param {String} url URL to font file
|
||||
*/
|
||||
|
||||
/**
|
||||
* Font data, can be an ArrayBuffer or JSON.
|
||||
*
|
||||
* @property font
|
||||
* @type {Object}
|
||||
* @default null
|
||||
*/
|
||||
|
||||
/**
|
||||
* If true the font glyphs are reversed.
|
||||
*
|
||||
* @property reversed
|
||||
* @type {boolean}
|
||||
* @default false
|
||||
*/
|
||||
function Font(url)
|
||||
{
|
||||
Resource.call(this, "font", "Font");
|
||||
|
||||
this.reversed = false;
|
||||
this.font = null;
|
||||
|
||||
if (url !== undefined) {
|
||||
//Arraybuffer
|
||||
if (url instanceof window.ArrayBuffer) {
|
||||
this.data = url;
|
||||
this.loadTTF();
|
||||
this.format = "arraybuffer";
|
||||
}
|
||||
//Opentype JSON
|
||||
else if (typeof url === "object") {
|
||||
this.data = url;
|
||||
this.font = url;
|
||||
this.format = "json";
|
||||
this.encoding = "json";
|
||||
}
|
||||
//URL
|
||||
else {
|
||||
this.encoding = url.split(".").pop().toLowerCase();
|
||||
this.name = FileSystem.getFileName(url);
|
||||
|
||||
if (this.encoding === "json") {
|
||||
this.data = JSON.parse(FileSystem.readFile(url));
|
||||
this.format = "json";
|
||||
this.font = this.data;
|
||||
}
|
||||
else if (this.encoding === "ttf" || this.encoding === "otf" || this.encoding === "ttc" || this.encoding === "otc") {
|
||||
this.data = FileSystem.readFileArrayBuffer(url);
|
||||
this.format = "arraybuffer";
|
||||
this.loadTTF();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Font.prototype = Object.create(Resource.prototype);
|
||||
|
||||
/**
|
||||
* Check if a file name refers to a font file.
|
||||
*
|
||||
* @method fileIsFont
|
||||
* @static
|
||||
* @param {String} fname
|
||||
* @return {boolean} True if the fname refers to a supported font format.
|
||||
*/
|
||||
Font.fileIsFont = function (file)
|
||||
{
|
||||
if (file !== undefined) {
|
||||
file = file.name.toLocaleLowerCase();
|
||||
|
||||
return file.endsWith("ttf") || file.endsWith("otf") || file.endsWith("ttc") || file.endsWith("otc") || file.endsWith("json");
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
Font.prototype.isFont = true;
|
||||
|
||||
/**
|
||||
* Reverse the font glyphs.
|
||||
*
|
||||
* Can be used to fix fonts that have paths defined CW.
|
||||
*
|
||||
* @method reverseGlyphs
|
||||
*/
|
||||
Font.prototype.reverseGlyphs = function ()
|
||||
{
|
||||
this.reversed = !this.reversed;
|
||||
|
||||
this.loadTTF();
|
||||
};
|
||||
|
||||
/**
|
||||
* Load font from data using the TTF loader.
|
||||
*
|
||||
* @method loadTTF
|
||||
*/
|
||||
Font.prototype.loadTTF = function ()
|
||||
{
|
||||
var loader = new TTFLoader();
|
||||
loader.reversed = this.reversed;
|
||||
this.font = loader.parse(this.data);
|
||||
};
|
||||
|
||||
/**
|
||||
* Generate font shapes used to create 3D geometries.
|
||||
*
|
||||
* @method generateShapes
|
||||
* @param {String} text
|
||||
* @param {Number} size
|
||||
* @param {Number} divisions
|
||||
* @return {Array} paths
|
||||
*/
|
||||
Font.prototype.generateShapes = function (text, size, divisions)
|
||||
{
|
||||
if (size === undefined) {
|
||||
size = 100;
|
||||
}
|
||||
|
||||
if (divisions === undefined) {
|
||||
divisions = 10;
|
||||
}
|
||||
|
||||
var data = this.font;
|
||||
var paths = createPaths(text);
|
||||
var shapes = [];
|
||||
|
||||
for (var p = 0; p < paths.length; p++) {
|
||||
Array.prototype.push.apply(shapes, paths[p].toShapes());
|
||||
}
|
||||
|
||||
return shapes;
|
||||
|
||||
//Create paths for text
|
||||
function createPaths(text)
|
||||
{
|
||||
var chars = String(text).split("");
|
||||
var scale = size / data.resolution;
|
||||
var lineHeight = (data.boundingBox.yMax - data.boundingBox.yMin) * scale;
|
||||
|
||||
var offsetX = 0, offsetY = 0;
|
||||
var paths = [];
|
||||
|
||||
for (var i = 0; i < chars.length; i++) {
|
||||
var char = chars[i];
|
||||
|
||||
if (char === "\n") {
|
||||
offsetY -= lineHeight;
|
||||
offsetX = 0;
|
||||
}
|
||||
else {
|
||||
var ret = createPath(char, scale, offsetX, offsetY);
|
||||
offsetX += ret.width;
|
||||
|
||||
paths.push(ret.path);
|
||||
}
|
||||
}
|
||||
|
||||
return paths;
|
||||
}
|
||||
|
||||
//Create path for a character
|
||||
function createPath(c, scale, offsetX, offsetY)
|
||||
{
|
||||
var glyph = data.glyphs[c] || data.glyphs["?"];
|
||||
|
||||
if (!glyph) {
|
||||
return;
|
||||
}
|
||||
|
||||
var path = new THREE.ShapePath();
|
||||
|
||||
//Temporary variables
|
||||
var pts = [], b2 = THREE.ShapeUtils.b2, b3 = THREE.ShapeUtils.b3;
|
||||
var x, y, cpx, cpy, cpx0, cpy0, cpx1, cpy1, cpx2, cpy2, laste;
|
||||
|
||||
if (glyph.o) {
|
||||
var outline = glyph._cachedOutline || (glyph._cachedOutline = glyph.o.split(" "));
|
||||
|
||||
for (var i = 0, l = outline.length; i < l;) {
|
||||
var action = outline[i++];
|
||||
|
||||
//Move to
|
||||
if (action === "m") {
|
||||
x = outline[i++] * scale + offsetX;
|
||||
y = outline[i++] * scale + offsetY;
|
||||
path.moveTo(x, y);
|
||||
}
|
||||
//Line to
|
||||
if (action === "l") {
|
||||
x = outline[i++] * scale + offsetX;
|
||||
y = outline[i++] * scale + offsetY;
|
||||
path.lineTo(x, y);
|
||||
}
|
||||
//Quadratic curve to
|
||||
else if (action === "q") {
|
||||
cpx = outline[i++] * scale + offsetX;
|
||||
cpy = outline[i++] * scale + offsetY;
|
||||
cpx1 = outline[i++] * scale + offsetX;
|
||||
cpy1 = outline[i++] * scale + offsetY;
|
||||
|
||||
path.quadraticCurveTo(cpx1, cpy1, cpx, cpy);
|
||||
laste = pts[pts.length - 1];
|
||||
|
||||
if (laste) {
|
||||
cpx0 = laste.x;
|
||||
cpy0 = laste.y;
|
||||
|
||||
for (var i2 = 1; i2 <= divisions; i2++) {
|
||||
var t = i2 / divisions;
|
||||
b2(t, cpx0, cpx1, cpx);
|
||||
b2(t, cpy0, cpy1, cpy);
|
||||
}
|
||||
}
|
||||
}
|
||||
//Bezier curve to
|
||||
else if (action === "b") {
|
||||
cpx = outline[i++] * scale + offsetX;
|
||||
cpy = outline[i++] * scale + offsetY;
|
||||
cpx1 = outline[i++] * scale + offsetX;
|
||||
cpy1 = outline[i++] * scale + offsetY;
|
||||
cpx2 = outline[i++] * scale + offsetX;
|
||||
cpy2 = outline[i++] * scale + offsetY;
|
||||
|
||||
path.bezierCurveTo(cpx1, cpy1, cpx2, cpy2, cpx, cpy);
|
||||
laste = pts[pts.length - 1];
|
||||
|
||||
if (laste) {
|
||||
cpx0 = laste.x;
|
||||
cpy0 = laste.y;
|
||||
|
||||
for (var i2 = 1; i2 <= divisions; i2++) {
|
||||
var t = i2 / divisions;
|
||||
b3(t, cpx0, cpx1, cpx2, cpx);
|
||||
b3(t, cpy0, cpy1, cpy2, cpy);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { width: glyph.ha * scale, path: path };
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Serialize font resource to json.
|
||||
*
|
||||
* Font data is stored as Base64 is present in a binary format, or JSON otherwise.
|
||||
*
|
||||
* @method toJSON
|
||||
* @param {Object} meta
|
||||
* @return {Object} json
|
||||
*/
|
||||
Font.prototype.toJSON = function (meta)
|
||||
{
|
||||
var data = Resource.prototype.toJSON.call(this, meta);
|
||||
|
||||
if (meta.fonts[this.uuid] !== undefined) {
|
||||
return meta.fonts[this.uuid];
|
||||
}
|
||||
|
||||
data.encoding = this.encoding;
|
||||
data.reversed = this.reversed;
|
||||
|
||||
if (this.format === "arraybuffer") {
|
||||
data.data = this.data;
|
||||
data.format = this.format;
|
||||
}
|
||||
else if (this.format === "base64") {
|
||||
data.data = ArraybufferUtils.fromBase64(this.data);
|
||||
data.format = "arraybuffer";
|
||||
}
|
||||
else if (this.format === "json") {
|
||||
data.data = this.data;
|
||||
data.format = this.format;
|
||||
}
|
||||
|
||||
meta.fonts[this.uuid] = data;
|
||||
|
||||
return data;
|
||||
};
|
@ -1,230 +0,0 @@
|
||||
import { Base64Utils } from '../Common/binary/Base64Utils';
|
||||
import { FileSystem } from '../Common/FileSystem';
|
||||
import { MathUtils } from '../Common/MathUtils';
|
||||
import { Resource } from './Resource';
|
||||
|
||||
/**
|
||||
* Image class is used to store image data that is used to create Textures.
|
||||
*
|
||||
* Images can be stored in mutiple formats but on serialization images are converted to JPEG if they are opaque or to PNG if they are transparent.
|
||||
*
|
||||
* GIF images are never converted to prevert animation capabilities.
|
||||
*
|
||||
* @class Image
|
||||
* @constructor
|
||||
* @extends {Resource}
|
||||
* @module Resources
|
||||
* @param {String} data Can be URL to image, ArrayBuffer for TGA data or base64 encoded data.
|
||||
*/
|
||||
export class Image extends Resource
|
||||
{
|
||||
constructor(url?: any, encoding?: any)
|
||||
{
|
||||
super("image", "Image");
|
||||
|
||||
if (url !== undefined)
|
||||
{
|
||||
//Arraybuffer data
|
||||
if (url instanceof ArrayBuffer)
|
||||
{
|
||||
this.loadTGAData(url);
|
||||
}
|
||||
//Base64 data
|
||||
else if (url.startsWith("data:image"))
|
||||
{
|
||||
this.encoding = Base64Utils.getFileFormat(url);
|
||||
this.format = "base64";
|
||||
this.data = url;
|
||||
}
|
||||
//URL
|
||||
else
|
||||
{
|
||||
this.encoding = FileSystem.getFileExtension(url);
|
||||
|
||||
if (this.encoding === "gif")
|
||||
{
|
||||
this.data = "data:image/" + this.encoding + ";base64," + FileSystem.readFileBase64(url);
|
||||
this.format = "base64";
|
||||
}
|
||||
if (this.encoding === "tga")
|
||||
{
|
||||
this.loadTGAData(FileSystem.readFileArrayBuffer(url));
|
||||
}
|
||||
else
|
||||
{
|
||||
/*let arraybuffer = FileSystem.readFileArrayBuffer(url);
|
||||
let view = new Uint8Array(arraybuffer);
|
||||
let blob = new Blob([view], {type: "image/" + this.encoding});
|
||||
|
||||
this.data = URL.createObjectURL(blob);
|
||||
this.format = "blob";*/
|
||||
|
||||
this.format = "url";
|
||||
this.data = url;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this.createSolidColor();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a file name refers to a supported binary image file.
|
||||
*
|
||||
* @method fileIsImage
|
||||
* @static
|
||||
* @param {File} file
|
||||
* @return {boolean} True if the file refers to a supported image format.
|
||||
*/
|
||||
static fileIsImage(file)
|
||||
{
|
||||
if (file !== undefined)
|
||||
{
|
||||
if (file.type.startsWith("image"))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
file = file.name.toLocaleLowerCase();
|
||||
return file.endsWith("tga");
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Create a new image with 1x1 resolution with solid color.
|
||||
*
|
||||
* Can be called externally on data load error to load dummy data.
|
||||
*
|
||||
* @method createSolidColor
|
||||
* @param {String} color Color code
|
||||
*/
|
||||
createSolidColor(color?)
|
||||
{
|
||||
let canvas = document.createElement("canvas");
|
||||
canvas.width = 1;
|
||||
canvas.height = 1;
|
||||
|
||||
let context = canvas.getContext("2d");
|
||||
context.fillStyle = (color !== undefined) ? color : MathUtils.randomColor();
|
||||
context.fillRect(0, 0, 1, 1);
|
||||
|
||||
this.data = canvas.toDataURL("image/png");
|
||||
this.format = "base64";
|
||||
this.encoding = "png";
|
||||
};
|
||||
|
||||
/**
|
||||
* Load .tga file from ArrayBuffer data.
|
||||
*
|
||||
* After loading data is converted to JPEG format and stored in base64 encoding.
|
||||
*
|
||||
* @method loadTGAData
|
||||
*/
|
||||
loadTGAData(data)
|
||||
{
|
||||
// let canvas = new THREE.TGALoader().parse(data);
|
||||
// this.encoding = "jpeg";
|
||||
// this.format = "base64";
|
||||
// this.data = canvas.toDataURL("image/jpeg", 1.0);
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if this image has alpha channel.
|
||||
*
|
||||
* This checks the file encoding if the file a GIF or a PNG is assumed that the file has alpha channel.
|
||||
*
|
||||
* @method hasTransparency
|
||||
* @return {boolean} True if the image is encoded as PNG or GIF
|
||||
*/
|
||||
hasTransparency()
|
||||
{
|
||||
return this.encoding === "png" || this.encoding === "gif";
|
||||
};
|
||||
|
||||
/**
|
||||
* Compresses image data to JPEG or PNG and stores in base64 encoding.
|
||||
*
|
||||
* If the image has transparency it is stored as PNG otherwise the image is stored in JPEG with 1.0 quality.
|
||||
*
|
||||
* Can be used to compress data and save space.
|
||||
*
|
||||
* @method encodeData
|
||||
*/
|
||||
encodeData()
|
||||
{
|
||||
let image = document.createElement("img");
|
||||
image.src = this.data;
|
||||
|
||||
let canvas = document.createElement("canvas");
|
||||
canvas.width = image.width;
|
||||
canvas.height = image.height;
|
||||
|
||||
let context = canvas.getContext("2d");
|
||||
context.drawImage(image, 0, 0, image.width, image.height);
|
||||
|
||||
//Check if the image has some tranparency
|
||||
let transparent = false;
|
||||
let data = context.getImageData(0, 0, image.width, image.height).data;
|
||||
for (let i = 3; i < data.length; i += 4)
|
||||
{
|
||||
if (data[i] !== 255)
|
||||
{
|
||||
transparent = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//Encode data
|
||||
if (transparent)
|
||||
{
|
||||
this.encoding = "png";
|
||||
this.format = "base64";
|
||||
this.data = canvas.toDataURL("image/png");
|
||||
}
|
||||
else
|
||||
{
|
||||
this.encoding = "jpeg";
|
||||
this.format = "base64";
|
||||
this.data = canvas.toDataURL("image/jpeg", 1.0);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Serialize Image resource to json.
|
||||
*
|
||||
* If image is stored as URL it is converter to PNG or JPEG.
|
||||
*
|
||||
* @method toJSON
|
||||
* @param {Object} meta
|
||||
* @return {Object} json
|
||||
*/
|
||||
toJSON(meta)
|
||||
{
|
||||
let data = super.toJSON(meta);
|
||||
|
||||
if (meta.images[this.uuid] !== undefined)
|
||||
{
|
||||
return meta.images[this.uuid];
|
||||
}
|
||||
|
||||
if (this.format === "url")
|
||||
{
|
||||
this.encodeData();
|
||||
}
|
||||
|
||||
data.encoding = this.encoding;
|
||||
data.format = this.format;
|
||||
data.data = this.data;
|
||||
|
||||
meta.images[this.uuid] = data;
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Model is used to load and check file type for external 3D models, animations, scenes etc.
|
||||
*
|
||||
* It wraps other threejs loaders and loads files using them depending on file format.
|
||||
*
|
||||
* @class Model
|
||||
* @extends {Resource}
|
||||
* @constructor
|
||||
* @module Resources
|
||||
*/
|
||||
function Model()
|
||||
{
|
||||
Resource.call(this, "model", "Model");
|
||||
}
|
||||
|
||||
Model.prototype = Object.create(Resource.prototype);
|
||||
|
||||
/**
|
||||
* Check if a file name refers to a 3D geometry file.
|
||||
*
|
||||
* @method fileIsFont
|
||||
* @static
|
||||
* @param {File} file
|
||||
* @return {boolean} True if the fname refers to a supported format.
|
||||
*/
|
||||
Model.fileIsModel = function (file)
|
||||
{
|
||||
if (file !== undefined) {
|
||||
file = file.name.toLocaleLowerCase();
|
||||
|
||||
return file.endsWith("svg") || file.endsWith("obj") || file.endsWith("3ds") || file.endsWith("dae") || file.endsWith("gltf") || file.endsWith("glb") || file.endsWith("3mf") || file.endsWith("awd") || file.endsWith("ply") || file.endsWith("vtk") || file.endsWith("vtp") || file.endsWith("wrl") || file.endsWith("vrml") || file.endsWith("fbx") || file.endsWith("pcd") || file.endsWith("stl") || file.endsWith("json");
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
@ -1,94 +0,0 @@
|
||||
import { MathUtils } from 'three';
|
||||
|
||||
/**
|
||||
* Resource class is used to represent resources.
|
||||
*
|
||||
* Resources store data that is used by objects.
|
||||
*
|
||||
* @class Resource
|
||||
* @module Resources
|
||||
* @constructor
|
||||
*/
|
||||
|
||||
/**
|
||||
* Resource name.
|
||||
*
|
||||
* Not required to be unique.
|
||||
*
|
||||
* @property name
|
||||
* @type {String}
|
||||
*/
|
||||
/**
|
||||
* UUID unique identifier.
|
||||
*
|
||||
* @property uuid
|
||||
* @type {String}
|
||||
*/
|
||||
/**
|
||||
* Resource type. Used to identify the type of the resource, usefull for serialization.
|
||||
*
|
||||
* @property type
|
||||
* @type {String}
|
||||
*/
|
||||
/**
|
||||
* Data format (base64, arraybuffer, blob, url, ...).
|
||||
*
|
||||
* Indicates the format used to store the data.
|
||||
*
|
||||
* @property format
|
||||
* @type {Object}
|
||||
*/
|
||||
/**
|
||||
* Data encoding (mp3, jpg, mp4, ...).
|
||||
*
|
||||
* Indicates how the data is encoded.
|
||||
*
|
||||
* @property encoding
|
||||
* @type {String, ArrayBuffer, ...}
|
||||
*/
|
||||
/**
|
||||
* Resource data.
|
||||
*
|
||||
* @property data
|
||||
* @type {Object}
|
||||
*/
|
||||
export class Resource
|
||||
{
|
||||
data: any;
|
||||
encoding: string;
|
||||
format: string;
|
||||
type: any;
|
||||
uuid: string;
|
||||
name: string;
|
||||
constructor(name, type)
|
||||
{
|
||||
this.name = name;
|
||||
this.uuid = MathUtils.generateUUID();
|
||||
this.type = type;
|
||||
|
||||
this.format = "";
|
||||
this.encoding = "";
|
||||
this.data = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize resource to json.
|
||||
*
|
||||
* Only serializes name, uuid and type.
|
||||
*
|
||||
* @method toJSON
|
||||
* @param {Object} meta
|
||||
* @return {Object} json
|
||||
*/
|
||||
toJSON(meta)
|
||||
{
|
||||
var data: any = {};
|
||||
|
||||
data.name = this.name;
|
||||
data.uuid = this.uuid;
|
||||
data.type = this.type;
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
}
|
@ -1,392 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Resource manager is used to manage available resources used by objects
|
||||
*
|
||||
* The resource manager is used to extend the Program object and is not designed to be used as a standalone
|
||||
*
|
||||
* The manager is used to manage the following types of resources:
|
||||
* - Images
|
||||
* - Videos
|
||||
* - Audio
|
||||
* - Fonts
|
||||
* - Textures
|
||||
* - Materials
|
||||
* - Geometries
|
||||
*
|
||||
* @class ResourceManager
|
||||
* @constructor
|
||||
* @module Resources
|
||||
*/
|
||||
|
||||
/**
|
||||
* Images.
|
||||
*
|
||||
* @property images
|
||||
* @type {Array}
|
||||
*/
|
||||
/**
|
||||
* Videos.
|
||||
*
|
||||
* @property videos
|
||||
* @type {Array}
|
||||
*/
|
||||
/**
|
||||
* Audio.
|
||||
*
|
||||
* @property audio
|
||||
* @type {Array}
|
||||
*/
|
||||
/**
|
||||
* Fonts.
|
||||
*
|
||||
* @property fonts
|
||||
* @type {Array}
|
||||
*/
|
||||
/**
|
||||
* Materials.
|
||||
*
|
||||
* @property materials
|
||||
* @type {Array}
|
||||
*/
|
||||
/**
|
||||
* Textures.
|
||||
*
|
||||
* @property textures
|
||||
* @type {Array}
|
||||
*/
|
||||
/**
|
||||
* Geometries.
|
||||
*
|
||||
* @property geometries
|
||||
* @type {Array}
|
||||
*/
|
||||
function ResourceManager()
|
||||
{
|
||||
THREE.Object3D.call(this);
|
||||
|
||||
this.images = [];
|
||||
this.videos = [];
|
||||
this.audio = [];
|
||||
this.fonts = [];
|
||||
this.materials = [];
|
||||
this.textures = [];
|
||||
this.geometries = [];
|
||||
}
|
||||
|
||||
ResourceManager.prototype = Object.create(THREE.Object3D.prototype);
|
||||
|
||||
/**
|
||||
* Get image by name.
|
||||
*
|
||||
* @method getImageByName
|
||||
* @param {String} name Image name
|
||||
* @return {Image} Image if found else null
|
||||
*/
|
||||
ResourceManager.prototype.getImageByName = function (name)
|
||||
{
|
||||
for (var i in this.images) {
|
||||
if (this.images[i].name === name) {
|
||||
return this.images[i];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get video by name.
|
||||
*
|
||||
* @method getVideoByName
|
||||
* @param {String} name Video name
|
||||
* @return {Video} Video if found else null
|
||||
*/
|
||||
ResourceManager.prototype.getVideoByName = function (name)
|
||||
{
|
||||
for (var i in this.videos) {
|
||||
if (this.videos[i].name === name) {
|
||||
return this.videos[i];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get material by its name.
|
||||
*
|
||||
* @method getMaterialByName
|
||||
* @param {String} name Material name
|
||||
* @return {Material} Material if found else null
|
||||
*/
|
||||
ResourceManager.prototype.getMaterialByName = function (name)
|
||||
{
|
||||
for (var i in this.materials) {
|
||||
if (this.materials[i].name === name) {
|
||||
return this.materials[i];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Add material to materials list.
|
||||
*
|
||||
* @method addMaterial
|
||||
* @param {Material} material Material to be added
|
||||
*/
|
||||
ResourceManager.prototype.addMaterial = function (material)
|
||||
{
|
||||
if (material instanceof THREE.Material) {
|
||||
this.materials[material.uuid] = material;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove material from materials list, also receives default material used to replace.
|
||||
*
|
||||
* @method removeMaterial
|
||||
* @param {Material} material Material to be removed from manager.
|
||||
* @param {Material} defaultMaterial Default mesh material to replace objects mesh materials.
|
||||
* @param {Material} defaultMaterialSprite Defaul sprite material.
|
||||
*/
|
||||
ResourceManager.prototype.removeMaterial = function (material, defaultMaterial, defaultMaterialSprite)
|
||||
{
|
||||
if (defaultMaterial === undefined) {
|
||||
defaultMaterial = new THREE.MeshBasicMaterial();
|
||||
}
|
||||
|
||||
if (defaultMaterialSprite === undefined) {
|
||||
defaultMaterialSprite = new THREE.SpriteMaterial();
|
||||
}
|
||||
|
||||
if (material instanceof THREE.Material) {
|
||||
delete this.materials[material.uuid];
|
||||
|
||||
this.traverse(function (child)
|
||||
{
|
||||
if (child.material !== undefined && child.material.uuid === material.uuid) {
|
||||
if (child instanceof THREE.Sprite) {
|
||||
child.material = defaultMaterialSprite;
|
||||
}
|
||||
else {
|
||||
child.material = defaultMaterial;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get texture by name.
|
||||
*
|
||||
* @method getTextureByName
|
||||
* @param {String} name Texture name.
|
||||
* @return {Texture} Texture is found else null.
|
||||
*/
|
||||
ResourceManager.prototype.getTextureByName = function (name)
|
||||
{
|
||||
for (var i in this.textures) {
|
||||
if (this.textures[i].name === name) {
|
||||
return this.textures[i];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Add texture to texture list.
|
||||
*
|
||||
* @method addTexture
|
||||
* @param {Texture} texture
|
||||
*/
|
||||
ResourceManager.prototype.addTexture = function (texture)
|
||||
{
|
||||
this.textures[texture.uuid] = texture;
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove texture from textures list (also receives default used to replace).
|
||||
*
|
||||
* @method removeTexture
|
||||
* @param {Texture} texture
|
||||
* @param {Texture} defaultTexture
|
||||
* @return {Texture} Texture if found, else null
|
||||
*/
|
||||
ResourceManager.prototype.removeTexture = function (texture, defaultTexture)
|
||||
{
|
||||
if (defaultTexture === undefined) {
|
||||
defaultTexture = new THREE.Texture();
|
||||
}
|
||||
|
||||
if (texture instanceof THREE.Texture) {
|
||||
delete this.textures[texture.uuid];
|
||||
|
||||
this.traverse(function (child)
|
||||
{
|
||||
if (child.material !== undefined) {
|
||||
var material = child.material;
|
||||
|
||||
if (material.map != null && material.map.uuid === texture.uuid) {
|
||||
material.map = defaultTexture;
|
||||
material.needsUpdate = true;
|
||||
}
|
||||
if (material.bumpMap != null && material.bumpMap.uuid === texture.uuid) {
|
||||
material.bumpMap = defaultTexture;
|
||||
material.needsUpdate = true;
|
||||
}
|
||||
if (material.normalMap != null && material.normalMap.uuid === texture.uuid) {
|
||||
material.normalMap = defaultTexture;
|
||||
material.needsUpdate = true;
|
||||
}
|
||||
if (material.displacementMap != null && material.displacementMap.uuid === texture.uuid) {
|
||||
material.displacementMap = defaultTexture;
|
||||
material.needsUpdate = true;
|
||||
}
|
||||
if (material.specularMap != null && material.specularMap.uuid === texture.uuid) {
|
||||
material.specularMap = defaultTexture;
|
||||
material.needsUpdate = true;
|
||||
}
|
||||
if (material.emissiveMap != null && material.emissiveMap.uuid === texture.uuid) {
|
||||
material.emissiveMap = defaultTexture;
|
||||
material.needsUpdate = true;
|
||||
}
|
||||
if (material.alphaMap != null && material.alphaMap.uuid === texture.uuid) {
|
||||
material.alphaMap = defaultTexture;
|
||||
material.needsUpdate = true;
|
||||
}
|
||||
if (material.roughnessMap != null && material.roughnessMap.uuid === texture.uuid) {
|
||||
material.roughnessMap = defaultTexture;
|
||||
material.needsUpdate = true;
|
||||
}
|
||||
if (material.metalnessMap != null && material.metalnessMap.uuid === texture.uuid) {
|
||||
material.metalnessMap = defaultTexture;
|
||||
material.needsUpdate = true;
|
||||
}
|
||||
if (material.envMap != null && material.envMap.uuid === texture.uuid) {
|
||||
material.envMap = null;
|
||||
material.needsUpdate = true;
|
||||
}
|
||||
}
|
||||
else if (child instanceof ParticleEmitter) {
|
||||
if (child.group.texture.uuid === texture.uuid) {
|
||||
child.group.texture = defaultTexture;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get font by name.
|
||||
*
|
||||
* @method getFontByName
|
||||
* @param {String} name
|
||||
* @return {Font} Font if found, else null
|
||||
*/
|
||||
ResourceManager.prototype.getFontByName = function (name)
|
||||
{
|
||||
for (var i in this.fonts) {
|
||||
if (this.fonts[i].name === name) {
|
||||
return this.fonts[i];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Add font to fonts list.
|
||||
*
|
||||
* @method addFont
|
||||
* @param {Font} font
|
||||
*/
|
||||
ResourceManager.prototype.addFont = function (font)
|
||||
{
|
||||
if (font instanceof Font) {
|
||||
this.fonts[font.uuid] = font;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove font from font list.
|
||||
*
|
||||
* @method removeFont
|
||||
* @param {Font} font
|
||||
* @param {Font} defaultFont
|
||||
*/
|
||||
ResourceManager.prototype.removeFont = function (font, defaultFont)
|
||||
{
|
||||
if (defaultFont === undefined) {
|
||||
defaultFont = new Font();
|
||||
}
|
||||
|
||||
if (font instanceof Font) {
|
||||
delete this.fonts[font.uuid];
|
||||
|
||||
this.traverse(function (child)
|
||||
{
|
||||
if (child.font !== undefined && child.font.uuid === font.uuid) {
|
||||
child.setFont(defaultFont);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get audio by name.
|
||||
*
|
||||
* @method getAudioByName
|
||||
* @param {String} name
|
||||
* @return {Audio} Audio if found, else null
|
||||
*/
|
||||
ResourceManager.prototype.getAudioByName = function (name)
|
||||
{
|
||||
for (var i in this.audio) {
|
||||
if (this.audio[i].name === name) {
|
||||
return this.audio[i];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Add audio to audio list.
|
||||
*
|
||||
* @param {Audio} audio
|
||||
* @method addAudio
|
||||
*/
|
||||
ResourceManager.prototype.addAudio = function (audio)
|
||||
{
|
||||
if (audio instanceof Audio) {
|
||||
this.audio[audio.uuid] = audio;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove audio.
|
||||
*
|
||||
* @param {Audio} audio
|
||||
* @param {Audio} defaultAudio
|
||||
* @method removeAudio
|
||||
*/
|
||||
ResourceManager.prototype.removeAudio = function (audio, defaultAudio)
|
||||
{
|
||||
if (defaultAudio === undefined) {
|
||||
defaultAudio = new Audio();
|
||||
}
|
||||
|
||||
if (audio instanceof Audio) {
|
||||
delete this.audio[audio.uuid];
|
||||
|
||||
this.traverse(function (child)
|
||||
{
|
||||
if (child.audio !== undefined && child.audio.uuid === audio.uuid) {
|
||||
child.setFont(defaultAudio);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
@ -1,40 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Text resource can be used to store text data or code.
|
||||
*
|
||||
* Text data can be used to store information to be consumend by the application or runtime code that can be imported by scripts.
|
||||
*
|
||||
* @class Text
|
||||
* @constructor
|
||||
* @extends {Resource}
|
||||
* @module Resources
|
||||
*/
|
||||
function Text()
|
||||
{
|
||||
Resource.call(this, "text", "Text");
|
||||
|
||||
this.format = "string";
|
||||
this.encoding = "txt";
|
||||
this.data = "";
|
||||
}
|
||||
|
||||
Text.prototype = Object.create(Resource.prototype);
|
||||
|
||||
/**
|
||||
* Serialize text resource data to json.
|
||||
*
|
||||
* @method toJSON
|
||||
* @param {meta} meta
|
||||
* @return {Object} data
|
||||
*/
|
||||
Text.prototype.toJSON = function (meta)
|
||||
{
|
||||
var data = Resource.prototype.toJSON.call(this, meta);
|
||||
|
||||
data.encoding = this.encoding;
|
||||
data.data = this.data;
|
||||
data.format = this.format;
|
||||
|
||||
return data;
|
||||
};
|
@ -1,79 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Video resources are used to store video.
|
||||
*
|
||||
* Video data stored in base64.
|
||||
*
|
||||
* @class Video
|
||||
* @constructor
|
||||
* @extends {Resource}
|
||||
* @module Resources
|
||||
* @param {String} url URL to video file
|
||||
*/
|
||||
function Video(url)
|
||||
{
|
||||
Resource.call(this, "video", "Video");
|
||||
|
||||
if (url !== undefined) {
|
||||
//Base64 data
|
||||
if (url.startsWith("data:video")) {
|
||||
this.encoding = Base64Utils.getFileFormat(url);
|
||||
this.format = "base64";
|
||||
this.data = url;
|
||||
}
|
||||
//URL
|
||||
else {
|
||||
this.encoding = url.split(".").pop().toLowerCase();
|
||||
this.data = "data:video/" + this.encoding + ";base64," + FileSystem.readFileBase64(url);
|
||||
this.format = "base64";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Video.prototype = Object.create(Resource.prototype);
|
||||
|
||||
/**
|
||||
* Check if a file name refers to a supported video file.
|
||||
*
|
||||
* @method fileIsVideo
|
||||
* @static
|
||||
* @param {File} file
|
||||
* @return {boolean} True if the file refers to a supported video format.
|
||||
*/
|
||||
Video.fileIsVideo = function (file)
|
||||
{
|
||||
if (file !== undefined) {
|
||||
if (file.type.startsWith("video")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Serialize resource to json.
|
||||
*
|
||||
* Video data is stored in Base64.
|
||||
*
|
||||
* @method toJSON
|
||||
* @param {Object} meta
|
||||
* @return {Object} json
|
||||
*/
|
||||
Video.prototype.toJSON = function (meta)
|
||||
{
|
||||
var data = Resource.prototype.toJSON.call(this, meta);
|
||||
|
||||
if (meta.videos[this.uuid] !== undefined) {
|
||||
return meta.videos[this.uuid];
|
||||
}
|
||||
|
||||
data.encoding = this.encoding;
|
||||
data.format = this.format;
|
||||
data.data = this.data;
|
||||
|
||||
meta.videos[this.uuid] = data;
|
||||
|
||||
return data;
|
||||
};
|
Loading…
Reference in new issue