1.0.2 • Published 6 years ago

altspacevrutil v1.0.2

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

AltspaceUtil

Provides helper functions, behaviors and A-Frame components for common functionality compatible with AltspaceSDK.

Usage

Include the utility library in your project:

<script src="https://cdn.rawgit.com/NGenesis/altspacevr-behaviors/v1.0.2/js/altspaceutil.min.js"></script>

API Reference

Functions

Behaviors

Events

A-Frame Components

Functions

altspaceutil.getFullspaceEnclosure

Gets the fullspace enclosure for the app.

Returns

TypeDescription
PromiseA promise that resolves to a fullspace Enclosure.

altspaceutil.getAbsoluteURL

Create an absolute URL from the specified relative URL, using the current host as the URL base.

Parameters

NameTypeDescription
urlStringA relative URL. Providing an absolute URL will return itself unchanged.

Returns

TypeDescription
StringAn absolute URL of the given relative URL.

altspaceutil.getBasePath

Create a base path from the specified file URL.

Parameters

NameTypeDescription
urlStringA URL to a file.

Returns

TypeDescription
StringA URL to the file's base path.

altspaceutil.isMobileApp

Indicates whether the app is being loaded on a mobile version of the Altspace client. This is typically determined by the user agent exposed to the app.

Returns

TypeDescription
BooleanWhether the app is running on a mobile client. Returns true for mobile clients, false otherwise.

altspaceutil.expandSerializationBuffer

Expands the Altspace client's serialization buffer to improve loading performance.

Parameters

NameTypeDescription
sizeNumberThe size to expand the serialization buffer by, in bytes.

altspaceutil.profileSerializationBuffer

Enables or disables the profiler for the Altspace client's serialization buffer to determine whether the buffer needs to be expanded. Profiler messages will be displayed in the console when enabled.

Parameters

NameTypeDescription
enabledBooleanSpecifies whether the serialization buffer is to be enabled.

altspaceutil.setCursorCollider

Sets the Altspace cursor collider property for the specified object.

Parameters

NameTypeDefaultDescription
object3dTHREE.Object3DAn object to set the cursor collider property for.
isCursorColliderBooleanSpecifies if the object is a cursor collider.
recursiveBooleanfalseSpecifies if the property change should also be applied recursively to all children of the object.

altspaceutil.isCursorCollider

Gets the Altspace cursor collider property for the specified object.

Parameters

NameTypeDescription
object3dTHREE.Object3DAn object to retrieve the cursor collider property from.

Returns

TypeDescription
BooleanWhether the object is a cursor collider.

altspaceutil.loadTexture

Loads a three.js texture from the specified texture file URL, optimizing for faster loading times in the Altspace client where appropriate.

Parameters

NameTypeDescription
urlStringA URL to a texture file.
configObjectOptional parameters to be passed to the texture loader (e.g. crossOrigin, withCredentials, path).

Returns

TypeDescription
THREE.TextureThe loaded texture.

Behaviors

altspaceutil.behaviors.NativeComponent (Example)

Provides support for AltspaceVR native components to be attached to objects, providing sane configuration defaults where appropriate.

Parameters

type - Native component type.

data - Native component properties. See here for available properties and defaults.

config - Optional parameters for specialized use cases. Many of these properties are provided only to override the internal behavior of a component in specialized use cases.

NameTypeDefaultDescription
targetEntityTHREE.Object3DnullAn object in the scene that a user will teleport to when entering a native portal.
useColliderBooleanfalseSpecifies whether cursor collision should be disabled on the native component.
sharedComponentBooleantrueSpecifies whether the native component should share the same THREE.Mesh instance with other native components.
recursiveMeshBooleanfalseSpecifies whether the native component is applied recursively to all THREE.Mesh children.
recursiveBooleanfalseSpecifies whether the native component is applied recursively to all children.
sendUpdatesBooleantrueSpecifies whether the native component should send updates to the native Altspace client.
updateOnStaleDataBooleantrueSpecifies whether the native component should send updates to the native Altspace client when a property has changed.
inheritParentDataBooleanfalseSpecifies whether the native component should inherit its property state from a parent component.
meshComponentBooleanfalseSpecifies whether the native component is treated as a mesh-specific component. This is used as a performance optimization to defer initialization of mesh-specific components (e.g. native parents and colliders) attached to placeholder objects, until another component is added that gives them functionality (e.g. text, spawner).

