Ejemplo n.º 1
0
        void AddEnvelopes(GmdcGroup g, Ambertation.Scenes.Mesh m, GmdcElement bonee, GmdcElement bonewighte, Hashtable jointmap)
        {
            if (bonee != null && true)
            {
                int pos = 0;
                foreach (SimPe.Plugin.Gmdc.GmdcElementValueOneInt vi in bonee.Values)
                {
                    byte[]       data = vi.Bytes;
                    IntArrayList used = new IntArrayList();

                    for (int datapos = 0; datapos < 3; datapos++)                 //we can only store 3 bone weights
                    {
                        byte b = data[datapos];
                        if (b != 0xff && b < g.UsedJoints.Count)
                        {
                            int bnr = g.UsedJoints[b];
                            if (used.Contains(bnr))
                            {
                                continue;
                            }


                            used.Add(bnr);
                            Ambertation.Scenes.Joint nj = jointmap[bnr] as Ambertation.Scenes.Joint;
                            if (nj != null)
                            {
                                double w = 1;
                                if (bonewighte != null)
                                {
                                    if (bonewighte.Values.Count > pos)
                                    {
                                        SimPe.Plugin.Gmdc.GmdcElementValueBase v = bonewighte.Values[pos];
                                        if (datapos < v.Data.Length)
                                        {
                                            w = v.Data[datapos];
                                        }
                                    }
                                }

                                //if there is no envelope for nj, make sure we get a new one
                                //with pos 0-Weights inserted
                                Ambertation.Scenes.Envelope e = m.GetJointEnvelope(nj, pos);
                                e.Weights.Add(w);
                                //added = true;
                            }
                        }
                    }

                    pos++;
                    m.SyncEnvelopeLenghts(pos); //fill all unset EnvelopeWeights with 0
                }                               // bonee.Values
            }
        }
Ejemplo n.º 2
0
        public Ambertation.Scenes.Scene GetScene(GmdcGroups groups, string absimgpath, string imgfolder, ElementOrder component)
        {
            if (absimgpath != null)
            {
                if (imgfolder == null)
                {
                    imgfolder = absimgpath;
                }
                imgfolder = imgfolder.Trim();
                if (imgfolder.Length > 0 && !imgfolder.EndsWith(@"\"))
                {
                    imgfolder += @"\";
                }

                if (!System.IO.Directory.Exists(absimgpath))
                {
                    System.IO.Directory.CreateDirectory(absimgpath);
                }
            }

            Scene scn = new Scene();

            Hashtable jointmap = new Hashtable();

            try
            {
                jointmap = AddJointsToScene(scn, component);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
            }


            TextureLocator tl = new TextureLocator(gmdc.Parent.Package);

            System.Collections.Hashtable txmts = tl.FindMaterials(gmdc.Parent);
            foreach (string key in txmtmap.Keys)
            {
                object o = txmtmap[key];
                if (o != null)
                {
                    txmts[key] = txmtmap[key];
                }
            }


            Hashtable txtrs = tl.FindReferencedTXTR(txmts, null);

            foreach (string key in txtrmap.Keys)
            {
                object o = txtrmap[key];
                if (o != null)
                {
                    txtrs[key] = o;
                }
            }

            txtrs = tl.GetLargestImages(txtrs);
            txmts = tl.GetMaterials(txmts, scn);
            tl.Dispose();

            foreach (GmdcGroup g in groups)
            {
                Ambertation.Scenes.Material mat = txmts[g.Name] as Ambertation.Scenes.Material;
                if (mat == null)
                {
                    mat = scn.CreateMaterial("mat_" + g.Name);
                }
                else
                {
                    mat.Name = "mat_" + g.Name;
                }
                System.IO.MemoryStream s = txtrs[g.Name] as System.IO.MemoryStream;
                if (s != null)
                {
                    try
                    {
                        System.Drawing.Image img = System.Drawing.Image.FromStream(s);
                        if (absimgpath != null)
                        {
                            img.Save(System.IO.Path.Combine(absimgpath, g.Name + ".png"), System.Drawing.Imaging.ImageFormat.Png);
                        }
                        mat.Texture.FileName     = imgfolder + g.Name + ".png";
                        mat.Texture.Size         = img.Size;
                        mat.Texture.TextureImage = img;
                    }
                    catch {}
                }
                Ambertation.Scenes.Mesh m = scn.CreateMesh(g.Name, mat);

                GmdcElement vertexe = g.Link.FindElementType(ElementIdentity.Vertex);
                //	GmdcElement vertexme = g.Link.FindElementType(ElementIdentity.MorphVertexDelta);
                GmdcElement normale    = g.Link.FindElementType(ElementIdentity.Normal);
                GmdcElement texte      = g.Link.FindElementType(ElementIdentity.UVCoordinate);
                GmdcElement bonee      = g.Link.FindElementType(ElementIdentity.BoneAssignment);
                GmdcElement bonewighte = g.Link.FindElementType(ElementIdentity.BoneWeights);
                GmdcElement bumpnormal = g.Link.FindElementType(ElementIdentity.BumpMapNormal);

                int nr = g.Link.GetElementNr(vertexe);
                //	int mnr = g.Link.GetElementNr(vertexme);
                for (int i = 0; i < g.Link.ReferencedSize; i++)
                {
                    Vector3f v = new Vector3f(g.Link.GetValue(nr, i).Data[0], g.Link.GetValue(nr, i).Data[1], g.Link.GetValue(nr, i).Data[2]);

                    /*Vector3f vm = new Vector3f(g.Link.GetValue(mnr, i).Data[0], g.Link.GetValue(mnr, i).Data[1], g.Link.GetValue(mnr, i).Data[2]);
                     * v += vm;*/
                    v = component.TransformScaled(v);

                    m.Vertices.Add(v.X, v.Y, v.Z);
                }

                if (normale != null)
                {
                    nr = g.Link.GetElementNr(normale);
                    for (int i = 0; i < g.Link.ReferencedSize; i++)
                    {
                        Vector3f v = new Vector3f(g.Link.GetValue(nr, i).Data[0], g.Link.GetValue(nr, i).Data[1], g.Link.GetValue(nr, i).Data[2]);
                        v = component.TransformNormal(v);
                        m.Normals.Add(v.X, v.Y, v.Z);
                    }
                }

                if (bumpnormal != null)
                {
                    nr = g.Link.GetElementNr(bumpnormal);
                    for (int i = 0; i < g.Link.ReferencedSize; i++)
                    {
                        Vector3f v = new Vector3f(g.Link.GetValue(nr, i).Data[0], g.Link.GetValue(nr, i).Data[1], g.Link.GetValue(nr, i).Data[2]);
                        v = component.TransformNormal(v);
                        m.BumpMapNormalDelta.Add(v.X, v.Y, v.Z);
                    }
                }

                if (texte != null)
                {
                    nr = g.Link.GetElementNr(texte);
                    for (int i = 0; i < g.Link.ReferencedSize; i++)
                    {
                        Vector2f v = new Vector2f(g.Link.GetValue(nr, i).Data[0], g.Link.GetValue(nr, i).Data[1]);
                        m.TextureCoordinates.Add(v.X, 1 - v.Y);
                    }
                }

                for (int i = 0; i < g.Faces.Count - 2; i += 3)
                {
                    m.FaceIndices.Add(g.Faces[i], g.Faces[i + 1], g.Faces[i + 2]);
                }

                AddEnvelopes(g, m, bonee, bonewighte, jointmap);
            }

            return(scn);
        }