util

Overview

Common functions.

Name Description Returns Parameters
addEventListener Add an event none util.addEventListener(eventType, callback)
clearAllEvents Clear all events none util.clearAllEvents()
clearAllTimers Clear all timers and unnecessary Timer IDs none util.clearAllTimers()
clearInterval Clear the timer generated by setInterval, need timerID none util.”util.clearInterval(intervalID)
clearScriptObjects Clear all objects created by scripts, including objects and GUIs none util.clearScriptObjects()
clearTimeout Clear timers generated by setTimeout. The Timer’s ID needs to be provided. none util.clearTimeout(timeoutID)
downloadTexture Download a texture from an external URL none util.downloadTexture({json}
downloadTextures Download multiple textures form an external URL none util.downloadTextures({json})
randomColor Generate a random RGBA type color RGBA util.randomColor()
randomFloat Generate a random float number between two specified values float util.randomFloat(a,b)
randomInt Generate a random integer between two specified values int util.randomInt(a,b)
randomVector3 Generate a random Vector 3 Vector3 util.randomVector3(randius)
setInterval Call a function after a defined time Int util.setInterval(callback,tickTime)
setRenderCallback Call a callback function every frame none util.setRenderCallback(callback)
setTimeout call a callback function when timeout, return timer ID int util.setTimeout(callback,delayTime)

util.addEventListener

Add event listener.

Parameters

Name Description
eventType string event type, e.g. click,dbclick,mouseup,mousedown,mousemove,drag,dragstart,dragend,keydown,keyup,resize
callback function callback function

Example

1
2
 //Add a listener to the event'click', this will create an object on a mouse click
 util.addEventListener("click", function(event) {object.create("FF2A3E364B1E4B928891E05A9279C7A7", event.pos);});

util.clearAllEvents

Parameters

None.

Example

1
2
 //Clear all the events within the scene
 util.clearAllEvents()

util.clearAllTimers

Parameters

None.

Example

1
2
 //Clear all the timers within the scene
 util.clearAllTimers()

util.clearInterval

Remove timer created by setInterval.

Parameters

Name Description
timerID number timer ID

Example

1
2
 //remove timer with ID 2
 util.clearInterval(2)

util.clearScriptObjects

Parameters

None.

Example

1
2
 //Clear all the script associate with object
 util.clearScriptObjects()

util.downloadTexture

Download texture from an external URL

Parameters

:header: Name, Description :widths: 5, 15
{json} json message ; including url , callback function

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
 /** Create a cube, download a texture from url,
 if the download is successful, set the cube's texture to earMat, and earMat to be earth's material
 (the texture is provided by uinnova, details on creating and using custom texture ,
 please contact uinnova inc.) */

 var earth = object.create("B723E9E1B279467EBC9433D30D35F683", Vec3(0, 5, 0));

 util.downloadTexture({

     "url": " http://img1.juimg.com/141102/330507-141102164G965.jpg ",

     "success": function(texture) {

     var earthMat = util.createMaterial(texture);

     earth.setMaterial(earthMat); }});

util.clearTimeout

Remove timer created by setTimeout.

Parameters

Name Description
timerID number timer ID

Example

1
2
 //remove timer with ID 2
 util.clearTimeout(2)

util.downloadTextures

Download multiple texture from an external URL

Parameters

:header: Name, Description :widths: 5, 15
{json} json message ; including url , callback function

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 /** Download textures from an external URL, if the download issuccessful ,
 set “Earth.jpg” to earthMat and earthMat to be earth's material;
 set texture “Moon.jpg” to moonMat, and MoonMat to be moon's material.
 (the texture is provided by uinnova, details on creating and using custom texture ,
 please contact uinnova inc.) */

 var earth = object.create("9f5681fe55674ce9b617f9fa23d9729b", Vec3(0, 5, 0));

 var moon = object.create("9f5681fe55674ce9b617f9fa23d9729b",Vec3(0, 7, 0),Vec3(0.2, 0.2, 0.2));

 util.downloadTextures({

     "url": "http://www.3dmomoda.com/mmdclient/script/examples/demos/earth_moon.zip",

     "success": function(textures) {

     var earthMat = util.createMaterial(textures["Earth.jpg"]);

     earth.setMaterial(earthMat);

     var moonMat = util.createMaterial(textures["Moon.jpg"]);

     moon.setMaterial(moonMat);}});

util.randomColor

Generate a random RGHA color.

Parameters

None.

Example

1
2
 //Generate a random RGBA type color and apply it as the color of the object'obj'
 obj.setColor(util.randomColor())

util.randomFloat

Generate a random float number between two specified values

Parameters

Name Description
a float upper bound value
b float lower bound value

Example

1
2
 // Generate a random number between 1(included) and 3(included)
 var d = util.randomFloat(1.0,3.0)

util.randomInt

Generate a random integer number between two specified values

Parameters

Name Description
a int upper bound value
b in lower bound value

Example

1
2
 // Generate a random number between 1(included) and 10(included)
 var d = util.randomInt(1,10)

util.randomVector3

Generate a random Vector3

Parameters

Name Description
radius number vector radius

Example

1
2
 // Generate a random vector between ([1, -1], 1, -1])
 var d = util.randomVector3(1)

util.setRenderCallback

Create callback function run every frame

Parameters

Name Description
callback function

Example

1
2
3
4
5
6
7
8
 //Create an object and add a gravitational weight of 3KG every frame.
 util.setRenderCallback(function(){

 var obj = object.create("AB052B5B646E4A48B9C045096FF9B088");

 obj.addGravity(3);

 })

util.setTimeout

Create a callback function with timeout, return the timer’s ID

Parameters

Name Description
callback function
timeout number

Example

1
2
 // Print'time over!'when timer is equal to 3
 var a=util.setTimeout(function() {print("time over!")}, 3000)