Methods

callComponentAction

Invokes an action associated with the native component, and does not provide a return value.

NameTypeDescription
actionNameStringThe action name to invoke on the native component.
actionArgsObjectArguments that will be passed along with the action when invoked.

callComponentFunc

Calls a function associated with the native component, and returns a promise of the value that will be returned by the native component function.

NameTypeDescription
functionNameStringThe function name to invoke on the native component.
functionArgsObjectArguments that will be passed to the function when called.

callComponent

This function has been Deprecated. See callComponentAction.

getAttribute

Retrieves an attribute associated with a native component. See here for available attributes.

NameTypeDescription
attributeNameStringAn attribute name associated with the native component.

Native Component Attributes

Attributes associated with a native component can be retrieved using the getAttribute method.

n-sound

NameTypeDescription
loadedBooleanIndicates that the component has been loaded by the AltspaceVR client.

n-gltf

NameTypeDescription
loadedBooleanIndicates that the component has been loaded by the AltspaceVR client.

n-container

NameTypeDescription
countNumberThe number of objects in the n-container.

Native Component Properties

n-object

data: {
	res: 'architecture/wall-4w-4h'
}

n-spawner

data: {
	res: 'interactables/basketball'
}

n-text

data: {
	text: '',
	fontSize: 10,
	width: 10,
	height: 1,
	horizontalAlign: 'middle',
	verticalAlign: 'middle'
}

n-sphere-collider

data: {
	isTrigger: false,
	center: { 'x': 0, 'y': 0, 'z': 0 },
	radius: 0,
	type: 'environment'
}

n-box-collider

data: {
	isTrigger: false,
	center: { 'x': 0, 'y': 0, 'z': 0 },
	size: { 'x': 0, 'y': 0, 'z': 0 },
	type: 'environment'
}

n-capsule-collider

data: {
	isTrigger: false,
	center: { 'x': 0, 'y': 0, 'z': 0 },
	radius: 0,
	height: 0,
	direction: 'y',
	type: 'environment'
}

n-mesh-collider

data: {
	isTrigger: false,
	convex: true,
	type: 'environment'
}

n-container

data: {
	capacity: 4
}

n-sound

data: {
	on: '',
	res: '',
	src: '',
	loop: false,
	volume: 1,
	autoplay: false,
	oneshot: false,
	spatialBlend: 1,
	pitch: 1,
	minDistance: 1,
	maxDistance: 12
}

n-skeleton-parent

data: {
	part: 'head',
	side: 'center',
	index: 0,
	userId: null // defaults to current user when omitted
}

n-cockpit-parent

No properties to be configured.

n-billboard

No properties to be configured.

n-layout-browser

data: {
	url: 'about:blank',
	isEnclosure: false
}

n-portal

data: {
	targetSpace: null, // defaults to current space when omited
	targetEvent: null, // defaults to current space when omited
	targetPosition: { x: 0, y: 0, z: 0 },
	targetQuaternion: { x: 0, y: 0, z: 0, w: 1 }
}

n-gltf

data: {
        sceneIndex: 0,
        url: ''
}

n-rigidbody

Experimental! This native component is not yet officially supported by the AltspaceSDK.

data: {
	mass: 1,
	drag: 0,
	angularDrag: 0.05,
	useGravity: true,
	isKinematic: false,
	positionConstraints: [false, false, false],
	rotationConstraints: [false, false, false],
}

altspaceutil.behaviors.NativeComponentSync (Example)

Provides support for AltspaceVR native component data to be synchronized over Firebase. The behavior must be used in conjunction with SceneSync, Object3DSync and a NativeComponent of the same type specified for NativeComponentSync.

Parameters

type - Type of native component to be synchronized. To retrieve the behavior using the getBehaviorByType method, prepend sync- to the name of the native component type (e.g. sync-n-text for an n-text native component).

config - Optional parameters.

NameTypeDefaultDescription
syncRefaltspace.utilities.behaviors.Object3DSyncnullA reference to the object syncing component. Defaults to using the syncing component of the object the behavior is attached to.

