Utils()

Utils实用函数。

Methods

static clearCaches()

清除缓存

static colorToHex(value) → {number}

将颜色数组转换为十六进制数字。
Parameters:
Name Type Description
value Array.<number> 颜色数组 [r, g, b],范围为 [0, 1]。
Returns:
number - 十六进制数字。
Example
let color = [1, 0, 0]; // 红色
let hex = THING.Utils.colorToHex(color);
// @expect(hex === 0xFF0000);

static colorToHexString(value) → {string}

将颜色数组转换为十六进制字符串。
Parameters:
Name Type Description
value Array.<number> 颜色数组 [r, g, b],范围为 [0, 1]。
Returns:
string - 十六进制字符串,如 '#FF0000'。
Example
let color = [1, 0, 0]; // 红色
let hexString = THING.Utils.colorToHexString(color);
// @expect(hexString === '#ff0000');

static createObject(type, options) → {object}

根据类型创建对象
Parameters:
Name Type Description
type string 对象类型。
options object 创建选项。
Returns:
object
Example
let line = THING.Utils.createObject('PixelLine');
let ret = line instanceof THING.PixelLine;
// @expect(ret == true)

static equalsColor(c1, c2, epsilonopt) → {boolean}

检查两个颜色在 epsilon 范围内是否相等。
Parameters:
Name Type Attributes Default Description
c1 Array.<number> 第一个颜色数组 [r, g, b],范围为 [0, 1]。
c2 Array.<number> 第二个颜色数组 [r, g, b],范围为 [0, 1]。
epsilon number <optional>
0.0001 epsilon 值。
Returns:
boolean - 如果颜色相等则返回 true。
Example
let color1 = [1, 0, 0];
let color2 = [1, 0, 0];
let equal = THING.Utils.equalsColor(color1, color2);
// @expect(equal === true);

static getPointerMoveThreshold() → {number}

获取鼠标移动判断的阈值距离。
Returns:
number - 阈值距离(像素)。
Example
let threshold = THING.DOM.Utils.getPointerMoveThreshold();

static getRegisteredComponent(name) → {object}

根据名称获取已注册的组件
Parameters:
Name Type Description
name string 组件名称
Returns:
object - 组件类
Example
// 注册一个按钮组件
class ButtonComponent {
  constructor() {
    this.type = 'button';
    this.text = '点击我';
  }

  onClick() {
    console.log('按钮被点击');
  }
}
THING.Utils.registerComponent('ButtonComponent', ButtonComponent);

// 获取注册的组件
const ButtonClass = THING.Utils.getRegisteredComponent('ButtonComponent');
if (ButtonClass) {
  const button = new ButtonClass();
  button.onClick(); // 输出: 按钮被点击
}

static getRegisteredComponents() → {object}

获取已注册的组件
Returns:
object - 组件。
Example
// 获取所有已注册的组件
const components = THING.Utils.getRegisteredComponents();

// 遍历所有组件
for (const name in components) {
  console.log('组件名称:', name);
  console.log('组件类:', components[name]);

  // 创建组件实例
  const instance = new components[name]();
}

// 检查特定组件是否已注册
if (components['ButtonComponent']) {
  console.log('按钮组件已注册');
}

// 获取已注册组件的数量
const count = Object.keys(components).length;
console.log('已注册组件数量:', count);

static isColorString(value) → {boolean}

检查值是否为有效的颜色格式。
Parameters:
Name Type Description
value * 要检查的值。
Returns:
boolean - 如果值是有效的颜色格式则返回 true。
Example
// 数组格式
THING.Utils.isColorString([1, 0, 0]); // true
// 十六进制数字
THING.Utils.isColorString(0xFF0000); // true
// 十六进制字符串
THING.Utils.isColorString('#FF0000'); // true
// RGB 字符串
THING.Utils.isColorString('rgb(255,0,0)'); // true
// RGBA 字符串
THING.Utils.isColorString('rgba(255,0,0,1)'); // true
// 颜色关键字
THING.Utils.isColorString('red'); // true

static isCompressedTexture(url) → {boolean}

检查是否为压缩纹理。
Parameters:
Name Type Description
url string 压缩纹理的URL。
Returns:
boolean
Example
let compress = THING.Utils.isCompressedTexture('image.dds');
let uncompress = THING.Utils.isCompressedTexture('image.png');
// @expect(compress == true && uncompress == false);

static isModelSceneExtension(ext)

检查是否为gltf或glb文件
Parameters:
Name Type Description
ext string 文件扩展名。
Example
let ret1 = THING.Utils.isModelSceneExtension('gltf');
let ret2 = THING.Utils.isModelSceneExtension('json');
// @expect(ret1 == true && ret2 == false)

static isModelSceneUrl(url)

检查是否为gltf或glb文件
Parameters:
Name Type Description
url string 场景的URL。
Example
let ret1 = THING.Utils.isModelSceneUrl('a.gltf');
let ret2 = THING.Utils.isModelSceneUrl('b.json');
// @expect(ret1 == true && ret2 == false)

