Class: Utils

THING.Utils()

Utils Useful functions.

Constructor

new Utils()

Source:

Methods

(static) createObject(type, options) → {Object}

create object by type
Parameters:
Name Type Description
type string the object type.
options Object the create options.
Source:
Returns:
Type
Object
Example
let line = THING.Utils.createObject('PixelLine');
let ret = line instanceof THING.PixelLine;
// @expect(ret == true)

(static) isCompressedTexture(url) → {Boolean}

Check whether it's compressed texture.
Parameters:
Name Type Description
url String The compressed texture url.
Source:
Returns:
Type
Boolean
Example
let compress = THING.Utils.isCompressedTexture('image.dds');
let uncompress = THING.Utils.isCompressedTexture('image.png');
// @expect(compress == true && uncompress == false);

(static) isModelSceneExtension(ext)

check is gltf or glb extension
Parameters:
Name Type Description
ext String the file extension.
Source:
Example
let ret1 = THING.Utils.isModelSceneExtension('gltf');
let ret2 = THING.Utils.isModelSceneExtension('json');
// @expect(ret1 == true && ret2 == false)

(static) isModelSceneUrl(url)

check is gltf or glb file
Parameters:
Name Type Description
url String the url of the scene.
Source:
Example
let ret1 = THING.Utils.isModelSceneUrl('a.gltf');
let ret2 = THING.Utils.isModelSceneUrl('b.json');
// @expect(ret1 == true && ret2 == false)

(static) isValidTexture(value)

check is valid texture
Parameters:
Name Type Description
value Object the texture object.
Source:
Example
let ret1 = THING.Utils.isValidTexture(new THING.ImageTexture());
let ret2 = THING.Utils.isValidTexture(new THING.CubeTexture());
let ret3 = THING.Utils.isValidTexture(new THING.RenderTexture());
let ret4 = THING.Utils.isValidTexture(new THING.VideoTexture());
let ret5 = THING.Utils.isValidTexture(new THING.EmptyTexture());
let ret6 = THING.Utils.isValidTexture(new THING.BaseObject());
// @expect(ret1 == true && ret2 == true && ret3 == true && ret4 == true && ret5 == true && ret6 == false)

(static) isVideoResource(resource)

Load binary file.
Parameters:
Name Type Description
resource Object The resource object.
Source:
Example
let resource = document.createElement('video');;
let check = THING.Utils.isVideoResource(resource);
// @expect(check == true);

(static) loadBinaryFile(url, onLoad, onProgress, onError, options)

Load binary file.
Parameters:
Name Type Description
url String The file URL.
onLoad function When load finished callback function.
onProgress function When loading in progression callback function.
onError function When laod error occurred callback function.
options LoadFileOptions The options.
Source:
Example
// Load 'my-file.bin' file in binary format.
THING.Utils.loadBinaryFile('my-file.bin',
	function(data) {
		console.log('Load finished', data);
	},
	function() {
		console.error('Load failed');
	}
);

(static) loadBinaryFileAsync(url, options) → {Promise.<any>}

Load binary file in async mode.
Parameters:
Name Type Description
url String The file URL.
options LoadFileOptions The options.
Deprecated:
  • 2.7
Source:
Returns:
Type
Promise.<any>
Example
// Load 'my-file.bin' file and wait to load finished.
await THING.Utils.loadBinaryFileAsync('my-file.bin');

(static) loadCode(code, options)

Load code.
Parameters:
Name Type Description
code String The code string.
options Object The options.
Properties
Name Type Description
es6Mode Boolean True indicates load code in es6 mode.
sourceURL String The source URL in sync mode.
onLoad function The load finished callback function.
onError function The error occurred callback function.
Source:
Example
THING.Utils.loadCode('console.log("Code load finished")');

(static) loadCodeAsync(code, options) → {Promise.<any>}

Load code in async mode.
Parameters:
Name Type Description
code String The code string.
options Object The options.
Properties
Name Type Description
es6Mode Boolean True indicates load code in es6 mode.
sourceURL String The source URL in sync mode.
Deprecated:
  • 2.7
Source:
Returns:
Type
Promise.<any>
Example
await THING.Utils.loadCodeAsync('console.log("Code load finished")');

(static) loadFile(resource, options)