altspaceutil.behaviors.TransformControls (Example)

Enables an object's position, rotation and scale to be manipulated in AltspaceVR using a draggable transform gizmo.

Parameters

config - Optional parameters.

NameTypeDefaultDescription
controlTypeStringnoneThe default control type to be selected. Supported control types are none, position, rotate or scale.
showButtonsBooleanfalseSpecifies whether buttons should be displayed to toggle between control types.
followTargetBooleantrueSpecified whether the transform gizmo should follow the object that is being manipulated.
targetTHREE.Object3DnullThe target that the transform gizmo should manipulate when interacted with. If omitted, the object that the behavior is associated with will be used as the target.
scaleNumber1Adjusts the scale of the transform gizmo.
allowNegativeScaleBooleanfalseSpecifies whether the scale transform gizmo will allow the target's scale to be negative.
positionAxisLockObjectSpecifies which axes of the position gizmo can be displayed and manipulated.
positionAxisLock.xBooleantrueX axis of the position gizmo.
positionAxisLock.yBooleantrueY axis of the position gizmo.
positionAxisLock.zBooleantrueZ axis of the position gizmo.
rotateAxisLockObjectSpecifies which axes of the rotate gizmo can be displayed and manipulated.
rotateAxisLock.xBooleantrueX axis of the rotate gizmo.
rotateAxisLock.yBooleantrueY axis of the rotate gizmo.
rotateAxisLock.zBooleantrueZ axis of the rotate gizmo.
scaleAxisLockObjectSpecifies which axes of the scale gizmo can be displayed and manipulated.
scaleAxisLock.xBooleantrueX axis of the scale gizmo.
scaleAxisLock.yBooleantrueY axis of the scale gizmo.
scaleAxisLock.zBooleantrueZ axis of the scale gizmo.
disableCollidersBooleantrueSpecifies whether colliders on the target object should be disabled.
disableChildCollidersBooleantrueSpecifies whether colliders on the target's children should be disabled.

Events

transform-controls-dragmove

Fires an event when the transform gizmo is being dragged.

NameTypeDescription
behaviorTransformControlsThe behavior that controls the transform gizmo.
parentTHREE.Object3DThe object that the transform gizmo is parented to.
transformTargetTHREE.Object3DThe object that the transform gizmo will manipulate.
transformTypeStringThe type of transform being performed. Possible values are position, rotate and scale.
transformAxisStringThe axis that the transform is being performed on. Possible values are x, y, z (for single axes) and xyz (for all axes).
transformDeltaTHREE.Vector3The transform delta that was applied to the target object.

transform-controls-dragbegin

Fires an event when the transform gizmo starts being dragged.

NameTypeDescription
behaviorTransformControlsThe behavior that controls the transform gizmo.
parentTHREE.Object3DThe object that the transform gizmo is parented to.
transformTargetTHREE.Object3DThe object that the transform gizmo will manipulate.
transformTypeStringThe type of transform being performed. Possible values are position, rotate and scale.
transformAxisStringThe axis that the transform is being performed on. Possible values are x, y, z (for single axes) and xyz (for all axes).

transform-controls-dragend

Fires an event when the transform gizmo is no longer being dragged.

NameTypeDescription
behaviorTransformControlsThe behavior that controls the transform gizmo.
parentTHREE.Object3DThe object that the transform gizmo is parented to.
transformTargetTHREE.Object3DThe object that the transform gizmo will manipulate.
transformTypeStringThe type of transform being performed. Possible values are position, rotate and scale.
transformAxisStringThe axis that the transform is being performed on. Possible values are x, y, z (for single axes) and xyz (for all axes).

altspaceutil.behaviors.OrbitControls (Example)

Provides a convenience wrapper for THREE.OrbitControls when working with altspace.utilities.Simulation.

altspaceutil.behaviors.UserEvents (Example)

Subscribes to avatar and user preference update events for a given list of users, and dispatches events which have been triggered by a given user changing their avatar and/or account preferences.

Parameters

config - Optional parameters.

