Class: App

THING.App(param)

App The main application.

Constructor

new App(param)

The application constructor.
Parameters:
Name Type Description
param AppOptions The initial parameters.
Source:
Example
// Load bundle scene
var app = new THING.App({
	 url: './scene-bundle',
	 onComplete: (e) = {
    console.log(e);
  }
});

// Load gltf scene
var app = new THING.App({
  url: './gltf/scene.gltf',
  onComplete: (e) => {
    console.log(e);
  }
});

Extends

Members

background :Number|String|Array.<Number>|THING.ImageTexture|THING.VideoTexture

Get/Set the background.
Type:
Source:
Example
THING.App.current.background = 'gray';
let ret = THING.App.current.background instanceof Array;
// @expect(ret == true )

camera :THING.Camera

Get/Set the camera.
Type:
Source:
Example
let camera = new THING.Camera();
camera.far = 100;
THING.App.current.camera = camera;
let ret = THING.App.current.camera.far == 100;
// @expect(ret == true)

(readonly) components :Map.<String, THING.BaseComponent>

Get all components.
Type:
Overrides:
Source:
Example
let obj = new THING.BaseObject();;
obj.addComponent(new THING.BaseComponent(), 'component1');
obj.addComponent(new THING.BaseComponent(), 'component2');
// @expect(obj.components.size == 2)

container :Object

Get the container.
Type:
  • Object
Source:
Example
//the html div id is div3d
let id = THING.App.current.container.id;
let ret = id == 'div3d';
// @expect(ret == true);

envMap :THING.CubeTexture

Get/Set the default environment map of scene.
Type:
Source:
Example
THING.App.current.background = new THING.CubeTexture('./skyboxes/bluesky');
let ret = THING.App.current.background.url == './skyboxes/bluesky';
// @expect(ret == true )

isApp :Boolean

Check class type.
Type:
  • Boolean
Source:
Example
if (app.isApp) {
		console.log(`It's app`);
	}

level :THING.LevelManager

Get the level manager.
Type:
Source:
Example
let app = THING.App.current;
let level = app.level;
let target = app.query('.Entity')[0];
level.change(target, {
  onComplete: function(){
		let ret = level.current == target;
		// @expect(ret == true);
  }
});

picker :THING.Picker

Get the picker.
Type:
Source:
Example
let picker = app.picker;
// @expect(picker != null)
picker.enable = false;
// @expect(picker.enable == false)

pixelRatio :Number

Get/Set pixel ratio. Set/Get Pixel Ratio The default is 1, and it can be set to a value between 0-1. The larger the value, the clearer the rendering effect (lower frame rate), The smaller the value, the blurrier the rendering (increased frame rate) On mobile devices, in order to render the frame rate, app.pixelRatio can be set to a value less than 1.
Type:
  • Number
Source:

(readonly) relationships :Array.<THING.Relationship>

get all relationships
Type:
Source:
Example
let app = THING.App.current;
 let count1 = app.relationships.length;
 app.relationshipManager.addRelationship({
		source: app.root,
	    target: app.root
 })
 let count2 = app.relationships.length;
 let ret = count2 - count1;
 // @expect(ret == 1)

root :THING.RootObject

Get the root.
Type:
Source:
Example
let root = THING.App.current.root;
 // @expect(root.isRootObject == true)

size :Array.<Number>

Get the size.
Type:
  • Array.<Number>
Source:
Example
//the div3d size is 1024*768
let size = THING.App.current.size;
	 // @expect(size[0] == 1024 && size[1] == 768);

tween :THING.TweenManager

Get the tween manager.
Type:
Source:

(static) current :THING.App

The current application.
Type:
Source:
Example
let app = THING.App.current;
let ret = app.picker != null;
// @expect(ret == true);

Methods

addComponent(component, name, args?) → {Boolean}

Add component.
Parameters:
Name Type Description
component THING.BaseComponent | Object The component class or component object.
name String The name.
args? Object The initial arguments to create component.
Overrides:
Source:
Returns:
Type
Boolean
Example
let obj = new THING.BaseObject();;
obj.addComponent(new THING.BaseComponent(), 'myComponent');
// @expect(obj.myComponent != null)

captureScreenshotToData(width, height) → {Uint8Array}

Capture screen shot into pixel buffer in RGBA color format.
Parameters:
Name Type Description
width Number The image width in pixel, if it not provide then use the current width.
height Number The image height in pixel, if it not provide then use the current height.
Source:
Returns:
Type
Uint8Array
Example
let data = THING.App.current.captureScreenshotToData(640, 480);
 let ret = data.length == (640 * 480 * 4);
 // @expect(ret == true);

captureScreenshotToFile(fileName, width, height)

