Skip to content

Commit

Permalink
Added 3D viewport models representing actors (beta)
Browse files Browse the repository at this point in the history
  • Loading branch information
BuilderDemo7 committed Nov 24, 2023
1 parent ab1b69c commit 28699aa
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 11 deletions.
141 changes: 135 additions & 6 deletions Zartex2/3D/Inspector3D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ public partial class viewport : UserControl

float textInfoHeight = 1.1f;

float deg = (180 / (float)Math.PI);

public static string modelsFolderName = "Models";

public static Model3D characterModel = getModel($"{modelsFolderName}/character.3ds");
public static Model3D vehicleModel = getModel($"{modelsFolderName}/vehicle.3ds");

public static SolidColorBrush Green = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 0, 255, 0));
public static SolidColorBrush Red = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 0, 255, 0));
public static SolidColorBrush White = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 255, 255, 255));
Expand All @@ -65,6 +72,18 @@ public Vector3D FindActorPosition(ActorDefinition actor)
MissionObject missionObject = null;
if (actor.ObjectId != -1 & actor.ObjectId < sceneObjects.Count)
missionObject = sceneObjects[actor.ObjectId];
// special for DPL format
if (actor.TypeId == 10)
{
foreach (var prop in actor.Properties)
{
if (prop is PathProperty)
{
var path = prop as PathProperty;
return new Vector3D(path.Path[0].X, path.Path[0].Z, path.Path[0].Y);
}
}
}
if (missionObject != null)
{
switch (actor.TypeId)
Expand Down Expand Up @@ -127,6 +146,7 @@ public Vector3D FindActorForwardVector(ActorDefinition actor)
if (prop is MatrixProperty)
{
MatrixProperty matrix = prop as MatrixProperty;
// wait that's not, ah, I don't care, just if it works
return new Vector3D(matrix.Forward.X, matrix.Forward.Z, matrix.Forward.Y);
}
}
Expand Down Expand Up @@ -203,8 +223,33 @@ public void UpdateScene(bool updateCamera = true)
// make sure the position isn't null or smth (I made this to identify it because it can't just be null)
if (!Vector3DIsDead(pos))
{
Transform3D tnf = new TranslateTransform3D(pos);
Vector3D fwd = FindActorForwardVector(actor);
float angle = ((float)Math.Atan2(fwd.Y, fwd.X)) * deg;
// calculates rotation
Transform3D rot = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0,0,1), angle));
// now the rotated transform with the position, etc.
Transform3D tnf = new MatrixTransform3D(new Matrix3D()
{
OffsetX = pos.X,
OffsetY = pos.Y,
OffsetZ = pos.Z,
// right
M11 = rot.Value.M11,
M12 = rot.Value.M12,
M13 = rot.Value.M13,
M14 = rot.Value.M14,
// forward
M21 = rot.Value.M21,
M22 = rot.Value.M22,
M23 = rot.Value.M23,
M24 = rot.Value.M24,
// up
M31 = rot.Value.M31,
M32 = rot.Value.M32,
M33 = rot.Value.M33,
M34 = rot.Value.M34,

}); //new TranslateTransform3D(pos);

var pureColor = System.Windows.Media.Color.FromArgb(255, (byte)actor.Color.R, (byte)actor.Color.G, (byte)actor.Color.B);
var actorColor = new System.Windows.Media.SolidColorBrush(pureColor);
Expand Down Expand Up @@ -235,6 +280,7 @@ public void UpdateScene(bool updateCamera = true)
});
continue; // cancel the others representations
}
// path representation
if (actor.TypeId == 6)
{
var lines = new LinesVisual3D();
Expand Down Expand Up @@ -282,14 +328,57 @@ public void UpdateScene(bool updateCamera = true)
});
continue; // cancel the others representations
}
// path representation (Driver: Parallel Lines)
// the code is very very similiar to the path representation of Driv3r
// but this time it gets the path property instead of the actual mission object, etc...
if (actor.TypeId == 10)
{
var lines = new LinesVisual3D();
lines.Color = pureColor;
lines.Thickness = pathLineThickness;

foreach(var prop in actor.Properties)
if (prop is PathProperty)
{
var path = prop as PathProperty;
int id = 0;
foreach (Vector4 pathvec in path.Path)
{
lines.Points.Add(new Point3D(pathvec.X, pathvec.Z, pathvec.Y));
if (id < path.Path.Length - 1)
lines.Points.Add(new Point3D(path.Path[id + 1].X, path.Path[id + 1].Z, path.Path[id + 1].Y));

id++;
//if (id == pathObject.Path.Length-1)
// last = new Vector3D(pathvec.X, pathvec.Z, pathvec.Y);
}
break; // breaks the loop as this is the property we want and no longer need to advance
}
// start point
vp.Children.Add(new SphereVisual3D()
{
Transform = tnf,
Fill = actorColor,
Radius = 0.2f
});
// lines connection
vp.Children.Add(lines);
continue; // cancel the others representations
}

