1
This commit is contained in:
@@ -9,54 +9,21 @@ public partial class PathRenderer : EntityBehaviour
|
||||
{
|
||||
[Export] private DeviceValueComponent valueComponent;
|
||||
[Export] private Node3D root;
|
||||
[Export] private Path3D path3D;
|
||||
|
||||
private readonly List<MeshInstance3D> meshInstances=new();
|
||||
public override void OnAwake()
|
||||
{
|
||||
base.OnAwake();
|
||||
valueComponent.OnValueChanged += OnValueChanged;
|
||||
}
|
||||
|
||||
private void OnValueChanged(string arg1, DateTime arg2)
|
||||
{
|
||||
foreach (var x in meshInstances)
|
||||
{
|
||||
x.QueueFree();
|
||||
}
|
||||
meshInstances.Clear();
|
||||
Vector3 lastPoint = Vector3.Zero;
|
||||
path3D.Curve.ClearPoints();
|
||||
foreach (var pair in JArray.Parse(arg1))
|
||||
{
|
||||
var x = pair["X"]!.ToObject<float>()!;
|
||||
var y = pair["Y"]!.ToObject<float>()!;
|
||||
var currentPoint =root.Position + new Vector3(x, 0, y);
|
||||
if (lastPoint != Vector3.Zero)
|
||||
{
|
||||
meshInstances.Add(Line(lastPoint, currentPoint,Colors.Orange));
|
||||
}
|
||||
lastPoint = currentPoint;
|
||||
path3D.Curve.AddPoint(new Vector3(x,0,y));
|
||||
}
|
||||
}
|
||||
|
||||
public static MeshInstance3D Line(Vector3 pos1, Vector3 pos2, Color? color = null)
|
||||
{
|
||||
var meshInstance = new MeshInstance3D();
|
||||
var immediateMesh = new ImmediateMesh();
|
||||
var material = new StandardMaterial3D();
|
||||
|
||||
meshInstance.Mesh = immediateMesh;
|
||||
meshInstance.CastShadow = GeometryInstance3D.ShadowCastingSetting.Off;
|
||||
|
||||
immediateMesh.SurfaceBegin(Mesh.PrimitiveType.Lines, material);
|
||||
immediateMesh.SurfaceAddVertex(pos1);
|
||||
immediateMesh.SurfaceAddVertex(pos2);
|
||||
immediateMesh.SurfaceEnd();
|
||||
|
||||
material.ShadingMode = BaseMaterial3D.ShadingModeEnum.Unshaded;
|
||||
material.AlbedoColor = color ?? Colors.WhiteSmoke;
|
||||
|
||||
(Engine.GetMainLoop() as SceneTree).Root.AddChild(meshInstance);
|
||||
|
||||
return meshInstance;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user