Capture screen shot into file.
Parameters:
Name Type Description
fileName String The file name.
width Number The image width in pixel.
height Number The image height in pixel.
Source:
Example
THING.App.current.captureScreenshotToFile('cameraCapture', 640, 480);

captureScreenshotToImage(width, height) → {Object}

Capture screen shot into image.
Parameters:
Name Type Description
width Number The image width in pixel.
height Number The image height in pixel.
Source:
Returns:
Type
Object
Example
let image = THING.App.current.captureScreenshotToImage(640, 480);
	let ret1 = image instanceof Image;
 let ret2 = image.width == 640 && image.height == 480;
 THING.Utils.setTimeout(() => {
	// @expect(ret1 == true && ret2 == true);
	}, 100);

create(param) → {THING.BaseObject}

Create Object
Parameters:
Name Type Description
param Object The parameter list
Properties
Name Type Description
type String Object type
options? Object Object create parameters
Source:
Returns:
Type
THING.BaseObject
Example
let app =  THING.App.current;
let box = app.create({
  type: 'Box',
  name: 'box',
  position: [1, 1, 1],
  onComplete: function() {
  	console.log('box01 created!');
  }
});
let ret = box instanceof THING.Box;
// @expect(ret == true);

dispose()

Dispose.
Source:

find(condition) → {THING.BaseObject}

Find children by conditions and returns the first one.
Parameters:
Name Type Description
condition String The conditions.
Source:
Returns:
Type
THING.BaseObject
Example
let obj = THING.App.current.find('.BaseObject');
 let ret = obj instanceof THING.BaseObject;
// @expect(ret == true)
 let cylinder = THING.App.current.find('.Cylinder');
 ret = cylinder == null;
// @expect(ret == true)

getAllComponents() → {Array.<THING.BaseComponent>}

Get all components(it would create all registered components).
Overrides:
Source:
Returns:
Type
Array.<THING.BaseComponent>
Example
let obj = new THING.BaseObject();;
obj.addComponent(new THING.BaseComponent(), 'component1');
obj.addComponent(new THING.BaseComponent(), 'component2');
let components = obj.getAllComponents();
// @expect(components.length == 2)

getComponentByName(name) → {THING.BaseComponent}

Get component by name.
Parameters:
Name Type Description
name String The name.
Overrides:
Source:
Returns:
Type
THING.BaseComponent
Example
let obj = new THING.BaseObject();;
obj.addComponent(new THING.BaseComponent(), 'myComponent');
let component = obj.getComponentByName('myComponent');
// @expect(component != null)

getComponentByType(type) → {THING.BaseComponent}

Get component by type.
Parameters:
Name Type Description
type * The component type.
Overrides:
Source:
Returns:
Type
THING.BaseComponent
Example
let obj = new THING.BaseObject();;
obj.addComponent(new THING.BaseComponent(), 'myComponent');
let component = obj.getComponentByType(THING.BaseComponent);
// @expect(component != null)

getComponentsByType(type) → {Array.<THING.BaseComponent>}

Get components by type.
Parameters:
Name Type Description
type * The component type.
Overrides:
Source:
Returns:
Type
Array.<THING.BaseComponent>
Example
let obj = new THING.BaseObject();;
obj.addComponent(new THING.BaseComponent(), 'component1');
obj.addComponent(new THING.BaseComponent(), 'component2');
let components = obj.getComponentsByType(THING.BaseComponent);
// @expect(components.length == 2)

hasComponent(name) → {Boolean}

Check whether has component.
Parameters:
Name Type Description
name String The name.
Overrides:
Source:
Returns:
Type
Boolean
Example
let obj = new THING.BaseObject();;
obj.addComponent(new THING.BaseComponent(), 'myComponent');
let ret = obj.hasComponent('myComponent')
// @expect(ret == true)

load(url, options) → {Promise}

Load scene file
Parameters:
Name Type Description
url String | Object The load URL or options.
options Object The load options.
Properties
Name Type Description
url String The resource URL.
dynamic Boolean Dynamic loading scene.
hidden Boolean Hidden loading.
useDefaultTheme Boolean Use defalt theme for scene. (The last registered theme is used by default)
useDefaultViewpoint Boolean Use default viewpoint for scene file.
useDefaultRenderSettings Boolean Use default render settings for scene file.
position Array Set the scene position.
rotation Array Set the scene rotation.
onComplete function The load complete callback function.
onProgress function The progress callback function.
onError function The error callback function.
Source:
Returns:
- A Promise that resolves when the loading is complete.
Type
Promise
Example
THING.App.current.load('./assets/scenes/scene.gltf').then((ev) => {
    let objs = ev.objects;
	   let count =  objs.length;
   // @expect(count > 0);
})

loadImageTexture(url, sampler) → {THING.ImageTexture}

