Entity

The purpose of the Entity class is to hold reference Renderer and Node, and use the position of Node and the orientation to update the buffer of Renderer before rendering. 

Create a new class called Entity in the Content folder of the AssistantItemFinder  project. With the new class open, add the namespace System.Numerics, Windows.Perception.Spatial, and AssistantItemFinder.Common and make the following amendments to the class:

internal class Entity
{
public string Tag { get; private set; }
public Node Node { get; set; }
public Renderer Renderer { get; set; }

public Vector3 Position { get; set; }
public Vector3 EulerAngles { get; set; }
public Vector3 Scale { get; set; }

public bool Visible { get; set; }
public bool Enabled { get; set; }

private Matrix4x4 transform = Matrix4x4.Identity;
public Matrix4x4 Transform
{
get{
return transform;
}
}

public Entity(string tag, bool visible = true, bool enabled = true)
{
Tag = tag;
Visible = visible;
Enabled = enabled;

Position = Vector3.Zero;
EulerAngles = Vector3.Zero;
Scale = Vector3.One;
}
}

In the preceding snippet, we have created the properties and constructor for our Entity class; no surprises here. Entity is responsible for rendering Node using the assigned Renderer. Entity itself contains Position and Rotation, all of which are relative to the anchor's coordinate system of Node and applied to the transformation when rendering. Next, we will add the Update method responsible for updating the instance's transform variable using the one passed in the coordinate system. Add the following method to your Entity class: 

public virtual void Update(StepTimer timer, SpatialCoordinateSystem referenceFrameCoordinateSystem)
{
if(!Enabled){ return; }

UpdateTransform(referenceFrameCoordinateSystem);
}

public void UpdateTransform(SpatialCoordinateSystem referenceFrameCoordinateSystem)
{
var trans = Node.GetTransform(referenceFrameCoordinateSystem);
if (trans.HasValue)
{
Matrix4x4 modelTranslation = Matrix4x4.CreateTranslation(Position);
Matrix4x4 modelRotation = Matrix4x4.CreateFromYawPitchRoll(
DegreeToRadian(EulerAngles.Y),
DegreeToRadian(EulerAngles.X),
DegreeToRadian(EulerAngles.Z));
Matrix4x4 modelScale = Matrix4x4.CreateScale(Scale);

transform = (modelScale * modelRotation * modelTranslation) *
trans.Value;
}
}

private float RadianToDegree(double angle)
{
return (float)(angle * (180.0 / Math.PI));
}

The UpdateTransform method is the main workhorse in the preceding snippet. It's responsible for constructing the model matrix transform using the Entity, Scale, Rotation, and Position properties, as well as taking into account the transformation matrix between the referenced coordinate system of Node via SpatialAnchor and the coordinate system passed in which will be the coordinate system associated to the reference frame.

The following snippet contains the final piece of code for our Entity class and is responsible for updating the assigned model matrix of Renderer before delegating the rendering to it:

public virtual void Render()
{
if (!Visible || Renderer == null){ return; }

Renderer.UpdateModelTransform(Transform);
Renderer.Render();
}

With the Node, Edge, and Entity classes now implemented, we will return to the AssistantItemFinderMain class and hook everything up. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.174.168