예제 #1
0
        public void LoadGameObject(IFileGeometry3D geo, FileInfo texture, string fileName)
        {
            var em = context.GetEntityManager();

            var box    = AxisAlignedBox.CreateFrom(geo.Positions);
            var center = box.Center;

            var c = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#B3B598");

            GraphicEntity en;

            if (geo.TextureCoors.Any())
            {
                en = EntityBuilders.BuildTextured(context, geo.Positions, geo.Indices,
                                                  geo.TextureCoors.ToArray(), texture,
                                                  SharpDX.Direct3D11.CullMode.None);
            }
            else
            {
                en = EntityBuilders.BuildColored(context, geo.Positions, geo.Indices, geo.Normals,
                                                 ToVector4(c), SharpDX.Direct3D11.CullMode.Front);
                en.UpdateComponent(FlatShadingGeometryComponent.Create());
            }
            en.UpdateComponent(TransformComponent.Create(Matrix4x4.CreateTranslation(Vector3.Zero - center)));
            GameObjects.Add(new SingleVisualObject(en.Tag, fileName));

            //var boxlines = PolylineGameObject.Create(
            //    Context,
            //    new ElementTag("poly"),
            //    GeometryBuilder.BuildBox(
            //        new AxisAlignedBox(box.Minimum, box.Maximum)),
            //    V4Colors.Blue
            //    );

            //Context.GetEntityManager().GetEntity(boxlines.Tag)
            //    .UpdateComponent(TransformComponent.Create(Matrix4x4.CreateTranslation(Vector3.Zero - center)));

            //GameObjects.Add(boxlines);
        }
예제 #2
0
        static void Import(string file, out IEnumerable <IFileGeometry3D> meshes, out FileInfo material, out AxisAlignedBox box)
        {
            box      = AxisAlignedBox.Zero;
            material = null;
            var f = new FileInfo(file);

            switch (f.Extension.ToLower())
            {
            case ".stl":
                meshes = G3Readers.ReadStl(f);
                box    = AxisAlignedBox.CreateFrom(meshes.First().Positions);
                break;

            case ".obj":
                var parser = new Utf8ByteOBJParser();
                using (var reader = new FileFormats.MemoryMappedFileReader(f)) {
                    parser.Read(reader.ReadSpan());
                }

                try {
                    material = parser.HasMTL ?
                               new FileInfo(parser.GetMaterialFilePath(f.Directory, f.Directory)) : null;
                } catch { }

                //var builder = new UnitedGroupsBulder(parser.GeometryCache);
                var builder = new GroupGeoBuilder(parser.GeometryCache);

                meshes = builder.Build();
                box    = AxisAlignedBox.CreateFrom(parser.GeometryCache.PositionsCache.AsReadOnly());

                break;

            default:
                throw new NotSupportedException($"'{f.Extension}' is not suppported format.");
            }
        }
예제 #3
0
 public static GeometryBoundsComponent Create(IReadOnlyList <Vector3> vertices)
 {
     return(new GeometryBoundsComponent(AxisAlignedBox.CreateFrom(vertices)));
 }
예제 #4
0
        public static ImmutableGeometryData BuildCylinder(Vector3 start, Vector3 axis, float radius, float lenght)
        {
            var positions = new Dictionary <Vector3, int>();
            var normals   = new List <Vector3>();
            var texCoor   = new List <Vector2>();
            var indices   = new List <int>();

            var circles = new List <List <Vector3> >();

            for (var z = 0; z < lenght; z += 10)
            {
                var circle = new List <Vector3>();
                var st     = (circles.Count % 2) == 0 ? 0f : 5f;
                for (var angle = st; angle < 360; angle += 10f)
                {
                    var rad = angle.ToRad();
                    var x   = radius * MathF.Cos(rad);
                    var y   = radius * MathF.Sin(rad);

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

            var prevCircle = circles[0];

            for (var i = 1; i < circles.Count; i++)
            {
                var circle = circles[i];

                var prevVb = prevCircle[0];
                var prevVt = circle[0];
                var cc0    = prevCircle.GetCenter();
                var cc1    = circle.GetCenter();

                int indx0, indx1, indx2, indx3;
                indx0 = indx1 = indx2 = indx3 = -1;
                for (var vI = 1; vI < prevCircle.Count; vI++)
                {
                    var vb = prevCircle[vI];
                    var vt = circle[vI];

                    var v0 = prevVb;
                    var v1 = prevVt;
                    var v2 = vb;
                    var v3 = vt;

                    if (!positions.TryGetValue(v0, out indx0))
                    {
                        indx0 = positions.Count;
                        positions.Add(v0, indx0);
                        normals.Add((v0 - cc0).Normalized());
                    }
                    if (!positions.TryGetValue(v1, out indx1))
                    {
                        indx1 = positions.Count;
                        positions.Add(v1, indx1);
                        normals.Add((v1 - cc1).Normalized());
                    }
                    if (!positions.TryGetValue(v2, out indx2))
                    {
                        indx2 = positions.Count;
                        positions.Add(v2, indx2);
                        normals.Add((v2 - cc0).Normalized());
                    }
                    if (!positions.TryGetValue(v3, out indx3))
                    {
                        indx3 = positions.Count;
                        positions.Add(v3, indx3);
                        normals.Add((v3 - cc0).Normalized());
                    }
                    indices.Add(indx1);
                    indices.Add(indx0);
                    indices.Add(indx2);

                    indices.Add(indx2);
                    indices.Add(indx3);
                    indices.Add(indx1);

                    prevVb = vb;
                    prevVt = vt;
                }
                indices.Add(positions[prevCircle[0]]);
                indices.Add(indx3);
                indices.Add(indx2);


                indices.Add(positions[circle[0]]);
                indices.Add(indx3);
                indices.Add(positions[prevCircle[0]]);


                prevCircle = circle;
            }

            var geo = new ImmutableGeometryData(positions.Keys.ToArray(), normals, indices);

            var box = AxisAlignedBox.CreateFrom(geo.Positions);

            var cross = Vector3.Cross(axis, Vector3.UnitZ);

            if (cross == Vector3.Zero)
            {
                cross = Vector3.UnitX;
            }
            var angleRad = axis.AngleRad(Vector3.UnitZ);

            var moveToZero           = Matrix4x4.CreateTranslation(-box.Center);
            var rotate               = Matrix4x4.CreateFromAxisAngle(cross, angleRad);
            var moveBoxCenterToStart = Matrix4x4.CreateTranslation(start);
            var moveCylStartToStart  = Matrix4x4.CreateTranslation(axis * box.Center.Length());

            geo = geo.Transform(moveToZero * rotate * moveBoxCenterToStart * moveCylStartToStart);

            return(geo);
        }