Load image texture from URL.
Parameters:
Name Type Description
url String The resource url.
sampler LoadTextureResourceSamplerInfo The sampler info.
Source:
Returns:
Type
THING.ImageTexture
Example
let image = THING.App.current.loadImageTexture('./flower.png');
	await image.waitForComplete();
	console.log(image);

loadPlugin(url, options) → {Promise}

Load plugin
Parameters:
Name Type Description
url String | Object The load URL or options.
options Object The load options.
Properties
Name Type Description
url String The plugin URL.
name String The plugin name.
onComplete function The load complete callback function.
onError function The error callback function.
Source:
Returns:
Type
Promise
Example
THING.App.current.loadPlugin('./assets/plugins/plugin.json').then((plugin)=>{
	console.log(plugin);
});

off(type, condition, tag)

Unregister event.
Parameters:
Name Type Description
type String The event type.
condition String The condition to select objects.
tag String The event tag.
Source:
Example
let app = THING.App.current;
let markOff = 0;
app.on('testOff', function(ev) {
		markOff = 1;
});
app.trigger('testOff');
// @expect(markOff == 1);
markOff = 0;
app.off('testOff');
app.trigger('testOff');
// @expect(markOff == 0);

on(type, condition, callback, tag, priority, options)

Register event.
Parameters:
Name Type Description
type String The event type.
condition String The condition to select objects.
callback EventCallback The callback function.
tag String The event tag.
priority Number The priority value(default is 0, higher value will be processed first)
options Object The options.
Properties
Name Type Description
useCapture Boolean True indicates capture all same events from children.
Source:
Example
let app = THING.App.current;
let markOn = 0;
app.on('testOn', function(ev) {
		markOn = 1;
});
app.trigger('testOn');
// @expect(markOn == 1);

once(type, condition, callback, tag, priority, options)

Register event what just trigger once time.
Parameters:
Name Type Description
type String The event type.
condition String The condition to select objects.
callback EventCallback The callback function.
tag String The event tag.
priority Number The priority value(default is 0, higher value will be processed first)
options Object The options.
Properties
Name Type Description
useCapture Boolean True indicates capture all same events from children.
Source:
Example
let app = THING.App.current;
let markOnce = 0;
app.once('testOnce', function(ev) {
		markOnce = 1;
});
app.trigger('testOnce');
// @expect(markOnce == 1);
markOnce = 0;
app.trigger('testOnce');
// @expect(markOnce == 0);

pauseEvent(type, condition, tag) → {Boolean}

Pause event.
Parameters:
Name Type Description
type String The event type.
condition String The condition to select objects.
tag String The event tag.
Source:
Returns:
Type
Boolean
Example
let app = THING.App.current;
let markPause = 0;
app.on('testPause', function(ev) {
		markPause = 1;
});
app.trigger('testPause');
// @expect(markPause == 1);
markPause = 0;
app.pauseEvent('testPause');
app.trigger('testPause');
// @expect(markPause == 0);

query(condition, options) → {THING.Selector}

Query children by condition.
Parameters:
Name Type Description
condition String The condition to select objects.
options ObjectQueryOptions The options.
Source:
Returns:
Type
THING.Selector
Example
let obj = THING.App.current.query('.BaseObject');
 let ret = obj[0] instanceof THING.BaseObject;
// @expect(ret == true)
 let cylinder = THING.App.current.query('.Cylinder');
 ret = cylinder.length == 0;
// @expect(ret == true)

queryById(condition, options) → {THING.Selector}

Query children by id.
Parameters:
Name Type Description
condition String The condition to select objects.
options Object The options.
Source:
Returns:
Type
THING.Selector
Example
let car = THING.App.current.queryById('1')
	let ret = car.length == 0;
 // @expect(ret == true);
 car = THING.App.current.queryById('car01')
 ret = car[0].id == 'car01';
 // @expect(ret == true)

queryByName(condition, options) → {THING.Selector}

Query children by name.
Parameters:
Name Type Description
condition String The condition to select objects.
options Object The options.
Source:
Returns:
Type
THING.Selector
Example
let car = THING.App.current.queryByName('car01')
	let ret = car.length == 0;
	// @expect(ret == true)
 car = THING.App.current.queryByName('car1')
 ret = car[0].name == 'car1';
// @expect(ret == true)

queryByRegExp(condition, options) → {THING.Selector}

Query children by reg exp.
Parameters:
Name Type Description
condition String The condition to select objects.
options Object The options.
Source:
Returns:
Type
THING.Selector
Example
let car = THING.App.current.queryByRegExp(/car/);
	let ret = car.length == 4;
 //@expect(ret == true)

queryByTags(condition, options) → {THING.Selector}

