3D model class
unknown
csharp
a year ago
6.7 kB
8
Indexable
// 3D model class
class Pito3DModel
{
Model _model;
Texture2D _texture;
Vector3 _position;
Vector3 _rotation;
BoundingBox _bounds;
float _scale = 1f;
JsonData _keys;
String _name;
bool _selected = false;
PitoLevelEditorGlobal EditorApp = PitoLevelEditorGlobal.Instance();
public Model model
{
get { return _model; }
set
{
Raylib.UnloadModel(_model);
_model = value;
unsafe
{
_model.Materials[0].Maps[0].Texture = _texture;
}
}
}
public String name
{
get { return _name; }
set { _name = value; }
}
public Texture2D texture
{
get { return _texture; }
set
{
Raylib.UnloadTexture(_texture);
_texture = value;
unsafe
{
_model.Materials[0].Maps[0].Texture = _texture;
}
}
}
public Vector3 position
{
get { return _position; }
set { _position = value; }
}
public Vector3 rotation
{
get { return _rotation; }
set { _rotation = value; }
}
public float scale
{
get { return _scale; }
set { _scale = value; }
}
public JsonData keys
{
get { return _keys; }
set { _keys = value; }
}
public bool selected
{
get { return _selected; }
set
{
_selected = value;
if (value)
{
EditorApp.RayForm.BlockInspector(false);
EditorApp.RayForm.sceneListBox.SelectedItem = name;
EditorApp.RayForm.transfromX.Value = (decimal)position.X;
EditorApp.RayForm.transfromY.Value = (decimal)position.Y;
EditorApp.RayForm.transfromZ.Value = (decimal)position.Z;
EditorApp.RayForm.rotationX.Value = (decimal)rotation.X;
EditorApp.RayForm.rotationY.Value = (decimal)rotation.Y;
EditorApp.RayForm.rotationZ.Value = (decimal)rotation.Z;
}
else
{
EditorApp.RayForm.BlockInspector(true);
EditorApp.RayForm.sceneListBox.SelectedIndex = -1;
EditorApp.RayForm.transfromX.Value = 0;
EditorApp.RayForm.transfromY.Value = 0;
EditorApp.RayForm.transfromZ.Value = 0;
EditorApp.RayForm.rotationX.Value = 0;
EditorApp.RayForm.rotationY.Value = 0;
EditorApp.RayForm.rotationZ.Value = 0;
}
}
}
public Pito3DModel(string model_path, string texture_path, Vector3 position, Vector3 rotation, float scale, JsonData keys)
{
if (!File.Exists(model_path))
{
MessagesAndHints.ShowErrorMessage($"Model doesn't exist!\n\n{model_path}");
return;
}
if (!File.Exists(texture_path))
{
MessagesAndHints.ShowErrorMessage($"Texture doesn't exist!\n\n{texture_path}");
return;
}
model = Raylib.LoadModel(model_path);
texture = Raylib.LoadTexture(texture_path);
unsafe
{
model.Materials[0].Maps[0].Texture = texture;
}
this.position = position;
this.rotation = rotation;
this.scale = scale;
this.keys = keys;
name = Path.GetFileName(model_path).Replace(".obj", "");
selected = false;
unsafe
{
this._bounds = Raylib.GetModelBoundingBox(model);
}
}
void _scale_model()
{
Rlgl.Scalef(scale, scale, scale);
}
void _rotate_model()
{
if (rotation.X != 0)
{
Rlgl.Rotatef(rotation.X, 1f, 0f, 0f);
}
else
{
Rlgl.Rotatef(0f, 1f, 0f, 0f);
}
if (rotation.Y != 0)
{
Rlgl.Rotatef(rotation.Y, 0f, 1f, 0f);
}
else
{
Rlgl.Rotatef(0f, 0f, 1f, 0f);
}
if (rotation.Z != 0)
{
Rlgl.Rotatef(rotation.Z, 0f, 0f, 1f);
}
else
{
Rlgl.Rotatef(0f, 0f, 0f, 1f);
}
}
void _translate_model()
{
if (position.X != 0)
{
Rlgl.Translatef(position.X, 0f, 0f);
}
else
{
Rlgl.Translatef(0f, 0f, 0f);
}
if (position.Y != 0)
{
Rlgl.Translatef(0f, position.Y, 0f);
}
else
{
Rlgl.Translatef(0f, 0f, 0f);
}
if (position.Z != 0)
{
Rlgl.Translatef(0f, 0f, position.Z);
}
else
{
Rlgl.Translatef(0f, 0f, 0f);
}
}
void _update_bbox_transform()
{
_bounds.Min = Raymath.Vector3Scale(_bounds.Min, scale);
_bounds.Max = Raymath.Vector3Scale(_bounds.Max, scale);
_bounds.Min = Raymath.Vector3Add(_bounds.Min, position);
_bounds.Max = Raymath.Vector3Add(_bounds.Max, position);
}
public void Draw()
{
if (Raylib.IsMouseButtonPressed(MouseButton.Left))
{
Ray collisionRay = Raylib.GetScreenToWorldRay(Raylib.GetMousePosition(), EditorApp.RayForm.editorCamera);
// Check collision between ray and box
if (Raylib.GetRayCollisionBox(collisionRay, _bounds).Hit)
{
selected = !selected;
}
else
{
selected = false;
}
Raylib.DrawRay(collisionRay, Color.Maroon);
}
if (selected)
{
Rlgl.PushMatrix();
// transform
_rotate_model();
_translate_model();
_scale_model();
// draw
Raylib.DrawModel(model, position, scale, Color.White);
Raylib.DrawBoundingBox(_bounds, Color.Red);
Rlgl.PopMatrix();
}
else
{
Rlgl.PushMatrix();
// transform
_rotate_model();
_translate_model();
_scale_model();
// draw
Raylib.DrawModel(model, position, scale, Color.White);
Rlgl.PopMatrix();
}
}
public void Delete()
{
Raylib.UnloadTexture(texture);
Raylib.UnloadModel(model);
}
}Editor is loading...
Leave a Comment