Table of Contents

Class ModelRayCaster

Namespace
Vintagestory.API.Client
Assembly
VintagestoryAPI.dll

Performs raycasting against a 3D mesh to determine UV coordinates from screen clicks. Also supports AABB-based hitbox detection for precise element selection. Useful for making clickable 3D UI elements.

Usage:

  1. Create raycaster: new ModelRayCaster(capi, () => yaw, () => pitch, () => scale, -1000)
  2. Load hitboxes: rayCaster.LoadHitboxes(hitboxes)
  3. On click: rayCaster.RayCastToHitbox(mouseX, mouseY, out ElementHitbox hitbox) Open in GitHub
public class ModelRayCaster
Inheritance
ModelRayCaster
Inherited Members
Extension Methods

Constructors

ModelRayCaster(ICoreClientAPI, Func<float>, Func<float>, Func<float>, double, Func<(float, float)>, bool)

Creates a new ModelRayCaster.

public ModelRayCaster(ICoreClientAPI capi, Func<float> getYaw, Func<float> getPitch, Func<float> getScale, double nearPlaneZ, Func<(float, float)> getScreenPosition = null, bool flipY = false)

Parameters

capi ICoreClientAPI

Client API

getYaw Func<float>

Function returning current Y rotation (yaw) in radians

getPitch Func<float>

Function returning current X rotation (pitch) in radians

getScale Func<float>

Function returning current model scale

nearPlaneZ double

Z coordinate of the near plane (typically -1000)

getScreenPosition Func<(float x, float y)>

Optional function returning screen position (x, y). Defaults to screen center if null.

flipY bool

Whether the model Y axis is flipped

Methods

GetMouseRay(int, int, out Vec3d, out Vec3d)

Converts screen coordinates to a 3D ray (origin and direction). Uses orthographic projection for UI-space raycasting.

public void GetMouseRay(int screenX, int screenY, out Vec3d origin, out Vec3d direction)

Parameters

screenX int
screenY int
origin Vec3d
direction Vec3d

HitPositionToUV(Vec3d, ElementHitbox, out Vec2f)

Converts a 3D hit position on an AABB to UV coordinates (0-1 range).

public bool HitPositionToUV(Vec3d hitPosition, ElementHitbox hitbox, out Vec2f uv)

Parameters

hitPosition Vec3d
hitbox ElementHitbox
uv Vec2f

Returns

bool

LoadHitboxes(List<ElementHitbox>)

Loads hitboxes for AABB-based raycasting.

public void LoadHitboxes(List<ElementHitbox> hitboxes)

Parameters

hitboxes List<ElementHitbox>

OnRenderFrame(float, EnumRenderStage, IShaderProgram)

Renders wireframe boxes for all hitboxes.

public void OnRenderFrame(float deltaTime, EnumRenderStage stage, IShaderProgram shader = null)

Parameters

deltaTime float
stage EnumRenderStage
shader IShaderProgram

RayCastToHitbox(int, int, out ElementHitbox, out Vec3d)

Casts a ray from screen coordinates and tests against loaded AABB hitboxes. Returns the closest hit hitbox and the hit position.

public bool RayCastToHitbox(int screenX, int screenY, out ElementHitbox hitbox, out Vec3d hitPosition)

Parameters

screenX int
screenY int
hitbox ElementHitbox
hitPosition Vec3d

Returns

bool

SetAnimatorMatrices(Func<float[]>)

Sets the animator matrices accessor so raycasting accounts for joint animations.

public void SetAnimatorMatrices(Func<float[]> getAnimatorMatrices)

Parameters

getAnimatorMatrices Func<float[]>

SetModelOriginOffset(float, float, float)

Sets the model origin offset (applied in model space after scale).

public void SetModelOriginOffset(float x, float y, float z)

Parameters

x float
y float
z float