Files
sbox-public/engine/Sandbox.Engine/Systems/SceneSystem/SceneCustomObject.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

88 lines
1.8 KiB
C#

using NativeEngine;
namespace Sandbox;
/// <summary>
/// A scene object that allows custom rendering within a scene world.
/// </summary>
public class SceneCustomObject : SceneObject
{
internal NativeEngine.CManagedSceneObject managedNative;
internal SceneCustomObject( HandleCreationData _ ) { }
public SceneCustomObject( SceneWorld sceneWorld )
{
Assert.IsValid( sceneWorld );
using ( var h = IHandle.MakeNextHandle( this ) )
{
CManagedSceneObject.Create( sceneWorld );
if ( native.IsValid )
{
// Start off with infinite bounds so it will actually get rendered
// if the user forgets/doesn't need bounds.
native.SetBoundsInfinite();
}
}
}
internal unsafe override void OnNativeInit( CSceneObject ptr )
{
base.OnNativeInit( ptr );
managedNative = (NativeEngine.CManagedSceneObject)ptr;
}
internal override void OnNativeDestroy()
{
managedNative = default;
base.OnNativeDestroy();
}
internal void RenderInternal()
{
if ( !this.IsValid() )
return;
try
{
RenderSceneObject();
}
catch ( System.Exception e )
{
Log.Error( e );
}
}
/// <summary>
/// Called by default version of <see cref="RenderSceneObject"/>.
/// </summary>
public Action<SceneObject> RenderOverride;
/// <summary>
/// Called when this scene object needs to be rendered.
/// Invokes <see cref="RenderOverride"/> by default. See the <see cref="Graphics" /> library for a starting point.
/// </summary>
public virtual void RenderSceneObject()
{
RenderOverride?.Invoke( this );
}
}
internal static class SceneCustomObjectRender
{
internal static void RenderObject( ManagedRenderSetup_t setup, SceneCustomObject obj )
{
if ( obj is null )
return;
using ( new Graphics.Scope( in setup ) )
{
obj.RenderInternal();
}
}
}