Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System.Threading;
using Sandbox.ModelEditor.Nodes;
using System.Threading;

namespace Editor;

[DropObject( "model", "vmdl", "vmdl_c" )]
partial class ModelDropObject : BaseDropObject
{
Model model;
string archetype;
bool physicsArchetype;
bool physics;

protected override async Task Initialize( string dragData, CancellationToken token )
{
Expand All @@ -18,27 +20,33 @@ protected override async Task Initialize( string dragData, CancellationToken tok
if ( token.IsCancellationRequested )
return;

archetype = asset.FindStringEditInfo( "model_archetype_id" );
var archetype = asset.FindStringEditInfo( "model_archetype_id" );

PackageStatus = "Loading Model";
model = await Model.LoadAsync( asset.Path );
PackageStatus = null;

Bounds = model.Bounds;
PivotPosition = Bounds.ClosestPoint( Vector3.Down * 10000 );
physics = (model.Physics?.Parts.Count ?? 0) > 0;
if ( physics && model.Physics.Parts.Any( p => p.Meshes.Any() ) ) // can't do rigid body with meshes
physics = false;
physicsArchetype = archetype == "physics_prop_model" || archetype == "jointed_physics_model" || archetype == "breakable_prop_model";
}

public override void OnUpdate()
{
using var scope = Gizmo.Scope( "DropObject", traceTransform );

Gizmo.Draw.Color = Color.White.WithAlpha( 0.3f );
Gizmo.Draw.LineBBox( Bounds );

Gizmo.Draw.Color = Color.White;

if ( model is not null )
{
if ( physics && physicsArchetype != Gizmo.IsShiftPressed )
Gizmo.Draw.Color = Theme.Blue.WithAlpha( 0.6f );
else
Gizmo.Draw.Color = Color.White.WithAlpha( 0.3f );
Gizmo.Draw.LineBBox( Bounds );

Gizmo.Draw.Color = Color.White;
var so = Gizmo.Draw.Model( model );
if ( so.IsValid() )
{
Expand All @@ -55,6 +63,19 @@ public override void OnUpdate()
}
}

private bool HasPropData()
{
if ( model.Data.Explosive )
return true;
if ( model.Data.Flammable )
return true;
if ( model.Data.Health > 0 )
return true;
if ( model.HasData<ModelBreakPiece[]>() )
return true;
return false;
}

public override async Task OnDrop()
{
await WaitForLoad();
Expand All @@ -70,12 +91,12 @@ public override async Task OnDrop()
GameObject.Name = model.ResourceName;
GameObject.WorldTransform = traceTransform;

bool isProp = (model.Physics?.Parts.Count ?? 0) > 0;
if ( isProp )
var rigidbody = physics && physicsArchetype != Gizmo.IsShiftPressed;
if ( rigidbody || HasPropData() )
{
var prop = GameObject.Components.Create<Prop>();
prop.Model = model;
prop.IsStatic = archetype == "static_prop_model";
prop.IsStatic = !rigidbody;
}
else if ( model.BoneCount > 0 )
{
Expand All @@ -86,6 +107,12 @@ public override async Task OnDrop()
{
var renderer = GameObject.Components.Create<ModelRenderer>();
renderer.Model = model;
if ( (model.Physics?.Parts.Count ?? 0) > 0 )
{
var collider = GameObject.Components.Create<ModelCollider>();
collider.Static = true;
collider.Model = model;
}
}

GameObject.Enabled = true;
Expand Down