Load file(s).
Parameters:
Name Type Description
resource String | Array.<String> The file(s) resource url.
options Object | Array.<String> The options.
Properties
Name Type Attributes Default Description
onLoad function The load finished callback function.
onError function The error occurred callback function.
cache Boolean <optional>
true True indicates try to use cache.
inOrder Boolean <optional>
true True indicates keep order to load files one by one.
Source:
Example
// Load 'my-lib.js' file by load and error callback functions
THING.Utils.loadFile('my-lib.js', {
	onLoad: function() {
		console.log('Load finished');
	},
	onError: function() {
		console.error('Load failed');
	}
});

(static) loadFileAsync(resource, options) → {Promise.<any>}

Load file(s) in async mode.
Parameters:
Name Type Description
resource String | Array.<String> The file(s) resource url.
options Object | Array.<String> The options.
Properties
Name Type Attributes Default Description
cache Boolean <optional>
true True indicates try to use cache.
inOrder Boolean <optional>
true True indicates keep order to load files one by one.
Deprecated:
  • 2.7
Source:
Returns:
Type
Promise.<any>
Example
// Load 'my-lib.js' file and wait to load completed
await THING.Utils.loadFileAsync('my-lib.js');

(static) loadImageFile(url, onLoad, onProgress, onError)

Load image file.
Parameters:
Name Type Description
url String The file URL.
onLoad function When load finished callback function.
onProgress function When loading in progression callback function.
onError function When laod error occurred callback function.
Source:
Example
// Load image resource by load callback function.
THING.Utils.loadImageFile('./assets/images/blue.png',
	function(image) {
		console.log(image);
	}
);

(static) loadImageFileAsync(url) → {Promise.<any>}

Load image file in async mode.
Parameters:
Name Type Description
url String The file URL.
Deprecated:
  • 2.7
Source:
Returns:
Type
Promise.<any>
Example
// Load image resource and wait to load finished.
await THING.Utils.loadImageFileAsync('image.png');

(static) loadJSONFile(url, onLoad, onProgress, onError, options)

Load JSON file.
Parameters:
Name Type Description
url String The file URL.
onLoad function When load finished callback function.
onProgress function When loading in progression callback function.
onError function When laod error occurred callback function.
options LoadFileOptions The options.
Source:
Example
// Load 'my-file.json' file in json format.
THING.Utils.loadJSONFile('my-file.json',
	function(data) {
		console.log('Load finished', data);
	},
	function() {
		console.error('Load failed');
	}
);

(static) loadJSONFileAsync(url, options) → {Promise.<any>}

Load JSON file(s) in async mode.
Parameters:
Name Type Description
url String | Array.<String> The file URL(s).
options LoadFileOptions The options.
Deprecated:
  • 2.7
Source:
Returns:
Type
Promise.<any>
Example
// Load 'my-file.json' file and wait to load finished.
await THING.Utils.loadJSONFileAsync('my-file.json');

(static) loadTextFile(url, onLoad, onProgress, onError, options)

Load text file.
Parameters:
Name Type Description
url String The file URL.
onLoad function When load finished callback function.
onProgress function When loading in progression callback function.
onError function When laod error occurred callback function.
options LoadFileOptions The options.
Source:
Example
// Load 'my-file.txt' file in text format.
THING.Utils.loadTextFile('my-file.txt',
	function(data) {
		console.log('Load finished', data);
	},
	function() {
		console.error('Load failed');
	}
);

(static) loadTextFileAsync(url, options) → {Promise.<any>}

Load text file in async mode.
Parameters:
Name Type Description
url String The file URL.
options LoadFileOptions The options.
Deprecated:
  • 2.7
Source:
Returns:
Type
Promise.<any>
Example
// Load 'my-file.txt' file and wait to load finished.
await THING.Utils.loadTextFileAsync('my-file.txt');

(static) loadWasmFile(url, options) → {Promise.<any>}

Load WASM file.
Parameters:
Name Type Description
url String The url.
options LoadWasmFileOptions The options.
Source:
Returns:
Type
Promise.<any>

(static) login(options) → {Promise.<any>}