NameTypeDefaultDescription
userIdsString[]nullAn array of (legacy) User IDs for each user to dispatch events for. When omitted, only avatar change events for the user currently logged in will be handled.
onRequestDataonRequestDatanullA precondition callback returning a boolean that determines if a user should have their data requested. User data is requested if the callback returns true, otherwise no action is taken.
refreshTimeNumber5000Duration to wait between user updates, in milliseconds.
traceBooleanfalseSpecifies whether debugging information should be displayed.

Callbacks

onRequestData

A precondition callback returning a boolean that determines if a user should have their data requested. User data is requested if the callback returns true, otherwise no action is taken.

NameTypeDescription
userIdStringUser ID of a user who will have their data requested.
objectTHREE.Object3DThe object that will emit the request.

Methods

subscribeUser

Subscribe to receiving events for a given User ID.

NameTypeDescription
userIdStringUser ID to receive events for.

unsubscribeUser

Unsubscribe from receiving events for a given User ID.

NameTypeDescription
userIdStringUser ID to stop receiving events for.

Events

avatarchange

Fires an event when the user changes avatar preferences.

NameTypeDescription
userIdStringUser ID of the user.
avatarIdStringAvatar type identifier that was selected by the user.
avatarClassStringAvatar type classification. Typically one of Pod, Robothead or Rubenoid, or empty when unclassified.
colorsObjectTHREE.Color preferences of the avatar. This typically provides primary and highlight properties for Pod avatars, and highlight for Robothead avatars.
rawColorsObjectRaw color preferences of the avatar. This typically provides primary and highlight properties for Pod avatars, and highlight for Robothead avatars.
texturesObjectTexture identifier preferences for the avatar. This typically provides hair, skin and clothing properties for Rubenoid avatars.
targetTHREE.Object3DThe object which emitted the event.

userchange

Fires an event when the user changes account preferences.

NameTypeDescription
userIdStringUser ID of the user.
usernameStringUsername of the user.
displayNameStringDisplay name of the user.
targetTHREE.Object3DThe object which emitted the event.

avatarstatus

Fires an event when the user's connection status changes.

NameTypeDescription
userIdStringUser ID of the user.
displayNameStringDisplay name of the user.
onlineBooleanSpecifies whether user is currently logged in.
targetTHREE.Object3DThe object which emitted the event.

altspaceutil.behaviors.PreloadNativeSounds (Example)

Preloads the specified sound files used by n-sound to ensure the resources are cached for subsequent uses. The behavior will remove itself automatically once the sound files have been preloaded or the specified timeout threshold has been reached.

Parameters

sounds - Native sound resources to be preloaded. Can either be an array of sound file paths or an array of NativeComponent n-sound data objects (e.g. { src: 'file.wav', volume: 1.5 }).

config - Optional parameters.

NameTypeDefaultDescription
disposeBooleantrueSpecifies whether the preloaded native sound objects will be destroyed after the preload has completed. By default, preloaded sound objects will be destroyed after being cached.
timeoutNumber10000Time in milliseconds to wait before ending the preload. A n-sound-preloaded event will be fired with a timeout property set to true when the timeout threshold has been reached. Specifying a zero or negative value will disable the timeout.

Events

n-sound-preloaded

Fires an event once all sounds have been preloaded, or the specified timeout threshold has been reached.

NameTypeDescription
behaviorPreloadNativeSoundsThe behavior that preloaded the sounds.
soundsTHREE.Object3D[]The sound objects that were preloaded. Sound objects are children of the object the behavior is associated with.
timeoutBooleanIndicates whether the timeout threshold has been reached before all sounds were preloaded.

altspaceutil.behaviors.HoverMaterialOpacity (Example)

Changes the opacity of an object's material when the cursor hovers over it, and restores the original opacity when the cursor is no longer hovering over the object.

Parameters

config - Optional parameters.

