Beispiel #1
0
        IEnumerator BuildMesh(MeshImporter.MeshContext x, int i)
        {
            using (MeasureTime("BuildMesh"))
            {
                MeshWithMaterials meshWithMaterials;
                if (EnableLoadBalancing)
                {
                    var buildMesh = MeshImporter.BuildMeshCoroutine(MaterialFactory, x);
                    yield return(buildMesh);

                    meshWithMaterials = buildMesh.Current as MeshWithMaterials;
                }
                else
                {
                    meshWithMaterials = MeshImporter.BuildMesh(MaterialFactory, x);
                }

                var mesh = meshWithMaterials.Mesh;

                // mesh name
                if (string.IsNullOrEmpty(mesh.name))
                {
                    mesh.name = string.Format("UniGLTF import#{0}", i);
                }
                var originalName = mesh.name;
                for (int j = 1; Meshes.Any(y => y.Mesh.name == mesh.name); ++j)
                {
                    mesh.name = string.Format("{0}({1})", originalName, j);
                }

                yield return(meshWithMaterials);
            }
        }
Beispiel #2
0
        void ImportMeshes()
        {
            OpenFileDialog dialog = new OpenFileDialog();

            dialog.Filter      = "Meshes (*.obj;*.dae;*.3ds;*.x)|*.obj;*.dae;*.3ds;*.x";
            dialog.Multiselect = true;
            if (dialog.ShowDialog() == false)
            {
                return;
            }

            foreach (var filePath in dialog.FileNames)
            {
                if (Meshes.Any(a => a.Name == Path.GetFileNameWithoutExtension(filePath)))
                {
                    MessageBox.Show("Already exists!");
                    continue;
                }
                Meshes.Add(_formatter.LoadMesh(filePath));
            }
        }
Beispiel #3
0
        async Task <MeshWithMaterials> BuildMeshAsync(IAwaitCaller awaitCaller, Func <string, IDisposable> MeasureTime, MeshData meshData, int i)
        {
            using (MeasureTime("BuildMesh"))
            {
                var meshWithMaterials = await MeshUploader.BuildMeshAndUploadAsync(awaitCaller, meshData, MaterialFactory.GetMaterial);

                var mesh = meshWithMaterials.Mesh;

                // mesh name
                if (string.IsNullOrEmpty(mesh.name))
                {
                    mesh.name = string.Format("UniGLTF import#{0}", i);
                }
                var originalName = mesh.name;
                for (int j = 1; Meshes.Any(y => y.Mesh.name == mesh.name); ++j)
                {
                    mesh.name = string.Format("{0}({1})", originalName, j);
                }

                return(meshWithMaterials);
            }
        }
Beispiel #4
0
        IEnumerator LoadMeshes()
        {
            var meshImporter = new MeshImporter();

            for (int i = 0; i < GLTF.meshes.Count; ++i)
            {
                var meshContext       = meshImporter.ReadMesh(this, i);
                var meshWithMaterials = MeshImporter.BuildMesh(this, meshContext);
                var mesh = meshWithMaterials.Mesh;
                if (string.IsNullOrEmpty(mesh.name))
                {
                    mesh.name = string.Format("UniGLTF import#{0}", i);
                }
                var originalName = mesh.name;
                for (int j = 1; Meshes.Any(y => y.Mesh.name == mesh.name); ++j)
                {
                    mesh.name = string.Format("{0}({1})", originalName, j);
                }
                Meshes.Add(meshWithMaterials);

                yield return(null);
            }
        }
        protected virtual Schedulable <Unit> LoadAsync()
        {
            return
                (Schedulable.Create()
                 .AddTask(Scheduler.ThreadPool, () =>
            {
                if (m_textures.Count == 0)
                {
                    //
                    // runtime
                    //
                    CreateTextureItems();
                }
                else
                {
                    //
                    // already CreateTextures(by assetPostProcessor or editor menu)
                    //
                }
            })
                 .ContinueWithCoroutine(Scheduler.ThreadPool, TexturesProcessOnAnyThread)
                 .ContinueWithCoroutine(Scheduler.MainThread, TexturesProcessOnMainThread)
                 .ContinueWithCoroutine(Scheduler.MainThread, LoadMaterials)
                 .OnExecute(Scheduler.ThreadPool, parent =>
            {
                // UniGLTF does not support draco
                // https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_draco_mesh_compression/README.md#conformance
                if (GLTF.extensionsRequired.Contains("KHR_draco_mesh_compression"))
                {
                    throw new UniGLTFNotSupportedException("draco is not supported");
                }

                // meshes
                var meshImporter = new MeshImporter();
                for (int i = 0; i < GLTF.meshes.Count; ++i)
                {
                    var index = i;
                    parent.AddTask(Scheduler.ThreadPool,
                                   () =>
                    {
                        using (MeasureTime("ReadMesh"))
                        {
                            return meshImporter.ReadMesh(this, index);
                        }
                    })
                    .ContinueWith(Scheduler.MainThread, x =>
                    {
                        using (MeasureTime("BuildMesh"))
                        {
                            var meshWithMaterials = MeshImporter.BuildMesh(this, x);

                            var mesh = meshWithMaterials.Mesh;

                            // mesh name
                            if (string.IsNullOrEmpty(mesh.name))
                            {
                                mesh.name = string.Format("UniGLTF import#{0}", i);
                            }
                            var originalName = mesh.name;
                            for (int j = 1; Meshes.Any(y => y.Mesh.name == mesh.name); ++j)
                            {
                                mesh.name = string.Format("{0}({1})", originalName, j);
                            }

                            return meshWithMaterials;
                        }
                    })
                    .ContinueWith(Scheduler.ThreadPool, x => Meshes.Add(x))
                    ;
                }
            })
                 .ContinueWithCoroutine(Scheduler.MainThread, LoadNodes)
                 .ContinueWithCoroutine(Scheduler.MainThread, BuildHierarchy)
                 .ContinueWith(Scheduler.MainThread, _ =>
            {
                using (MeasureTime("AnimationImporter"))
                {
                    AnimationImporter.ImportAnimation(this);
                }
            })
                 .ContinueWith(Scheduler.CurrentThread,
                               _ =>
            {
                OnLoadModel();
                if (m_showSpeedLog)
                {
                    Debug.Log(GetSpeedLog());
                }
                return Unit.Default;
            }));
        }
