Skip to main content

ClothVisual

Class

Simulates and renders cloth visuals in a Lens.

  • Available on all platforms
Platform details: Snapchat, SPECS, Camera Kit Web, Camera Kit Mobile fully supported.

Platform details

  • Snapchat — Fully supported.

  • SPECS — Fully supported.

  • Camera Kit Web — Fully supported.

  • Camera Kit Mobile — Fully supported.

Index

Remarks

Handles the mesh data of cloth and prepares it for cloth simulation. Also controls all the parameters of the cloth simulator and colliders.

Example

// @input Component.ClothVisual clothVisual
// @input Asset.RenderMesh mesh
// @input Asset.Material material

var clothVisual = script.clothVisual;

clothVisual.bendMode = ClothVisual.BendMode.Isometric;
clothVisual.mesh = script.mesh;
clothVisual.mainMaterial = script.material;
clothVisual.gravity = new vec3(0, -100, 0);
clothVisual.stretchStiffness = 0.1;
clothVisual.bendStiffness = 0.1;
clothVisual.friction = 0.1;
clothVisual.repulsionEnabled = true;
clothVisual.repulsionOffset = 0.1;
clothVisual.repulsionStiffness = 0.1;
clothVisual.repulsionFriction = 0.1;
clothVisual.iterationsPerStep = 60;
clothVisual.frameRate = 30;
clothVisual.colliders = script.Colliders;
clothVisual.debugModeEnabled = false;
clothVisual.drawCollider = true;
clothVisual.updateNormalsEnabled = true;
clothVisual.mergeCloseVerticesEnabled = true;
clothVisual.onInitialized = clothInitCallback;
clothVisual.updatePriority = 0;

function clothInitCallback(clothVisualArg) {
clothVisualArg.resetSimulation();
}

Extends

Properties

bendMode

bendMode: BendMode

Select bend mode to use (Isometric bending/Linear bending).

Default Value

ClothVisual.BendMode.Isometric

bendStiffness

bendStiffness: number

BendStiffness value.

Since

Lens Scripting Version 149

Default Value

0.1

bendStiffnessVertexWeight

bendStiffnessVertexWeight: number

BendStiffness value weight on all the VertexSettings.

Since

Lens Scripting Version 149

Default Value

1

colliders

colliders: ColliderComponent[]

List of colliders assigned.

Since

Lens Scripting Version 149

Default Value

[]

enabled

enabled: boolean

If disabled, the Component will stop enacting its behavior.

Default Value

true

Inherited from

MaterialMeshVisual.enabled


extentsTarget

extentsTarget: ScreenTransform

When a ScreenTransform is present on this SceneObject, and extentsTarget is a child of this SceneObject, extentsTarget will be repositioned to match the exact area this MeshVisual is being rendered. Very useful for Image and Text components.

Default Value

null

Inherited from

MaterialMeshVisual.extentsTarget


externalBodyMeshWeight

externalBodyMeshWeight: number

The influence of the external body mesh acting on the cloth visual.

Since

Lens Scripting Version 167

Default Value

1

friction

friction: number

Friction value.

Since

Lens Scripting Version 149

Default Value

0

frictionVertexWeight

frictionVertexWeight: number

Friction value weight on all the VertexSettings.

Since

Lens Scripting Version 149

Default Value

1

gravity

gravity: vec3

Gravity force vector.

Since

Lens Scripting Version 149

Default Value

(0, -10, 0)

horizontalAlignment

horizontalAlignment: HorizontalAlignment

When a ScreenTransform is attached to the same SceneObject, this controls how the mesh will be positioned horizontally depending on stretchMode.

Default Value

HorizontalAlignment.Center

Inherited from

MaterialMeshVisual.horizontalAlignment


isBeingDestroyed

readonly isBeingDestroyed: boolean

Returns true if this Component is currently being destroyed. Becomes true as soon as destruction begins via Component#destroy or destruction of its owning SceneObject, before any OnDestroyEvent callbacks fire.

Since

Lens Scripting Version 364

Example

if (!script.isBeingDestroyed) {
// Component is still fully alive
}

Inherited from

MaterialMeshVisual.isBeingDestroyed


isEnabledInHierarchy

readonly isEnabledInHierarchy: boolean

Returns true if this Component, its SceneObject, and all of that SceneObjects parents are enabled.

Inherited from

MaterialMeshVisual.isEnabledInHierarchy


iterationsPerStep

