Exemplo n.º 1
0
        public static List <PolyChunk> Merge(List <PolyChunk> source)
        {
            var          strips = new List <(NJS_MATERIAL mat, List <PolyChunk> pre, PolyChunkStrip str)>();
            NJS_MATERIAL curmat = new NJS_MATERIAL();
            var          cnks   = new List <PolyChunk>();

            foreach (var chunk in source)
            {
                curmat.UpdateFromPolyChunk(chunk);
                if (chunk is PolyChunkStrip str)
                {
                    bool found = false;
                    foreach (var set in strips)
                    {
                        if (set.mat.Equals(curmat) && set.str.Type == str.Type)
                        {
                            found = true;
                            set.str.Strips.AddRange(str.Strips);
                            break;
                        }
                    }
                    if (!found)
                    {
                        strips.Add((curmat, cnks, str));
                    }
                    curmat = new NJS_MATERIAL(curmat);
                    cnks   = new List <PolyChunk>();
                }
                else
                {
                    cnks.Add(chunk);
                }
            }
            return(strips.SelectMany(a => a.pre.Append(a.str)).ToList());
        }
Exemplo n.º 2
0
 public MeshInfo(NJS_MATERIAL material, Poly[] polys, VertexData[] vertices, bool hasUV, bool hasVC)
 {
     Material = material;
     Polys    = polys;
     Vertices = vertices;
     HasUV    = hasUV;
     HasVC    = hasVC;
 }
Exemplo n.º 3
0
 /// <summary>
 /// Use this if you need a copy of a material.
 /// </summary>
 /// <param name="copy"></param>
 public NJS_MATERIAL(NJS_MATERIAL copy)
 {
     AmbientColor  = copy.AmbientColor;
     DiffuseColor  = copy.DiffuseColor;
     SpecularColor = copy.SpecularColor;
     Exponent      = copy.Exponent;
     TextureID     = copy.TextureID;
     Flags         = copy.Flags;
 }
Exemplo n.º 4
0
 public void UpdateFlags(NJS_MATERIAL mat)
 {
     IgnoreLight        = mat.IgnoreLighting;
     IgnoreSpecular     = mat.IgnoreSpecular;
     IgnoreAmbient      = mat.IgnoreAmbient;
     UseAlpha           = mat.UseAlpha;
     DoubleSide         = mat.DoubleSided;
     FlatShading        = mat.FlatShading;
     EnvironmentMapping = mat.EnvironmentMap;
 }
Exemplo n.º 5
0
        public override bool Equals(object obj)
        {
            if (!(obj is NJS_MATERIAL))
            {
                return(false);
            }
            NJS_MATERIAL other = (NJS_MATERIAL)obj;

            return(AmbientColor == other.AmbientColor && DiffuseColor == other.DiffuseColor && SpecularColor == other.SpecularColor &&
                   Exponent == other.Exponent && TextureID == other.TextureID && Flags == other.Flags);
        }
Exemplo n.º 6
0
 public PolyChunkTinyTextureID(NJS_MATERIAL mat)
     : this()
 {
     MipmapDAdjust = mat.MipmapDAdjust;
     ClampV        = mat.ClampV;
     ClampU        = mat.ClampU;
     FlipV         = mat.FlipV;
     FlipU         = mat.FlipU;
     TextureID     = (ushort)mat.TextureID;
     SuperSample   = mat.SuperSample;
     FilterMode    = mat.FilterMode;
 }
Exemplo n.º 7
0
        public override void ProcessVertexData()
        {
            List <MeshInfo> result = new List <MeshInfo>();

            foreach (NJS_MESHSET mesh in Mesh)
            {
                bool              hasVColor = mesh.VColor != null;
                bool              hasUV     = mesh.UV != null;
                List <Poly>       polys     = new List <Poly>();
                List <VertexData> verts     = new List <VertexData>();
                int currentstriptotal       = 0;
                foreach (Poly poly in mesh.Poly)
                {
                    Poly newpoly = null;
                    switch (mesh.PolyType)
                    {
                    case Basic_PolyType.Triangles:
                        newpoly = new Triangle();
                        break;

                    case Basic_PolyType.Quads:
                        newpoly = new Quad();
                        break;

                    case Basic_PolyType.NPoly:
                    case Basic_PolyType.Strips:
                        newpoly = new Strip(poly.Indexes.Length, ((Strip)poly).Reversed);
                        break;
                    }
                    for (int i = 0; i < poly.Indexes.Length; i++)
                    {
                        newpoly.Indexes[i] = (ushort)verts.Count;
                        verts.Add(new VertexData(
                                      Vertex[poly.Indexes[i]],
                                      Normal[poly.Indexes[i]],
                                      hasVColor ? Color.FromArgb(mesh.VColor[currentstriptotal].R, mesh.VColor[currentstriptotal].G, mesh.VColor[currentstriptotal].B)  : null,
                                      hasUV ? mesh.UV[currentstriptotal++] : null));
                    }
                    polys.Add(newpoly);
                }
                NJS_MATERIAL mat = null;
                if (Material != null && mesh.MaterialID < Material.Count)
                {
                    mat = Material[mesh.MaterialID];
                }
                result.Add(new MeshInfo(mat, polys.ToArray(), verts.ToArray(), hasUV, hasVColor));
            }
            MeshInfo = result.ToArray();
        }