Beispiel #6
0
        public Entity CreateEntity()
        {
            var result = new Entity();

            var boneTransforms = new TransformComponent[Bones.Length];

            for (var i = 0; i < Bones.Length; i++)
            {
                var bone = Bones[i];

                var parentTransform = bone.Parent != null
                    ? boneTransforms[bone.Parent.Index]
                    : result.Transform;

                var boneEntity = new Entity();
                boneEntity.Name = bone.Name + " Animation Offset Parent";
                boneEntity.Transform.LocalPosition = bone.Translation;
                boneEntity.Transform.LocalRotation = bone.Rotation;

                parentTransform.Children.Add(boneEntity.Transform);

                var animatedBoneEntity = new Entity();
                animatedBoneEntity.Name = bone.Name;
                boneEntity.AddChild(animatedBoneEntity);

                boneTransforms[i] = animatedBoneEntity.Transform;
            }

            result.Components.Add(new ModelComponent(boneTransforms, Meshes.Any(x => x.Skinned)));

            foreach (var mesh in Meshes)
            {
                var boneEntity = boneTransforms[mesh.ParentBone.Index].Entity;

                if (mesh.Skinned)
                {
                    // Add skinned mesh component to root model entity,
                    // not bone entity.
                    result.Components.Add(new MeshComponent
                    {
                        Mesh = mesh
                    });
                }
                else
                {
                    boneEntity.Components.Add(new MeshComponent
                    {
                        Mesh = mesh
                    });
                }
            }

            foreach (var animation in Animations)
            {
                result.Components.Add(new AnimationComponent
                {
                    Animation = animation
                });
            }

            return(result);
        }
Beispiel #7
0
        public void Save(Stream stream)
        {
            var packageXml = new XDocument(new XElement("PartPackage"));

            packageXml.Root.Add(new XElement("Info",
                                             new XElement("Part", PartID),
                                             new XElement("Description", Description)
                                             ));

            if (Meshes.Any())
            {
                var meshesElem = new XElement("Meshes");
                packageXml.Root.Add(meshesElem);
                foreach (var partMesh in Meshes)
                {
                    meshesElem.Add(partMesh.Serialize());
                }
            }

            if (DecorationImages.Any())
            {
                var decoElem = packageXml.Root.AddElement("Decorations");
                foreach (var decImg in DecorationImages)
                {
                    decoElem.Add(decImg.Serialize());
                }
            }

            if (Configurations.Any())
            {
                var brickElem = packageXml.Root.AddElement("Configurations");
                foreach (var brick in Configurations)
                {
                    var elem = new XElement("Brick",
                                            new XAttribute("ElementID", brick.ElementID),
                                            new XAttribute("MaterialID", brick.MaterialID));


                    foreach (var dec in brick.Decorations)
                    {
                        elem.Add(XmlHelper.DefaultSerialize(dec));
                    }

                    foreach (var subMat in brick.SubMaterials)
                    {
                        elem.Add(XmlHelper.DefaultSerialize(subMat));
                    }

                    brickElem.Add(elem);
                }
            }

            using (var zipStream = new ZipOutputStream(stream))
            {
                zipStream.SetLevel(1);

                zipStream.PutNextEntry(new ZipEntry(PACKAGE_XML_FILENAME));
                packageXml.Save(zipStream);
                zipStream.CloseEntry();

                if (Primitive != null)
                {
                    zipStream.PutNextEntry(new ZipEntry("primitive.xml"));
                    Primitive.Save(zipStream);
                    zipStream.CloseEntry();
                }

                foreach (var partMesh in Meshes)
                {
                    zipStream.PutNextEntry(new ZipEntry($"{MESH_FOLDER}{partMesh.GetFileName()}"));
                    using (var ms = new MemoryStream())
                    {
                        partMesh.Mesh.Save(ms);
                        ms.Seek(0, SeekOrigin.Begin);
                        ms.CopyTo(zipStream);
                    }
                    zipStream.CloseEntry();
                }

                foreach (var decImg in DecorationImages)
                {
                    zipStream.PutNextEntry(new ZipEntry($"{DECO_FOLDER}{decImg.GetFileName()}"));
                    decImg.Image.Save(zipStream, decImg.Image.RawFormat);
                    zipStream.CloseEntry();
                }
            }
        }
