Last active
November 14, 2020 06:50
-
-
Save amitkhare/950ac2a794a374cd4c718763efc12c46 to your computer and use it in GitHub Desktop.
ProjectionMath for Wasp3D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region NameSpaceRegion | |
using System; | |
using System.Windows.Forms; | |
using System.Data; | |
using System.Drawing; | |
using System.Collections; | |
using System.ComponentModel; | |
using System.Xml; | |
using System.Xml.Linq; | |
using BeeSys.Wasp3D.Hosting; | |
using BeeSys.Wasp3D.HostingX; | |
using System.Collections.Generic; | |
using amitkhare; | |
#endregion | |
namespace BeeSys.Wasp3D.Scene { | |
public partial class SceneGraph { | |
/// This method is called when scene initialize | |
private void OnInit() { } | |
private void calculate() { | |
// create and add windows rectangle nodes to a list | |
List<RoundedRect> windows = new List<RoundedRect>(); | |
windows.Add(Win_1); | |
windows.Add(Win_2); | |
windows.Add(Win_3); | |
windows.Add(Win_4); | |
windows.Add(Win_5); | |
windows.Add(Win_6); | |
windows.Add(Win_7); | |
windows.Add(Win_8); | |
// get rows from UDT // #2 is table index | |
DataRowCollection udtRows = UDT_1.Tables[2].GetTable(true).Rows; | |
ProjectionMath projectionMath = new ProjectionMath(WFloat.AreaWidth,WFloat.AreaHeight); | |
// loop through fetched udtRows | |
int j = 0; | |
foreach(DataRow row in udtRows) { | |
// create dataRow from UDT row | |
UDTDataRow dataRow = new UDTDataRow(row); | |
// set projected position and dimention to window rectangle list | |
Vector2 newDimention = projectionMath.GetDimention(dataRow.Width, dataRow.Height); | |
windows[j].Translation = projectionMath.GetPosition(dataRow.Pos_X, dataRow.Pos_Y, dataRow.Pos_Z); | |
windows[j].RectangleWidth = newDimention.x; | |
windows[j].RectangleHeight = newDimention.y; | |
windows[j].Visibility = 1.0f; | |
j++; | |
} | |
// hide leftover window rectangles | |
for (int i = j; i < windows.Count; i++) { | |
windows[i].RectangleWidth = 0; | |
windows[i].Visibility = 0.0f; | |
} | |
} | |
/// this method is called when named event is raised | |
private void SceneGraph_Named(string name) { | |
try { | |
if(name == "calculate") calculate(); | |
} catch (System.Exception ex) { | |
LogWriter.WriteLog(ex); | |
} | |
} | |
} | |
} | |
namespace amitkhare { | |
public class UDTDataRow { | |
public float Width, Height, Pos_X, Pos_Y, Pos_Z; | |
public string Texture; | |
public Vector3 UVW_Scale; | |
public UDTDataRow(DataRow row) { | |
this.Width = float.Parse(row["Width"].ToString()); | |
this.Height = float.Parse(row["Height"].ToString()); | |
this.Pos_X = float.Parse(row["Position_X"].ToString()); | |
this.Pos_Y = float.Parse(row["Position_Y"].ToString()); | |
this.Pos_Z = float.Parse(row["Position_Z"].ToString()); | |
this.Texture = row["Texture"].ToString(); | |
string[] UVWArr = (row["UVW_Scale"].ToString()).Split(','); | |
this.UVW_Scale = new Vector3(float.Parse(UVWArr[0]), float.Parse(UVWArr[1]), float.Parse(UVWArr[2])); | |
} | |
} | |
public class ProjectionMath { | |
float AreaWidth, AreaHeight, rangeMin, rangeMax; | |
public ProjectionMath(float AreaWidth, float AreaHeight, float rangeMin = -100, float rangeMax = 100) { | |
this.AreaWidth = AreaWidth; | |
this.AreaHeight = AreaHeight; | |
this.rangeMin = rangeMin; | |
this.rangeMax = rangeMax; | |
} | |
public static byte FloatToByte(float f) { | |
return (byte)(f * 255 % 256); | |
} | |
public Vector2 GetDimention(float width, float height) { | |
float outWidth = AreaWidth * (width / 100); | |
float outHeight = AreaHeight * (height / 100); | |
return new Vector2(outWidth, outHeight); | |
} | |
public Vector3 GetPosition(float inX, float inY, float inZ) { | |
float XA = - (AreaWidth / 2); | |
float XB = (AreaWidth / 2); | |
float YA = - (AreaHeight / 2); | |
float YB = (AreaHeight / 2); | |
float ZA = 0; | |
float ZB = 0; | |
float outX = (inX - rangeMin) /(rangeMax - rangeMin) * (XB - XA) + XA; | |
float outY = (inY - rangeMin) /(rangeMax - rangeMin) * (YB - YA) + YA; | |
float outZ = (inZ - rangeMin) /(rangeMax - rangeMin) * (ZB - ZA) + ZA; | |
return new Vector3(outX, outY, outZ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Required Variables and UDT
Window rectangles
preview
UDT Structure
Filter UDT