static isValidTexture(value)

检查是否为有效的纹理
Parameters:
Name Type Description
value object 纹理对象。
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)

加载二进制文件。
Parameters:
Name Type Description
resource object 资源对象。
Example
let resource = document.createElement('video');;
let check = THING.Utils.isVideoResource(resource);
// @expect(check == true);

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

加载二进制文件。
Parameters:
Name Type Description
url string 文件的URL。
onLoad function 加载完成时的回调函数。
onProgress function 加载过程中的回调函数。
onError function 加载错误时的回调函数。
options LoadFileOptions 选项。
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>}

异步加载二进制文件。
Parameters:
Name Type Description
url string 文件的URL。
options LoadFileOptions 选项。
Returns:
Promise.<any>
Deprecated:
  • 2.7
Example
// 加载'my-file.bin'文件并等待加载完成。
await THING.Utils.loadBinaryFileAsync('my-file.bin');

static loadCode(code, options)

加载代码。
Parameters:
Name Type Description
code string 代码字符串。
options object 选项。
Properties
Name Type Description
es6Mode boolean 是否以es6模式加载代码。
sourceURL string 同步模式下的源URL。
onLoad function 加载完成的回调函数。
onError function 发生错误的回调函数。
Example
THING.Utils.loadCode('console.log("代码加载完成")');

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

异步加载代码。
Parameters:
Name Type Description
code string 代码字符串。
options object 选项。
Properties
Name Type Description
es6Mode boolean 是否以es6模式加载代码。
sourceURL string 同步模式下的源URL。
Returns:
Promise.<any>
Deprecated:
  • 2.7
Example
await THING.Utils.loadCodeAsync('console.log("代码加载完成")');

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

加载文件。
Parameters:
Name Type Description
resource string | Array.<string> 文件的资源URL。
options object | Array.<string> 选项。
Properties
Name Type Attributes Default Description
onLoad function 加载完成的回调函数。
onError function 发生错误的回调函数。
cache boolean <optional>
true 是否使用缓存。
inOrder boolean <optional>
true 是否按顺序加载文件。
Returns:
Promise.<any>
Example
// 通过加载和错误回调函数加载'my-lib.js'文件
THING.Utils.loadFile('my-lib.js', {
	onLoad: function() {
		console.log('加载完成');
	},
	onError: function() {
		console.error('加载失败');
	}
});

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

异步加载文件(们)。
Parameters:
Name Type Description
resource string | Array.<string> 文件的资源URL。
options object | Array.<string> 选项。
Properties
Name Type Attributes Default Description
cache boolean <optional>
true 是否使用缓存。
inOrder boolean <optional>
true 是否按顺序加载文件。
Returns:
Promise.<any>
Deprecated:
  • 2.7
Example
// 加载'my-lib.js'文件并等待加载完成
await THING.Utils.loadFileAsync('my-lib.js');

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

加载图片文件。
Parameters:
Name Type Description
url string 图片文件的URL。
onLoad function 加载完成时的回调函数。
onProgress function 加载过程中的回调函数。
onError function 加载错误时的回调函数。
options
Example
// 通过加载回调函数加载图片资源。
THING.Utils.loadImageFile('./assets/images/blue.png',
	function(image) {
		console.log(image);
	}
);

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

异步加载图片文件
Parameters:
Name Type Description
url string 图片地址
options
Returns:
Promise.<any>
Deprecated:
  • 2.7
Example
// Load image resource and wait to load finished.
await THING.Utils.loadImageFileAsync('image.png');

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

加载JSON文件。
Parameters:
Name Type Description
url string 文件的URL。
onLoad function 加载完成时的回调函数。
onProgress function 加载过程中的回调函数。
onError function 加载错误时的回调函数。
options LoadFileOptions 选项。
Example
// 以JSON格式加载'my-file.json'文件。
THING.Utils.loadJSONFile('my-file.json',
	function(data) {
		console.log('加载完成', data);
	},
	function() {
		console.error('加载失败');
	}
);

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

异步加载JSON文件。
Parameters:
Name Type Description
url string | Array.<string> 文件的URL。
options LoadFileOptions 选项。
Returns:
Promise.<any>
Deprecated:
  • 2.7
Example
// 加载'my-file.json'文件并等待加载完成。
await THING.Utils.loadJSONFileAsync('my-file.json');

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

加载文本文件。
Parameters:
Name Type Description
url string 文件的URL。
onLoad function 加载完成时的回调函数。
onProgress function 加载过程中的回调函数。
onError function 加载错误时的回调函数。
options LoadFileOptions 选项。
Example
// 以文本格式加载'my-file.txt'文件。
THING.Utils.loadTextFile('my-file.txt',
	function(data) {
		console.log('加载完成', data);
	},
	function() {
		console.error('加载失败');
	}
);

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