iterationsPerStep: number

Number of simulation iterations to perform per each step. Higher number contributes to higher quality, but can be difficult for device performance.

Default Value

10

mainMaterial

mainMaterial: Material

Returns the first Material.

Since

Lens Scripting Version 304

Default Value

null

Inherited from

MaterialMeshVisual.mainMaterial


mainPass

mainPass: Pass

Returns the mainPass of the mainMaterial.

Since

Lens Scripting Version 304

Default Value

null

Inherited from

MaterialMeshVisual.mainPass


mass

mass: number

Mass value.

Since

Lens Scripting Version 149

Default Value

1

massVertexWeight

massVertexWeight: number

Mass value weight on all the VertexSettings.

Since

Lens Scripting Version 149

Default Value

1

materials

materials: Material[]

Get the array of materials used by the MaterialMeshVisual.

Since

Lens Scripting Version 304

Default Value

[]

Inherited from

MaterialMeshVisual.materials


maxAcceleration

maxAcceleration: number

Control for the acceleration on motion of vertices in cloth simulation.

Since

Lens Scripting Version 149

Default Value

10000

mergeCloseVerticesEnabled

mergeCloseVerticesEnabled: boolean

Whether to merge close vertices (Lens Studio might split vertices when loading FBX mesh). Changing this resets the simulation.

Since

Lens Scripting Version 149

Default Value

true

mergeCloseVerticesThreshold

mergeCloseVerticesThreshold: number

Threshold of close vertices. Changing this value resets the simulation.

Since

Lens Scripting Version 149

Default Value

1e-6

mesh

mesh: RenderMesh

Attached mesh.

Since

Lens Scripting Version 149

Default Value

null

meshShadowMode

meshShadowMode: MeshShadowMode

None = 0, Caster = 1, Receiver = 2

Default Value

MeshShadowMode.None

Inherited from

MaterialMeshVisual.meshShadowMode


onInitialized

onInitialized: (clothVisual) => void

Function called when the ClothVisual is initialized.

Parameters

ParameterType
clothVisualClothVisual

Returns

void

Since

Lens Scripting Version 149

Default Value

null

propertyOverrides

readonly propertyOverrides: PropertyOverrides

Overrides the property on the material, without affecting other visuals referencing the same material. See PropertyOverrides.

Since

Lens Scripting Version 304

Inherited from

MaterialMeshVisual.propertyOverrides


renderOrder

renderOrder: number

The order in which the visual should be rendered.

Since

Lens Scripting Version 323

Default Value

0

Inherited from

MaterialMeshVisual.renderOrder


repulsionEnabled

repulsionEnabled: boolean

Indicates whether we will enable collision repulsion with collider models.

Since

Lens Scripting Version 149

Default Value

false

repulsionFriction

repulsionFriction: number

Collision friction to dampen relative motion.

Since

Lens Scripting Version 149

Default Value

0

repulsionOffset

repulsionOffset: number

Indicates the offset we set when the cloth mesh is too close to the colliders.

Since

Lens Scripting Version 149

Default Value

0

repulsionStiffness

repulsionStiffness: number

Indicates the stiffness of repulsion when collision.

Since

Lens Scripting Version 149

Default Value

1

sceneObject

readonly sceneObject: SceneObject

The SceneObject this component is on.

Inherited from

MaterialMeshVisual.sceneObject


shadowColor

shadowColor: vec4

Affects the color of shadows being cast by this MeshVisual. The color of the cast shadow is a mix between shadowColor and the material's base texture color. The alpha value of shadowColor controls the mixing of these two colors, with 0 = shadowColor and 1 = shadowColor * textureColor.

Default Value

(1, 1, 1, 1)

Inherited from

MaterialMeshVisual.shadowColor


shadowDensity

shadowDensity: number

Density of shadows cast by this MeshVisual.

Default Value

1

Inherited from

MaterialMeshVisual.shadowDensity


simulatedMesh

readonly simulatedMesh: RenderMesh

Returns the modified simulated mesh which can be used in another RenderMeshVisual if the same simulated mesh is needed. Useful when creating effects that might require the same mesh to be rendered twice, such as with a mirror effect. Prevents the need to run a simulation twice.

Since

Lens Scripting Version 167


stretchMode

stretchMode: StretchMode

When a ScreenTransform is attached to the same SceneObject, this controls how the mesh will be stretched relative to the ScreenTransform's boundaries.

Default Value

