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:
- Create raycaster: new ModelRayCaster(capi, () => yaw, () => pitch, () => scale, -1000)
- Load hitboxes: rayCaster.LoadHitboxes(hitboxes)
- 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
capiICoreClientAPIClient API
getYawFunc<float>Function returning current Y rotation (yaw) in radians
getPitchFunc<float>Function returning current X rotation (pitch) in radians
getScaleFunc<float>Function returning current model scale
nearPlaneZdoubleZ coordinate of the near plane (typically -1000)
getScreenPositionFunc<(float x, float y)>Optional function returning screen position (x, y). Defaults to screen center if null.
flipYboolWhether 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
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
hitPositionVec3dhitboxElementHitboxuvVec2f
Returns
LoadHitboxes(List<ElementHitbox>)
Loads hitboxes for AABB-based raycasting.
public void LoadHitboxes(List<ElementHitbox> hitboxes)
Parameters
hitboxesList<ElementHitbox>
OnRenderFrame(float, EnumRenderStage, IShaderProgram)
Renders wireframe boxes for all hitboxes.
public void OnRenderFrame(float deltaTime, EnumRenderStage stage, IShaderProgram shader = null)
Parameters
deltaTimefloatstageEnumRenderStageshaderIShaderProgram
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
screenXintscreenYinthitboxElementHitboxhitPositionVec3d
Returns
SetAnimatorMatrices(Func<float[]>)
Sets the animator matrices accessor so raycasting accounts for joint animations.
public void SetAnimatorMatrices(Func<float[]> getAnimatorMatrices)
Parameters
SetModelOriginOffset(float, float, float)
Sets the model origin offset (applied in model space after scale).
public void SetModelOriginOffset(float x, float y, float z)