123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Windows.Forms;
- using Autodesk.Max;
- using MaxSharp;
- using SharpDX;
- using Color = MaxSharp.Color;
- namespace Max2Babylon
- {
- public static class Tools
- {
- public const float Epsilon = 0.001f;
- public static bool IsTextureCube(string filepath)
- {
- try
- {
- if (Path.GetExtension(filepath).ToLower() != ".dds")
- {
- return false;
- }
- var data = File.ReadAllBytes(filepath);
- var intArray = new int[data.Length / 4];
- Buffer.BlockCopy(data, 0, intArray, 0, data.Length);
- return (intArray[28] & 0x200) == 0x200;
- }
- catch
- {
- return false;
- }
- }
- public static Vector3 ToEulerAngles(this IQuat q)
- {
- // Store the Euler angles in radians
- var pitchYawRoll = new Vector3();
- double sqw = q.W * q.W;
- double sqx = q.X * q.X;
- double sqy = q.Y * q.Y;
- double sqz = q.Z * q.Z;
- // If quaternion is normalised the unit is one, otherwise it is the correction factor
- double unit = sqx + sqy + sqz + sqw;
- double test = q.X * q.Y + q.Z * q.W;
- if (test > 0.4999f * unit) // 0.4999f OR 0.5f - EPSILON
- {
- // Singularity at north pole
- pitchYawRoll.Y = 2f * (float)Math.Atan2(q.X, q.W); // Yaw
- pitchYawRoll.X = (float)Math.PI * 0.5f; // Pitch
- pitchYawRoll.Z = 0f; // Roll
- return pitchYawRoll;
- }
- if (test < -0.4999f * unit) // -0.4999f OR -0.5f + EPSILON
- {
- // Singularity at south pole
- pitchYawRoll.Y = -2f * (float)Math.Atan2(q.X, q.W); // Yaw
- pitchYawRoll.X = -(float)Math.PI * 0.5f; // Pitch
- pitchYawRoll.Z = 0f; // Roll
- return pitchYawRoll;
- }
- pitchYawRoll.Y = (float)Math.Atan2(2f * q.Y * q.W - 2f * q.X * q.Z, sqx - sqy - sqz + sqw); // Yaw
- pitchYawRoll.X = (float)Math.Asin(2f * test / unit); // Pitch
- pitchYawRoll.Z = (float)Math.Atan2(2f * q.X * q.W - 2f * q.Y * q.Z, -sqx + sqy - sqz + sqw); // Roll
- return pitchYawRoll;
- }
- public static void PreparePipeline(IINode node, bool deactivate)
- {
- var obj = node.ObjectRef;
- if (obj == null || obj.SuperClassID != SClass_ID.GenDerivob)
- {
- return;
- }
- var derivedObject = obj as IIDerivedObject;
- if (derivedObject == null)
- {
- return;
- }
- for (var index = 0; index < derivedObject.NumModifiers; index++)
- {
- var modifier = derivedObject.GetModifier(index);
- //if (modifier.ClassID.PartA == 9815843 && modifier.ClassID.PartB == 87654) // Skin
- //{
- // if (deactivate)
- // {
- // modifier.DisableMod();
- // }
- // else
- // {
- // modifier.EnableMod();
- // }
- //}
- }
- }
- public static VNormal[] ComputeNormals(IMesh mesh)
- {
- var vnorms = new VNormal[mesh.NumVerts];
- var fnorms = new Vector3[mesh.NumFaces];
- for (var index = 0; index < mesh.NumVerts; index++)
- {
- vnorms[index] = new VNormal();
- }
- for (var index = 0; index < mesh.NumFaces; index++)
- {
- var face = mesh.Faces[index];
- Vector3 v0 = mesh.Verts[(int)face.V[0]].ToVector3();
- Vector3 v1 = mesh.Verts[(int)face.V[1]].ToVector3();
- Vector3 v2 = mesh.Verts[(int)face.V[2]].ToVector3();
- fnorms[index] = Vector3.Cross((v1 - v0), (v2 - v1));
- for (var j = 0; j < 3; j++)
- {
- vnorms[(int)face.V[j]].AddNormal(fnorms[index], face.SmGroup);
- }
- fnorms[index].Normalize();
- }
- for (var index = 0; index < mesh.NumVerts; index++)
- {
- vnorms[index].Normalize();
- }
- return vnorms;
- }
- public static bool IsEqualTo(this float[] value, float[] other)
- {
- if (value.Length != other.Length)
- {
- return false;
- }
- return !value.Where((t, i) => Math.Abs(t - other[i]) > Epsilon).Any();
- }
- public static float[] ToArray(this IMatrix3 value)
- {
- var row0 = value.GetRow(0).ToArraySwitched();
- var row1 = value.GetRow(1).ToArraySwitched();
- var row2 = value.GetRow(2).ToArraySwitched();
- var row3 = value.GetRow(3).ToArraySwitched();
- return new[]
- {
- row0[0], row0[1], row0[2], 0,
- row2[0], row2[1], row2[2], 0,
- row1[0], row1[1], row1[2], 0,
- row3[0], row3[1], row3[2], 1
- };
- }
- public static IPoint3 ToPoint3(this Vector3 value)
- {
- return Loader.Global.Point3.Create(value.X, value.Y, value.Z);
- }
- public static Vector3 ToVector3(this IPoint3 value)
- {
- return new Vector3(value.X, value.Y, value.Z);
- }
- public static Quaternion ToQuat(this IQuat value)
- {
- return new Quaternion(value.X, value.Z, value.Y, value.W);
- }
- public static float[] ToArray(this IQuat value)
- {
- return new[] { value.X, value.Z, value.Y, value.W };
- }
- public static float[] Scale(this Color value, float scale)
- {
- return new[] { value.r * scale, value.g * scale, value.b * scale };
- }
- public static float[] ToArray(this Color value)
- {
- return new[] { value.r, value.g, value.b };
- }
- public static float[] ToArray(this IPoint4 value)
- {
- return new[] { value.X, value.Y, value.Z, value.W };
- }
- public static float[] ToArray(this IPoint3 value)
- {
- return new[] { value.X, value.Y, value.Z };
- }
- public static float[] ToArray(this IPoint2 value)
- {
- return new[] { value.X, value.Y };
- }
- public static float[] ToArraySwitched(this IPoint2 value)
- {
- return new[] { value.X, 1.0f - value.Y };
- }
- public static float[] ToArraySwitched(this IPoint3 value)
- {
- return new[] { value.X, value.Z, value.Y };
- }
- public static float[] ToArray(this IColor value)
- {
- return new[] { value.R, value.G, value.B };
- }
- public static IEnumerable<Node> NodesListBySuperClass(this Scene scene, SuperClassID sid)
- {
- return from n in scene.NodeTree where n.Object != null && n._Node.EvalWorldState(0, false).Obj.SuperClassID == sid select n;
- }
- public static IEnumerable<Node> NodesListBySuperClasses(this Scene scene, SuperClassID[] sids)
- {
- return from n in scene.NodeTree where n.Object != null && sids.Any(sid => n._Node.EvalWorldState(0, false).Obj.SuperClassID == sid) select n;
- }
- public static float ConvertFov(float fov)
- {
- return (float)(2.0f * Math.Atan(Math.Tan(fov / 2.0f) / Loader.Core.ImageAspRatio));
- }
- public static bool HasParent(this Node node)
- {
- return node.Parent != null && node.Parent.Object != null;
- }
- public static Guid GetGuid(this Animatable node)
- {
- var appData = node.GetAppData(new ClassID(Loader.Class_ID), SuperClassID.BaseNode);
- var uidData = appData.GetChunk(0);
- Guid uid;
- if (uidData != null)
- {
- uid = new Guid(uidData);
- }
- else
- {
- uid = Guid.NewGuid();
- appData.AddChunk(0, uid.ToByteArray());
- }
- return uid;
- }
- public static Guid GetGuid(this IINode node)
- {
- return GetGuid(Animatable.CreateWrapper<Node>(node));
- }
- public static string GetLocalData(this Node node)
- {
- var appData = node.GetAppData(new ClassID(Loader.Class_ID), SuperClassID.BaseNode);
- var uidData = appData.GetChunk(1);
- if (uidData != null)
- {
- return System.Text.Encoding.UTF8.GetString(uidData);
- }
- return "";
- }
- public static void SetLocalData(this Node node, string value)
- {
- var appData = node.GetAppData(new ClassID(Loader.Class_ID), SuperClassID.BaseNode);
- var uidData = appData.GetChunk(1);
- if (uidData != null)
- {
- appData.RemoveChunk(1);
- }
- appData.AddChunk(1, System.Text.Encoding.UTF8.GetBytes(value));
- }
- public static IMatrix3 GetWorldMatrix(this Node node, TimeValue t, bool parent)
- {
- return node._Node.GetWorldMatrix(t, parent);
- }
- public static IMatrix3 GetWorldMatrix(this IINode node, TimeValue t, bool parent)
- {
- var tm = node.GetNodeTM(t, Interval.Forever._IInterval);
- var ptm = node.ParentNode.GetNodeTM(t, Interval.Forever._IInterval);
- if (!parent)
- return tm;
- if (node.ParentNode.SuperClassID == SuperClassID.Camera)
- {
- var r = ptm.GetRow(3);
- ptm.IdentityMatrix();
- ptm.SetRow(3, r);
- }
- ptm.Invert();
- return tm.Multiply(ptm);
- }
- public static ITriObject GetMesh(this IObject obj)
- {
- if (obj.CanConvertToType(ClassID.TriObject._IClass_ID) == 0)
- return null;
- return obj.ConvertToType(0, ClassID.TriObject._IClass_ID) as ITriObject;
- }
- public static bool IsAlmostEqualTo(this IPoint4 current, IPoint4 other, float epsilon)
- {
- if (Math.Abs(current.X - other.X) > epsilon)
- {
- return false;
- }
- if (Math.Abs(current.Y - other.Y) > epsilon)
- {
- return false;
- }
- if (Math.Abs(current.Z - other.Z) > epsilon)
- {
- return false;
- }
- if (Math.Abs(current.W - other.W) > epsilon)
- {
- return false;
- }
- return true;
- }
- public static bool IsAlmostEqualTo(this IPoint3 current, IPoint3 other, float epsilon)
- {
- if (Math.Abs(current.X - other.X) > epsilon)
- {
- return false;
- }
- if (Math.Abs(current.Y - other.Y) > epsilon)
- {
- return false;
- }
- if (Math.Abs(current.Z - other.Z) > epsilon)
- {
- return false;
- }
- return true;
- }
- public static bool IsAlmostEqualTo(this IPoint2 current, IPoint2 other, float epsilon)
- {
- if (Math.Abs(current.X - other.X) > epsilon)
- {
- return false;
- }
- if (Math.Abs(current.Y - other.Y) > epsilon)
- {
- return false;
- }
- return true;
- }
- public static bool GetBoolProperty(this IINode node, string propertyName, int defaultState = 0)
- {
- int state = defaultState;
- node.GetUserPropBool(ref propertyName, ref state);
- return state == 1;
- }
- public static float GetFloatProperty(this IINode node, string propertyName, float defaultState = 0)
- {
- float state = defaultState;
- node.GetUserPropFloat(ref propertyName, ref state);
- return state;
- }
- public static float[] GetVector3Property(this IINode node, string propertyName)
- {
- float state0 = 0;
- string name = propertyName + "_x";
- node.GetUserPropFloat(ref name, ref state0);
- float state1 = 0;
- name = propertyName + "_y";
- node.GetUserPropFloat(ref name, ref state1);
- float state2 = 0;
- name = propertyName + "_z";
- node.GetUserPropFloat(ref name, ref state2);
- return new[] { state0, state1, state2 };
- }
- public static bool PrepareCheckBox(CheckBox checkBox, IINode node, string propertyName, int defaultState = 0)
- {
- var state = node.GetBoolProperty(propertyName, defaultState);
- if (checkBox.CheckState == CheckState.Indeterminate)
- {
- checkBox.CheckState = state ? CheckState.Checked : CheckState.Unchecked;
- }
- else
- {
- if (!state && checkBox.CheckState == CheckState.Checked ||
- state && checkBox.CheckState == CheckState.Unchecked)
- {
- checkBox.CheckState = CheckState.Indeterminate;
- return true;
- }
- }
- return false;
- }
- public static void PrepareCheckBox(CheckBox checkBox, List<IINode> nodes, string propertyName, int defaultState = 0)
- {
- checkBox.CheckState = CheckState.Indeterminate;
- foreach (var node in nodes)
- {
- if (PrepareCheckBox(checkBox, node, propertyName, defaultState))
- {
- break;
- }
- }
- }
- public static void UpdateCheckBox(CheckBox checkBox, IINode node, string propertyName)
- {
- if (checkBox.CheckState != CheckState.Indeterminate)
- {
- node.SetUserPropBool(ref propertyName, checkBox.CheckState == CheckState.Checked);
- }
- }
- public static void UpdateCheckBox(CheckBox checkBox, List<IINode> nodes, string propertyName)
- {
- foreach (var node in nodes)
- {
- UpdateCheckBox(checkBox, node, propertyName);
- }
- }
- public static void PrepareNumericUpDown(NumericUpDown nup, List<IINode> nodes, string propertyName, float defaultState = 0)
- {
- nup.Value = (decimal)nodes[0].GetFloatProperty(propertyName, defaultState);
- }
- public static void UpdateNumericUpDown(NumericUpDown nup, List<IINode> nodes, string propertyName)
- {
- foreach (var node in nodes)
- {
- node.SetUserPropFloat(ref propertyName, (float)nup.Value);
- }
- }
- public static void PrepareVector3Control(Vector3Control vector3Control, IINode node, string propertyName, float defaultX = 0, float defaultY = 0, float defaultZ = 0)
- {
- vector3Control.X = node.GetFloatProperty(propertyName + "_x", defaultX);
- vector3Control.Y = node.GetFloatProperty(propertyName + "_y", defaultY);
- vector3Control.Z = node.GetFloatProperty(propertyName + "_z", defaultZ);
- }
- public static void UpdateVector3Control(Vector3Control vector3Control, IINode node, string propertyName)
- {
- string name = propertyName + "_x";
- node.SetUserPropFloat(ref name, vector3Control.X);
- name = propertyName + "_y";
- node.SetUserPropFloat(ref name, vector3Control.Y);
- name = propertyName + "_z";
- node.SetUserPropFloat(ref name, vector3Control.Z);
- }
- public static void UpdateVector3Control(Vector3Control vector3Control, List<IINode> nodes, string propertyName)
- {
- foreach (var node in nodes)
- {
- UpdateVector3Control(vector3Control, node, propertyName);
- }
- }
- }
- }
|