NameTypeDefaultDescription
materialTHREE.MaterialnullA reference to the material whose opacity will be updated. Defaults to material of the object the behavior is attached to.
opacityNumber1The value that will be applied to the object's current material opacity when the cursor hovers over it.
beginDurationNumber75Duration the hovered opacity adjustment effect is intended to take to complete, in milliseconds.
endDurationNumber75Duration the unhovered opacity adjustment effect is intended to take to complete, in milliseconds.
revertOnDisposeBooleantrueSpecifies whether the object's original material opacity should be restored when the behavior has been destroyed.
eventListenerTHREE.Object3DnullSpecifies an optional object that will listen for cursor events. By default the object that the behavior is attached to will be used as the event listener.
hoverChildrenBooleantrueSpecifies whether hovering over children of the event listener object should invoke the hover effect.

altspaceutil.behaviors.HoverMaterialColor (Example)

Changes the color of an object's material when the cursor hovers over it, and restores the original color when the cursor is no longer hovering over the object.

Parameters

config - Optional parameters.

NameTypeDefaultDescription
materialTHREE.MaterialnullA reference to the material whose color will be updated. Defaults to material of the object the behavior is attached to.
colorTHREE.Colornew THREE.Color('yellow')The value that will be applied to the object's material color when the cursor hovers over it.
beginDurationNumber75Duration the hovered color adjustment effect is intended to take to complete, in milliseconds.
endDurationNumber75Duration the unhovered color adjustment effect is intended to take to complete, in milliseconds.
revertOnDisposeBooleantrueSpecifies whether the object's original material color should be restored when the behavior has been destroyed.
eventListenerTHREE.Object3DnullSpecifies an optional object that will listen for cursor events. By default the object that the behavior is attached to will be used as the event listener.
hoverChildrenBooleantrueSpecifies whether hovering over children of the event listener object should invoke the hover effect.

altspaceutil.behaviors.NativeTextMaterial (Example)

Updates the color and opacity of a n-text native component using a material source.

Parameters

config - Optional parameters.

NameTypeDefaultDescription
materialTHREE.MaterialnullA reference to the material whose properties will be applied to the n-text native component. Defaults to material of the object the behavior is attached to.
colorBooleantrueSpecifies whether the n-text native component should use the color of the source material.
opacityBooleantrueSpecifies whether the n-text native component should use the opacity of the source material.

altspaceutil.behaviors.TWEEN (Example)

Provides a convenience wrapper for tween.js to manage and update TWEEN and TWEEN.Group objects. Refer to the tween.js user guide for further information.

Parameters

NameTypeDefaultDescription
tweengroupTWEEN.GroupTWEENA tween group to be managed by the behavior. When ommitted, the global TWEEN object will be managed by the behavior.

A-Frame Components

altspace-transform-controls (Example)

Enables an object's position, rotation and scale to be manipulated in AltspaceVR using a draggable transform gizmo.

Properties

NameTypeDefaultDescription
control-typeStringnoneThe default control type to be selected. Supported control types are none, position, rotate or scale.
show-buttonsBooleanfalseSpecifies whether buttons should be displayed to toggle between control types.
follow-targetBooleantrueSpecified whether the transform gizmo should follow the object that is being manipulated.
targetSelectornullThe target that the transform gizmo should manipulate when interacted with. If omitted, the object that the behavior is associated with will be used as the target.
scaleNumber1Adjusts the scale of the transform gizmo.
allow-negative-scaleBooleanfalseSpecifies whether the scale transform gizmo will allow the target's scale to be negative.
sync-eventsBooleantrueSpecifies whether the sync ownership is gained when drag events are fired. Requires sync and sync-transform components be present on the target object.
position-axis-lockStringxyzSpecifies which axes of the position gizmo can be displayed and manipulated.
rotate-axis-lockStringxyzSpecifies which axes of the rotate gizmo can be displayed and manipulated.
scale-axis-lockStringxyzSpecifies which axes of the scale gizmo can be displayed and manipulated.
disable-collidersBooleantrueSpecifies whether colliders on the target object should be disabled.
disable-child-collidersBooleantrueSpecifies whether colliders on the target's children should be disabled.

n-text-material (Example)

Updates the color and opacity of a n-text native component using a material source.

Properties

NameTypeDefaultDescription
materialSelectorA reference to the object whose material properties will be applied to the n-text native component. Defaults to material of the object the component is attached to.
colorBooleantrueSpecifies whether the n-text native component should use the color of the source material.
opacityBooleantrueSpecifies whether the n-text native component should use the opacity of the source material.

Resources