Beispiel #8
0
        protected virtual Schedulable <Unit> LoadAsync()
        {
            return
                (Schedulable.Create()
                 .AddTask(Scheduler.ThreadPool, () =>
            {
                if (m_textures.Count == 0)
                {
                    //
                    // runtime
                    //
                    CreateTextureItems();
                }
                else
                {
                    //
                    // already CreateTextures(by assetPostProcessor or editor menu)
                    //
                }
            })
                 .ContinueWithCoroutine(Scheduler.ThreadPool, () =>
            {
                using (MeasureTime("TexturesProcessOnAnyThread"))
                {
                    return TexturesProcessOnAnyThread();
                }
            })
                 .ContinueWithCoroutine(Scheduler.MainThread, () =>
            {
                using (MeasureTime("TexturesProcessOnMainThread"))
                {
                    return TexturesProcessOnMainThread();
                }
            })
                 .ContinueWithCoroutine(Scheduler.MainThread, () =>
            {
                using (MeasureTime("LoadMaterials"))
                {
                    return LoadMaterials();
                }
            })
                 .OnExecute(Scheduler.ThreadPool, parent =>
            {
                if (GLTF.meshes
                    .SelectMany(x => x.primitives)
                    .Any(x => x.extensions.KHR_draco_mesh_compression != null))
                {
                    throw new UniGLTFNotSupportedException("draco is not supported");
                }

                // meshes
                var meshImporter = new MeshImporter();
                for (int i = 0; i < GLTF.meshes.Count; ++i)
                {
                    var index = i;
                    parent.AddTask(Scheduler.ThreadPool,
                                   () =>
                    {
                        using (MeasureTime("ReadMesh"))
                        {
                            return meshImporter.ReadMesh(this, index);
                        }
                    })
                    .ContinueWith(Scheduler.MainThread, x =>
                    {
                        using (MeasureTime("BuildMesh"))
                        {
                            var meshWithMaterials = MeshImporter.BuildMesh(this, x);

                            var mesh = meshWithMaterials.Mesh;

                            // mesh name
                            if (string.IsNullOrEmpty(mesh.name))
                            {
                                mesh.name = string.Format("UniGLTF import#{0}", i);
                            }
                            var originalName = mesh.name;
                            for (int j = 1; Meshes.Any(y => y.Mesh.name == mesh.name); ++j)
                            {
                                mesh.name = string.Format("{0}({1})", originalName, j);
                            }

                            return meshWithMaterials;
                        }
                    })
                    .ContinueWith(Scheduler.ThreadPool, x => Meshes.Add(x))
                    ;
                }
            })
                 .ContinueWithCoroutine(Scheduler.MainThread, () =>
            {
                using (MeasureTime("LoadNodes"))
                {
                    return LoadNodes();
                }
            })
                 .ContinueWithCoroutine(Scheduler.MainThread, () =>
            {
                using (MeasureTime("BuildHierarchy"))
                {
                    return BuildHierarchy();
                }
            })
                 .ContinueWith(Scheduler.MainThread, _ =>
            {
                AnimationImporter.ImportAnimation(this);
            })
                 .ContinueWith(Scheduler.CurrentThread,
                               _ =>
            {
                OnLoadModel();
                Debug.Log(GetSpeedLog());
                return Unit.Default;
            }));
        }
Beispiel #9
0
 protected bool CollisionDetected() => Meshes.Any(mesh => NearObjects().FindAll(obj => obj.Collisionable).Any(obj => CollidesWith(obj)));
Beispiel #10
0
 public bool CollidesWith(GameObject foreign)
 {
     return(Meshes.Any(mesh => foreign.Meshes.Any(foreignMesh => TgcCollisionUtils.classifyBoxBox(mesh.BoundingBox, foreignMesh.BoundingBox) != TgcCollisionUtils.BoxBoxResult.Afuera)));
 }