Beispiel #1
0
        public static Tuple <Vector3Collection, Color4Collection> BPtoPointCloud(Vector3Collection imageSpace, List <LMData> lmDataList, double angleThreshold = 5, double minCountPercent = 0)
        {
            if (minCountPercent > 1 || minCountPercent < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(minCountPercent));
            }
            Vector3Collection vector3sOut = new Vector3Collection();
            Color4Collection  color4sOut  = new Color4Collection();

            if (imageSpace.Count == 0 || lmDataList.Count == 0)
            {
                var tupleOut1 = new Tuple <Vector3Collection, Color4Collection>(vector3sOut, color4sOut);
                return(tupleOut1);
            }

            int[] counts     = new int[imageSpace.Count];
            var   templmlist = lmDataList.ToArray();


            foreach (var lmData in templmlist)
            {
                if (lmData.Type == LMData.InteractionType.Compton)
                {
                    Parallel.For(0, imageSpace.Count, i =>
                    {
                        if (IsEffectedBPPoint(lmData.ScatterLMDataInfos[0].RelativeInteractionPoint3D, lmData.ScatterLMDataInfos[0].InteractionEnergy,
                                              lmData.AbsorberLMDataInfos[0].RelativeInteractionPoint3D, lmData.AbsorberLMDataInfos[0].InteractionEnergy,
                                              imageSpace[i].ToPoint3D(), angleThreshold))
                        {
                            counts[i]++;
                        }
                    });
                }
            }



            int maxCount = counts.Max();
            int minCount = Convert.ToInt32(Math.Round(maxCount * minCountPercent));

            if (maxCount < 5)
            {
                var tupleOut2 = new Tuple <Vector3Collection, Color4Collection>(vector3sOut, color4sOut);
                return(tupleOut2);
            }


            for (int i = 0; i < imageSpace.Count; i++)
            {
                if (counts[i] > minCount)
                {
                    vector3sOut.Add(imageSpace[i]);
                    color4sOut.Add(ColorScaleJet(counts[i], minCount, maxCount));
                }
            }

            var tupleOut3 = new Tuple <Vector3Collection, Color4Collection>(vector3sOut, color4sOut);

            return(tupleOut3);
        }
Beispiel #2
0
        void DrawBPPointCloudToSLAMPointCloud()
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            VMStatus = $"Reconing......SLAM! {elapsedTime} ms";


            var vc            = new Vector3Collection();
            var cc            = new Color4Collection();
            var tempposeVect  = new List <double[]>();
            var tempColorVect = new List <double[]>();


            RealsenseControl.GetReconSLAMPointCloud(ref tempposeVect, ref tempColorVect);
            for (int i = 0; i < tempposeVect.Count; i++)
            {
                vc.Add(new Vector3(Convert.ToSingle(tempposeVect[i][0]), Convert.ToSingle(tempposeVect[i][1]), Convert.ToSingle(tempposeVect[i][2])));
                cc.Add(new Color4(Convert.ToSingle(tempColorVect[i][0]), Convert.ToSingle(tempColorVect[i][1]), Convert.ToSingle(tempColorVect[i][2]), 0.5f));
            }

            SLAMReconPointCloud = new PointGeometry3D()
            {
                Positions = vc, Colors = cc
            };
            sw.Stop();
            elapsedTime = sw.ElapsedMilliseconds;
            VMStatus    = "Reconing......SLAM Done!";
        }
Beispiel #3
0
        private void LoadThreadFunction()
        {
            PCD wrapper = new PCD();

            Points[] rawPoints = wrapper.LoadPointcloud(Location);

            var points    = new PointGeometry3D();
            var col       = new Color4Collection();
            var ptPos     = new Vector3Collection();
            var ptIdx     = new IntCollection();
            var ptNormals = new Vector3Collection();

            var numberOfElements = rawPoints.Length;

            var additionalTurns = 0;

            foreach (var point in rawPoints)
            {
                ptIdx.Add(ptPos.Count);

                ptPos.Add(new Vector3(point.x, point.z, -point.y));
                col.Add(new Color4(new Color3(point.r / (float)255, point.g / (float)255, point.b / (float)255)));
                ptNormals.Add(new Vector3(0, 1, 0));
            }

            if ((rawPoints.Length / 3) * 3 != rawPoints.Length)
            {
                additionalTurns = ((rawPoints.Length / 3 + 1) * 3) - rawPoints.Length;
            }

            for (int i = 0; i < additionalTurns; i++)
            {
                ptIdx.Add(ptPos.Count);

                ptPos.Add(ptPos[ptPos.Count - 1]);
                col.Add(col[col.Count - 1]);
                ptNormals.Add(ptNormals[ptNormals.Count - 1]);
            }

            points.Positions = ptPos;
            points.Indices   = ptIdx;
            points.Colors    = col;
            //points.Normals = ptNormals;

            Data = points;
        }
Beispiel #4
0
        public static (Vector3Collection, Color4Collection) BPtoPointCloud2Pi(Vector3Collection imageSpace, List <LMData> lmDataList, double angleThreshold = 5, double minCountPercent = 0)
        {
            if (minCountPercent > 1 || minCountPercent < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(minCountPercent));
            }
            Vector3Collection vector3sOut = new Vector3Collection();
            Color4Collection  color4sOut  = new Color4Collection();

            if (imageSpace.Count == 0 || lmDataList.Count == 0)
            {
                return(vector3sOut, color4sOut);
            }

            int[] counts = new int[imageSpace.Count];


            foreach (var lmData in lmDataList)
            {
                if (lmData.Type == LMData.InteractionType.Compton)
                {
                    for (int i = 0; i < imageSpace.Count; ++i)
                    {
                        if (IsEffectedBPPoint2Pi(lmData.ScatterLMDataInfo.TransformedInteractionPoint3D, lmData.ScatterLMDataInfo.InteractionEnergy,
                                                 lmData.AbsorberLMDataInfo.TransformedInteractionPoint3D, lmData.AbsorberLMDataInfo.InteractionEnergy,
                                                 imageSpace[i].ToPoint3D(), lmData.DeviceTransformMatrix, angleThreshold))
                        {
                            counts[i]++;
                        }
                    }
                }
            }



            int maxCount = counts.Max();
            int minCount = Convert.ToInt32(Math.Round(maxCount * minCountPercent));

            if (maxCount < 5)
            {
                return(vector3sOut, color4sOut);
            }


            for (int i = 0; i < imageSpace.Count; i++)
            {
                if (counts[i] > minCount)
                {
                    vector3sOut.Add(imageSpace[i]);
                    color4sOut.Add(ColorScaleJet(counts[i], minCount, maxCount));
                }
            }

            return(vector3sOut, color4sOut);
        }
Beispiel #5
0
        public static Color4Collection ScaleGridColors(IEnumerable <HelixToolkit.Wpf.SharpDX.Geometry3D.Line> lines, double min, double max, Enums.colorScales colorScale)
        {
            var colorList = new Color4Collection();

            switchColorScale(colorScale);

            foreach (var line in lines)
            {
                var avg = (line.P0.Z + line.P1.Z) / 2;
                if (avg > (float)max)
                {
                    colorList.Add(new Color4(scaleHolder((float)min, (float)max, (float)max)));
                    colorList.Add(new Color4(scaleHolder((float)min, (float)max, (float)max)));
                }
                else if (avg < (float)min)
                {
                    colorList.Add(new Color4(scaleHolder((float)min, (float)max, (float)min)));
                    colorList.Add(new Color4(scaleHolder((float)min, (float)max, (float)min)));
                }
                else
                {
                    colorList.Add(scaleHolder((float)min, (float)max, (float)avg));
                    colorList.Add(scaleHolder((float)min, (float)max, (float)avg));
                }
            }
            return(colorList);
        }
Beispiel #6
0
        private void LoadThreadFunction()
        {
            var pt = new PotreeNode(new Uri(Location));

            var points = new PointGeometry3D();
            var col    = new Color4Collection();
            var ptPos  = new Vector3Collection();
            var ptIdx  = new IntCollection();

            var j = 0;

            foreach (var p in pt.Compilation.Points)
            {
                ptPos.Add(new Vector3(p.x, p.y, p.z));
                ptIdx.Add(j);
                col.Add(new Color4(p.r / 255f, p.g / 255f, p.b / 255f, p.a / 255f));
                j++;
            }

            var additionalTurns = 0;

            if ((pt.Compilation.Points.Count / 3) * 3 != pt.Compilation.Points.Count)
            {
                additionalTurns = ((pt.Compilation.Points.Count / 3 + 1) * 3) - pt.Compilation.Points.Count;
            }

            for (int i = 0; i < additionalTurns; i++)
            {
                ptIdx.Add(ptPos.Count);

                ptPos.Add(ptPos[ptPos.Count - 1]);
                col.Add(col[col.Count - 1]);
            }

            points.Positions = ptPos;
            points.Indices   = ptIdx;
            points.Colors    = col;

            Data = points;
        }
Beispiel #7
0
        private void LoadThreadFunction()
        {
            var pt = new Potree(new Uri(Filename));

            var points = new PointGeometry3D();
            var col = new Color4Collection();
            var ptPos = new Vector3Collection();
            var ptIdx = new IntCollection();

            var j = 0;

            foreach (var p in pt.Compilation.Points)
            {
                ptPos.Add(new Vector3(p.x, p.y, p.z));
                ptIdx.Add(j);
                col.Add(new Color4(p.r/255f, p.g / 255f, p.b / 255f, p.a / 255f));
                j++;
            }

            var additionalTurns = 0;

            if ((pt.Compilation.Points.Count / 3) * 3 != pt.Compilation.Points.Count)
            {
                additionalTurns = ((pt.Compilation.Points.Count / 3 + 1) * 3) - pt.Compilation.Points.Count;
            }

            for (int i = 0; i < additionalTurns; i++)
            {
                ptIdx.Add(ptPos.Count);

                ptPos.Add(ptPos[ptPos.Count - 1]);
                col.Add(col[col.Count - 1]);
            }

            points.Positions = ptPos;
            points.Indices = ptIdx;
            points.Colors = col;

            Data = points;
        }
        /// <summary>
        /// Add axis in positive, 0.1 m
        /// </summary>
        private void AddAxisPoints()
        {
            Vector3Collection vector3s = new Vector3Collection();
            Color4Collection  color4s  = new Color4Collection();

            vector3s.Add(new SharpDX.Vector3());
            color4s.Add(new SharpDX.Color4(0, 0, 0, 1));

            for (int i = 1; i < 50; i++)
            {
                if (i % 10 == 0)
                {
                    vector3s.Add(new SharpDX.Vector3()
                    {
                        X = 0.1f * i
                    });
                    color4s.Add(new SharpDX.Color4(0, 1, 1, 1));
                    vector3s.Add(new SharpDX.Vector3()
                    {
                        Y = 0.1f * i
                    });
                    color4s.Add(new SharpDX.Color4(1, 0, 1, 1));
                    vector3s.Add(new SharpDX.Vector3()
                    {
                        Z = 0.1f * i
                    });
                    color4s.Add(new SharpDX.Color4(1, 1, 0, 1));
                }
                else
                {
                    vector3s.Add(new SharpDX.Vector3()
                    {
                        X = 0.1f * i
                    });
                    color4s.Add(new SharpDX.Color4(1, 0, 0, 1));
                    vector3s.Add(new SharpDX.Vector3()
                    {
                        Y = 0.1f * i
                    });
                    color4s.Add(new SharpDX.Color4(0, 1, 0, 1));
                    vector3s.Add(new SharpDX.Vector3()
                    {
                        Z = 0.1f * i
                    });
                    color4s.Add(new SharpDX.Color4(0, 0, 1, 1));
                }
            }


            AxisPoint = new PointGeometry3D()
            {
                Positions = vector3s, Colors = color4s
            };
        }
Beispiel #9
0
        public static PointGeometry3D ConvertToPointGeometry3D(Points[] points)
        {
            var geometry  = new PointGeometry3D();
            var col       = new Color4Collection();
            var ptPos     = new Vector3Collection();
            var ptIdx     = new IntCollection();
            var ptNormals = new Vector3Collection();

            var additionalTurns = 0;

            foreach (var point in points)
            {
                ptIdx.Add(ptPos.Count);

                ptPos.Add(new Vector3(point.x, point.y, point.z));
                col.Add(new Color4(new Color3(point.r / (float)255, point.g / (float)255, point.b / (float)255)));
                ptNormals.Add(new Vector3(0, 1, 0));
            }

            if ((points.Length / 3) * 3 != points.Length)
            {
                additionalTurns = ((points.Length / 3 + 1) * 3) - points.Length;
            }

            for (int i = 0; i < additionalTurns; i++)
            {
                ptIdx.Add(ptPos.Count);

                ptPos.Add(ptPos[ptPos.Count - 1]);
                col.Add(col[col.Count - 1]);
                ptNormals.Add(ptNormals[ptNormals.Count - 1]);
            }

            geometry.Positions = ptPos;
            geometry.Indices   = ptIdx;
            geometry.Colors    = col;

            return(geometry);
        }
Beispiel #10
0
        public static Color4Collection ScalePointColors(Vector3Collection points, double actualMin, double actualMax, double newMin, double newMax, Enums.colorScales colorScale)
        {
            var temp = new Color4Collection();

            switchColorScale(colorScale);


            foreach (var item in points)
            {
                if (item.Z > (float)newMax)
                {
                    temp.Add(new Color4(VisualizationTools.scaleHolder((float)actualMin, (float)newMax, (float)newMax)));
                }
                else if (item.Z < (float)newMin)
                {
                    temp.Add(new Color4(VisualizationTools.scaleHolder((float)newMin, (float)actualMax, (float)newMin)));
                }
                else
                {
                    temp.Add(new Color4(VisualizationTools.scaleHolder((float)newMin, (float)newMax, item.Z)));
                }
            }
            return(temp);
        }
Beispiel #11
0
        public void SetOspMaterial()
        {
            if (DiffuseColor != null)
            {
                this.Material = new PhongMaterial();

                Color4Collection colors = new Color4Collection();

                for (int i = 0; i < this.Geometry.Positions.Count; i++)
                {
                    colors.Add(DiffuseColor.ToColor4());
                }

                this.Geometry.Colors = colors;
            }
        }
Beispiel #12
0
        public static Color4Collection ToColor4Collection(this byte[] collection)
        {
            var colors = new Color4Collection();

            for (var i = 0; i < collection.Count(); i += 4)
            {
                var a        = collection[i] / 255.0f;
                var b        = collection[i + 1] / 255.0f;
                var c        = collection[i + 2] / 255.0f;
                var d        = collection[i + 3] / 255.0f;
                var newColor = new Color4(a, b, c, d);
                colors.Add(newColor);
            }

            return(colors);
        }
Beispiel #13
0
        public void UpdateSLAMPointCloud()
        {
            int     Error        = 0;
            var     pose         = new List <HelixToolkit.Wpf.SharpDX.Geometry3D.Line>();
            var     line         = new LineBuilder();
            Vector3 previousPose = new Vector3(Convert.ToSingle(poseX), Convert.ToSingle(poseY), Convert.ToSingle(poseZ));

            while (IsSLAMOn)
            {
                var vc = new Vector3Collection();
                var id = new IntCollection();
                var cc = new Color4Collection();

                var     poseVect    = new List <double[]>();
                var     colorVect   = new List <double[]>();
                Vector3 currentPose = new Vector3(Convert.ToSingle(poseX), Convert.ToSingle(poseY), Convert.ToSingle(poseZ));

                line.AddLine(previousPose, currentPose);

                SLAMPoseInfo = line.ToLineGeometry3D();
                previousPose = currentPose;

                try
                {
                    RealsenseControl.GetSLAMPointCloud(ref poseVect, ref colorVect, T265ToLACCOffset.X, T265ToLACCOffset.Y, T265ToLACCOffset.Z);

                    for (int i = 0; i < poseVect.Count; i++)
                    {
                        vc.Add(new Vector3(Convert.ToSingle(poseVect[i][0]), Convert.ToSingle(poseVect[i][1]), Convert.ToSingle(poseVect[i][2])));
                        cc.Add(new Color4(0.1f, 0.1f, 0.1f, 0.5f));
                        //cc.Add(new Color4(Convert.ToSingle(colorVect[i][0]), Convert.ToSingle(colorVect[i][1]), Convert.ToSingle(colorVect[i][2]), 0.5f));
                        id.Add(i);
                    }
                    SLAMPointCloud = new PointGeometry3D()
                    {
                        Positions = vc, Indices = id, Colors = cc
                    };
                }
                catch
                {
                    Error++;
                    Trace.WriteLine("Error Count is " + Error);
                }
                Thread.Sleep(50);
            }
        }
Beispiel #14
0
        /// <summary>
        /// Create the grid
        /// </summary>
        private void DrawGrid()
        {
            Grid = new LineGeometry3D();
            var positions = new Vector3Collection();
            var indices   = new IntCollection();
            var colors    = new Color4Collection();

            for (var i = 0; i < 10; i += 1)
            {
                for (var j = 0; j < 10; j += 1)
                {
                    DrawGridPatch(positions, indices, colors, -50 + i * 10, -50 + j * 10);
                }
            }

            Grid.Positions = positions;
            Grid.Indices   = indices;
            Grid.Colors    = colors;

            Axes = new LineGeometry3D();
            var axesPositions = new Vector3Collection();
            var axesIndices   = new IntCollection();
            var axesColors    = new Color4Collection();

            // Draw the coordinate axes
            axesPositions.Add(new Vector3());
            axesIndices.Add(axesPositions.Count - 1);
            axesPositions.Add(new Vector3(50, 0, 0));
            axesIndices.Add(axesPositions.Count - 1);
            axesColors.Add(SharpDX.Color.Red);
            axesColors.Add(SharpDX.Color.Red);

            axesPositions.Add(new Vector3());
            axesIndices.Add(axesPositions.Count - 1);
            axesPositions.Add(new Vector3(0, 5, 0));
            axesIndices.Add(axesPositions.Count - 1);
            axesColors.Add(SharpDX.Color.Blue);
            axesColors.Add(SharpDX.Color.Blue);

            axesPositions.Add(new Vector3());
            axesIndices.Add(axesPositions.Count - 1);
            axesPositions.Add(new Vector3(0, 0, -50));
            axesIndices.Add(axesPositions.Count - 1);
            axesColors.Add(SharpDX.Color.Green);
            axesColors.Add(SharpDX.Color.Green);

            Axes.Positions = axesPositions;
            Axes.Indices   = axesIndices;
            Axes.Colors    = axesColors;
        }
Beispiel #15
0
        public static Color4Collection Parse(string source)
        {
            IFormatProvider formatProvider = CultureInfo.InvariantCulture;

            var th       = new TokenizerHelper(source, formatProvider);
            var resource = new Color4Collection();

            Color4 value;

            while (th.NextToken())
            {
                value = new Color4(
                    Convert.ToSingle(th.GetCurrentToken(), formatProvider),
                    Convert.ToSingle(th.NextTokenRequired(), formatProvider),
                    Convert.ToSingle(th.NextTokenRequired(), formatProvider),
                    Convert.ToSingle(th.NextTokenRequired(), formatProvider));

                resource.Add(value);
            }

            return(resource);
        }
Beispiel #16
0
        public void UpdateRealTimePointCloud()
        {
            int error = 0;

            while (IsRealsensePipelineOn)
            {
                var vc = new Vector3Collection();
                var id = new IntCollection();
                var cc = new Color4Collection();

                var poseVect  = new List <double[]>();
                var colorVect = new List <double[]>();
                //Debug.WriteLine("Start to get Reatime Data");
                try
                {
                    RealsenseControl.GetRealTimePointCloud(ref poseVect, ref colorVect, T265ToLACCOffset.X, T265ToLACCOffset.Y, T265ToLACCOffset.Z);

                    for (int i = 0; i < poseVect.Count; i++)
                    {
                        vc.Add(new Vector3(Convert.ToSingle(poseVect[i][0]), Convert.ToSingle(poseVect[i][1]), Convert.ToSingle(poseVect[i][2])));
                        cc.Add(new Color4(Convert.ToSingle(colorVect[i][0]), Convert.ToSingle(colorVect[i][1]), Convert.ToSingle(colorVect[i][2]), 0.8f));
                        //id.Add(i);
                    }


                    RTPointCloud = new PointGeometry3D()
                    {
                        Positions = vc, Colors = cc
                    };
                }
                catch
                {
                    error++;
                    Trace.WriteLine("Error Count is " + error);
                }
            }
        }
Beispiel #17
0
        public static void AddPointCloud(Dictionary <KeyValuePair <int, int>, double> PointCollection, out PointModel Points,
                                         out GridModel Grid, out AxisModel Axis, out HelixToolkit.Wpf.SharpDX.Camera Camera, bool cameraType = false)
        {
            Camera = null;
            Points = null;
            Grid   = null;
            Axis   = null;

            var points = new PointGeometry3D();
            var col    = new Color4Collection();  // color gradient
            var ptPos  = new Vector3Collection(); // point positions
            var ptIdx  = new IntCollection();     // point indexes

            if (PointCollection != null && PointCollection.Count() > 1)
            {
                //newdataFromTxt(path, positionX, positionY, positionZ);
                var maxX    = PointCollection.Max(x => x.Key.Key);   // X
                var minX    = PointCollection.Min(x => x.Key.Key);   // X
                var maxY    = PointCollection.Max(x => x.Key.Value); // Y
                var minY    = PointCollection.Min(x => x.Key.Value); // Y
                var maxZ    = PointCollection.Max(x => x.Value);     // Z
                var minZ    = PointCollection.Min(x => x.Value);     // Z
                var minYObj = PointCollection.First(x => x.Key.Value == minY);

                Trace.WriteLine($"{maxX} {minX} {maxY} {minY} {maxZ} {minZ}");

                if (PointCollection.Count() <= 0)
                {
                    return;
                }
                foreach (var point in PointCollection)
                {
                    var positionToColour = point.Value;

                    ptIdx.Add(ptPos.Count);
                    ptPos.Add(new Vector3((float)point.Key.Key, ((float)point.Key.Value), (float)point.Value));
                    col.Add(new Color4(RedBlueScale((float)minZ, (float)maxZ, (float)positionToColour)));
                }
                Points = new PointModel
                {
                    // indexes and color gradient
                    PointsGeometry = new PointGeometry3D {
                        Positions = ptPos, Indices = ptIdx, Colors = col
                    },
                    PointsColor     = Colors.White,
                    PointsTransform = new TranslateTransform3D(-minX, -minY, -minZ)
                };
                // floor plane grid
                LineBuilder gridLines = new LineBuilder();
                VisualizationTools.CreateGrid(gridLines, maxX, minX, maxY, minY, minZ);
                Grid = new GridModel
                {
                    GridGeometry  = gridLines.ToLineGeometry3D(),
                    GridColor     = new Color4(153 / 255.0f, 204 / 255.0f, 255 / 255.0f, 0.3f).ToColor(),
                    GridTransform = new TranslateTransform3D(0, 0, 0)
                };
                // lines
                LineBuilder arrows = new LineBuilder();
                VisualizationTools.CreateAxes(arrows, (float)maxX, (float)minX, (float)maxY, (float)minY, (float)maxZ, (float)minZ);
                Axis = new AxisModel
                {
                    AxisGeometry  = arrows.ToLineGeometry3D(),
                    AxisColor     = new Color4(0, 255 / 255.0f, 255 / 255.0f, 0.5f).ToColor(),
                    AxisTransform = new TranslateTransform3D(0, 0, 0)
                };

                if (cameraType == false)
                {
                    Camera = new HelixToolkit.Wpf.SharpDX.PerspectiveCamera
                    {
                        Position         = new Point3D(0, 0, Math.Abs(minZ * 100)),
                        LookDirection    = new Vector3D(((float)maxX - (float)minX) / 2, ((float)maxY - (float)minY) / 2, -Math.Abs(minZ * 100)),//z kamerą jest problem przy przybliżaniu prawdopodobnie przez ten LookDirection albo sposób poruszania kamerą
                        UpDirection      = new Vector3D(0, 1, 0),
                        FarPlaneDistance = 5000000
                    };
                }
                else
                {
                    Camera = new HelixToolkit.Wpf.SharpDX.OrthographicCamera
                    {
                        Position         = new Point3D(0, 0, Math.Abs(minZ * 100)),
                        LookDirection    = new Vector3D(((float)maxX - (float)minX) / 2, ((float)maxY - (float)minY) / 2, -Math.Abs(minZ * 100)),//z kamerą jest problem przy przybliżaniu prawdopodobnie przez ten LookDirection albo sposób poruszania kamerą
                        UpDirection      = new Vector3D(0, 1, 0),
                        FarPlaneDistance = 5000000
                    };
                }
            }
        }
Beispiel #18
0
        public void UpdateSLAMPointCloud()
        {
            var     line         = new LineBuilder();
            Vector3 previousPose = new Vector3(Convert.ToSingle(systemPoseX), Convert.ToSingle(systemPoseY), Convert.ToSingle(systemPoseZ));


            var     poseVect  = new List <double[]>();
            var     colorVect = new List <double[]>();
            Point3D lineVect  = new Point3D(0, 0, 0.3);

            while (IsSLAMOn)
            {
                Thread.Sleep(500);
                var vc = new Vector3Collection();
                var cc = new Color4Collection();
                poseVect  = new List <double[]>();
                colorVect = new List <double[]>();
                Vector3 currentPose = new Vector3(Convert.ToSingle(systemPoseX), Convert.ToSingle(systemPoseY), Convert.ToSingle(systemPoseZ));

                line.AddLine(previousPose, currentPose);

                SLAMPoseInfo = line.ToLineGeometry3D();
                previousPose = currentPose;


                RealsenseControl.GetSLAMPointCloud(ref poseVect, ref colorVect);

                for (int i = 0; i < poseVect.Count; i++)
                {
                    vc.Add(new Vector3(Convert.ToSingle(poseVect[i][0]), Convert.ToSingle(poseVect[i][1]), Convert.ToSingle(poseVect[i][2])));
                    //cc.Add(new Color4(0.1f, 0.1f, 0.1f, 0.5f));
                    cc.Add(new Color4(Convert.ToSingle(colorVect[i][0]), Convert.ToSingle(colorVect[i][1]), Convert.ToSingle(colorVect[i][2]), 0.5f));
                    //id.Add(i);
                }
                for (int i = 0; i < 10; ++i)
                {
                    var pointx = new Point3D(0.1 * i, 0, 0);
                    var pointy = new Point3D(0, 0.1 * i, 0);
                    var pointz = new Point3D(0, 0, 0.1 * i);
                    vc.Add(new Vector3(Convert.ToSingle(pointx.X), Convert.ToSingle(pointx.Y), Convert.ToSingle(pointx.Z)));
                    cc.Add(new Color4(1f, 0f, 0f, 1f));
                    vc.Add(new Vector3(Convert.ToSingle(pointy.X), Convert.ToSingle(pointy.Y), Convert.ToSingle(pointy.Z)));
                    cc.Add(new Color4(0f, 1f, 0f, 1f));
                    vc.Add(new Vector3(Convert.ToSingle(pointz.X), Convert.ToSingle(pointz.Y), Convert.ToSingle(pointz.Z)));
                    cc.Add(new Color4(0f, 0f, 1f, 1f));
                }


                for (int i = 0; i < 10; ++i)
                {
                    var pointx = currentSystemTranformation.Transform(new Point3D(0.1 * i, 0, 0));
                    var pointy = currentSystemTranformation.Transform(new Point3D(0, 0.1 * i, 0));
                    var pointz = currentSystemTranformation.Transform(new Point3D(0, 0, 0.1 * i));
                    vc.Add(new Vector3(Convert.ToSingle(pointx.X), Convert.ToSingle(pointx.Y), Convert.ToSingle(pointx.Z)));
                    cc.Add(new Color4(1f, 0f, 0f, 1f));
                    vc.Add(new Vector3(Convert.ToSingle(pointy.X), Convert.ToSingle(pointy.Y), Convert.ToSingle(pointy.Z)));
                    cc.Add(new Color4(0f, 1f, 0f, 1f));
                    vc.Add(new Vector3(Convert.ToSingle(pointz.X), Convert.ToSingle(pointz.Y), Convert.ToSingle(pointz.Z)));
                    cc.Add(new Color4(0f, 0f, 1f, 1f));
                }

                SLAMPointCloud = new PointGeometry3D()
                {
                    Positions = vc, Colors = cc
                };

                SLAMVector3s = vc;
            }
            Debug.WriteLine("SLAM Point Cloud Count is " + poseVect.Count);
        }
Beispiel #19
0
        private void LoadThreadFunction()
        {
            PCD wrapper = new PCD();

            Points[] rawPoints = wrapper.LoadPointcloud(Filename);

            var points = new PointGeometry3D();
            var col = new Color4Collection();
            var ptPos = new Vector3Collection();
            var ptIdx = new IntCollection();
            var ptNormals = new Vector3Collection();

            var numberOfElements = rawPoints.Length;

            var additionalTurns = 0;

            foreach (var point in rawPoints)
            {
                ptIdx.Add(ptPos.Count);

                ptPos.Add(new Vector3(point.x, point.z, -point.y));
                col.Add(new Color4(new Color3(point.r / (float)255, point.g / (float)255, point.b / (float)255)));
                ptNormals.Add(new Vector3(0, 1, 0));
            }

            if ((rawPoints.Length / 3) * 3 != rawPoints.Length)
            {
                additionalTurns = ((rawPoints.Length / 3 + 1) * 3) - rawPoints.Length;
            }

            for (int i = 0; i < additionalTurns; i++)
            {
                ptIdx.Add(ptPos.Count);

                ptPos.Add(ptPos[ptPos.Count-1]);
                col.Add(col[col.Count - 1]);
                ptNormals.Add(ptNormals[ptNormals.Count - 1]);
            }

            points.Positions = ptPos;
            points.Indices = ptIdx;
            points.Colors = col;
            //points.Normals = ptNormals;

            Data = points;
        }
Beispiel #20
0
        /// <summary>
        /// Parses the MDL file to obtain model information
        /// </summary>
        /// <param name="selectedItem">The currently selected item</param>
        /// <param name="selectedRace">The currently selected race</param>
        /// <param name="selectedBody">The currently selected body</param>
        /// <param name="selectedPart">The currently selected part</param>
        /// <param name="selectedCategory">The items category </param>
        public MDL(ItemData selectedItem, string selectedCategory, string selectedRace, string selectedBody, string selectedPart)
        {
            string itemType  = Helper.GetCategoryType(selectedCategory);
            string MDLFolder = "";

            if (itemType.Equals("weapon") || itemType.Equals("food"))
            {
                if (selectedPart.Equals("Secondary"))
                {
                    MDLFolder = string.Format(Strings.WeapMDLFolder, selectedItem.SecondaryModelID, selectedItem.SecondaryModelBody);
                    MDLFile   = string.Format(Strings.WeapMDLFile, selectedItem.SecondaryModelID, selectedItem.SecondaryModelBody);
                }
                else
                {
                    MDLFolder = string.Format(Strings.WeapMDLFolder, selectedItem.PrimaryModelID, selectedItem.PrimaryModelBody);
                    MDLFile   = string.Format(Strings.WeapMDLFile, selectedItem.PrimaryModelID, selectedItem.PrimaryModelBody);
                }
            }
            else if (itemType.Equals("accessory"))
            {
                MDLFolder = string.Format(Strings.AccMDLFolder, selectedItem.PrimaryModelID);
                MDLFile   = string.Format(Strings.AccMDLFile, selectedRace, selectedItem.PrimaryModelID, Info.slotAbr[selectedCategory]);
            }
            else if (itemType.Equals("character"))
            {
                if (selectedItem.ItemName.Equals(Strings.Body))
                {
                    MDLFolder = string.Format(Strings.BodyMDLFolder, selectedRace, selectedBody.PadLeft(4, '0'));
                    MDLFile   = string.Format(Strings.BodyMDLFile, selectedRace, selectedBody.PadLeft(4, '0'), selectedPart);
                }
                else if (selectedItem.ItemName.Equals(Strings.Face))
                {
                    MDLFolder = string.Format(Strings.FaceMDLFolder, selectedRace, selectedBody.PadLeft(4, '0'));
                    MDLFile   = string.Format(Strings.FaceMDLFile, selectedRace, selectedBody.PadLeft(4, '0'), selectedPart);
                }
                else if (selectedItem.ItemName.Equals(Strings.Hair))
                {
                    MDLFolder = string.Format(Strings.HairMDLFolder, selectedRace, selectedBody.PadLeft(4, '0'));
                    MDLFile   = string.Format(Strings.HairMDLFile, selectedRace, selectedBody.PadLeft(4, '0'), selectedPart);
                }
                else if (selectedItem.ItemName.Equals(Strings.Tail))
                {
                    MDLFolder = string.Format(Strings.TailMDLFolder, selectedRace, selectedBody.PadLeft(4, '0'));
                    MDLFile   = string.Format(Strings.TailMDLFile, selectedRace, selectedBody.PadLeft(4, '0'), selectedPart);
                }
            }
            else if (itemType.Equals("monster"))
            {
                bool isDemiHuman = false;
                if (selectedItem.PrimaryMTRLFolder != null)
                {
                    isDemiHuman = selectedItem.PrimaryMTRLFolder.Contains("demihuman");
                }

                string ID   = "";
                string body = "";

                if (selectedCategory.Equals(Strings.Pets))
                {
                    int part = 1;

                    if (selectedItem.ItemName.Equals(Strings.Selene) || selectedItem.ItemName.Equals(Strings.Bishop_Autoturret))
                    {
                        part = 2;
                    }

                    ID   = Info.petID[selectedItem.ItemName];
                    body = part.ToString().PadLeft(4, '0');
                }
                else
                {
                    ID   = selectedItem.PrimaryModelID.PadLeft(4, '0');
                    body = selectedItem.PrimaryModelBody;
                }

                if (isDemiHuman)
                {
                    MDLFolder = string.Format(Strings.DemiMDLFolder, ID, body);
                    MDLFile   = string.Format(Strings.DemiMDLFile, ID, body, selectedPart);
                }
                else
                {
                    MDLFolder = string.Format(Strings.MonsterMDLFolder, ID, body);
                    MDLFile   = string.Format(Strings.MonsterMDLFile, ID, body);
                }
            }
            else
            {
                MDLFolder = string.Format(Strings.EquipMDLFolder, selectedItem.PrimaryModelID);

                if (selectedPart.Equals("-"))
                {
                    MDLFile = string.Format(Strings.EquipMDLFile, selectedRace, selectedItem.PrimaryModelID, Info.slotAbr[selectedCategory]);
                }
                else
                {
                    MDLFile = string.Format(Strings.EquipMDLFile, selectedRace, selectedItem.PrimaryModelID, selectedPart);
                }
            }

            fullPath = MDLFolder + "/" + MDLFile;
            int offset = Helper.GetDataOffset(FFCRC.GetHash(MDLFolder), FFCRC.GetHash(MDLFile), Strings.ItemsDat);

            if (offset == 0)
            {
                if (itemType.Equals("weapon"))
                {
                    if (selectedPart.Equals("Secondary"))
                    {
                        MDLFolder = string.Format(Strings.EquipMDLFolder, selectedItem.SecondaryModelID);
                        MDLFile   = string.Format(Strings.EquipMDLFile, "0101", selectedItem.SecondaryModelID, Info.slotAbr[Strings.Hands]);

                        offset = Helper.GetDataOffset(FFCRC.GetHash(MDLFolder), FFCRC.GetHash(MDLFile), Strings.ItemsDat);
                    }
                }
            }

            int datNum = ((offset / 8) & 0x000f) / 2;

            var MDLDatData = Helper.GetType3DecompressedData(offset, datNum, Strings.ItemsDat);

            using (BinaryReader br = new BinaryReader(new MemoryStream(MDLDatData.Item1)))
            {
                // The size of the header + (size of the mesh information block (136 bytes) * the number of meshes) + padding
                br.BaseStream.Seek(64 + 136 * MDLDatData.Item2 + 4, SeekOrigin.Begin);

                var modelStringCount = br.ReadInt32();
                var stringBlockSize  = br.ReadInt32();
                var stringBlock      = br.ReadBytes(stringBlockSize);

                var unknown = br.ReadBytes(4);

                var totalMeshCount       = br.ReadInt16();
                var attributeStringCount = br.ReadInt16();
                var meshPartsCount       = br.ReadInt16();
                var materialStringCount  = br.ReadInt16();
                var boneStringCount      = br.ReadInt16();
                var boneListCount        = br.ReadInt16();

                var unknown1 = br.ReadInt16();
                var unknown2 = br.ReadInt16();
                var unknown3 = br.ReadInt16();
                var unknown4 = br.ReadInt16();
                var unknown5 = br.ReadInt16();
                var unknown6 = br.ReadInt16();

                br.ReadBytes(10);

                var unknown7 = br.ReadInt16();

                br.ReadBytes(16);

                using (BinaryReader br1 = new BinaryReader(new MemoryStream(stringBlock)))
                {
                    br1.BaseStream.Seek(0, SeekOrigin.Begin);

                    for (int i = 0; i < attributeStringCount; i++)
                    {
                        while (br1.ReadByte() != 0)
                        {
                            //extract each atribute string here
                        }
                    }

                    for (int i = 0; i < boneStringCount; i++)
                    {
                        byte        b;
                        List <byte> boneName = new List <byte>();
                        while ((b = br1.ReadByte()) != 0)
                        {
                            boneName.Add(b);
                        }
                        string bone = Encoding.ASCII.GetString(boneName.ToArray());
                        bone = bone.Replace("\0", "");

                        boneStrings.Add(bone);
                    }

                    for (int i = 0; i < materialStringCount; i++)
                    {
                        byte        b;
                        List <byte> name = new List <byte>();
                        while ((b = br1.ReadByte()) != 0)
                        {
                            name.Add(b);
                        }
                        string material = Encoding.ASCII.GetString(name.ToArray());
                        material = material.Replace("\0", "");

                        materialStrings.Add(material);
                    }
                }

                br.ReadBytes(32 * unknown5);

                for (int i = 0; i < 3; i++)
                {
                    LevelOfDetail LoD = new LevelOfDetail();
                    LoD.MeshOffset = br.ReadInt16();
                    LoD.MeshCount  = br.ReadInt16();

                    br.ReadBytes(40);

                    LoD.VertexDataSize = br.ReadInt32();
                    LoD.IndexDataSize  = br.ReadInt32();
                    LoD.VertexOffset   = br.ReadInt32();
                    LoD.IndexOffset    = br.ReadInt32();

                    modelData.LoD.Add(LoD);
                }

                var savePos = br.BaseStream.Position;

                for (int i = 0; i < modelData.LoD.Count; i++)
                {
                    List <MeshDataInfo> meshInfoList = new List <MeshDataInfo>();

                    for (int j = 0; j < modelData.LoD[i].MeshCount; j++)
                    {
                        modelData.LoD[i].MeshList.Add(new Mesh());
                        meshInfoList.Clear();

                        br.BaseStream.Seek((i * 136) + 68, SeekOrigin.Begin);
                        var dataBlockNum = br.ReadByte();

                        while (dataBlockNum != 255)
                        {
                            MeshDataInfo meshInfo = new MeshDataInfo()
                            {
                                VertexDataBlock = dataBlockNum,
                                Offset          = br.ReadByte(),
                                DataType        = br.ReadByte(),
                                UseType         = br.ReadByte()
                            };

                            meshInfoList.Add(meshInfo);

                            br.ReadBytes(4);

                            dataBlockNum = br.ReadByte();
                        }

                        modelData.LoD[i].MeshList[j].MeshDataInfoList = meshInfoList.ToArray();
                    }
                }

                br.BaseStream.Seek(savePos, SeekOrigin.Begin);

                for (int x = 0; x < modelData.LoD.Count; x++)
                {
                    for (int i = 0; i < modelData.LoD[x].MeshCount; i++)
                    {
                        MeshInfo meshInfo = new MeshInfo()
                        {
                            VertexCount     = br.ReadInt32(),
                            IndexCount      = br.ReadInt32(),
                            MaterialNum     = br.ReadInt16(),
                            MeshPartOffset  = br.ReadInt16(),
                            MeshPartCount   = br.ReadInt16(),
                            BoneListIndex   = br.ReadInt16(),
                            IndexDataOffset = br.ReadInt32()
                        };

                        for (int j = 0; j < 3; j++)
                        {
                            meshInfo.VertexDataOffsets.Add(br.ReadInt32());
                        }

                        for (int k = 0; k < 3; k++)
                        {
                            meshInfo.VertexSizes.Add(br.ReadByte());
                        }

                        meshInfo.VertexDataBlockCount = br.ReadByte();

                        modelData.LoD[x].MeshList[i].MeshInfo = meshInfo;
                    }
                }

                br.ReadBytes(attributeStringCount * 4);
                br.ReadBytes(unknown6 * 20);

                for (int i = 0; i < modelData.LoD.Count; i++)
                {
                    foreach (var mesh in modelData.LoD[i].MeshList)
                    {
                        for (int j = 0; j < mesh.MeshInfo.MeshPartCount; j++)
                        {
                            MeshPart meshPart = new MeshPart()
                            {
                                IndexOffset = br.ReadInt32(),
                                IndexCount  = br.ReadInt32(),
                                Attributes  = br.ReadInt32(),
                                BoneOffset  = br.ReadInt16(),
                                BoneCount   = br.ReadInt16()
                            };

                            mesh.MeshPartList.Add(meshPart);
                        }
                    }
                }

                br.ReadBytes(unknown7 * 12);
                br.ReadBytes(materialStringCount * 4);
                br.ReadBytes(boneStringCount * 4);

                for (int i = 0; i < boneListCount; i++)
                {
                    Bones bones = new Bones();

                    for (int j = 0; j < 64; j++)
                    {
                        bones.BoneData.Add(br.ReadInt16());
                    }

                    bones.BoneCount = br.ReadInt32();

                    modelData.BoneSet.Add(bones);
                }

                //br.ReadBytes(unknown1 * 16);

                Dictionary <int, int>         indexMin     = new Dictionary <int, int>();
                Dictionary <int, List <int> > extraIndices = new Dictionary <int, List <int> >();
                List <ExtraIndex>             indexCounts  = new List <ExtraIndex>();

                var pCount  = 0;
                var pCount1 = 0;
                var pCount2 = 0;

                if (unknown1 > 0)
                {
                    for (int i = 0; i < unknown1; i++)
                    {
                        //not sure
                        br.ReadBytes(4);

                        //LoD[0] Extra Data Index
                        var p1 = br.ReadUInt16();

                        //LoD[1] Extra Data Index
                        var p2 = br.ReadUInt16();

                        //LoD[2] Extra Data Index
                        var p3 = br.ReadUInt16();


                        //LoD[0] Extra Data Part Count
                        var p1n = br.ReadUInt16();
                        pCount += p1n;

                        //LoD[1] Extra Data Part Count
                        var p2n = br.ReadUInt16();
                        pCount1 += p2n;

                        //LoD[2] Extra Data Part Count
                        var p3n = br.ReadUInt16();
                        pCount2 += p3n;
                    }
                }

                Dictionary <int, int> indexLoc = new Dictionary <int, int>();

                if (unknown1 > 0)
                {
                    for (int i = 0; i < modelData.LoD[0].MeshCount; i++)
                    {
                        var ido = modelData.LoD[0].MeshList[i].MeshInfo.IndexDataOffset;
                        indexLoc.Add(ido, i);
                    }
                }


                List <int>            maskCounts       = new List <int>();
                Dictionary <int, int> totalExtraCounts = new Dictionary <int, int>();
                if (unknown2 > 0)
                {
                    for (int i = 0; i < pCount; i++)
                    {
                        //Index Offset Start
                        var m1   = br.ReadInt32();
                        var iLoc = 0;
                        if (indexLoc.ContainsKey(m1))
                        {
                            iLoc = indexLoc[m1];
                        }

                        //index count
                        var mCount = br.ReadInt32();

                        //index offset in unk3
                        var mOffset = br.ReadInt32();

                        indexCounts.Add(new ExtraIndex()
                        {
                            IndexLocation = iLoc, IndexCount = mCount
                        });
                        maskCounts.Add(mCount);
                    }

                    br.ReadBytes((pCount1 + pCount2) * 12);
                }

                int totalLoD0MaskCount = 0;

                if (unknown2 > 0)
                {
                    for (int i = 0; i < pCount; i++)
                    {
                        totalLoD0MaskCount += maskCounts[i];
                    }
                }

                if (unknown3 > 0)
                {
                    var unk3Remainder = (unknown3 * 4) - (totalLoD0MaskCount * 4);

                    foreach (var ic in indexCounts)
                    {
                        HashSet <int> mIndexList = new HashSet <int>();

                        for (int i = 0; i < ic.IndexCount; i++)
                        {
                            //index its replacing? attatched to?
                            br.ReadBytes(2);
                            //extra index following last equipment index
                            var mIndex = br.ReadInt16();
                            mIndexList.Add(mIndex);

                            if (extraIndices.ContainsKey(ic.IndexLocation))
                            {
                                extraIndices[ic.IndexLocation].Add(mIndex);
                            }
                            else
                            {
                                extraIndices.Add(ic.IndexLocation, new List <int>()
                                {
                                    mIndex
                                });
                            }
                        }

                        if (totalExtraCounts.ContainsKey(ic.IndexLocation))
                        {
                            totalExtraCounts[ic.IndexLocation] += mIndexList.Count;
                        }
                        else
                        {
                            totalExtraCounts.Add(ic.IndexLocation, mIndexList.Count);
                        }
                    }
                    //the rest of unk3
                    br.ReadBytes(unk3Remainder);
                }



                if (unknown3 > 0)
                {
                    foreach (var ei in extraIndices)
                    {
                        indexMin.Add(ei.Key, ei.Value.Min());
                    }

                    extraIndexData.indexCounts      = indexCounts;
                    extraIndexData.indexMin         = indexMin;
                    extraIndexData.totalExtraCounts = totalExtraCounts;
                    extraIndexData.extraIndices     = extraIndices;

                    modelData.ExtraData = extraIndexData;
                }

                //br.ReadBytes(unknown3 * 4);

                var boneIndexSize = br.ReadInt32();

                for (int i = 0; i < boneIndexSize / 2; i++)
                {
                    modelData.BoneIndicies.Add(br.ReadInt16());
                }

                int padding = br.ReadByte();
                br.ReadBytes(padding);

                for (int i = 0; i < 4; i++)
                {
                    ModelMaterial.BoundingBox boundingBox = new ModelMaterial.BoundingBox();
                    for (int j = 0; j < 4; j++)
                    {
                        boundingBox.PointA.Add(br.ReadSingle());
                    }
                    for (int k = 0; k < 4; k++)
                    {
                        boundingBox.PointB.Add(br.ReadSingle());
                    }

                    modelData.BoundingBoxes.Add(boundingBox);
                }

                //float4x4
                for (int i = 0; i < boneStringCount; i++)
                {
                    boneTransforms.Add(br.ReadSingle());
                    boneTransforms.Add(br.ReadSingle());
                    boneTransforms.Add(br.ReadSingle());
                    boneTransforms.Add(br.ReadSingle());

                    boneTransforms.Add(br.ReadSingle());
                    boneTransforms.Add(br.ReadSingle());
                    boneTransforms.Add(br.ReadSingle());
                    boneTransforms.Add(br.ReadSingle());
                }

                for (int i = 0; i < 3; i++)
                {
                    for (int j = 0; j < modelData.LoD[i].MeshCount; j++)
                    {
                        Mesh mesh = modelData.LoD[i].MeshList[j];

                        for (int k = 0; k < mesh.MeshInfo.VertexDataBlockCount; k++)
                        {
                            br.BaseStream.Seek(modelData.LoD[i].VertexOffset + mesh.MeshInfo.VertexDataOffsets[k], SeekOrigin.Begin);

                            mesh.MeshVertexData.Add(br.ReadBytes(mesh.MeshInfo.VertexSizes[k] * mesh.MeshInfo.VertexCount));
                        }

                        br.BaseStream.Seek(modelData.LoD[i].IndexOffset + (mesh.MeshInfo.IndexDataOffset * 2), SeekOrigin.Begin);

                        mesh.IndexData = br.ReadBytes(2 * mesh.MeshInfo.IndexCount);
                    }
                }

                int vertex = 0, coordinates = 0, normals = 0, tangents = 0, colors = 0, blendWeights = 0, blendIndices = 0;

                for (int i = 0; i < modelData.LoD[0].MeshCount; i++)
                {
                    objBytes.Clear();

                    var vertexList        = new Vector3Collection();
                    var texCoordList      = new Vector2Collection();
                    var texCoordList2     = new Vector2Collection();
                    var normalList        = new Vector3Collection();
                    var tangentList       = new Vector3Collection();
                    var colorsList        = new Color4Collection();
                    var indexList         = new IntCollection();
                    var blendWeightList   = new List <float>();
                    var blendWeightList2  = new List <float[]>();
                    var blendIndicesList  = new List <int>();
                    var blendIndicesList2 = new List <int[]>();
                    var weightCounts      = new List <int>();


                    Mesh mesh = modelData.LoD[0].MeshList[i];

                    MeshDataInfo[] meshDataInfoList = mesh.MeshDataInfoList;

                    int c = 0;
                    foreach (var meshDataInfo in meshDataInfoList)
                    {
                        if (meshDataInfo.UseType == 0)
                        {
                            vertex = c;
                        }
                        else if (meshDataInfo.UseType == 1)
                        {
                            blendWeights = c;
                        }
                        else if (meshDataInfo.UseType == 2)
                        {
                            blendIndices = c;
                        }
                        else if (meshDataInfo.UseType == 3)
                        {
                            normals = c;
                        }
                        else if (meshDataInfo.UseType == 4)
                        {
                            coordinates = c;
                        }
                        else if (meshDataInfo.UseType == 6)
                        {
                            tangents = c;
                        }
                        else if (meshDataInfo.UseType == 7)
                        {
                            colors = c;
                        }

                        c++;
                    }

                    /*
                     * -----------------
                     * Vertex
                     * -----------------
                     */
                    using (BinaryReader br1 = new BinaryReader(new MemoryStream(mesh.MeshVertexData[meshDataInfoList[vertex].VertexDataBlock])))
                    {
                        for (int j = 0; j < mesh.MeshInfo.VertexCount; j++)
                        {
                            br1.BaseStream.Seek(j * mesh.MeshInfo.VertexSizes[meshDataInfoList[vertex].VertexDataBlock] + meshDataInfoList[vertex].Offset, SeekOrigin.Begin);

                            Vector3 vVector = new Vector3();
                            if (meshDataInfoList[vertex].DataType == 13 || meshDataInfoList[vertex].DataType == 14)
                            {
                                System.Half h1 = System.Half.ToHalf((ushort)br1.ReadInt16());
                                System.Half h2 = System.Half.ToHalf((ushort)br1.ReadInt16());
                                System.Half h3 = System.Half.ToHalf((ushort)br1.ReadInt16());

                                float x = HalfHelper.HalfToSingle(h1);
                                float y = HalfHelper.HalfToSingle(h2);
                                float z = HalfHelper.HalfToSingle(h3);

                                vVector = new Vector3(x, y, z);
                                objBytes.Add("v " + x.ToString("N5") + " " + y.ToString("N5") + " " + z.ToString("N5") + " ");
                            }
                            else if (meshDataInfoList[vertex].DataType == 2)
                            {
                                var x = BitConverter.ToSingle(br1.ReadBytes(4), 0);
                                var y = BitConverter.ToSingle(br1.ReadBytes(4), 0);
                                var z = BitConverter.ToSingle(br1.ReadBytes(4), 0);

                                vVector = new Vector3(x, y, z);
                                objBytes.Add("v " + x.ToString("N5") + " " + y.ToString("N5") + " " + z.ToString("N5") + " ");
                            }

                            vertexList.Add(vVector);
                        }
                    }


                    /*
                     * -----------------
                     * Blend Weight
                     * -----------------
                     */
                    using (BinaryReader br1 = new BinaryReader(new MemoryStream(mesh.MeshVertexData[meshDataInfoList[blendWeights].VertexDataBlock])))
                    {
                        for (int j = 0; j < mesh.MeshInfo.VertexCount; j++)
                        {
                            br1.BaseStream.Seek(j * mesh.MeshInfo.VertexSizes[meshDataInfoList[blendWeights].VertexDataBlock] + meshDataInfoList[blendWeights].Offset, SeekOrigin.Begin);

                            float x = br1.ReadByte() / 255.0f;
                            float y = br1.ReadByte() / 255.0f;
                            float z = br1.ReadByte() / 255.0f;
                            float w = br1.ReadByte() / 255.0f;

                            int count = 0;
                            if (x != 0)
                            {
                                blendWeightList.Add(x);
                                count++;

                                if (y != 0)
                                {
                                    blendWeightList.Add(y);
                                    count++;

                                    if (z != 0)
                                    {
                                        blendWeightList.Add(z);
                                        count++;

                                        if (w != 0)
                                        {
                                            blendWeightList.Add(w);
                                            count++;
                                        }
                                    }
                                }
                            }

                            if (count == 1)
                            {
                                blendWeightList2.Add(new float[] { x });
                            }
                            else if (count == 2)
                            {
                                blendWeightList2.Add(new float[] { x, y });
                            }
                            else if (count == 3)
                            {
                                blendWeightList2.Add(new float[] { x, y, z });
                            }
                            else if (count == 4)
                            {
                                blendWeightList2.Add(new float[] { x, y, z, w });
                            }

                            weightCounts.Add(count);
                        }
                    }


                    /*
                     * -----------------
                     * Blend Index
                     * -----------------
                     */
                    using (BinaryReader br1 = new BinaryReader(new MemoryStream(mesh.MeshVertexData[meshDataInfoList[blendIndices].VertexDataBlock])))
                    {
                        for (int j = 0; j < mesh.MeshInfo.VertexCount; j++)
                        {
                            br1.BaseStream.Seek(j * mesh.MeshInfo.VertexSizes[meshDataInfoList[blendIndices].VertexDataBlock] + meshDataInfoList[blendIndices].Offset, SeekOrigin.Begin);

                            int x = br1.ReadByte();
                            int y = br1.ReadByte();
                            int z = br1.ReadByte();
                            int w = br1.ReadByte();

                            if (weightCounts[j] == 1)
                            {
                                blendIndicesList.Add(x);
                                blendIndicesList2.Add(new int[] { x });
                            }
                            else if (weightCounts[j] == 2)
                            {
                                blendIndicesList.Add(x);
                                blendIndicesList.Add(y);
                                blendIndicesList2.Add(new int[] { x, y });
                            }
                            else if (weightCounts[j] == 3)
                            {
                                blendIndicesList.Add(x);
                                blendIndicesList.Add(y);
                                blendIndicesList.Add(z);
                                blendIndicesList2.Add(new int[] { x, y, z });
                            }
                            else if (weightCounts[j] == 4)
                            {
                                blendIndicesList.Add(x);
                                blendIndicesList.Add(y);
                                blendIndicesList.Add(z);
                                blendIndicesList.Add(w);
                                blendIndicesList2.Add(new int[] { x, y, z, w });
                            }
                        }
                    }


                    /*
                     * -----------------
                     * Texture Coordinates
                     * -----------------
                     */
                    using (BinaryReader br1 = new BinaryReader(new MemoryStream(mesh.MeshVertexData[meshDataInfoList[coordinates].VertexDataBlock])))
                    {
                        for (int j = 0; j < mesh.MeshInfo.VertexCount; j++)
                        {
                            br1.BaseStream.Seek(j * mesh.MeshInfo.VertexSizes[meshDataInfoList[coordinates].VertexDataBlock] + meshDataInfoList[coordinates].Offset, SeekOrigin.Begin);

                            float x = 0;
                            float y = 0;
                            float z = 0;
                            float w = 0;
                            if (meshDataInfoList[coordinates].DataType == 13 || meshDataInfoList[coordinates].DataType == 14)
                            {
                                var sx = (ushort)br1.ReadInt16();
                                var sy = (ushort)br1.ReadInt16();
                                var sz = (ushort)br1.ReadInt16();
                                var sw = (ushort)br1.ReadInt16();

                                var h1 = new SharpDX.Half(sx);
                                var h2 = new SharpDX.Half(sy);
                                var h3 = new SharpDX.Half(sz);
                                var h4 = new SharpDX.Half(sw);

                                x = h1;
                                y = h2;
                                z = h3;
                                w = h4;
                            }
                            else if (meshDataInfoList[coordinates].DataType == 1)
                            {
                                x = br1.ReadSingle();
                                y = br1.ReadSingle();
                            }
                            else
                            {
                                x = br1.ReadSingle();
                                y = br1.ReadSingle();
                                z = br1.ReadSingle();
                                w = br1.ReadSingle();
                            }


                            var ox = x - Math.Truncate(x);
                            var oy = y - Math.Truncate(y);

                            objBytes.Add("vt " + ox.ToString("N5") + " " + (1 - y).ToString("N5") + " ");
                            texCoordList.Add(new Vector2(x, y));
                            texCoordList2.Add(new Vector2(z, w));
                        }
                    }



                    /*
                     * -----------------
                     * Normals
                     * -----------------
                     */
                    using (BinaryReader br1 = new BinaryReader(new MemoryStream(mesh.MeshVertexData[meshDataInfoList[normals].VertexDataBlock])))
                    {
                        for (int j = 0; j < mesh.MeshInfo.VertexCount; j++)
                        {
                            br1.BaseStream.Seek(j * mesh.MeshInfo.VertexSizes[meshDataInfoList[normals].VertexDataBlock] + meshDataInfoList[normals].Offset, SeekOrigin.Begin);

                            float x = 0;
                            float y = 0;
                            float z = 0;
                            float w = 0;

                            if (meshDataInfoList[normals].DataType == 13 || meshDataInfoList[normals].DataType == 14)
                            {
                                System.Half h1 = System.Half.ToHalf((ushort)br1.ReadInt16());
                                System.Half h2 = System.Half.ToHalf((ushort)br1.ReadInt16());
                                System.Half h3 = System.Half.ToHalf((ushort)br1.ReadInt16());
                                System.Half h4 = System.Half.ToHalf((ushort)br1.ReadInt16());

                                x = HalfHelper.HalfToSingle(h1);
                                y = HalfHelper.HalfToSingle(h2);
                                z = HalfHelper.HalfToSingle(h3);
                                w = HalfHelper.HalfToSingle(h4);
                            }
                            else
                            {
                                x = br1.ReadSingle();
                                y = br1.ReadSingle();
                                z = br1.ReadSingle();
                            }

                            var nv = new Vector3(x, y, z);

                            objBytes.Add("vn " + x.ToString("N5") + " " + y.ToString("N5") + " " + z.ToString("N5") + " ");
                            normalList.Add(nv);
                        }
                    }


                    /*
                     * -----------------
                     * Tangents
                     * -----------------
                     */
                    using (BinaryReader br1 = new BinaryReader(new MemoryStream(mesh.MeshVertexData[meshDataInfoList[tangents].VertexDataBlock])))
                    {
                        for (int j = 0; j < mesh.MeshInfo.VertexCount; j++)
                        {
                            br1.BaseStream.Seek(j * mesh.MeshInfo.VertexSizes[meshDataInfoList[tangents].VertexDataBlock] + meshDataInfoList[tangents].Offset, SeekOrigin.Begin);

                            int x = br1.ReadByte();
                            int y = br1.ReadByte();
                            int z = br1.ReadByte();
                            int w = br1.ReadByte();

                            var x1 = x * 2 / 255f - 1f;
                            var y1 = y * 2 / 255f - 1f;
                            var z1 = z * 2 / 255f - 1f;
                            var w1 = w * 2 / 255f - 1f;


                            var nv = new Vector3(x1, y1, z1);

                            tangentList.Add(nv);
                        }
                    }


                    /*
                     * -----------------
                     * Vertex Color
                     * -----------------
                     */
                    using (BinaryReader br1 = new BinaryReader(new MemoryStream(mesh.MeshVertexData[meshDataInfoList[colors].VertexDataBlock])))
                    {
                        for (int j = 0; j < mesh.MeshInfo.VertexCount; j++)
                        {
                            br1.BaseStream.Seek(j * mesh.MeshInfo.VertexSizes[meshDataInfoList[colors].VertexDataBlock] + meshDataInfoList[colors].Offset, SeekOrigin.Begin);

                            int a = br1.ReadByte();
                            int r = br1.ReadByte();
                            int g = br1.ReadByte();
                            int b = br1.ReadByte();

                            colorsList.Add(new Color4(r, g, b, a));
                        }
                    }


                    /*
                     * -----------------
                     * Index
                     * -----------------
                     */
                    using (BinaryReader br1 = new BinaryReader(new MemoryStream(mesh.IndexData)))
                    {
                        for (int j = 0; j < mesh.MeshInfo.IndexCount; j += 3)
                        {
                            int i1 = br1.ReadInt16();
                            int i2 = br1.ReadInt16();
                            int i3 = br1.ReadInt16();

                            objBytes.Add("f " + (i1 + 1) + "/" + (i1 + 1) + "/" + (i1 + 1) + " " + (i2 + 1) + "/" + (i2 + 1) + "/" + (i2 + 1) + " " + (i3 + 1) + "/" + (i3 + 1) + "/" + (i3 + 1) + " ");

                            indexList.Add(i1);
                            indexList.Add(i2);
                            indexList.Add(i3);
                        }
                    }

                    ModelMeshData modelMeshData = new ModelMeshData()
                    {
                        Vertices              = vertexList,
                        Normals               = normalList,
                        TextureCoordinates    = texCoordList,
                        TextureCoordinates2   = texCoordList2,
                        BiTangents            = tangentList,
                        Indices               = indexList,
                        VertexColors          = colorsList,
                        OBJFileData           = objBytes.ToArray(),
                        BoneStrings           = boneStrings,
                        BoneIndices           = modelData.BoneIndicies,
                        BoneTransforms        = boneTransforms,
                        BlendWeights          = blendWeightList,
                        BlendIndices          = blendIndicesList,
                        WeightCounts          = weightCounts,
                        MeshPartList          = mesh.MeshPartList,
                        BlendIndicesArrayList = blendIndicesList2,
                        BlendWeightsArrayList = blendWeightList2,
                        MaterialNum           = mesh.MeshInfo.MaterialNum,
                        MeshPartCount         = mesh.MeshInfo.MeshPartCount,
                        MeshPartOffset        = mesh.MeshInfo.MeshPartOffset
                    };

                    meshList.Add(modelMeshData);
                }
            }
        }
Beispiel #21
0
        public static PointGeometry3D ConvertToPointGeometry3D(Points[] points)
        {
            var geometry = new PointGeometry3D();
            var col = new Color4Collection();
            var ptPos = new Vector3Collection();
            var ptIdx = new IntCollection();
            var ptNormals = new Vector3Collection();

            var additionalTurns = 0;

            foreach (var point in points)
            {
                ptIdx.Add(ptPos.Count);

                ptPos.Add(new Vector3(point.x, point.y, point.z));
                col.Add(new Color4(new Color3(point.r / (float)255, point.g / (float)255, point.b / (float)255)));
                ptNormals.Add(new Vector3(0, 1, 0));
            }

            if ((points.Length / 3) * 3 != points.Length)
            {
                additionalTurns = ((points.Length / 3 + 1) * 3) - points.Length;
            }

            for (int i = 0; i < additionalTurns; i++)
            {
                ptIdx.Add(ptPos.Count);

                ptPos.Add(ptPos[ptPos.Count - 1]);
                col.Add(col[col.Count - 1]);
                ptNormals.Add(ptNormals[ptNormals.Count - 1]);
            }

            geometry.Positions = ptPos;
            geometry.Indices = ptIdx;
            geometry.Colors = col;

            return geometry;
        }
Beispiel #22
0
        /// <summary>
        /// Parses the MDL file to obtain model information
        /// </summary>
        /// <param name="selectedItem">The currently selected item</param>
        /// <param name="selectedRace">The currently selected race</param>
        /// <param name="selectedBody">The currently selected body</param>
        /// <param name="selectedPart">The currently selected part</param>
        /// <param name="selectedCategory">The items category </param>
        public MDL(ItemData selectedItem, string selectedCategory, string selectedRace, string selectedBody, string selectedPart)
        {
            string itemType  = Helper.GetCategoryType(selectedCategory);
            string MDLFolder = "";

            if (itemType.Equals("weapon") || itemType.Equals("food"))
            {
                if (selectedPart.Equals("Secondary"))
                {
                    MDLFolder = string.Format(Strings.WeapMDLFolder, selectedItem.SecondaryModelID, selectedItem.SecondaryModelBody);
                    MDLFile   = string.Format(Strings.WeapMDLFile, selectedItem.SecondaryModelID, selectedItem.SecondaryModelBody);
                }
                else
                {
                    MDLFolder = string.Format(Strings.WeapMDLFolder, selectedItem.PrimaryModelID, selectedItem.PrimaryModelBody);
                    MDLFile   = string.Format(Strings.WeapMDLFile, selectedItem.PrimaryModelID, selectedItem.PrimaryModelBody);
                }
            }
            else if (itemType.Equals("accessory"))
            {
                MDLFolder = string.Format(Strings.AccMDLFolder, selectedItem.PrimaryModelID);
                MDLFile   = string.Format(Strings.AccMDLFile, selectedRace, selectedItem.PrimaryModelID, Info.slotAbr[selectedCategory]);
            }
            else if (itemType.Equals("character"))
            {
                if (selectedItem.ItemName.Equals(Strings.Body))
                {
                    MDLFolder = string.Format(Strings.BodyMDLFolder, selectedRace, selectedBody.PadLeft(4, '0'));
                    MDLFile   = string.Format(Strings.BodyMDLFile, selectedRace, selectedBody.PadLeft(4, '0'), selectedPart);
                }
                else if (selectedItem.ItemName.Equals(Strings.Face))
                {
                    MDLFolder = string.Format(Strings.FaceMDLFolder, selectedRace, selectedBody.PadLeft(4, '0'));
                    MDLFile   = string.Format(Strings.FaceMDLFile, selectedRace, selectedBody.PadLeft(4, '0'), selectedPart);
                }
                else if (selectedItem.ItemName.Equals(Strings.Hair))
                {
                    MDLFolder = string.Format(Strings.HairMDLFolder, selectedRace, selectedBody.PadLeft(4, '0'));
                    MDLFile   = string.Format(Strings.HairMDLFile, selectedRace, selectedBody.PadLeft(4, '0'), selectedPart);
                }
                else if (selectedItem.ItemName.Equals(Strings.Tail))
                {
                    MDLFolder = string.Format(Strings.TailMDLFolder, selectedRace, selectedBody.PadLeft(4, '0'));
                    MDLFile   = string.Format(Strings.TailMDLFile, selectedRace, selectedBody.PadLeft(4, '0'), selectedPart);
                }
            }
            else if (itemType.Equals("monster"))
            {
                bool isDemiHuman = false;
                if (selectedItem.PrimaryMTRLFolder != null)
                {
                    isDemiHuman = selectedItem.PrimaryMTRLFolder.Contains("demihuman");
                }

                string ID   = "";
                string body = "";

                if (selectedCategory.Equals(Strings.Pets))
                {
                    ID   = Info.petID[selectedItem.ItemName];
                    body = "0001";
                }
                else
                {
                    ID   = selectedItem.PrimaryModelID.PadLeft(4, '0');
                    body = selectedItem.PrimaryModelBody;
                }

                if (isDemiHuman)
                {
                    MDLFolder = string.Format(Strings.DemiMDLFolder, ID, body);
                    MDLFile   = string.Format(Strings.DemiMDLFile, ID, body, selectedPart);
                }
                else
                {
                    MDLFolder = string.Format(Strings.MonsterMDLFolder, ID, body);
                    MDLFile   = string.Format(Strings.MonsterMDLFile, ID, body);
                }
            }
            else
            {
                MDLFolder = string.Format(Strings.EquipMDLFolder, selectedItem.PrimaryModelID);
                MDLFile   = string.Format(Strings.EquipMDLFile, selectedRace, selectedItem.PrimaryModelID, Info.slotAbr[selectedCategory]);
            }

            int offset = Helper.GetItemOffset(FFCRC.GetHash(MDLFolder), FFCRC.GetHash(MDLFile));

            int datNum = ((offset / 8) & 0x000f) / 2;

            offset = Helper.OffsetCorrection(datNum, offset);

            var MDLDatData = Helper.GetType3DecompressedData(offset, datNum);

            using (BinaryReader br = new BinaryReader(new MemoryStream(MDLDatData.Item1)))
            {
                ModelData modelData = new ModelData();

                // The size of the header + (size of the mesh information block (136 bytes) * the number of meshes) + padding
                br.BaseStream.Seek(64 + 136 * MDLDatData.Item2 + 4, SeekOrigin.Begin);

                var modelStringCount = br.ReadInt32();
                var stringBlockSize  = br.ReadInt32();
                var stringBlock      = br.ReadBytes(stringBlockSize);

                var unknown = br.ReadBytes(4);

                var totalMeshCount       = br.ReadInt16();
                var attributeStringCount = br.ReadInt16();
                var meshPartsCount       = br.ReadInt16();
                var materialStringCount  = br.ReadInt16();
                var boneStringCount      = br.ReadInt16();
                var boneListCount        = br.ReadInt16();

                var unknown1 = br.ReadInt16();
                var unknown2 = br.ReadInt16();
                var unknown3 = br.ReadInt16();
                var unknown4 = br.ReadInt16();
                var unknown5 = br.ReadInt16();
                var unknown6 = br.ReadInt16();

                br.ReadBytes(10);

                var unknown7 = br.ReadInt16();

                br.ReadBytes(16);

                using (BinaryReader br1 = new BinaryReader(new MemoryStream(stringBlock)))
                {
                    br1.BaseStream.Seek(0, SeekOrigin.Begin);

                    for (int i = 0; i < attributeStringCount; i++)
                    {
                        while (br1.ReadByte() != 0)
                        {
                            //extract each atribute string here
                        }
                    }

                    for (int i = 0; i < boneStringCount; i++)
                    {
                        while (br1.ReadByte() != 0)
                        {
                            //extact each bone string here
                        }
                    }

                    for (int i = 0; i < materialStringCount; i++)
                    {
                        byte        b;
                        List <byte> name = new List <byte>();
                        while ((b = br1.ReadByte()) != 0)
                        {
                            name.Add(b);
                        }
                        string material = Encoding.ASCII.GetString(name.ToArray());
                        material = material.Replace("\0", "");

                        materialStrings.Add(material);
                    }
                }

                br.ReadBytes(32 * unknown5);

                for (int i = 0; i < 3; i++)
                {
                    LevelOfDetail LoD = new LevelOfDetail();
                    LoD.MeshOffset = br.ReadInt16();
                    LoD.MeshCount  = br.ReadInt16();

                    br.ReadBytes(40);

                    LoD.VertexDataSize = br.ReadInt32();
                    LoD.IndexDataSize  = br.ReadInt32();
                    LoD.VertexOffset   = br.ReadInt32();
                    LoD.IndexOffset    = br.ReadInt32();

                    modelData.LoD.Add(LoD);
                }

                var savePos = br.BaseStream.Position;

                for (int i = 0; i < modelData.LoD.Count; i++)
                {
                    List <MeshDataInfo> meshInfoList = new List <MeshDataInfo>();

                    for (int j = 0; j < modelData.LoD[i].MeshCount; j++)
                    {
                        modelData.LoD[i].MeshList.Add(new Mesh());
                        meshInfoList.Clear();

                        br.BaseStream.Seek((i * 136) + 68, SeekOrigin.Begin);
                        var dataBlockNum = br.ReadByte();

                        while (dataBlockNum != 255)
                        {
                            MeshDataInfo meshInfo = new MeshDataInfo()
                            {
                                VertexDataBlock = dataBlockNum,
                                Offset          = br.ReadByte(),
                                DataType        = br.ReadByte(),
                                UseType         = br.ReadByte()
                            };

                            meshInfoList.Add(meshInfo);

                            br.ReadBytes(4);

                            dataBlockNum = br.ReadByte();
                        }

                        modelData.LoD[i].MeshList[j].MeshDataInfoList = meshInfoList.ToArray();
                    }
                }

                br.BaseStream.Seek(savePos, SeekOrigin.Begin);

                for (int x = 0; x < modelData.LoD.Count; x++)
                {
                    for (int i = 0; i < modelData.LoD[x].MeshCount; i++)
                    {
                        MeshInfo meshInfo = new MeshInfo()
                        {
                            VertexCount     = br.ReadInt32(),
                            IndexCount      = br.ReadInt32(),
                            MaterialNum     = br.ReadInt16(),
                            MeshPartOffset  = br.ReadInt16(),
                            MeshPartCount   = br.ReadInt16(),
                            BoneListIndex   = br.ReadInt16(),
                            IndexDataOffset = br.ReadInt32()
                        };

                        for (int j = 0; j < 3; j++)
                        {
                            meshInfo.VertexDataOffsets.Add(br.ReadInt32());
                        }

                        for (int k = 0; k < 3; k++)
                        {
                            meshInfo.VertexSizes.Add(br.ReadByte());
                        }

                        meshInfo.VertexDataBlockCount = br.ReadByte();

                        modelData.LoD[x].MeshList[i].MeshInfo = meshInfo;
                    }
                }

                br.ReadBytes(attributeStringCount * 4);
                br.ReadBytes(unknown6 * 20);

                for (int i = 0; i < modelData.LoD.Count; i++)
                {
                    foreach (var mesh in modelData.LoD[i].MeshList)
                    {
                        for (int j = 0; j < mesh.MeshInfo.MeshPartCount; j++)
                        {
                            MeshPart meshPart = new MeshPart()
                            {
                                IndexOffset = br.ReadInt32(),
                                IndexCount  = br.ReadInt32(),
                                Attributes  = br.ReadInt32(),
                                BoneOffset  = br.ReadInt16(),
                                BoneCount   = br.ReadInt16()
                            };

                            mesh.MeshPartList.Add(meshPart);
                        }
                    }
                }

                br.ReadBytes(unknown7 * 12);
                br.ReadBytes(materialStringCount * 4);
                br.ReadBytes(boneStringCount * 4);

                for (int i = 0; i < boneListCount; i++)
                {
                    Bones bones = new Bones();

                    for (int j = 0; j < 64; j++)
                    {
                        bones.BoneData.Add(br.ReadInt16());
                    }

                    bones.BoneCount = br.ReadInt32();

                    modelData.BoneSet.Add(bones);
                }

                br.ReadBytes(unknown1 * 16);
                br.ReadBytes(unknown2 * 12);
                br.ReadBytes(unknown3 * 4);

                var boneIndexSize = br.ReadInt32();

                for (int i = 0; i < boneIndexSize / 2; i++)
                {
                    modelData.BoneIndicies.Add(br.ReadInt16());
                }

                int padding = br.ReadByte();
                br.ReadBytes(padding);

                for (int i = 0; i < 4; i++)
                {
                    ModelMaterial.BoundingBox boundingBox = new ModelMaterial.BoundingBox();
                    for (int j = 0; j < 4; j++)
                    {
                        boundingBox.PointA.Add(br.ReadSingle());
                    }
                    for (int k = 0; k < 4; k++)
                    {
                        boundingBox.PointB.Add(br.ReadSingle());
                    }

                    modelData.BoundingBoxes.Add(boundingBox);
                }

                for (int i = 0; i < 3; i++)
                {
                    for (int j = 0; j < modelData.LoD[i].MeshCount; j++)
                    {
                        Mesh mesh = modelData.LoD[i].MeshList[j];

                        for (int k = 0; k < mesh.MeshInfo.VertexDataBlockCount; k++)
                        {
                            br.BaseStream.Seek(modelData.LoD[i].VertexOffset + mesh.MeshInfo.VertexDataOffsets[k], SeekOrigin.Begin);

                            mesh.MeshVertexData.Add(br.ReadBytes(mesh.MeshInfo.VertexSizes[k] * mesh.MeshInfo.VertexCount));
                        }

                        br.BaseStream.Seek(modelData.LoD[i].IndexOffset + (mesh.MeshInfo.IndexDataOffset * 2), SeekOrigin.Begin);

                        mesh.IndexData = br.ReadBytes(2 * mesh.MeshInfo.IndexCount);
                    }
                }

                int vertex = 0, coordinates = 0, normals = 0, tangents = 0, colors = 0;

                for (int i = 0; i < modelData.LoD[0].MeshCount; i++)
                {
                    objBytes.Clear();

                    var vertexList   = new Vector3Collection();
                    var texCoordList = new Vector2Collection();
                    var normalList   = new Vector3Collection();
                    var tangentList  = new Vector3Collection();
                    var colorsList   = new Color4Collection();
                    var indexList    = new IntCollection();

                    Mesh mesh = modelData.LoD[0].MeshList[i];

                    MeshDataInfo[] meshDataInfoList = mesh.MeshDataInfoList;

                    int c = 0;
                    foreach (var meshDataInfo in meshDataInfoList)
                    {
                        if (meshDataInfo.UseType == 0)
                        {
                            vertex = c;
                        }
                        else if (meshDataInfo.UseType == 3)
                        {
                            normals = c;
                        }
                        else if (meshDataInfo.UseType == 4)
                        {
                            coordinates = c;
                        }
                        else if (meshDataInfo.UseType == 6)
                        {
                            tangents = c;
                        }
                        else if (meshDataInfo.UseType == 7)
                        {
                            colors = c;
                        }

                        c++;
                    }

                    using (BinaryReader br1 = new BinaryReader(new MemoryStream(mesh.MeshVertexData[meshDataInfoList[vertex].VertexDataBlock])))
                    {
                        for (int j = 0; j < mesh.MeshInfo.VertexCount; j++)
                        {
                            br1.BaseStream.Seek(j * mesh.MeshInfo.VertexSizes[meshDataInfoList[vertex].VertexDataBlock] + meshDataInfoList[vertex].Offset, SeekOrigin.Begin);

                            if (meshDataInfoList[vertex].DataType == 13 || meshDataInfoList[vertex].DataType == 14)
                            {
                                System.Half h1 = System.Half.ToHalf((ushort)br1.ReadInt16());
                                System.Half h2 = System.Half.ToHalf((ushort)br1.ReadInt16());
                                System.Half h3 = System.Half.ToHalf((ushort)br1.ReadInt16());

                                float x = HalfHelper.HalfToSingle(h1);
                                float y = HalfHelper.HalfToSingle(h2);
                                float z = HalfHelper.HalfToSingle(h3);

                                objBytes.Add("v " + x.ToString() + " " + y.ToString() + " " + z.ToString() + " ");
                                vertexList.Add(new Vector3(x, y, z));
                            }
                            else if (meshDataInfoList[vertex].DataType == 2)
                            {
                                float x = br1.ReadSingle();
                                float y = br1.ReadSingle();
                                float z = br1.ReadSingle();

                                objBytes.Add("v " + x.ToString() + " " + y.ToString() + " " + z.ToString() + " ");
                                vertexList.Add(new Vector3(x, y, z));
                            }
                        }
                    }

                    using (BinaryReader br1 = new BinaryReader(new MemoryStream(mesh.MeshVertexData[meshDataInfoList[coordinates].VertexDataBlock])))
                    {
                        for (int j = 0; j < mesh.MeshInfo.VertexCount; j++)
                        {
                            br1.BaseStream.Seek(j * mesh.MeshInfo.VertexSizes[meshDataInfoList[coordinates].VertexDataBlock] + meshDataInfoList[coordinates].Offset, SeekOrigin.Begin);

                            System.Half h1 = System.Half.ToHalf((ushort)br1.ReadInt16());
                            System.Half h2 = System.Half.ToHalf((ushort)br1.ReadInt16());

                            float x = HalfHelper.HalfToSingle(h1);
                            float y = HalfHelper.HalfToSingle(h2);

                            objBytes.Add("vt " + x.ToString() + " " + (y * -1f).ToString() + " ");
                            texCoordList.Add(new Vector2(x, y));
                        }
                    }

                    using (BinaryReader br1 = new BinaryReader(new MemoryStream(mesh.MeshVertexData[meshDataInfoList[normals].VertexDataBlock])))
                    {
                        for (int j = 0; j < mesh.MeshInfo.VertexCount; j++)
                        {
                            br1.BaseStream.Seek(j * mesh.MeshInfo.VertexSizes[meshDataInfoList[normals].VertexDataBlock] + meshDataInfoList[normals].Offset, SeekOrigin.Begin);

                            System.Half h1 = System.Half.ToHalf((ushort)br1.ReadInt16());
                            System.Half h2 = System.Half.ToHalf((ushort)br1.ReadInt16());
                            System.Half h3 = System.Half.ToHalf((ushort)br1.ReadInt16());

                            objBytes.Add("vn " + HalfHelper.HalfToSingle(h1).ToString() + " " + HalfHelper.HalfToSingle(h2).ToString() + " " + HalfHelper.HalfToSingle(h3).ToString() + " ");
                            normalList.Add(new Vector3(HalfHelper.HalfToSingle(h1), HalfHelper.HalfToSingle(h2), HalfHelper.HalfToSingle(h3)));
                        }
                    }

                    using (BinaryReader br1 = new BinaryReader(new MemoryStream(mesh.MeshVertexData[meshDataInfoList[tangents].VertexDataBlock])))
                    {
                        for (int j = 0; j < mesh.MeshInfo.VertexCount; j++)
                        {
                            br1.BaseStream.Seek(j * mesh.MeshInfo.VertexSizes[meshDataInfoList[tangents].VertexDataBlock] + meshDataInfoList[tangents].Offset, SeekOrigin.Begin);

                            float x = br1.ReadByte() / 255f;
                            float y = br1.ReadByte() / 255f;
                            float z = br1.ReadByte() / 255f;

                            tangentList.Add(new Vector3(x, y, z));
                        }
                    }

                    using (BinaryReader br1 = new BinaryReader(new MemoryStream(mesh.MeshVertexData[meshDataInfoList[colors].VertexDataBlock])))
                    {
                        for (int j = 0; j < mesh.MeshInfo.VertexCount; j++)
                        {
                            br1.BaseStream.Seek(j * mesh.MeshInfo.VertexSizes[meshDataInfoList[colors].VertexDataBlock] + meshDataInfoList[colors].Offset, SeekOrigin.Begin);

                            int a = br1.ReadByte();
                            int r = br1.ReadByte();
                            int g = br1.ReadByte();
                            int b = br1.ReadByte();

                            colorsList.Add(new Color4(r, g, b, a));
                        }
                    }

                    using (BinaryReader br1 = new BinaryReader(new MemoryStream(mesh.IndexData)))
                    {
                        for (int j = 0; j < mesh.MeshInfo.IndexCount; j += 3)
                        {
                            int i1 = br1.ReadInt16();
                            int i2 = br1.ReadInt16();
                            int i3 = br1.ReadInt16();

                            objBytes.Add("f " + (i1 + 1) + "/" + (i1 + 1) + "/" + (i1 + 1) + " " + (i2 + 1) + "/" + (i2 + 1) + "/" + (i2 + 1) + " " + (i3 + 1) + "/" + (i3 + 1) + "/" + (i3 + 1) + " ");

                            indexList.Add(i1);
                            indexList.Add(i2);
                            indexList.Add(i3);
                        }
                    }

                    ModelMeshData modelMeshData = new ModelMeshData()
                    {
                        Vertices           = vertexList,
                        Normals            = normalList,
                        TextureCoordinates = texCoordList,
                        Tangents           = tangentList,
                        Indices            = indexList,
                        VertexColors       = colorsList,
                        OBJFileData        = objBytes.ToArray()
                    };

                    meshList.Add(modelMeshData);
                }
            }
        }
Beispiel #23
0
        public static Color4Collection ToColor4Collection(this byte[] collection)
        {
            var colors = new Color4Collection();
            for(var i = 0; i<collection.Count(); i+=4)
            {
                var a = collection[i]/255.0f;
                var b = collection[i + 1]/255.0f;
                var c = collection[i + 2]/255.0f;
                var d = collection[i + 3]/255.0f;
                var newColor = new Color4(a, b, c, d);
                colors.Add(newColor);
            }

            return colors;
        }
Beispiel #24
0
        /// <summary>
        /// Parses the MDL file to obtain model information
        /// </summary>
        /// <param name="selectedItem">The currently selected item</param>
        /// <param name="selectedRace">The currently selected race</param>
        /// <param name="selectedBody">The currently selected body</param>
        /// <param name="selectedPart">The currently selected part</param>
        /// <param name="selectedCategory">The items category </param>
        public MDL(ItemData selectedItem, string selectedCategory, string selectedRace, string selectedBody, string selectedPart)
        {
            string itemType  = Helper.GetCategoryType(selectedCategory);
            string MDLFolder = "";

            if (itemType.Equals("weapon") || itemType.Equals("food"))
            {
                if (selectedPart.Equals("Secondary"))
                {
                    MDLFolder = string.Format(Strings.WeapMDLFolder, selectedItem.SecondaryModelID, selectedItem.SecondaryModelBody);
                    MDLFile   = string.Format(Strings.WeapMDLFile, selectedItem.SecondaryModelID, selectedItem.SecondaryModelBody);
                }
                else
                {
                    MDLFolder = string.Format(Strings.WeapMDLFolder, selectedItem.PrimaryModelID, selectedItem.PrimaryModelBody);
                    MDLFile   = string.Format(Strings.WeapMDLFile, selectedItem.PrimaryModelID, selectedItem.PrimaryModelBody);
                }
            }
            else if (itemType.Equals("accessory"))
            {
                MDLFolder = string.Format(Strings.AccMDLFolder, selectedItem.PrimaryModelID);
                MDLFile   = string.Format(Strings.AccMDLFile, selectedRace, selectedItem.PrimaryModelID, Info.slotAbr[selectedCategory]);
            }
            else if (itemType.Equals("character"))
            {
                if (selectedItem.ItemName.Equals(Strings.Body))
                {
                    MDLFolder = string.Format(Strings.BodyMDLFolder, selectedRace, selectedBody.PadLeft(4, '0'));
                    MDLFile   = string.Format(Strings.BodyMDLFile, selectedRace, selectedBody.PadLeft(4, '0'), selectedPart);
                }
                else if (selectedItem.ItemName.Equals(Strings.Face))
                {
                    MDLFolder = string.Format(Strings.FaceMDLFolder, selectedRace, selectedBody.PadLeft(4, '0'));
                    MDLFile   = string.Format(Strings.FaceMDLFile, selectedRace, selectedBody.PadLeft(4, '0'), selectedPart);
                }
                else if (selectedItem.ItemName.Equals(Strings.Hair))
                {
                    MDLFolder = string.Format(Strings.HairMDLFolder, selectedRace, selectedBody.PadLeft(4, '0'));
                    MDLFile   = string.Format(Strings.HairMDLFile, selectedRace, selectedBody.PadLeft(4, '0'), selectedPart);
                }
                else if (selectedItem.ItemName.Equals(Strings.Tail))
                {
                    MDLFolder = string.Format(Strings.TailMDLFolder, selectedRace, selectedBody.PadLeft(4, '0'));
                    MDLFile   = string.Format(Strings.TailMDLFile, selectedRace, selectedBody.PadLeft(4, '0'), selectedPart);
                }
            }
            else if (itemType.Equals("monster"))
            {
                bool isDemiHuman = false;
                if (selectedItem.PrimaryMTRLFolder != null)
                {
                    isDemiHuman = selectedItem.PrimaryMTRLFolder.Contains("demihuman");
                }

                if (isDemiHuman)
                {
                    MDLFolder = string.Format(Strings.DemiMDLFolder, selectedItem.PrimaryModelID.PadLeft(4, '0'), selectedItem.PrimaryModelBody.PadLeft(4, '0'));
                    MDLFile   = string.Format(Strings.DemiMDLFile, selectedItem.PrimaryModelID.PadLeft(4, '0'), selectedItem.PrimaryModelBody, selectedPart);
                }
                else
                {
                    MDLFolder = string.Format(Strings.MonsterMDLFolder, selectedItem.PrimaryModelID.PadLeft(4, '0'), selectedItem.PrimaryModelBody.PadLeft(4, '0'));
                    MDLFile   = string.Format(Strings.MonsterMDLFile, selectedItem.PrimaryModelID.PadLeft(4, '0'), selectedItem.PrimaryModelBody.PadLeft(4, '0'));
                }
            }
            else
            {
                MDLFolder = string.Format(Strings.EquipMDLFolder, selectedItem.PrimaryModelID);

                if (selectedPart.Equals("-"))
                {
                    MDLFile = string.Format(Strings.EquipMDLFile, selectedRace, selectedItem.PrimaryModelID, Info.slotAbr[selectedCategory]);
                }
                else
                {
                    MDLFile = string.Format(Strings.EquipMDLFile, selectedRace, selectedItem.PrimaryModelID, selectedPart);
                }
            }

            fullPath = MDLFolder + "/" + MDLFile;
            int offset = Helper.GetDataOffset(FFCRC.GetHash(MDLFolder), FFCRC.GetHash(MDLFile), Strings.ItemsDat);

            if (offset == 0)
            {
                if (itemType.Equals("weapon"))
                {
                    if (selectedPart.Equals("Secondary"))
                    {
                        MDLFolder = string.Format(Strings.EquipMDLFolder, selectedItem.SecondaryModelID);
                        MDLFile   = string.Format(Strings.EquipMDLFile, "0101", selectedItem.SecondaryModelID, Info.slotAbr[Strings.Hands]);

                        offset = Helper.GetDataOffset(FFCRC.GetHash(MDLFolder), FFCRC.GetHash(MDLFile), Strings.ItemsDat);
                    }
                }
            }

            int datNum = ((offset / 8) & 0x000f) / 2;

            var MDLDatData = Helper.GetType3DecompressedData(offset, datNum, Strings.ItemsDat);

            using (BinaryReader br = new BinaryReader(new MemoryStream(MDLDatData.Item1)))
            {
                // The size of the header + (size of the mesh information block (136 bytes) * the meshes) + padding
                br.BaseStream.Seek(64 + 136 * MDLDatData.Item2 + 4, SeekOrigin.Begin);

                var modelStringCount = br.ReadInt32();
                var stringBlockSize  = br.ReadInt32();
                var stringBlock      = br.ReadBytes(stringBlockSize);

                var unknown = br.ReadBytes(4);

                var totalMeshCount       = br.ReadInt16();
                var attributeStringCount = br.ReadInt16();
                var meshPartsCount       = br.ReadInt16();
                var materialStringCount  = br.ReadInt16();
                var boneStringCount      = br.ReadInt16();
                var boneListCount        = br.ReadInt16();


                //Has something to do with amount of extra vertex data.
                var unknown1 = br.ReadInt16();

                //Has something to do with amount of extra vertex data.
                var unknown2 = br.ReadInt16();


                var unknown3 = br.ReadInt16();

                // Seems to always be 1027
                var unknown4 = br.ReadInt16();


                var unknown5 = br.ReadInt16();
                var unknown6 = br.ReadInt16();

                br.ReadBytes(10);

                var unknown7 = br.ReadInt16();

                br.ReadBytes(16);

                using (BinaryReader br1 = new BinaryReader(new MemoryStream(stringBlock)))
                {
                    br1.BaseStream.Seek(0, SeekOrigin.Begin);

                    for (int i = 0; i < attributeStringCount; i++)
                    {
                        byte        a;
                        List <byte> atrName = new List <byte>();
                        while ((a = br1.ReadByte()) != 0)
                        {
                            atrName.Add(a);
                        }
                        string atr = Encoding.ASCII.GetString(atrName.ToArray());
                        atr = atr.Replace("\0", "");

                        modelData.Attributes.Add(atr);
                        atrStrings.Add(atr);
                    }

                    for (int i = 0; i < boneStringCount; i++)
                    {
                        byte        b;
                        List <byte> boneName = new List <byte>();
                        while ((b = br1.ReadByte()) != 0)
                        {
                            boneName.Add(b);
                        }
                        string bone = Encoding.ASCII.GetString(boneName.ToArray());
                        bone = bone.Replace("\0", "");

                        modelData.Bones.Add(bone);
                        boneStrings.Add(bone);
                    }

                    for (int i = 0; i < materialStringCount; i++)
                    {
                        byte        b;
                        List <byte> name = new List <byte>();
                        while ((b = br1.ReadByte()) != 0)
                        {
                            name.Add(b);
                        }
                        string material = Encoding.ASCII.GetString(name.ToArray());
                        material = material.Replace("\0", "");

                        modelData.Materials.Add(material);
                        materialStrings.Add(material);
                    }
                }

                byte[] unknown5Bytes = br.ReadBytes(32 * unknown5);

                for (int i = 0; i < 3; i++)
                {
                    LevelOfDetail LoD = new LevelOfDetail();
                    LoD.MeshOffset = br.ReadInt16();
                    LoD.MeshCount  = br.ReadInt16();

                    br.ReadBytes(40);

                    LoD.VertexDataSize = br.ReadInt32();
                    LoD.IndexDataSize  = br.ReadInt32();
                    LoD.VertexOffset   = br.ReadInt32();
                    LoD.IndexOffset    = br.ReadInt32();

                    modelData.LoD.Add(LoD);
                }

                var savePos      = br.BaseStream.Position;
                var lodStructPos = 68;

                for (int i = 0; i < modelData.LoD.Count; i++)
                {
                    List <MeshDataInfo> meshInfoList = new List <MeshDataInfo>();

                    for (int j = 0; j < modelData.LoD[i].MeshCount; j++)
                    {
                        modelData.LoD[i].MeshList.Add(new Mesh());
                        meshInfoList.Clear();

                        br.BaseStream.Seek((j * 136) + lodStructPos, SeekOrigin.Begin);
                        var dataBlockNum = br.ReadByte();

                        while (dataBlockNum != 255)
                        {
                            MeshDataInfo meshInfo = new MeshDataInfo()
                            {
                                VertexDataBlock = dataBlockNum,
                                Offset          = br.ReadByte(),
                                DataType        = br.ReadByte(),
                                UseType         = br.ReadByte()
                            };

                            meshInfoList.Add(meshInfo);

                            br.ReadBytes(4);

                            dataBlockNum = br.ReadByte();
                        }

                        modelData.LoD[i].MeshList[j].MeshDataInfoList = meshInfoList.ToArray();
                    }
                    lodStructPos += 136 * modelData.LoD[i].MeshCount;
                }

                br.BaseStream.Seek(savePos, SeekOrigin.Begin);

                for (int x = 0; x < modelData.LoD.Count; x++)
                {
                    for (int i = 0; i < modelData.LoD[x].MeshCount; i++)
                    {
                        MeshInfo meshInfo = new MeshInfo()
                        {
                            VertexCount     = br.ReadInt32(),
                            IndexCount      = br.ReadInt32(),
                            MaterialNum     = br.ReadInt16(),
                            MeshPartOffset  = br.ReadInt16(),
                            MeshPartCount   = br.ReadInt16(),
                            BoneListIndex   = br.ReadInt16(),
                            IndexDataOffset = br.ReadInt32()
                        };

                        modelData.LoD[x].MeshList[i].MaterialId = meshInfo.MaterialNum;
                        var typeChar = materialStrings[meshInfo.MaterialNum][4].ToString() + materialStrings[meshInfo.MaterialNum][9].ToString();

                        modelData.LoD[x].MeshList[i].BoneListIndex = meshInfo.BoneListIndex;
                        if (typeChar.Equals("cb"))
                        {
                            modelData.LoD[x].MeshList[i].IsBody = true;
                        }

                        for (int j = 0; j < 3; j++)
                        {
                            meshInfo.VertexDataOffsets.Add(br.ReadInt32());
                        }

                        for (int k = 0; k < 3; k++)
                        {
                            meshInfo.VertexSizes.Add(br.ReadByte());
                        }

                        meshInfo.VertexDataBlockCount = br.ReadByte();

                        modelData.LoD[x].MeshList[i].MeshInfo = meshInfo;
                    }
                }

                byte[] attributeOffsetBytes = br.ReadBytes(attributeStringCount * 4);
                br.ReadBytes(unknown6 * 20);

                for (int i = 0; i < modelData.LoD.Count; i++)
                {
                    foreach (var mesh in modelData.LoD[i].MeshList)
                    {
                        for (int j = 0; j < mesh.MeshInfo.MeshPartCount; j++)
                        {
                            MeshPart meshPart = new MeshPart()
                            {
                                IndexOffset = br.ReadInt32(),
                                IndexCount  = br.ReadInt32(),
                                Attributes  = br.ReadInt32(),
                                BoneOffset  = br.ReadInt16(),
                                BoneCount   = br.ReadInt16()
                            };

                            mesh.MeshPartList.Add(meshPart);
                        }
                    }
                }

                br.ReadBytes(unknown7 * 12);
                br.ReadBytes(materialStringCount * 4);

                List <int> boneIncrements = new List <int>();

                // Loop through the increments and save their data.
                for (int i = 0; i < boneStringCount; i++)
                {
                    int increment = br.ReadInt32();
                    boneIncrements.Add(increment);
                }


                for (int i = 0; i < boneListCount; i++)
                {
                    Bones bones = new Bones();

                    for (int j = 0; j < 64; j++)
                    {
                        bones.BoneData.Add(br.ReadInt16());
                    }

                    bones.BoneCount = br.ReadInt32();

                    modelData.BoneSet.Add(bones);
                }

                //br.ReadBytes(unknown1 * 16);

                Dictionary <int, int>         indexMin      = new Dictionary <int, int>();
                Dictionary <int, List <int> > extraIndices  = new Dictionary <int, List <int> >();
                Dictionary <int, List <int> > extraIndices2 = new Dictionary <int, List <int> >();
                var indexCounts = new List <List <ExtraIndex> >();

                // One for each LoD level.
                indexCounts.Add(new List <ExtraIndex>());
                indexCounts.Add(new List <ExtraIndex>());
                indexCounts.Add(new List <ExtraIndex>());

                var pCounts = new int[3];

                if (unknown1 > 0)
                {
                    for (int i = 0; i < unknown1; i++)
                    {
                        //not sure
                        br.ReadBytes(4);

                        //LoD[0] Extra Data Index
                        var p1 = br.ReadUInt16();

                        //LoD[1] Extra Data Index
                        var p2 = br.ReadUInt16();

                        //LoD[2] Extra Data Index
                        var p3 = br.ReadUInt16();


                        //LoD[0] Extra Data Part Count
                        var p1n = br.ReadUInt16();
                        pCounts[0] += p1n;

                        //LoD[1] Extra Data Part Count
                        var p2n = br.ReadUInt16();
                        pCounts[1] += p2n;

                        //LoD[2] Extra Data Part Count
                        var p3n = br.ReadUInt16();
                        pCounts[2] += p3n;
                    }
                }

                Dictionary <int, int> indexLoc = new Dictionary <int, int>();

                if (unknown1 > 0)
                {
                    for (int i = 0; i < modelData.LoD[LoDViewLevel].MeshCount; i++)
                    {
                        var ido = modelData.LoD[LoDViewLevel].MeshList[i].MeshInfo.IndexDataOffset;

                        if (!indexLoc.ContainsKey(ido))
                        {
                            indexLoc.Add(ido, i);
                        }
                    }
                }


                var totalMaskCounts = new int[3];
                Dictionary <int, int> totalExtraCounts = new Dictionary <int, int>();
                if (unknown2 > 0)
                {
                    for (int x = 0; x < pCounts.Length; x++)
                    {
                        for (int i = 0; i < pCounts[x]; i++)
                        {
                            //Index Offset Start
                            var m1 = br.ReadInt32();

                            //index count
                            var mCount = br.ReadInt32();

                            //index offset in unk3
                            var mOffset = br.ReadInt32();

                            var iLoc = 0;
                            if (indexLoc.ContainsKey(m1))
                            {
                                iLoc = indexLoc[m1];
                            }

                            indexCounts[x].Add(new ExtraIndex()
                            {
                                IndexLocation = iLoc, IndexCount = mCount
                            });
                            totalMaskCounts[x] += mCount;
                        }
                    }
                }


                if (unknown3 > 0)
                {
                    //var unk3Remainder = (unknown3 * 4) - (totalLoD0MaskCount * 4);
                    var c = 0;
                    for (int l = 0; l < indexCounts.Count; l++)
                    {
                        foreach (var ic in indexCounts[l])
                        {
                            HashSet <int> mIndexList = new HashSet <int>();

                            for (int i = 0; i < ic.IndexCount; i++)
                            {
                                //index its replacing? attatched to?
                                var oIndex = br.ReadUInt16();

                                //extra index following last equipment index
                                var mIndex = br.ReadUInt16();

                                if (l == LoDViewLevel)
                                {
                                    mIndexList.Add(mIndex);

                                    if (extraIndices.ContainsKey(ic.IndexLocation))
                                    {
                                        extraIndices[ic.IndexLocation].Add(mIndex);
                                        extraIndices2[ic.IndexLocation].Add(oIndex);
                                    }
                                    else
                                    {
                                        extraIndices.Add(ic.IndexLocation, new List <int>()
                                        {
                                            mIndex
                                        });
                                        extraIndices2.Add(ic.IndexLocation, new List <int>()
                                        {
                                            oIndex
                                        });
                                    }
                                }
                            }

                            if (l == LoDViewLevel)
                            {
                                if (totalExtraCounts.ContainsKey(ic.IndexLocation))
                                {
                                    totalExtraCounts[ic.IndexLocation] += mIndexList.Count;
                                }
                                else
                                {
                                    totalExtraCounts.Add(ic.IndexLocation, mIndexList.Count);
                                }
                            }
                            c++;
                        }
                    }

                    //the rest of unk3
                    //br.ReadBytes(unk3Remainder);

                    foreach (var ei in extraIndices)
                    {
                        indexMin.Add(ei.Key, ei.Value.Min());
                    }

                    extraIndexData.indexCounts      = indexCounts[LoDViewLevel];
                    extraIndexData.indexMin         = indexMin;
                    extraIndexData.totalExtraCounts = totalExtraCounts;
                    extraIndexData.extraIndices     = extraIndices;
                    extraIndexData.extraIndices2    = extraIndices2;

                    modelData.ExtraData = extraIndexData;
                }

                //br.ReadBytes(unknown3 * 4);

                var boneIndexSize = br.ReadInt32();

                for (int i = 0; i < boneIndexSize / 2; i++)
                {
                    modelData.BoneIndicies.Add(br.ReadUInt16());
                }

                int padding = br.ReadByte();
                br.ReadBytes(padding);

                for (int i = 0; i < 4; i++)
                {
                    ModelMaterial.BoundingBox boundingBox = new ModelMaterial.BoundingBox();
                    for (int j = 0; j < 4; j++)
                    {
                        boundingBox.PointA.Add(br.ReadSingle());
                    }
                    for (int k = 0; k < 4; k++)
                    {
                        boundingBox.PointB.Add(br.ReadSingle());
                    }

                    modelData.BoundingBoxes.Add(boundingBox);
                }

                //float4x4
                for (int i = 0; i < boneStringCount; i++)
                {
                    string boneName = boneStrings[i];

                    Vector4 transform1 = new Vector4();
                    Vector4 transform2 = new Vector4();

                    transform1.X = br.ReadSingle();
                    transform1.Y = br.ReadSingle();
                    transform1.Z = br.ReadSingle();
                    transform1.W = br.ReadSingle();

                    transform2.X = br.ReadSingle();
                    transform2.Y = br.ReadSingle();
                    transform2.Z = br.ReadSingle();
                    transform2.W = br.ReadSingle();

                    boneTransforms.Add(transform1.X);
                    boneTransforms.Add(transform1.Y);
                    boneTransforms.Add(transform1.Z);
                    boneTransforms.Add(transform1.W);

                    boneTransforms.Add(transform2.X);
                    boneTransforms.Add(transform2.Y);
                    boneTransforms.Add(transform2.Z);
                    boneTransforms.Add(transform2.W);
                }

                for (int i = 0; i < 3; i++)
                {
                    for (int j = 0; j < modelData.LoD[i].MeshCount; j++)
                    {
                        Mesh mesh = modelData.LoD[i].MeshList[j];

                        for (int k = 0; k < mesh.MeshInfo.VertexDataBlockCount; k++)
                        {
                            br.BaseStream.Seek(modelData.LoD[i].VertexOffset + mesh.MeshInfo.VertexDataOffsets[k], SeekOrigin.Begin);

                            mesh.MeshVertexData.Add(br.ReadBytes(mesh.MeshInfo.VertexSizes[k] * mesh.MeshInfo.VertexCount));
                        }

                        br.BaseStream.Seek(modelData.LoD[i].IndexOffset + (mesh.MeshInfo.IndexDataOffset * 2), SeekOrigin.Begin);

                        mesh.IndexData = br.ReadBytes(2 * mesh.MeshInfo.IndexCount);
                    }
                }



                int vertex = 0, coordinates = 0, normals = 0, tangents = 0, colors = 0, blendWeights = 0, blendIndices = 0;

                for (int i = 0; i < modelData.LoD[LoDViewLevel].MeshCount; i++)
                {
                    objBytes.Clear();

                    var vertexList        = new Vector3Collection();
                    var texCoordList      = new Vector2Collection();
                    var texCoordList2     = new Vector2Collection();
                    var normalList        = new Vector3Collection();
                    var tangentList       = new Vector3Collection();
                    var colorsList        = new Color4Collection();
                    var indexList         = new IntCollection();
                    var blendWeightList   = new List <float>();
                    var blendWeightList2  = new List <float[]>();
                    var blendIndicesList  = new List <int>();
                    var blendIndicesList2 = new List <int[]>();
                    var weightCounts      = new List <int>();
                    var extraVerts        = new HashSet <Vector3>();
                    Dictionary <int, Vector3> extraVertDict = new Dictionary <int, Vector3>();


                    Mesh mesh = modelData.LoD[LoDViewLevel].MeshList[i];

                    MeshDataInfo[] meshDataInfoList = mesh.MeshDataInfoList;

                    int c = 0;
                    foreach (var meshDataInfo in meshDataInfoList)
                    {
                        if (meshDataInfo.UseType == 0)
                        {
                            vertex = c;
                        }
                        else if (meshDataInfo.UseType == 1)
                        {
                            blendWeights = c;
                        }
                        else if (meshDataInfo.UseType == 2)
                        {
                            blendIndices = c;
                        }
                        else if (meshDataInfo.UseType == 3)
                        {
                            normals = c;
                        }
                        else if (meshDataInfo.UseType == 4)
                        {
                            coordinates = c;
                        }
                        else if (meshDataInfo.UseType == 6)
                        {
                            tangents = c;
                        }
                        else if (meshDataInfo.UseType == 7)
                        {
                            colors = c;
                        }

                        c++;
                    }

                    /*
                     * -----------------
                     * Vertex
                     * -----------------
                     */

                    // Only actually read the buffers if they have data in them.
                    if (mesh.MeshVertexData.Count > 0)
                    {
                        using (BinaryReader br1 = new BinaryReader(new MemoryStream(mesh.MeshVertexData[meshDataInfoList[vertex].VertexDataBlock])))
                        {
                            for (int j = 0; j < mesh.MeshInfo.VertexCount; j++)
                            {
                                br1.BaseStream.Seek(j * mesh.MeshInfo.VertexSizes[meshDataInfoList[vertex].VertexDataBlock] + meshDataInfoList[vertex].Offset, SeekOrigin.Begin);

                                Vector3 vVector = new Vector3();
                                if (meshDataInfoList[vertex].DataType == 13 || meshDataInfoList[vertex].DataType == 14)
                                {
                                    System.Half h1 = System.Half.ToHalf(br1.ReadUInt16());
                                    System.Half h2 = System.Half.ToHalf(br1.ReadUInt16());
                                    System.Half h3 = System.Half.ToHalf(br1.ReadUInt16());

                                    float x = HalfHelper.HalfToSingle(h1);
                                    float y = HalfHelper.HalfToSingle(h2);
                                    float z = HalfHelper.HalfToSingle(h3);

                                    vVector = new Vector3(x, y, z);
                                    objBytes.Add("v " + x.ToString("N5") + " " + y.ToString("N5") + " " + z.ToString("N5") + " ");
                                }
                                else if (meshDataInfoList[vertex].DataType == 2)
                                {
                                    var x = BitConverter.ToSingle(br1.ReadBytes(4), 0);
                                    var y = BitConverter.ToSingle(br1.ReadBytes(4), 0);
                                    var z = BitConverter.ToSingle(br1.ReadBytes(4), 0);

                                    vVector = new Vector3(x, y, z);
                                    objBytes.Add("v " + x.ToString("N5") + " " + y.ToString("N5") + " " + z.ToString("N5") + " ");
                                }

                                vertexList.Add(vVector);
                            }
                        }


                        /*
                         * -----------------
                         * Blend Weight
                         * -----------------
                         */
                        using (BinaryReader br1 = new BinaryReader(new MemoryStream(mesh.MeshVertexData[meshDataInfoList[blendWeights].VertexDataBlock])))
                        {
                            for (int j = 0; j < mesh.MeshInfo.VertexCount; j++)
                            {
                                br1.BaseStream.Seek(j * mesh.MeshInfo.VertexSizes[meshDataInfoList[blendWeights].VertexDataBlock] + meshDataInfoList[blendWeights].Offset, SeekOrigin.Begin);

                                float x = br1.ReadByte() / 255.0f;
                                float y = br1.ReadByte() / 255.0f;
                                float z = br1.ReadByte() / 255.0f;
                                float w = br1.ReadByte() / 255.0f;

                                int count = 0;
                                if (x != 0)
                                {
                                    blendWeightList.Add(x);
                                    count++;

                                    if (y != 0)
                                    {
                                        blendWeightList.Add(y);
                                        count++;

                                        if (z != 0)
                                        {
                                            blendWeightList.Add(z);
                                            count++;

                                            if (w != 0)
                                            {
                                                blendWeightList.Add(w);
                                                count++;
                                            }
                                        }
                                    }
                                }

                                if (count == 1)
                                {
                                    blendWeightList2.Add(new float[] { x });
                                }
                                else if (count == 2)
                                {
                                    blendWeightList2.Add(new float[] { x, y });
                                }
                                else if (count == 3)
                                {
                                    blendWeightList2.Add(new float[] { x, y, z });
                                }
                                else if (count == 4)
                                {
                                    blendWeightList2.Add(new float[] { x, y, z, w });
                                }

                                weightCounts.Add(count);
                            }
                        }


                        /*
                         * -----------------
                         * Blend Index
                         * -----------------
                         */
                        using (BinaryReader br1 = new BinaryReader(new MemoryStream(mesh.MeshVertexData[meshDataInfoList[blendIndices].VertexDataBlock])))
                        {
                            for (int j = 0; j < mesh.MeshInfo.VertexCount; j++)
                            {
                                br1.BaseStream.Seek(j * mesh.MeshInfo.VertexSizes[meshDataInfoList[blendIndices].VertexDataBlock] + meshDataInfoList[blendIndices].Offset, SeekOrigin.Begin);

                                int x = br1.ReadByte();
                                int y = br1.ReadByte();
                                int z = br1.ReadByte();
                                int w = br1.ReadByte();

                                if (weightCounts[j] == 1)
                                {
                                    blendIndicesList.Add(x);
                                    blendIndicesList2.Add(new int[] { x });
                                }
                                else if (weightCounts[j] == 2)
                                {
                                    blendIndicesList.Add(x);
                                    blendIndicesList.Add(y);
                                    blendIndicesList2.Add(new int[] { x, y });
                                }
                                else if (weightCounts[j] == 3)
                                {
                                    blendIndicesList.Add(x);
                                    blendIndicesList.Add(y);
                                    blendIndicesList.Add(z);
                                    blendIndicesList2.Add(new int[] { x, y, z });
                                }
                                else if (weightCounts[j] == 4)
                                {
                                    blendIndicesList.Add(x);
                                    blendIndicesList.Add(y);
                                    blendIndicesList.Add(z);
                                    blendIndicesList.Add(w);
                                    blendIndicesList2.Add(new int[] { x, y, z, w });
                                }
                            }
                        }

                        /*
                         * -----------------
                         * Texture Coordinates
                         * -----------------
                         */
                        using (BinaryReader br1 = new BinaryReader(new MemoryStream(mesh.MeshVertexData[meshDataInfoList[coordinates].VertexDataBlock])))
                        {
                            for (int j = 0; j < mesh.MeshInfo.VertexCount; j++)
                            {
                                br1.BaseStream.Seek(j * mesh.MeshInfo.VertexSizes[meshDataInfoList[coordinates].VertexDataBlock] + meshDataInfoList[coordinates].Offset, SeekOrigin.Begin);

                                float x = 0;
                                float y = 0;
                                float z = 0;
                                float w = 0;
                                if (meshDataInfoList[coordinates].DataType == 13 || meshDataInfoList[coordinates].DataType == 14)
                                {
                                    var sx = br1.ReadUInt16();
                                    var sy = br1.ReadUInt16();
                                    var sz = br1.ReadUInt16();
                                    var sw = br1.ReadUInt16();

                                    var h1 = new SharpDX.Half(sx);
                                    var h2 = new SharpDX.Half(sy);
                                    var h3 = new SharpDX.Half(sz);
                                    var h4 = new SharpDX.Half(sw);

                                    x = h1;
                                    y = h2;
                                    z = h3;
                                    w = h4;
                                }
                                else if (meshDataInfoList[coordinates].DataType == 1)
                                {
                                    x = br1.ReadSingle();
                                    y = br1.ReadSingle();
                                }
                                else
                                {
                                    x = br1.ReadSingle();
                                    y = br1.ReadSingle();
                                    z = br1.ReadSingle();
                                    w = br1.ReadSingle();
                                }


                                var ox = x - Math.Truncate(x);
                                var oy = y - Math.Truncate(y);

                                objBytes.Add("vt " + ox.ToString("N5") + " " + (1 - y).ToString("N5") + " ");
                                texCoordList.Add(new Vector2(x, y));
                                texCoordList2.Add(new Vector2(z, w));
                            }
                        }



                        /*
                         * -----------------
                         * Normals
                         * -----------------
                         */
                        using (BinaryReader br1 = new BinaryReader(new MemoryStream(mesh.MeshVertexData[meshDataInfoList[normals].VertexDataBlock])))
                        {
                            for (int j = 0; j < mesh.MeshInfo.VertexCount; j++)
                            {
                                br1.BaseStream.Seek(j * mesh.MeshInfo.VertexSizes[meshDataInfoList[normals].VertexDataBlock] + meshDataInfoList[normals].Offset, SeekOrigin.Begin);

                                float x = 0;
                                float y = 0;
                                float z = 0;
                                float w = 0;

                                if (meshDataInfoList[normals].DataType == 13 || meshDataInfoList[normals].DataType == 14)
                                {
                                    System.Half h1 = System.Half.ToHalf(br1.ReadUInt16());
                                    System.Half h2 = System.Half.ToHalf(br1.ReadUInt16());
                                    System.Half h3 = System.Half.ToHalf(br1.ReadUInt16());
                                    System.Half h4 = System.Half.ToHalf(br1.ReadUInt16());

                                    x = HalfHelper.HalfToSingle(h1);
                                    y = HalfHelper.HalfToSingle(h2);
                                    z = HalfHelper.HalfToSingle(h3);
                                    w = HalfHelper.HalfToSingle(h4);
                                }
                                else
                                {
                                    x = br1.ReadSingle();
                                    y = br1.ReadSingle();
                                    z = br1.ReadSingle();
                                }

                                var nv = new Vector3(x, y, z);

                                objBytes.Add("vn " + x.ToString("N5") + " " + y.ToString("N5") + " " + z.ToString("N5") + " ");
                                normalList.Add(nv);
                            }
                        }


                        /*
                         * -----------------
                         * Tangents
                         * -----------------
                         */
                        using (BinaryReader br1 = new BinaryReader(new MemoryStream(mesh.MeshVertexData[meshDataInfoList[tangents].VertexDataBlock])))
                        {
                            for (int j = 0; j < mesh.MeshInfo.VertexCount; j++)
                            {
                                br1.BaseStream.Seek(j * mesh.MeshInfo.VertexSizes[meshDataInfoList[tangents].VertexDataBlock] + meshDataInfoList[tangents].Offset, SeekOrigin.Begin);

                                int x = br1.ReadByte();
                                int y = br1.ReadByte();
                                int z = br1.ReadByte();
                                int w = br1.ReadByte();

                                var x1 = x * 2 / 255f - 1f;
                                var y1 = y * 2 / 255f - 1f;
                                var z1 = z * 2 / 255f - 1f;
                                var w1 = w * 2 / 255f - 1f;


                                var nv = new Vector3(x1, y1, z1);

                                tangentList.Add(nv);
                            }
                        }


                        /*
                         * -----------------
                         * Vertex Color
                         * -----------------
                         */
                        using (BinaryReader br1 = new BinaryReader(new MemoryStream(mesh.MeshVertexData[meshDataInfoList[colors].VertexDataBlock])))
                        {
                            for (int j = 0; j < mesh.MeshInfo.VertexCount; j++)
                            {
                                br1.BaseStream.Seek(j * mesh.MeshInfo.VertexSizes[meshDataInfoList[colors].VertexDataBlock] + meshDataInfoList[colors].Offset, SeekOrigin.Begin);

                                int a = br1.ReadByte();
                                int r = br1.ReadByte();
                                int g = br1.ReadByte();
                                int b = br1.ReadByte();

                                colorsList.Add(new Color4(r, g, b, a));
                            }
                        }
                    }


                    /*
                     * -----------------
                     * Index
                     * -----------------
                     */
                    using (BinaryReader br1 = new BinaryReader(new MemoryStream(mesh.IndexData)))
                    {
                        for (int j = 0; j < mesh.MeshInfo.IndexCount; j += 3)
                        {
                            int i1 = br1.ReadUInt16();
                            int i2 = br1.ReadUInt16();
                            int i3 = br1.ReadUInt16();

                            objBytes.Add("f " + (i1 + 1) + "/" + (i1 + 1) + "/" + (i1 + 1) + " " + (i2 + 1) + "/" + (i2 + 1) + "/" + (i2 + 1) + " " + (i3 + 1) + "/" + (i3 + 1) + "/" + (i3 + 1) + " ");

                            indexList.Add(i1);
                            indexList.Add(i2);
                            indexList.Add(i3);
                        }
                    }

                    ModelMeshData modelMeshData = new ModelMeshData()
                    {
                        Vertices              = vertexList,
                        Normals               = normalList,
                        TextureCoordinates    = texCoordList,
                        TextureCoordinates2   = texCoordList2,
                        BiTangents            = tangentList,
                        Indices               = indexList,
                        VertexColors          = colorsList,
                        OBJFileData           = objBytes.ToArray(),
                        BoneStrings           = boneStrings,
                        AttributeStrings      = atrStrings,
                        BoneIndices           = modelData.BoneIndicies,
                        BoneTransforms        = boneTransforms,
                        BlendWeights          = blendWeightList,
                        BlendIndices          = blendIndicesList,
                        WeightCounts          = weightCounts,
                        MeshPartList          = mesh.MeshPartList,
                        BlendIndicesArrayList = blendIndicesList2,
                        BlendWeightsArrayList = blendWeightList2,
                        MaterialNum           = mesh.MeshInfo.MaterialNum,
                        MeshPartCount         = mesh.MeshInfo.MeshPartCount,
                        MeshPartOffset        = mesh.MeshInfo.MeshPartOffset
                    };

                    if (extraIndices2.ContainsKey(i))
                    {
                        foreach (var id in extraIndices2[i])
                        {
                            if (!extraVertDict.ContainsKey(id))
                            {
                                if (modelMeshData.Indices.Count >= id)
                                {
                                    var v = 0;
                                    try
                                    {
                                        v = modelMeshData.Indices[id];
                                    }
                                    catch
                                    {
                                        v = 0;
                                    }

                                    extraVertDict.Add(id, modelMeshData.Vertices[v]);
                                }
                                else
                                {
                                    //new Thread(() => MessageBox.Show("There was an error reading the models extra data.\n\n" +
                                    //    "Mesh " + i + " Index " + id + "\n\n" +
                                    //    "This is likely caused by parts of a mesh being deleted and may cause crashes in-game.\n\n" +
                                    //    "Consider using Advanced Import to Fix or Disable Hiding for the above mesh.",
                                    //    "Extra Data Warning" + Info.appVersion, MessageBoxButtons.OK, MessageBoxIcon.Warning)).Start();
                                    new Thread(() => MessageBox.Show(string.Format(Dialogs.MDLExtraDataWarning, i, id),
                                                                     Dialogs.ExtraDataWarning + Info.appVersion, MessageBoxButtons.OK, MessageBoxIcon.Warning)).Start();

                                    break;
                                }
                            }
                        }
                    }


                    mesh.extraVertDict = extraVertDict;

                    meshList.Add(modelMeshData);
                }
            }
        }
Beispiel #25
0
        private void Update_Preview()
        {
            try
            {
                if (ImageConverter.IsLoaded && UsePreviewBox.IsChecked.Value)
                {
                    int coord   = CoordinateAxis.SelectedIndex;
                    int verAlig = VerticalAlignmentBox.SelectedIndex;
                    int horAlig = HorizontalAlignmentBox.SelectedIndex;
                    //Mat smat = ImageConverter.GetModifiedImage();
                    //System.Windows.Size size = ImageConverter.GetBlocks();
                    //Bitmap bitmap = smat.ToBitmap();
                    //var mb = new MeshBuilder(false, true);

                    //IList<Point3D> pnts = new List<Point3D>
                    //{
                    //    new Point3D(0, 0, 0),
                    //    new Point3D(size.Width, 0, 0),
                    //    new Point3D(size.Width, size.Height, 0),
                    //    new Point3D(0, size.Height, 0)
                    //};

                    //mb.AddPolygon(pnts);

                    //var mesh = mb.ToMesh(false);

                    //PointCollection pntCol = new PointCollection
                    //{
                    //    new System.Windows.Point(0, 0),
                    //    new System.Windows.Point(bitmap.Size.Width, 0),
                    //    new System.Windows.Point(bitmap.Size.Width, bitmap.Size.Height),
                    //    new System.Windows.Point(0, bitmap.Size.Height)
                    //};
                    //mesh.TextureCoordinates = pntCol;

                    //ImageBrush brush = new ImageBrush();

                    //using (Stream stream = new MemoryStream())
                    //{
                    //    bitmap.Save(stream, ImageFormat.Png);
                    //    stream.Seek(0, SeekOrigin.Begin);
                    //    BitmapImage img = new BitmapImage();
                    //    img.BeginInit();
                    //    img.CacheOption = BitmapCacheOption.OnLoad;
                    //    img.StreamSource = stream;
                    //    img.EndInit();
                    //    brush.ImageSource = img;
                    //}

                    //brush.TileMode = TileMode.Tile;
                    //brush.ViewportUnits = BrushMappingMode.Absolute;
                    //brush.ViewboxUnits = BrushMappingMode.Absolute;
                    //brush.Stretch = Stretch.None;
                    //brush.AlignmentX = AlignmentX.Left;
                    //brush.AlignmentY = AlignmentY.Top;
                    //brush.Viewport = new System.Windows.Rect(0, 0, brush.ImageSource.Width, brush.ImageSource.Height);
                    //DiffuseMaterial mat = new DiffuseMaterial(brush);

                    //GeometryModel3D gModel3D = new GeometryModel3D { Geometry = mesh, Material = mat, BackMaterial = mat };

                    //PreviewModel.Content = gModel3D;
                    Particle[] particles = ImageConverter.GetParticles(coord, verAlig, horAlig);
                    //ParticleModel.Children.Clear();
                    var points  = new PointGeometry3D();
                    var vectors = new Vector3Collection();
                    var colors  = new Color4Collection();
                    var ptIdx   = new IntCollection();
                    int i       = 0;
                    foreach (Particle particle in particles)
                    {
                        vectors.Add(new Vector3((float)particle.x, (float)particle.y, (float)particle.z));
                        if (UseStaticDustColor.IsChecked.Value)
                        {
                            Color c = (Color)ColorConverter.ConvertFromString(ColorCodeBox.Text);
                            colors.Add(new Color4(c.R / 255f, c.G / 255f, c.B / 255f, 1.0f));
                        }
                        else
                        {
                            colors.Add(new Color4(particle.r / 255f, particle.g / 255f, particle.b / 255f, 1.0f));
                        }
                        ptIdx.Add(i);
                        i++;
                    }
                    points.Positions       = vectors;
                    points.Colors          = colors;
                    points.Indices         = ptIdx;
                    ParticleModel.Geometry = points;
                    double size = double.Parse(ParticleSizeBox.Text);
                    ParticleModel.Size   = new System.Windows.Size(3 * Math.Sqrt(size), 3 * Math.Sqrt(size));
                    ParticleCounter.Text = $"Particles: {particles.Length}";
                    if (particles.Length >= 2000)
                    {
                        ParticleCounter.Foreground = new SolidColorBrush(Colors.Red);
                        CounterAlert.Visibility    = Visibility.Visible;
                    }
                    else
                    {
                        ParticleCounter.Foreground = new SolidColorBrush(Colors.Snow);
                        CounterAlert.Visibility    = Visibility.Hidden;
                    }
                }
            }
            catch (Exception e)
            {
                Logger.WriteExceptionLog(e);
                MessageBox.Show("プレビューの更新に失敗しました\nFailed to update preview.",
                                "エラー/Error",
                                MessageBoxButton.OK,
                                MessageBoxImage.Error);
                this.Close();
            }
        }
Beispiel #26
0
        public static (Vector3Collection, Color4Collection, Bitmap) BPtoPointCloudBitmap(Vector3Collection imageSpace, List <float[]> uvs, List <LMData> lmDataList, int height, int width, bool isTransFormed = false, double angleThreshold = 5, double minCountPercent = 0)
        {
            if (minCountPercent > 1 || minCountPercent < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(minCountPercent));
            }
            Vector3Collection vector3sOut = new Vector3Collection();
            Color4Collection  color4sOut  = new Color4Collection();
            Bitmap            bitmapOut   = new Bitmap(1, 1);

            if (imageSpace.Count == 0 || lmDataList.Count == 0 || height == 0 || width == 0)
            {
                return(vector3sOut, color4sOut, bitmapOut);
            }

            bitmapOut = new Bitmap(width, height);

            int[] counts = new int[imageSpace.Count];

            if (isTransFormed)
            {
                foreach (var lmData in lmDataList)
                {
                    if (lmData.Type == LMData.InteractionType.Compton)
                    {
                        Parallel.For(0, imageSpace.Count, i =>// for (int i = 0; i < imageSpace.Count; ++i)
                        {
                            if (IsEffectedBPPoint(lmData.ScatterLMDataInfo.TransformedInteractionPoint3D, lmData.ScatterLMDataInfo.InteractionEnergy,
                                                  lmData.AbsorberLMDataInfo.TransformedInteractionPoint3D, lmData.AbsorberLMDataInfo.InteractionEnergy,
                                                  imageSpace[i].ToPoint3D(), angleThreshold))
                            {
                                counts[i]++;
                            }
                        });
                    }
                }
            }
            else
            {
                foreach (var lmData in lmDataList)
                {
                    if (lmData.Type == LMData.InteractionType.Compton)
                    {
                        for (int i = 0; i < imageSpace.Count; ++i)
                        {
                            if (IsEffectedBPPoint(lmData.ScatterLMDataInfo.RelativeInteractionPoint3D, lmData.ScatterLMDataInfo.InteractionEnergy,
                                                  lmData.AbsorberLMDataInfo.RelativeInteractionPoint3D, lmData.AbsorberLMDataInfo.InteractionEnergy,
                                                  imageSpace[i].ToPoint3D(), angleThreshold))
                            {
                                counts[i]++;
                            }
                        }
                    }
                }
            }


            int maxCount = counts.Max();
            int minCount = Convert.ToInt32(Math.Round(maxCount * minCountPercent));

            if (maxCount < 5)
            {
                return(vector3sOut, color4sOut, bitmapOut);
            }


            for (int i = 0; i < imageSpace.Count; i++)
            {
                if (counts[i] > minCount)
                {
                    vector3sOut.Add(imageSpace[i]);
                    Color4 jetColor = ColorScaleJet(counts[i], minCount, maxCount);

                    System.Drawing.Color bitMapColor = System.Drawing.Color.FromArgb(Convert.ToInt32(jetColor.Alpha * 255),
                                                                                     Convert.ToInt32(jetColor.Red * 255),
                                                                                     Convert.ToInt32(jetColor.Green * 255),
                                                                                     Convert.ToInt32(jetColor.Blue * 255));
                    color4sOut.Add(jetColor);

                    int u = (int)Math.Round(uvs[i][0] * width);
                    int v = (int)Math.Round(uvs[i][1] * height);
                    if (u == width)
                    {
                        --u;
                    }
                    if (v == height)
                    {
                        --v;
                    }

                    bitmapOut.SetPixel(u, v, bitMapColor);
                }
            }

            var tupleOut3 = new Tuple <Vector3Collection, Color4Collection>(vector3sOut, color4sOut);

            return(vector3sOut, color4sOut, bitmapOut);
        }
Beispiel #27
0
        private static void DrawGridPatch(
            Vector3Collection positions, IntCollection indices, Color4Collection colors, int startX, int startY)
        {
            var c1 = (Color)ColorConverter.ConvertFromString("#c5d1d8");

            c1.Clamp();
            var c2 = (Color)ColorConverter.ConvertFromString("#ddeaf2");

            c2.Clamp();

            var darkGridColor  = new Color4(new Vector4(c1.ScR, c1.ScG, c1.ScB, 1));
            var lightGridColor = new Color4(new Vector4(c2.ScR, c2.ScG, c2.ScB, 1));

            const int size = 10;

            for (var x = startX; x <= startX + size; x++)
            {
                if (x == 0 && startY < 0)
                {
                    continue;
                }

                var v = new Vector3(x, -.001f, startY);
                positions.Add(v);
                indices.Add(positions.Count - 1);
                positions.Add(new Vector3(x, -.001f, startY + size));
                indices.Add(positions.Count - 1);

                if (x % 5 == 0)
                {
                    colors.Add(darkGridColor);
                    colors.Add(darkGridColor);
                }
                else
                {
                    colors.Add(lightGridColor);
                    colors.Add(lightGridColor);
                }
            }

            for (var y = startY; y <= startY + size; y++)
            {
                if (y == 0 && startX >= 0)
                {
                    continue;
                }

                positions.Add(new Vector3(startX, -.001f, y));
                indices.Add(positions.Count - 1);
                positions.Add(new Vector3(startX + size, -.001f, y));
                indices.Add(positions.Count - 1);

                if (y % 5 == 0)
                {
                    colors.Add(darkGridColor);
                    colors.Add(darkGridColor);
                }
                else
                {
                    colors.Add(lightGridColor);
                    colors.Add(lightGridColor);
                }
            }
        }
Beispiel #28
0
        /// <summary>
        /// Create the grid
        /// </summary>
        private void DrawGrid()
        {
            Grid = new LineGeometry3D();
            var positions = new Vector3Collection();
            var indices = new IntCollection();
            var colors = new Color4Collection();

            for(var i= 0; i < 10; i += 1)
            {
                for (var j = 0; j < 10; j += 1)
                {
                    DrawGridPatch(positions, indices, colors, -50 + i * 10, -50 + j * 10);
                }
            }

            Grid.Positions = positions;
            Grid.Indices = indices;
            Grid.Colors = colors;

            Axes = new LineGeometry3D();
            var axesPositions = new Vector3Collection();
            var axesIndices = new IntCollection();
            var axesColors = new Color4Collection();

            // Draw the coordinate axes
            axesPositions.Add(new Vector3());
            axesIndices.Add(axesPositions.Count - 1);
            axesPositions.Add(new Vector3(50, 0, 0));
            axesIndices.Add(axesPositions.Count - 1);
            axesColors.Add(SharpDX.Color.Red);
            axesColors.Add(SharpDX.Color.Red);

            axesPositions.Add(new Vector3());
            axesIndices.Add(axesPositions.Count - 1);
            axesPositions.Add(new Vector3(0, 5, 0));
            axesIndices.Add(axesPositions.Count - 1);
            axesColors.Add(SharpDX.Color.Blue);
            axesColors.Add(SharpDX.Color.Blue);

            axesPositions.Add(new Vector3());
            axesIndices.Add(axesPositions.Count - 1);
            axesPositions.Add(new Vector3(0, 0, -50));
            axesIndices.Add(axesPositions.Count - 1);
            axesColors.Add(SharpDX.Color.Green);
            axesColors.Add(SharpDX.Color.Green);

            Axes.Positions = axesPositions;
            Axes.Indices = axesIndices;
            Axes.Colors = axesColors;

        }
Beispiel #29
0
        public MainViewModel()
        {            // titles
            this.Title     = "DynamicTexture Demo";
            this.SubTitle  = "WPF & SharpDX";
            EffectsManager = new DefaultEffectsManager();
            this.Camera    = new HelixToolkit.Wpf.SharpDX.PerspectiveCamera
            {
                Position      = new Point3D(10, 10, 10),
                LookDirection = new Vector3D(-10, -10, -10),
                UpDirection   = new Vector3D(0, 1, 0)
            };
            this.Light1Color       = Colors.White;
            this.Light1Direction   = new Vector3D(-10, -10, -10);
            this.AmbientLightColor = Colors.Black;

            var b2 = new MeshBuilder(true, true, true);

            b2.AddSphere(new Vector3(0f, 0f, 0f), 4, 64, 64);
            this.Model      = b2.ToMeshGeometry3D();
            Model.IsDynamic = true;
            this.InnerModel = new MeshGeometry3D()
            {
                Indices            = Model.Indices,
                Positions          = Model.Positions,
                Normals            = Model.Normals,
                TextureCoordinates = Model.TextureCoordinates,
                Tangents           = Model.Tangents,
                BiTangents         = Model.BiTangents,
                IsDynamic          = true
            };

            var image = TextureModel.Create(new System.Uri(@"test.png", System.UriKind.RelativeOrAbsolute).ToString());

            this.ModelMaterial = new PhongMaterial
            {
                AmbientColor      = Colors.Gray.ToColor4(),
                DiffuseColor      = Colors.White.ToColor4(),
                SpecularColor     = Colors.White.ToColor4(),
                SpecularShininess = 100f,
                DiffuseAlphaMap   = image,
                DiffuseMap        = TextureModel.Create(new System.Uri(@"TextureCheckerboard2.dds", System.UriKind.RelativeOrAbsolute).ToString()),
                NormalMap         = TextureModel.Create(new System.Uri(@"TextureCheckerboard2_dot3.dds", System.UriKind.RelativeOrAbsolute).ToString()),
            };

            this.InnerModelMaterial = new PhongMaterial
            {
                AmbientColor      = Colors.Gray.ToColor4(),
                DiffuseColor      = new Color4(0.75f, 0.75f, 0.75f, 1.0f),
                SpecularColor     = Colors.White.ToColor4(),
                SpecularShininess = 100f,
                DiffuseAlphaMap   = image,
                DiffuseMap        = TextureModel.Create(new System.Uri(@"TextureNoise1.jpg", System.UriKind.RelativeOrAbsolute).ToString()),
                NormalMap         = ModelMaterial.NormalMap
            };


            initialPosition = Model.Positions;
            initialIndicies = Model.Indices;
            #region Point Model
            PointModel = new PointGeometry3D()
            {
                IsDynamic = true, Positions = Model.Positions
            };
            int count  = PointModel.Positions.Count;
            var colors = new Color4Collection(count);
            for (int i = 0; i < count / 2; ++i)
            {
                colors.Add(new Color4(0, 1, 1, 1));
            }
            for (int i = 0; i < count / 2; ++i)
            {
                colors.Add(new Color4(0, 0, 0, 0));
            }
            PointModel.Colors = colors;
            #endregion

            #region Line Model
            LineModel = new LineGeometry3D()
            {
                IsDynamic = true, Positions = new Vector3Collection(PointModel.Positions)
            };
            LineModel.Positions.Add(Vector3.Zero);
            var indices = new IntCollection(count * 2);
            for (int i = 0; i < count; ++i)
            {
                indices.Add(count);
                indices.Add(i);
            }
            LineModel.Indices = indices;
            colors            = new Color4Collection(LineModel.Positions.Count);
            for (int i = 0; i < count; ++i)
            {
                colors.Add(new Color4((float)i / count, 1 - (float)i / count, 0, 1));
            }
            colors.Add(Colors.Blue.ToColor4());
            LineModel.Colors = colors;
            LineMaterial     = new LineArrowHeadMaterial()
            {
                Color = Colors.White, Thickness = 0.5, ArrowSize = 0.02
            };
            #endregion
            var token = cts.Token;
            Task.Run(() =>
            {
                while (!token.IsCancellationRequested)
                {
                    Timer_Tick();
                    Task.Delay(16).Wait();
                }
            }, token);
            //timer.Interval = TimeSpan.FromMilliseconds(16);
            //timer.Tick += Timer_Tick;
            //timer.Start();
        }
Beispiel #30
0
        private static void DrawGridPatch(
            Vector3Collection positions, IntCollection indices, Color4Collection colors, int startX, int startY)
        {
            var c1 = (Color)ColorConverter.ConvertFromString("#c5d1d8");
            c1.Clamp();
            var c2 = (Color)ColorConverter.ConvertFromString("#ddeaf2");
            c2.Clamp();

            var darkGridColor = new Color4(new Vector4(c1.ScR,c1.ScG ,c1.ScB, 1));
            var lightGridColor = new Color4(new Vector4(c2.ScR, c2.ScG, c2.ScB, 1));

            const int size = 10;

            for (var x = startX; x <= startX + size; x++)
            {
                if (x == 0 && startY < 0) continue;

                var v = new Vector3(x, -.001f, startY);
                positions.Add(v);
                indices.Add(positions.Count - 1);
                positions.Add(new Vector3(x, -.001f, startY + size));
                indices.Add(positions.Count - 1);

                if (x % 5 == 0)
                {
                    colors.Add(darkGridColor);
                    colors.Add(darkGridColor);
                }
                else
                {
                    colors.Add(lightGridColor);
                    colors.Add(lightGridColor);
                }
            }

            for (var y = startY; y <= startY + size; y++)
            {
                if (y == 0 && startX >= 0) continue;

                positions.Add(new Vector3(startX, -.001f, y));
                indices.Add(positions.Count - 1);
                positions.Add(new Vector3(startX + size, -.001f, y));
                indices.Add(positions.Count - 1);

                if (y % 5 == 0)
                {
                    colors.Add(darkGridColor);
                    colors.Add(darkGridColor);
                }
                else
                {
                    colors.Add(lightGridColor);
                    colors.Add(lightGridColor);
                }
            }
        }
        private void AddPointCloud()
        {
            var points = new PointGeometry3D();
            var col    = new Color4Collection();  // gradient na kolory
            var ptPos  = new Vector3Collection(); // pozycje punktów
            var ptIdx  = new IntCollection();     // indeksy punktów


            int dataSampling = 2; // taki prosty przeskok miedzy punktami /można zrobić coś na podstawie odległości kamery albo różnicy wysokości między kolejnymi punktami

            // wpisywane na sztywno dane (lepiej zrobić funkcję obsługującą odpowiednie typy)
            // path = "C:/Users/astat/Desktop/DaneViewer/xyzi_wrakII.txt";
            //path = "C:/Users/astat/Desktop/DaneViewer/export_xyzi.txt";
            if (filepath != null && positionX != null && positionY != null && positionZ != null)
            {
                dataFromTxt(filepath, positionX, positionY, positionZ);
                //newdataFromTxt(path, positionX, positionY, positionZ);
                double maxX = positionX.Max();
                double minX = positionX.Min();
                double maxY = positionY.Max();
                double minY = positionY.Min();
                double maxZ = positionZ.Max();
                double minZ = positionZ.Min();

                Trace.WriteLine($"{maxX} {minX} {maxY} {minY} {maxZ} {minZ}");

                for (int i = 0; i < positionX.Count(); i += dataSampling) // tutaj powinna być pętla foreach ale potrzebowałem prostego przerzedzenia dannych
                {
                    //  Trace.WriteLine($"{i} {positionX[i]} {positionY[i]} {positionZ[i]}");
                    bool reverse = true;

                    var positionZReversed = maxZ - positionZ[i]; // czasami dostawałem punkty z odwróconą osią Z po to ta zmienna

                    var positionToColour = Math.Abs(positionZ[i] / (maxZ - minZ));

                    ptIdx.Add(ptPos.Count);
                    if (reverse == true)
                    {
                        ptPos.Add(new Vector3((float)positionX[i], (float)positionY[i], (float)positionZReversed));
                    }
                    else
                    {
                        ptPos.Add(new Vector3((float)positionX[i], (float)positionY[i], (float)positionZ[i]));
                    }

                    // Trace.WriteLine($"{i} {positionX[i]} {positionY[i]} {positionZ[i]} {positionToColour}");

                    var colourR = Math.Sqrt(-positionToColour + 0.75);
                    var colourG = (Math.Sin(4 * positionToColour - 0.2)) / 1.6;//tutaj trzeba potworzyć funkcje matematyczne żeby zmieniały się kolorki/ można też użyć warunków i color4.Scale
                    var colourB = Math.Sqrt(positionToColour - 0.25);

                    col.Add(new Color4((float)colourR, (float)colourG, (float)colourB, 1f)); //te kolory to czarna magia ale nie są według żadnej skali tylko żeby ładnie wyglądały hehe
                }                                                                            //Trace.WriteLine($"{i} {positionToColour} {colourR} {colourG} {colourB} {col[i]}");

                points.Positions = ptPos;
                points.Indices   = ptIdx;
                points.Colors    = col;
                PointsColor      = Colors.White; // <- nie pamiętam po co to tutaj ale bez tego nie działa
                Points           = points;
                PointsTransform  = new Media3D.TranslateTransform3D(0, 0, minZ);

                // floor plane grid

                CreateGrid(maxX, minX, maxY, minY, minZ);
                Grid          = lines.ToLineGeometry3D();
                GridColor     = new Color4(153 / 255.0f, 204 / 255.0f, 255 / 255.0f, (float)0.3).ToColor();;
                GridTransform = new Media3D.TranslateTransform3D(0, 0, minZ);

                // strzałki

                CreateArrows((float)maxX, (float)minX, (float)maxY, (float)minY, (float)maxZ, (float)minZ);
                Arrow          = arrows.ToLineGeometry3D();
                ArrowColor     = new Color4(0, 255 / 255.0f, 255 / 255.0f, (float)0.5).ToColor();;
                ArrowTransform = new Media3D.TranslateTransform3D(0, 0, minZ);
            }
        }