Exemplo n.º 8
0
 public PolyChunkMaterial(NJS_MATERIAL mat)
 {
     SourceAlpha      = mat.SourceAlpha;
     DestinationAlpha = mat.DestinationAlpha;
     Diffuse          = mat.DiffuseColor;
     if (mat.AmbientColor != Color.White)
     {
         Ambient = mat.AmbientColor;
     }
     if (mat.SpecularColor != Color.Transparent)
     {
         Specular         = mat.SpecularColor;
         SpecularExponent = (byte)mat.Exponent;
     }
 }
Exemplo n.º 9
0
        private List <MeshInfo> ProcessPolyList(string name, List <PolyChunk> strips, int start)
        {
            List <MeshInfo> result = new List <MeshInfo>();

            for (int i = start; i < strips.Count; i++)
            {
                PolyChunk chunk = strips[i];
                MaterialBuffer.UpdateFromPolyChunk(chunk);
                switch (chunk.Type)
                {
                case ChunkType.Bits_CachePolygonList:
                    byte cachenum = ((PolyChunkBitsCachePolygonList)chunk).List;
#if modellog
                    Extensions.Log("Caching Poly List " + name + "[" + (i + 1) + "] to cache #" + cachenum + Environment.NewLine);
#endif
                    PolyCache[cachenum] = new CachedPoly(name, strips, i + 1);
                    return(result);

                case ChunkType.Bits_DrawPolygonList:
                    cachenum = ((PolyChunkBitsDrawPolygonList)chunk).List;
                    CachedPoly cached = PolyCache[cachenum];
#if modellog
                    Extensions.Log("Drawing Poly List " + cached.Name + "[" + cached.Index + "] from cache #" + cachenum + Environment.NewLine);
#endif
                    result.AddRange(ProcessPolyList(cached.Name, cached.Polys, cached.Index));
                    break;

                case ChunkType.Strip_Strip:
                case ChunkType.Strip_StripUVN:
                case ChunkType.Strip_StripUVH:
                case ChunkType.Strip_StripNormal:
                case ChunkType.Strip_StripUVNNormal:
                case ChunkType.Strip_StripUVHNormal:
                case ChunkType.Strip_StripColor:
                case ChunkType.Strip_StripUVNColor:
                case ChunkType.Strip_StripUVHColor:
                case ChunkType.Strip_Strip2:
                case ChunkType.Strip_StripUVN2:
                case ChunkType.Strip_StripUVH2:
                {
                    PolyChunkStrip c2 = (PolyChunkStrip)chunk;
#if modellog
                    Extensions.Log("Strip " + c2.Type + Environment.NewLine);
#endif
                    bool hasVColor = false;
                    switch (chunk.Type)
                    {
                    case ChunkType.Strip_StripColor:
                    case ChunkType.Strip_StripUVNColor:
                    case ChunkType.Strip_StripUVHColor:
                        hasVColor = true;
                        break;
                    }
                    bool hasUV = false;
                    switch (chunk.Type)
                    {
                    case ChunkType.Strip_StripUVN:
                    case ChunkType.Strip_StripUVH:
                    case ChunkType.Strip_StripUVNColor:
                    case ChunkType.Strip_StripUVHColor:
                    case ChunkType.Strip_StripUVN2:
                    case ChunkType.Strip_StripUVH2:
                        hasUV = true;
                        break;
                    }
                    List <Poly>       polys = new List <Poly>();
                    List <VertexData> verts = new List <VertexData>();
#if modellog
                    List <ushort> indexes = new List <ushort>();
#endif
                    foreach (PolyChunkStrip.Strip strip in c2.Strips)
                    {
#if modellog
                        indexes.AddRange(strip.Indexes);
#endif
                        Strip str = new Strip(strip.Indexes.Length, strip.Reversed);
                        for (int k = 0; k < strip.Indexes.Length; k++)
                        {
                            str.Indexes[k] = (ushort)verts.Count;
                            verts.Add(new VertexData(
                                          VertexBuffer[strip.Indexes[k]].Position,
                                          VertexBuffer[strip.Indexes[k]].Normal,
                                          hasVColor ? (Color?)strip.VColors[k] : VertexBuffer[strip.Indexes[k]].Color,
                                          hasUV ? strip.UVs[k] : null));
                        }
                        polys.Add(str);
                    }
                    if (!hasVColor)
                    {
                        hasVColor = verts.Any(a => a.Color.HasValue && a.Color.Value != Color.White);
                    }
#if modellog
                    indexes = new List <ushort>(System.Linq.Enumerable.Distinct(indexes));
                    indexes.Sort();
                    StringBuilder sb  = new StringBuilder("Vertex Usage: ");
                    ushort        ist = indexes[0];
                    for (int k = 0; k < indexes.Count - 1; k++)
                    {
                        if (indexes[k + 1] != indexes[k] + 1)
                        {
                            sb.Append(" " + ist);
                            if (indexes[k] != ist)
                            {
                                sb.Append("-" + indexes[k]);
                            }
                            ist = indexes[++k];
                        }
                    }
                    sb.Append(" " + ist);
                    if (indexes[indexes.Count - 1] != ist)
                    {
                        sb.Append("-" + indexes[indexes.Count - 1]);
                    }
                    sb.Append(Environment.NewLine);
                    Extensions.Log(sb.ToString());
#endif
                    result.Add(new MeshInfo(MaterialBuffer, polys.ToArray(), verts.ToArray(), hasUV, hasVColor));
                    MaterialBuffer = new NJS_MATERIAL(MaterialBuffer);
                }
                break;
                }
            }
            return(result);
        }