StretchMode.Stretch

Inherited from

MaterialMeshVisual.stretchMode


stretchStiffness

stretchStiffness: number

StretchStiffness Value.

Since

Lens Scripting Version 149

Default Value

0.5

stretchStiffnessVertexWeight

stretchStiffnessVertexWeight: number

StretchStiffness Value weight on all the VertexSettings.

Since

Lens Scripting Version 149

Default Value

1

uniqueIdentifier

readonly uniqueIdentifier: string

Since

Lens Scripting Version 176

Inherited from

MaterialMeshVisual.uniqueIdentifier


updateNormalsEnabled

updateNormalsEnabled: boolean

Whether to update normals for the cloth mesh each frame in order to get reflection update.

Since

Lens Scripting Version 149

Default Value

false

verticalAlignment

verticalAlignment: VerticalAlignment

When a ScreenTransform is attached to the same SceneObject, this controls how the mesh will be positioned vertically depending on stretchMode.

Default Value

VerticalAlignment.Center

Inherited from

MaterialMeshVisual.verticalAlignment

Accessors

mainPassOverrides

Get Signature

get mainPassOverrides(): PassPropertyOverrides

Overrides the mainPass on the material, without affecting other visuals referencing the same material. Reads give a PassPropertyOverrides. See PropertyOverrides.

Since

Lens Scripting Version 304

Default Value
{}
Returns

PassPropertyOverrides

Set Signature

set mainPassOverrides(value): void

Parameters
ParameterType
valuePassPropertyOverrides | {[key: string]: any; }
Returns

void

Inherited from

MaterialMeshVisual.mainPassOverrides

Methods

addCollider()

addCollider(colliderComponent): void

Adds a collider to the list of colliders.

Parameters

ParameterType
colliderComponentColliderComponent

Returns

void

Since

Lens Scripting Version 149


addMaterial()

addMaterial(material): void

Adds a Material to use for rendering.

Parameters

ParameterType
materialMaterial

Returns

void

Since

Lens Scripting Version 304

Inherited from

MaterialMeshVisual.addMaterial


clearColliders()

clearColliders(): void

Clears the colliders list.

Returns

void

Since

Lens Scripting Version 149


clearMaterials()

clearMaterials(): void

Clears all Materials.

Returns

void

Since

Lens Scripting Version 304

Inherited from

MaterialMeshVisual.clearMaterials


createVertexSettings()

static createVertexSettings(): VertexSimulationSettings

Creates a new instance of vertex simulation settings.

Returns

VertexSimulationSettings

Since

Lens Scripting Version 149


destroy()

destroy(): void

Destroys the component.

Returns

void

Inherited from

MaterialMeshVisual.destroy


getAllColors()

getAllColors(): vec4[]

Returns all available vertex colors on cloth mesh.

Returns

vec4[]

Since

Lens Scripting Version 149


getMaterial()

getMaterial(index): Material

Returns the Material at index index.

Parameters

ParameterType
indexnumber

Returns

Material

Since

Lens Scripting Version 304

Inherited from

MaterialMeshVisual.getMaterial


getMaterialsCount()

getMaterialsCount(): number

Returns the number of Materials used for rendering.

Returns

number

Since

Lens Scripting Version 304

Inherited from

MaterialMeshVisual.getMaterialsCount


getPointColorByIndex()

getPointColorByIndex(index): vec4

Returns the vertex color by vertex index.

Parameters

ParameterType
indexnumber

Returns

vec4

Since

Lens Scripting Version 149


getPointIndicesByColor()

getPointIndicesByColor(color, colorMask): number[]

Returns all the indices of vertices that are labeled by this color.

Parameters

ParameterType
colorvec4
colorMaskvec4b

Returns

number[]

Since

Lens Scripting Version 149


getPointIndicesByMask()

getPointIndicesByMask(colorMask): number[]

Returns all the indices on the cloth mesh that are matching the color mask. Match means that the color has value on the channels which in colorMask is true.

Parameters

ParameterType
colorMaskvec4b

Returns

number[]

Since

Lens Scripting Version 149


getRenderOrder()

getRenderOrder(): number

Returns the order of this Visual in the render queue.

Returns

number

Inherited from

MaterialMeshVisual.getRenderOrder


getSceneObject()

getSceneObject(): SceneObject

Returns the SceneObject the component is attached to.

Returns

SceneObject

Inherited from

MaterialMeshVisual.getSceneObject


