Exemple #1
0
        public override ModelContent Process(Assimp.Scene scene, string filename, ContentProcessorContext context)
        {
            try
            {
                Assimp.AssimpContext c = new Assimp.AssimpContext();

                Assimp.ExportFormatDescription des = c.GetSupportedExportFormats()[0];
                //c.ExportFile(scene,Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),"test.dae"),des.FormatId);
                ModelContent content = new ModelContent();
                content.Meshes = new MeshContent[scene.MeshCount];
                for (int meshIndex = 0; meshIndex < scene.MeshCount; meshIndex++)
                {
                    var sceneMesh   = scene.Meshes[meshIndex];
                    var meshContent = new MeshContent();
                    meshContent.PrimitiveCount = sceneMesh.FaceCount;

                    meshContent.Vertices = new VertexPositionNormalTexture[meshContent.PrimitiveCount * 3];
                    int vertex = 0;
                    //TODO: indexing
                    foreach (var f in sceneMesh.Faces)
                    {
                        foreach (var i in f.Indices)
                        {
                            var             pos  = sceneMesh.Vertices[i];
                            var             norm = sceneMesh.Normals[i];
                            Assimp.Vector3D tex  = new Assimp.Vector3D();
                            if (sceneMesh.TextureCoordinateChannels.Length > 0 && sceneMesh.TextureCoordinateChannels[0].Count > i)
                            {
                                tex = sceneMesh.TextureCoordinateChannels[0][i];
                            }
                            var translated = new Vector3(pos.X, pos.Y, pos.Z) + settings.Translate;
                            meshContent.Vertices[vertex++] = new VertexPositionNormalTexture(
                                new Vector3(translated.X * settings.Scale.X, translated.Y * settings.Scale.Y, translated.Z * settings.Scale.Z),
                                new Vector3(norm.X, norm.Y, norm.Z),
                                new Vector2(tex.X, -tex.Y));
                        }
                    }

                    /*for (int i = 0; i < sceneMesh.VertexCount; i++)
                     * {
                     *
                     * }*/

                    content.Meshes[meshIndex] = meshContent;
                }
                content.Nodes    = new List <NodeContent>();
                content.RootNode = ParseNode(content, scene.RootNode);
                foreach (var animation in scene.Animations)
                {
                    var anim = new AnimationContent();
                    anim.Channels = new List <AnimationNodeContent>();
                    foreach (var channel in animation.NodeAnimationChannels)
                    {
                        AnimationNodeContent node = new AnimationNodeContent();
                        var curNode = content.Nodes.First(n => n.Name == channel.NodeName);
                        node.Node   = curNode;
                        node.Frames = new List <AnimationFrame>();
                        int   frameCount = Math.Max(Math.Max(channel.PositionKeyCount, channel.RotationKeyCount), channel.ScalingKeyCount);
                        float diff = 0.0f, maxTime = 0;;
                        for (int i = 0; i < frameCount; i++)
                        {
                            AnimationFrame frame = new AnimationFrame();

                            if (i < channel.PositionKeyCount)
                            {
                                frame.Frame = (float)channel.PositionKeys[i].Time;
                            }
                            else if (i < channel.RotationKeyCount)
                            {
                                frame.Frame = (float)channel.RotationKeys[i].Time;
                            }
                            else if (i < channel.ScalingKeyCount)
                            {
                                frame.Frame = (float)channel.ScalingKeys[i].Time;
                            }
                            if (i == 0)
                            {
                                diff = frame.Frame;
                            }
                            frame.Frame -= diff;
                            frame.Frame  = (float)(frame.Frame / animation.TicksPerSecond);
                            maxTime      = Math.Max(frame.Frame, maxTime);
                            //TODO: interpolation
                            var rot = channel.RotationKeyCount == 0 ? new Assimp.Quaternion(1, 0, 0, 0) : i >= channel.RotationKeyCount ? channel.RotationKeys.Last().Value : channel.RotationKeys[i].Value;
                            var loc = channel.PositionKeyCount == 0 ? new Assimp.Vector3D() : i >= channel.PositionKeyCount ? channel.PositionKeys.Last().Value : channel.PositionKeys[i].Value;
                            var sca = channel.ScalingKeyCount == 0 ? new Assimp.Vector3D(1, 1, 1) : i >= channel.ScalingKeyCount ? channel.ScalingKeys.Last().Value : channel.ScalingKeys[i].Value;
                            rot.Normalize();

                            frame.Transform = new AnimationTransform(node.Node.Name, new Vector3((loc.X + settings.Translate.X), (loc.Y + settings.Translate.Y), (loc.Z + settings.Translate.Z)),
                                                                     new Vector3(sca.X * settings.Scale.X, sca.Y * settings.Scale.Y, sca.Z * settings.Scale.Z), new Quaternion(rot.X, rot.Y, rot.Z, rot.W));
                            node.Frames.Add(frame);
                        }
                        anim.MaxTime = maxTime;
                        anim.Channels.Add(node);
                    }
                    content.Animations.Add(anim);
                }
                PostProcess(content, content.RootNode);



                return(content);
            }
            catch (Exception ex)
            {
                context.RaiseBuildMessage(filename, ex.Message, BuildMessageEventArgs.BuildMessageType.Error);
            }
            return(null);
        }