异步加载文本文件。
Parameters:
Name Type Description
url string 文件的URL。
options LoadFileOptions 选项。
Returns:
Promise.<any>
Deprecated:
  • 2.7
Example
// 加载'my-file.txt'文件并等待加载完成。
await THING.Utils.loadTextFileAsync('my-file.txt');

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

登录。
Parameters:
Name Type Description
options string | LoginOptions URL或选项。
Returns:
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 parseColor(value, defaultValue, targetopt) → {Array}

解析颜色值并返回 RGBA 数组。
Parameters:
Name Type Attributes Description
value Array | number | string 颜色值,可以是数组、十六进制数字或字符串格式。
defaultValue Array 默认返回值,当解析失败时返回。
target Array <optional>
目标数组,用于存储解析后的颜色值。
Returns:
Array - 返回 RGBA 数组,范围为 [0~1],如果解析失败则返回 defaultValue。
Example
// 解析颜色值
let color = THING.Utils.parseColor('#FF0000');
// @expect(color === [1, 0, 0]);
// 解析颜色值
let color = THING.Utils.parseColor('rgb(255,0,0)');
// @expect(color === [1, 0, 0]);
// 解析颜色值
let color = THING.Utils.parseColor('hsl(120, 100%, 50%)');
// @expect(color === [0, 1, 0]);

static parseCubeTextureUrlsByPath(path) → {string}

解析立方体纹理的URL。
Parameters:
Name Type Description
path string 立方体纹理的路径。
Returns:
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}

解析飞行参数。
Parameters:
Name Type Description
param object 飞行参数。
Returns:
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}

解析插值类型
Parameters:
Name Type Description
name string | object 插值名称。
Returns:
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}

解析循环类型。
Parameters:
Name Type Description
name string 循环类型。
Returns:
string
Example
let ret1 = THING.Utils.parseLoopType('repeat');
let ret2 = THING.Utils.parseLoopType('pingpong');
let ret3 = THING.Utils.parseLoopType('temp');
// @expect(ret1 == '重复' && ret2 == '来回' && ret3 == '');

static parseMouseButtonType(value)

当执行动作时。
Parameters:
Name Type Description
value number 按钮类型。
Example
let type = THING.Utils.parseMouseButtonType(1);
// @expect(type == '中键');

static registerClass(clsName, cls, out, force) → {boolean}

注册类。
Parameters:
Name Type Default Description
clsName string 类名。
cls * 类型。
out * 附加对象。
force boolean true 是否强制覆盖已存在的类。
Returns:
boolean - 是否注册成功。
Example
class MyClass {};
	THING.Utils.registerClass('MyClass', MyClass);
	let cls = THING.Utils.getRegisteredClass('MyClass');
	// @expect(cls != null);

static registerComponent(clsName, cls, out, force) → {boolean}

注册组件。
Parameters:
Name Type Default Description
clsName string 类名。
cls * 类型。
out object 附加对象。
force boolean true 是否强制覆盖已存在的组件。
Returns:
boolean - 是否注册成功。
Example
// 定义一个自定义组件类
class ColorComponent {
  constructor() {
    this.type = 'color';
    this.value = '#000000';
  }

  setColor(color) {
    this.value = color;
  }
}

// 注册组件
THING.Utils.registerComponent('ColorComponent', ColorComponent);

// 使用注册的组件
const ColorClass = THING.Utils.getRegisteredComponent('ColorComponent');
const colorComp = new ColorClass();
colorComp.setColor('#FF0000');

static saveAsFile(fileName, data) → {boolean}

保存数据为文件。
Parameters:
Name Type Description
fileName string 文件名。
data string | Blob | Image 文件数据。
Returns:
boolean
Example
let data = JSON.stringify('{ name: "很高兴见到你" }');
THING.Utils.saveAsFile('test.json', data);

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

保存数据为图片。
Parameters:
Name Type Description
width number 图片的宽度(像素)。
height number 图片的高度(像素)。
pixelBuffer Uint8Array 图片的像素缓冲区。
Returns:
object
Example
// 保存一个32x32像素的图片,像素颜色随机
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 setPointerMoveThreshold(threshold)

设置鼠标移动判断的阈值距离。
Parameters:
Name Type Description
threshold number 阈值距离(像素),默认值为2。
Example
THING.DOM.Utils.setPointerMoveThreshold(5);

static unregisterComponent(name)

注销组件
Parameters:
Name Type Description
name string 组件名称
Example
// 注册一个测试组件
class TestComponent {
  constructor() {
    this.name = 'TestComponent';
  }
}
THING.Utils.registerComponent('TestComponent', TestComponent);

// 注销组件
THING.Utils.unregisterComponent('TestComponent');

// 尝试获取已注销的组件
const comp = THING.Utils.getRegisteredComponent('TestComponent');
console.log(comp); // undefined