// common representation
var representation1 = new CubeVisual3D()
var representation1 = getRepresentationModelForActorType(actor.TypeId,pureColor); /*new CubeVisual3D()
{
Transform = tnf,
Fill = actorColor,
};
};*/
// forward representation
representation1.Transform = tnf;
// set color

sceneDevice.Children.Add(representation1);
// I don't think I need this anymore because there is models that you can know what they are facing
/*
var representation2 = new ArrowVisual3D()
{
Diameter = arrowHeadSize,
Expand All @@ -298,9 +387,8 @@ public void UpdateScene(bool updateCamera = true)
Transform = tnf,
Fill = actorColor,
};
sceneDevice.Children.Add(representation1);
if (!Vector3DIsDead(fwd))
sceneDevice.Children.Add(representation2);
sceneDevice.Children.Add(representation2); */
}
//}

Expand All @@ -320,7 +408,7 @@ public viewport()
//viewport3D.Viewport3D.Children.Add(device);
}

public Model3D getModel(string path)
public static Model3D getModel(string path)
{
Model3D device = null;
try
Expand All @@ -332,6 +420,47 @@ public Model3D getModel(string path)
{ }
return device;
}

public static Model3DGroup getModelAsGroup(string path)
{
Model3DGroup device = null;
try
{
ModelImporter import = new ModelImporter();
device = import.Load(path);
}
catch (Exception e)
{ }
return device;
}

public ModelVisual3D getRepresentationModelForActorType(int type,System.Windows.Media.Color color)
{
ModelVisual3D device = new ModelVisual3D();
Model3D model = null;
switch (type)
{
case 2:
model = characterModel;
break;
case 3:
model = vehicleModel;
break;
}
if (model == null) { return new CubeVisual3D() { Fill = new SolidColorBrush(color) }; }

GeometryModel3D md = model as GeometryModel3D;

if (md != null)
{
DiffuseMaterial material = new DiffuseMaterial(new SolidColorBrush(color));
md.Material = material;
md.BackMaterial = material;
}

device.Content = model;
return device;
}

// clicked on a node then put the camera on it
public void NodesOnClick(object sender, TreeViewEventArgs e)
Expand Down
62 changes: 57 additions & 5 deletions Zartex2/MissionScript/Logic/NodeProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static Type GetPropertyTypeById(int type)
case 23: return typeof(MatrixProperty);
// new types reserves
case 24: return typeof(UnknownTypeProperty);
case 25: return typeof(UnknownTypeProperty);
case 25: return typeof(PathProperty);
case 26: return typeof(UnknownTypeProperty);
case 27: return typeof(UnknownTypeProperty);
case 28: return typeof(ObjectTypeProperty);
Expand Down Expand Up @@ -469,18 +469,18 @@ public override void LoadData(Stream stream)
if (size != Size)
throw new InvalidOperationException("Invalid matrix property!");

Left = stream.Read<Vector4>();
Up = stream.Read<Vector4>();
Forward = stream.Read<Vector4>();
Up = stream.Read<Vector4>();
Left = stream.Read<Vector4>();
Value = stream.Read<Vector4>(); // reads position
}

public override void SaveData(Stream stream)
{
stream.Write(Size);
stream.Write(Left);
stream.Write(Up);
stream.Write(Forward);
stream.Write(Up);
stream.Write(Left);
stream.Write(Value);
}

Expand Down Expand Up @@ -761,6 +761,58 @@ public AudioProperty(AudioInfo value)
}
}

public sealed class PathProperty : NodeProperty
{
public override int TypeId
{
get { return 25; }
}

public override int Size
{
get { return (Path.Length*16)+4; }
}

public override string Notes
{
get { return "Represents a path."; }
}

public override string ToString()
{
return $"Path ({Path.Length})";
}

public Vector4[] Path { get; set; }

public override void LoadData(Stream stream)
{
stream.Position += 4; // skip size
var count = stream.ReadInt32();
Path = new Vector4[count];
for (int id = 0; id<count; id++)
{
Path[id] = stream.Read<Vector4>();
}
}

public override void SaveData(Stream stream)
{
stream.Write(Size);
stream.Write((int)Path.Length);
foreach(Vector4 vec in Path)
{
stream.Write<Vector4>(vec);
}
}

public PathProperty() { }
public PathProperty(Vector4[] path)
{
Path = path;
}
}

public abstract class VectorProperty : NodeProperty
{
public override int Size
Expand Down
Binary file added Zartex2/Models/character.3ds
Binary file not shown.
Binary file added Zartex2/Models/vehicle.3ds
Binary file not shown.
6 changes: 6 additions & 0 deletions Zartex2/Zartex.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@
<None Include="deprecated\Nodes.cs">
<SubType>Code</SubType>
</None>
<None Include="Models\character.3ds">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Models\vehicle.3ds">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down

0 comments on commit 28699aa

Please sign in to comment.