Exemple #2
0
        public static void ExportScene(string folderName, Assimp.ExportFormatDescription format, string textureExtension, out string[] textureNames)
        {
            List <string> textureNamesList = new List <string>();

            lock (renderableJSPs)
                foreach (var v in renderableJSPs)
                {
                    try
                    {
                        textureNamesList.AddRange(v.Textures);
                        Assimp_IO.ExportAssimp(
                            Path.Combine(folderName, v.assetName + "." + format.FileExtension),
                            ReadFileMethods.ReadRenderWareFile(v.Data), true, format, textureExtension, Matrix.Identity);
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show($"Unable to export asset {v}: {e.Message}");
                    }
                }

            lock (renderableAssets)
                foreach (var v in renderableAssets)
                {
                    try
                    {
                        Asset  modelAsset;
                        string assetName;
                        Matrix world;

                        if (v is EntityAsset entity)
                        {
                            if (entity.isInvisible || entity.DontRender || entity is AssetTRIG)
                            {
                                continue;
                            }

                            if (entity is AssetPKUP pkup)
                            {
                                if (AssetPICK.pickEntries.ContainsKey(pkup.PickReferenceID))
                                {
                                    modelAsset = (Asset)renderingDictionary[pkup.PickReferenceID];
                                }
                                else
                                {
                                    continue;
                                }
                            }
                            else
                            {
                                modelAsset = (Asset)renderingDictionary[entity.Model_AssetID];
                            }

                            assetName = entity.assetName;
                            world     = entity.world;
                        }
                        else if (v is AssetDYNA dyna && !AssetDYNA.dontRender && dyna is IRenderableAsset)
                        {
                            if (dyna.isInvisible)
                            {
                                continue;
                            }

                            if (dyna is DynaGObjectRing ring)
                            {
                                modelAsset = (Asset)renderingDictionary[DynaGObjectRingControl.RingModelAssetID];
                                world      = ring.world;
                            }
                            else if (dyna is DynaEnemySB enemySb)
                            {
                                modelAsset = (Asset)renderingDictionary[enemySb.Model_AssetID];
                                world      = enemySb.world;
                            }
                            else
                            {
                                continue;
                            }

                            assetName = dyna.assetName;
                        }
                        else
                        {
                            continue;
                        }

                        if (modelAsset is AssetMINF minf)
                        {
                            modelAsset = (Asset)renderingDictionary[minf.MinfReferences[0].Model_AssetID];
                        }

                        if (modelAsset is AssetMODL modl)
                        {
                            textureNamesList.AddRange(modl.Textures);
                        }
                        else
                        {
                            continue;
                        }

                        Assimp_IO.ExportAssimp(
                            Path.Combine(folderName, assetName + "." + format.FileExtension),
                            ReadFileMethods.ReadRenderWareFile(modl.Data), true, format, textureExtension, world);
                    }