Skip to content

Instantly share code, notes, and snippets.

@Doprez
Created May 12, 2023 00:37
Show Gist options
  • Save Doprez/5327fbd7d8c371938088db81d82d153b to your computer and use it in GitHub Desktop.
Save Doprez/5327fbd7d8c371938088db81d82d153b to your computer and use it in GitHub Desktop.
Basic Stride LOD example
[DataContract("SimpleLODComponent", Inherited = true)]
[ComponentCategory("Model")]
public class SimpleLOD : AsyncScript
{
[DataMember(0, "LOD's")]
public List<LODData> LODs = new List<LODData>();
private Entity _camera;
private ModelComponent _modelComponent;
public override async Task Execute()
{
_modelComponent = Entity.Get<ModelComponent>();
_camera = Entity.GetComponentInScene<CameraComponent>().Entity;
while (Game.IsRunning)
{
UpdateCurrentLOD();
await Script.NextFrame();
}
}
private void UpdateCurrentLOD()
{
var distance = Vector3.Distance(_camera.WorldPosition(), Entity.WorldPosition());
for (int i = LODs.Count - 1; i >= 0; i--)
{
if (distance < LODs[i].DistanceToActivate && _modelComponent.Model != LODs[i].MeshLOD)
{
_modelComponent.Model = LODs[i].MeshLOD;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment