Exemplo n.º 1
0
        protected override void Init(MyObjectBuilder_DefinitionBase ob)
        {
            base.Init(ob);

            var objectBuilder = ob as MyObjectBuilder_BlockNavigationDefinition;

            Debug.Assert(ob != null);
            if (ob == null)
            {
                return;
            }

            if (objectBuilder.NoEntry || objectBuilder.Triangles == null)
            {
                NoEntry = true;
            }
            else
            {
                NoEntry = false;
                var newMesh = new MyGridNavigationMesh(null, null, objectBuilder.Triangles.Length);

                Vector3I maxPos = objectBuilder.Size - Vector3I.One - objectBuilder.Center;
                Vector3I minPos = -(Vector3I)(objectBuilder.Center);

                foreach (var triOb in objectBuilder.Triangles)
                {
                    Vector3 pa = (Vector3)triOb.Points[0];
                    Vector3 pb = (Vector3)triOb.Points[1];
                    Vector3 pc = (Vector3)triOb.Points[2];

                    var tri = newMesh.AddTriangle(ref pa, ref pb, ref pc);

                    var center = (pa + pb + pc) / 3.0f;

                    // We want to move the triangle vertices more towards the triangle center to ensure correct calculation of containing cube
                    Vector3  cvA      = (center - pa) * 0.0001f;
                    Vector3  cvB      = (center - pb) * 0.0001f;
                    Vector3  cvC      = (center - pc) * 0.0001f;
                    Vector3I gridPosA = Vector3I.Round(pa + cvA);
                    Vector3I gridPosB = Vector3I.Round(pb + cvB);
                    Vector3I gridPosC = Vector3I.Round(pc + cvC);
                    Vector3I.Clamp(ref gridPosA, ref minPos, ref maxPos, out gridPosA);
                    Vector3I.Clamp(ref gridPosB, ref minPos, ref maxPos, out gridPosB);
                    Vector3I.Clamp(ref gridPosC, ref minPos, ref maxPos, out gridPosC);
                    Vector3I min, max;
                    Vector3I.Min(ref gridPosA, ref gridPosB, out min);
                    Vector3I.Min(ref min, ref gridPosC, out min);
                    Vector3I.Max(ref gridPosA, ref gridPosB, out max);
                    Vector3I.Max(ref max, ref gridPosC, out max);

                    Vector3I pos = min;
                    for (var it = new Vector3I.RangeIterator(ref min, ref max); it.IsValid(); it.GetNext(out pos))
                    {
                        newMesh.RegisterTriangle(tri, ref pos);
                    }
                }

                m_mesh = newMesh;
            }
        }
        protected override void Init(MyObjectBuilder_DefinitionBase ob)
        {
            base.Init(ob);

            var objectBuilder = ob as MyObjectBuilder_BlockNavigationDefinition;
            Debug.Assert(ob != null);
            if (ob == null) return;

            if (objectBuilder.NoEntry || objectBuilder.Triangles == null)
            {
                NoEntry = true;
            }
            else
            {
                NoEntry = false;
                var newMesh = new MyGridNavigationMesh(null, null, objectBuilder.Triangles.Length);

                Vector3I maxPos = objectBuilder.Size - Vector3I.One - objectBuilder.Center;
                Vector3I minPos = - (Vector3I)(objectBuilder.Center);

                foreach (var triOb in objectBuilder.Triangles)
                {
                    Vector3 pa = (Vector3)triOb.Points[0];
                    Vector3 pb = (Vector3)triOb.Points[1];
                    Vector3 pc = (Vector3)triOb.Points[2];

                    var tri = newMesh.AddTriangle(ref pa, ref pb, ref pc);

                    var center = (pa + pb + pc) / 3.0f;

                    // We want to move the triangle vertices more towards the triangle center to ensure correct calculation of containing cube
                    Vector3 cvA = (center - pa) * 0.0001f;
                    Vector3 cvB = (center - pb) * 0.0001f;
                    Vector3 cvC = (center - pc) * 0.0001f;
                    Vector3I gridPosA = Vector3I.Round(pa + cvA);
                    Vector3I gridPosB = Vector3I.Round(pb + cvB);
                    Vector3I gridPosC = Vector3I.Round(pc + cvC);
                    Vector3I.Clamp(ref gridPosA, ref minPos, ref maxPos, out gridPosA);
                    Vector3I.Clamp(ref gridPosB, ref minPos, ref maxPos, out gridPosB);
                    Vector3I.Clamp(ref gridPosC, ref minPos, ref maxPos, out gridPosC);
                    Vector3I min, max;
                    Vector3I.Min(ref gridPosA, ref gridPosB, out min);
                    Vector3I.Min(ref min, ref gridPosC, out min);
                    Vector3I.Max(ref gridPosA, ref gridPosB, out max);
                    Vector3I.Max(ref max, ref gridPosC, out max);

                    Vector3I pos = min;
                    for (var it = new Vector3I.RangeIterator(ref min, ref max); it.IsValid(); it.GetNext(out pos))
                    {
                        newMesh.RegisterTriangle(tri, ref pos);
                    }
                }

                m_mesh = newMesh;
            }
        }
Exemplo n.º 3
0
        protected override unsafe void Init(MyObjectBuilder_DefinitionBase ob)
        {
            base.Init(ob);
            MyObjectBuilder_BlockNavigationDefinition definition = ob as MyObjectBuilder_BlockNavigationDefinition;

            if (ob != null)
            {
                if (definition.NoEntry || (definition.Triangles == null))
                {
                    this.NoEntry = true;
                }
                else
                {
                    this.NoEntry = false;
                    MyGridNavigationMesh mesh = new MyGridNavigationMesh(null, null, definition.Triangles.Length, null);
                    Vector3I             max  = (Vector3I)((definition.Size - Vector3I.One) - definition.Center);
                    Vector3I             min  = (Vector3I) - definition.Center;
                    MyObjectBuilder_BlockNavigationDefinition.Triangle[] triangles = definition.Triangles;
                    int index = 0;
                    while (index < triangles.Length)
                    {
                        Vector3I vectori6;
                        Vector3I vectori7;
                        MyObjectBuilder_BlockNavigationDefinition.Triangle triangle1 = triangles[index];
                        Vector3 a = (Vector3)triangle1.Points[0];
                        Vector3 b = (Vector3)triangle1.Points[1];
                        Vector3 c = (Vector3)triangle1.Points[2];
                        MyNavigationTriangle tri = mesh.AddTriangle(ref a, ref b, ref c);
                        Vector3   vector4        = ((((a + b) + c) / 3f) - a) * 0.0001f;
                        Vector3   vector5        = ((((a + b) + c) / 3f) - b) * 0.0001f;
                        Vector3   vector6        = ((((a + b) + c) / 3f) - c) * 0.0001f;
                        Vector3I  result         = Vector3I.Round(a + vector4);
                        Vector3I  vectori4       = Vector3I.Round(b + vector5);
                        Vector3I  vectori5       = Vector3I.Round(c + vector6);
                        Vector3I *vectoriPtr1    = (Vector3I *)ref result;
                        Vector3I.Clamp(ref (Vector3I) ref vectoriPtr1, ref min, ref max, out result);
                        Vector3I *vectoriPtr2 = (Vector3I *)ref vectori4;
                        Vector3I.Clamp(ref (Vector3I) ref vectoriPtr2, ref min, ref max, out vectori4);
                        Vector3I *vectoriPtr3 = (Vector3I *)ref vectori5;
                        Vector3I.Clamp(ref (Vector3I) ref vectoriPtr3, ref min, ref max, out vectori5);
                        Vector3I.Min(ref result, ref vectori4, out vectori6);
                        Vector3I *vectoriPtr4 = (Vector3I *)ref vectori6;
                        Vector3I.Min(ref (Vector3I) ref vectoriPtr4, ref vectori5, out vectori6);
                        Vector3I.Max(ref result, ref vectori4, out vectori7);
                        Vector3I *vectoriPtr5 = (Vector3I *)ref vectori7;
                        Vector3I.Max(ref (Vector3I) ref vectoriPtr5, ref vectori5, out vectori7);
                        Vector3I gridPos = vectori6;
                        Vector3I_RangeIterator iterator = new Vector3I_RangeIterator(ref vectori6, ref vectori7);
                        while (true)
                        {
                            if (!iterator.IsValid())
                            {
                                index++;
                                break;
                            }
                            mesh.RegisterTriangle(tri, ref gridPos);
                            iterator.GetNext(out gridPos);
                        }
                    }
                    this.m_mesh = mesh;
                }
            }
        }