getTransform()

getTransform(): Transform

Returns the Transform this component is attached to.

Returns

Transform

Inherited from

MaterialMeshVisual.getTransform


getTypeName()

static getTypeName(): TypeName<ClothVisual>

Returns the fully-qualified JavaScript type name for this class (for example, "Component.Camera"). This is a static accessor (no instance required) and returns the same value as the instance getTypeName().

Returns

TypeName<ClothVisual>

Overrides

MaterialMeshVisual.getTypeName


getTypeName()

getTypeName(): string

Returns the name of this object's type.

Returns

string

Inherited from

MaterialMeshVisual.getTypeName


getVertexBinding()

getVertexBinding(index): SceneObject

Gets binding SceneObject of the vertex.

Parameters

ParameterType
indexnumber

Returns

SceneObject

Since

Lens Scripting Version 149


getVertexSettings()

getVertexSettings(index): VertexSimulationSettings

Gets simulation settings of the vertex.

Parameters

ParameterType
indexnumber

Returns

VertexSimulationSettings

Since

Lens Scripting Version 149


isHardwareSupported()

isHardwareSupported(): boolean

Returns true if the Cloth Simulation feature is supported by the current device.

Returns

boolean

Since

Lens Scripting Version 149


isInitialized()

isInitialized(): boolean

Returns true if the Cloth Simulation and resources are initialized. Always return false if device is not supported.

Returns

boolean

Since

Lens Scripting Version 149


isOfType()

isOfType(type): boolean

Returns true if the object matches or derives from the passed in type.

Parameters

ParameterType
typestring

Returns

boolean

Inherited from

MaterialMeshVisual.isOfType


isSame()

isSame(other): boolean

Returns true if this object is the same as other. Useful for checking if two references point to the same thing.

Parameters

ParameterType
otherScriptObject

Returns

boolean

Inherited from

MaterialMeshVisual.isSame


localAabbMax()

localAabbMax(): vec3

Range maximum of the local-space axis-aligned bounding box (AABB) of the visual.

Returns

vec3

Since

Lens Scripting Version 156

Inherited from

MaterialMeshVisual.localAabbMax


localAabbMin()

localAabbMin(): vec3

Range minimum of the local-space axis-aligned bounding box (AABB) of the visual.

Returns

vec3

Since

Lens Scripting Version 156

Inherited from

MaterialMeshVisual.localAabbMin


removeColliderByIndex()

removeColliderByIndex(index): ColliderComponent

Removes collider by its index and returns the removed collider.

Parameters

ParameterType
indexnumber

Returns

ColliderComponent

Since

Lens Scripting Version 149


resetSimulation()

resetSimulation(): void

Resets the cloth simulation.

Returns

void

Since

Lens Scripting Version 149


setRenderOrder()

setRenderOrder(value): void

Sets the order of this Visual in the render queue.

Parameters

ParameterType
valuenumber

Returns

void

Inherited from

MaterialMeshVisual.setRenderOrder


setVertexBinding()

setVertexBinding(index, bindingObj): void

Sets the binding SceneObject for the vertex.

Parameters

ParameterType
indexnumber
bindingObjSceneObject

Returns

void

Since

Lens Scripting Version 149


setVertexSettings()

setVertexSettings(index, vertexSettings?): void

Sets simulation settings of the vertex.

Parameters

ParameterType
indexnumber
vertexSettings?VertexSimulationSettings

Returns

void

Since

Lens Scripting Version 149


snap()

snap(camera): void

Projects screen positions from camera's view onto the mesh's UVs. If the MeshVisual's material uses the same texture as the camera input, the MeshVisual will look identical to the part of the screen it covers.

Parameters

ParameterType
cameraCamera

Returns

void

Inherited from

MaterialMeshVisual.snap


worldAabbMax()

worldAabbMax(): vec3

Range maximum of the world-space axis-aligned bounding box (AABB) of the visual.

Returns

vec3

Since

Lens Scripting Version 156

Inherited from

MaterialMeshVisual.worldAabbMax


worldAabbMin()

worldAabbMin(): vec3

Range minimum of the world-space axis-aligned bounding box (AABB) of the visual.

Returns

vec3

Since

Lens Scripting Version 156

Inherited from

MaterialMeshVisual.worldAabbMin

See also

Cloth Simulation

Nested types

Enumerations

EnumerationDescription
BendModeCloth bend mode to use for cloth simulation.