Raycasting

[Last update: 01/30/2020]

Summary

Raycasting is a function that determines whether a user-specified Cubism mesh intersects with arbitrary coordinates.
It is possible to obtain the coordinates of a click/tap or a hit detection between any object in the scene and a specified mesh.
For more information on how to use Raycasting, please click here.

Raycasting in Cubism SDK for Unity consists of two major components.

  1. Components for specifying ArtMesh to be judged
  2. Component that performs hit detection between input coordinates and the specified ArtMesh

 

1. Components for specifying ArtMesh to be judged

Use CubismRaycastable to specify the mesh to be used for the hit detection.

CubismRaycastable is used by attaching it to GameObjects placed under [Prefab root]/Drawables/.
The ArtMesh with the same ID as the GameObject to which it is attached is used for hit detection.

CubismRaycastable has a parameter that sets the accuracy of its mesh hit detection.

CubismRaycastable.Precision has two values that can be set, each as follows.

  • BoundingBox

A rectangle with four horizontal or vertical sides that encloses the mesh is used as the hit criterion.
Depending on the mesh geometry, it may also take hits at coordinates outside the mesh, but it offers better performance than Triangles.

  • Triangles

The shape of the mesh is used as the range of the perturbation.
It is less performant than the BoundingBox, but it can make judgments within an accurate range.

 

 

2. Component that performs hit detection between input coordinates and the specified ArtMesh

CubismRaycaster is used to obtain the actual percussion.
When using CubismRaycaster, attach to the root of Cubism's Prefab.

Gets a reference to all CubismRaycastables attached to the Prefab during initialization of the CubismRaycaster.
When a hit mesh is added/removed during execution, CubismRaycaster.Refresh() is called to reacquire the reference.

 

To get a hit from the coordinates, use CubismRaycaster.Raycast().

 

CubismRaycaster.Raycast() returns the number of meshes that get hit detection in the return value.
In addition, information on the mesh from which the hit decision was obtained is set to the instance of the CubismRaycastHit[] type passed as an argument.

CubismRaycaster.Raycast() retrieves up to the number of elements of an instance of type CubismRaycastHit[] if multiple meshes overlap on the same coordinates.
If there are more overlapping meshes than the number of elements, the results will not be retrieved for the overlying meshes.

CubismRaycastHit is a structure with information about the mesh from which the hit was obtained.

  • Drawable

This is a reference to the ArtMesh from which the hit decision was obtained.

  • Distance

The distance from the specified coordinates.
The linear distance between origin or ray.origin passed as an argument to CubismRaycaster.Raycast() and Transform.position of the Drawable.

  • LocalPosition

These are the local coordinates of the ArtMesh from which the hit decision was obtained.

  • WorldPosition

These are the world coordinates of the ArtMesh from which the hit was obtained.

© 2010 - 2022 Live2D Inc.