Exemplo n.º 10
0
        public override void ProcessShapeMotionVertexData(NJS_MOTION motion, float frame, int animindex)
        {
            if (!motion.Models.ContainsKey(animindex))
            {
                ProcessVertexData();
                return;
            }
            Vertex[]      vertdata = Vertex;
            Vertex[]      normdata = Normal;
            AnimModelData data     = motion.Models[animindex];

            if (data.Vertex.Count > 0)
            {
                vertdata = data.GetVertex(frame);
            }
            if (data.Normal.Count > 0)
            {
                normdata = data.GetNormal(frame);
            }
            List <MeshInfo> result = new List <MeshInfo>();

            foreach (NJS_MESHSET mesh in Mesh)
            {
                bool              hasVColor = mesh.VColor != null;
                bool              hasUV     = mesh.UV != null;
                List <Poly>       polys     = new List <Poly>();
                List <VertexData> verts     = new List <VertexData>();
                int currentstriptotal       = 0;
                foreach (Poly poly in mesh.Poly)
                {
                    Poly newpoly = null;
                    switch (mesh.PolyType)
                    {
                    case Basic_PolyType.Triangles:
                        newpoly = new Triangle();
                        break;

                    case Basic_PolyType.Quads:
                        newpoly = new Quad();
                        break;

                    case Basic_PolyType.NPoly:
                    case Basic_PolyType.Strips:
                        newpoly = new Strip(poly.Indexes.Length, ((Strip)poly).Reversed);
                        break;
                    }
                    for (int i = 0; i < poly.Indexes.Length; i++)
                    {
                        newpoly.Indexes[i] = (ushort)verts.Count;
                        verts.Add(new VertexData(
                                      vertdata[poly.Indexes[i]],
                                      normdata[poly.Indexes[i]],
                                      hasVColor ? (Color?)mesh.VColor[currentstriptotal] : null,
                                      hasUV ? mesh.UV[currentstriptotal++] : null));
                    }
                    polys.Add(newpoly);
                }
                NJS_MATERIAL mat = null;
                if (Material != null && mesh.MaterialID < Material.Count)
                {
                    mat = Material[mesh.MaterialID];
                }
                result.Add(new MeshInfo(mat, polys.ToArray(), verts.ToArray(), hasUV, hasVColor));
            }
            MeshInfo = result.ToArray();
        }