Query children by tag.
Parameters:
Name Type Description
condition String The condition to select objects.
options Object The options.
Source:
Returns:
Type
THING.Selector
Example
let car = THING.App.current.queryByTags('car');
	let ret = car.length == 0;
 // @expect(ret == true)
	let entity = THING.App.current.queryByTags('Entity');
	ret = entity[0].tags.has('Entity');
 // @expect(ret == true)

queryByType(condition, options) → {THING.Selector}

Query children by type.
Parameters:
Name Type Description
condition String The condition to select objects.
options Object The options.
Source:
Returns:
Type
THING.Selector
Example
let car = THING.App.current.queryByType('car')
	let ret = car.length == 0;
// @expect(ret == true)
 let obj = THING.App.current.queryByType('Entity')
 ret = obj[0].type == 'Entity';
// @expect(ret == true)

queryByUUID(condition, options) → {THING.Selector}

Query children by uuid.
Parameters:
Name Type Description
condition String The condition to select objects.
options Object The options.
Source:
Returns:
Type
THING.Selector
Example
let car = app.queryByUUID('1')
	let ret = car.length == 0;
 // @expect(ret == true);
 car = app.queryByUUID('1605')
 ret = car[0].uuid == '1605';
 // @expect(ret == true)

queryByUserData(condition, options) → {THING.Selector}

Query children by userData.
Parameters:
Name Type Description
condition String The condition to select objects.
options Object The options.
Source:
Returns:
Type
THING.Selector
Example
let car = THING.App.current.queryByUserData('test=1');
	let ret = car.length == 0;
 // @expect(ret == true)
 car = THING.App.current.queryByUserData('id=666');
 ret = car[0].userData.id == 666;
// @expect(ret == true)

queryRelationships(options) → {Array.<Relationship>}

Query relationships.
Parameters:
Name Type Description
options Object The options.
Properties
Name Type Description
type String The type of relationship.
name String The name of relationship.
Source:
Returns:
Type
Array.<Relationship>
Example
let lightSwitch = new THING.Box();
let light = new THING.Box();
let rel = new THING.Relationship({
     type: 'control',
     source: lightSwitch,
     target: light
});
let relationships = app.queryRelationships({type: 'control'});
let ret1 = relationships[0].source == lightSwitch;
let ret2 = relationships[0].target == light;
// @expect(ret1 == true && ret2 == true);

removeAllComponents()

Remove all components.
Overrides:
Source:
Example
let obj = new THING.BaseObject();;
obj.addComponent(new THING.BaseComponent(), 'component1');
obj.addComponent(new THING.BaseComponent(), 'component2');
obj.removeAllComponents();
// @expect(obj.components.size == 0)

removeComponent(name)

Remove component.
Parameters:
Name Type Description
name String The name.
Overrides:
Source:
Example
let obj = new THING.BaseObject();;
obj.addComponent(new THING.BaseComponent(), 'myComponent');
obj.removeComponent('myComponent');
// @expect(obj.components.size == 0)

resumeEvent(type, condition, tag) → {Boolean}

Resume event.
Parameters:
Name Type Description
type String The event type.
condition String The condition to select objects.
tag String The event tag.
Source:
Returns:
Type
Boolean
Example
let app = THING.App.current;
let markResume = 0;
app.on('testResume', function(ev) {
		markResume = 1;
});
app.trigger('testResume');
// @expect(markResume == 1);
markResume = 0;
app.pauseEvent('testResume');
app.trigger('testResume');
// @expect(markResume == 0);
app.resumeEvent('testResume');
app.trigger('testResume');
// @expect(markResume == 1);

traverseComponentByType(type, callback)

Traverse component by type.
Parameters:
Name Type Description
type * The component type.
callback TraverseComponentByTypeCallback The callback function.
Overrides:
Source:
Example
object.addComponent(new MyComponent(), 'myComponent');
	object.traverseComponentByType(MyComponent, (component, name) => {
		console.log(component, name);
	});

trigger(type, ev, options) → {*}

Trigger event.
Parameters:
Name Type Description
type String The event type.
ev Object The event info.
options Object The options.
Source:
Returns:
Type
*
Example
let markTrigger = {};
	THING.App.current.on('myEvent', function(ev) {
		markTrigger = ev;
	});
	THING.App.current.trigger('myEvent', { result: true });
 let ret = markTrigger.result;
 // @expect(ret == true);

uninstall(name) → {Boolean}

Uninstall plugin by name.
Parameters:
Name Type Description
name String The plugin name.
Source:
Returns:
Type
Boolean
Example
THING.App.current.uninstall(plugin.name);

(static) addCompleteCallback(callback, priority)

Add a callback to the end of the app instantiation
Parameters:
Name Type Default Description
callback function The callback.
priority Number 0 The priority. (The default is 0, the smaller one is executed first)
Source:
Example
THING.App.addCompleteCallback((app)=>{
		console.log(The app instantiation is complete.);
},-10);