Login.
Parameters:
Name Type Description
options String | LoginOptions The url or options.
Source:
Returns:
Type
Promise.<any>
Example
let promise1 = THING.Utils.login('http://127.0.0.1:3000/auth.json');
	let promise2 = THING.Utils.login({
		method: 'GET',
		url: 'http://127.0.0.1:3000/auth.json',
		wasmRootPath: 'js/wasm',
		postFields: 'post data'
	});

(static) parseCubeTextureUrlsByPath(path) → {String}

parse the cubetexture url
Parameters:
Name Type Description
path String the cubetexture path.
Source:
Returns:
Type
String
Example
let ret1 = THING.Utils.parseCubeTextureUrlsByPath();
let ret2 = THING.Utils.parseCubeTextureUrlsByPath('./tempPath');
let ret3 = THING.Utils.parseCubeTextureUrlsByPath('./tempPath/');
// @expect(ret1 == null && ret2[0] == './tempPath/posx.jpg' && ret3[0] == './tempPath/posx.jpg')

(static) parseFlyParam(param) → {Object}

parse fly param.
Parameters:
Name Type Description
param Object The fly param.
Source:
Returns:
Type
Object
Example
let ret1 = THING.Utils.parseFlyParam(100);
let obj = new THING.BaseObject();
let ret2 = THING.Utils.parseFlyParam(obj);
// @expect(ret1 == 100 && ret2.target == obj);

(static) parseLerpType(name) → {function}

parse lerp type
Parameters:
Name Type Description
name String | Object the lerp type or function.
Source:
Returns:
Type
function
Example
let func1 = THING.Utils.parseLerpType('quadratic.in');
let ret1 = func1(10);
let customFunc = (amount) => {return amount + amount;};
let func2 = THING.Utils.parseLerpType(customFunc);
let ret2 = func2(10);
let func3 = THING.Utils.parseLerpType('temp');
let ret3 = func3(10);
// @expect(ret1 == 100 && ret2 == 20 && ret3 == 10)

(static) parseLoopType(name) → {String}

parse loop type.
Parameters:
Name Type Description
name String The loop type.
Source:
Returns:
Type
String
Example
let ret1 = THING.Utils.parseLoopType('repeat');
let ret2 = THING.Utils.parseLoopType('pingpong');
let ret3 = THING.Utils.parseLoopType('temp');
// @expect(ret1 == 'Repeat' && ret2 == 'PingPong' && ret3 == '');

(static) parseMouseButtonType(value)

When run action.
Parameters:
Name Type Description
value number The button type.
Source:
Example
let type = THING.Utils.parseMouseButtonType(1);
// @expect(type == 'Middle');

(static) registerClassType(classType, type)

Register class type.
Parameters:
Name Type Description
classType * The class type.
type String The type name, it would define property with 'is${type}' as getter.
Source:
Example
THING.Utils.registerClassType(MyObject, 'MyObject');

(static) saveAsFile(fileName, data) → {Boolean}

Save data as file.
Parameters:
Name Type Description
fileName String The file name.
data String | Blob | Image The file data.
Source:
Returns:
Type
Boolean
Example
let data = JSON.stringify('{ name: "Nice to meet you"}');
THING.Utils.saveAsFile('test.json', data);

(static) saveAsImage(width, height, pixelBuffer) → {Object}

Save data as image.
Parameters:
Name Type Description
width Number The image width in pixel.
height Number The image height in pixel.
pixelBuffer Uint8Array The image pixel buffer.
Source:
Returns:
Type
Object
Example
// Save 32x32 image with random pixel color
const width = 32, height = 32;
let pixelBuffer = [];
for(let y = 0; y < height; y++) {
	for(let x = 0; x < width * 4; x++) {
		pixelBuffer[y * width * 4 + x] = THING.Math.randomInt(0, 255);
	}
}
THING.Utils.saveAsImage(width, height, new Uint8Array(pixelBuffer));

(static) uniqueObjects(objects)

unique the object array. process the repeat and inherit object
Parameters:
Name Type Description
objects Array.<Object> object array.
Source:
Example
let object1 = new THING.Object3D();
let object2 = new THING.Object3D();
object1.add(object2);
let objArr = [object1, object2, object2];
let ret = THING.Utils.uniqueObjects(objArr);
// @expect(ret.length == 1 && ret[0] == object1)