Example #1
0
        static CL.source CreateSource(string id, Func <int, Vector4> get, int components, int len)
        {
            var src = new CL.source();

            src.id = id;
            var floats = new float[len * components];

            for (int i = 0; i < len; i++)
            {
                var v4 = get(i);
                floats[i * components]     = v4.X;
                floats[i * components + 1] = v4.Y;
                if (components > 2)
                {
                    floats[i * components + 2] = v4.Z;
                }
                if (components > 3)
                {
                    floats[i * components + 3] = v4.W;
                }
            }

            string arrId = id + "-array";

            src.Item = new CL.float_array()
            {
                id   = arrId,
                Text = string.Join(" ", floats.Select((x) => x.ToString(CultureInfo.InvariantCulture)))
            };
            src.technique_common = new CL.sourceTechnique_common();
            var acc = new CL.accessor()
            {
                source = "#" + arrId,
                count  = (ulong)len,
                stride = (ulong)components
            };

            src.technique_common.accessor = acc;
            if (components == 2)
            {
                acc.param = new CL.param[]
                {
                    new CL.param()
                    {
                        name = "U", type = "float"
                    },
                    new CL.param()
                    {
                        name = "V", type = "float"
                    }
                };
            }
            else if (components == 3)
            {
                acc.param = new CL.param[]
                {
                    new CL.param()
                    {
                        name = "X", type = "float"
                    },
                    new CL.param()
                    {
                        name = "Y", type = "float"
                    },
                    new CL.param()
                    {
                        name = "Z", type = "float"
                    }
                };
            }
            else if (components == 4)
            {
                acc.param = new CL.param[]
                {
                    new CL.param()
                    {
                        name = "R", type = "float"
                    },
                    new CL.param()
                    {
                        name = "G", type = "float"
                    },
                    new CL.param()
                    {
                        name = "B", type = "float"
                    },
                    new CL.param()
                    {
                        name = "A", type = "float"
                    }
                };
            }

            return(src);
        }
Example #2
0
        static ExportModel ProcessModel(ModelFile mdl, ResourceManager resources)
        {
            mdl.Path = mdl.Path.Replace(' ', '_');
            var ex = new ExportModel();

            for (int midx = 0; midx < mdl.Levels.Length; midx++)
            {
                var lvl       = mdl.Levels[midx];
                var processed = ProcessRef(lvl, resources);
                var geo       = new CL.geometry();
                geo.name = geo.id = mdl.Path + "-level" + midx;
                var mesh = new CL.mesh();
                geo.Item = mesh;
                CL.source positions;
                CL.source normals = null;
                CL.source colors  = null;
                CL.source tex1    = null;
                CL.source tex2    = null;
                int       idxC    = 1;
                positions = CreateSource(
                    geo.name + "-positions",
                    (k) => new Vector4(processed.Vertices[k].Position, 0),
                    3, processed.Vertices.Length);
                mesh.vertices = new CL.vertices()
                {
                    id    = geo.name + "-vertices",
                    input = new CL.InputLocal[]
                    {
                        new CL.InputLocal()
                        {
                            semantic = "POSITION", source = "#" + positions.id
                        }
                    }
                };
                var sources = new List <CL.source>()
                {
                    positions
                };
                if ((processed.FVF & D3DFVF.NORMAL) == D3DFVF.NORMAL)
                {
                    normals = CreateSource(
                        geo.name + "-normals",
                        (k) => new Vector4(processed.Vertices[k].Normal, 0),
                        3, processed.Vertices.Length);
                    sources.Add(normals);
                    idxC++;
                }

                if ((processed.FVF & D3DFVF.DIFFUSE) == D3DFVF.DIFFUSE)
                {
                    colors = CreateSource(
                        geo.name + "-color",
                        (k) =>
                    {
                        var c = Color4.FromRgba(processed.Vertices[k].Diffuse);
                        return(new Vector4(c.R, c.G, c.B, c.A));
                    }, 4, processed.Vertices.Length);
                    sources.Add(colors);
                    idxC++;
                }

                bool doTex1, doTex2 = false;
                if ((processed.FVF & D3DFVF.TEX2) == D3DFVF.TEX2)
                {
                    doTex1 = doTex2 = true;
                }
                else if ((processed.FVF & D3DFVF.TEX1) == D3DFVF.TEX1)
                {
                    doTex1 = true;
                }
                else
                {
                    doTex1 = doTex2 = false;
                }
                if (doTex1)
                {
                    tex1 = CreateSource(
                        geo.name + "-tex1",
                        (k) => new Vector4(processed.Vertices[k].TextureCoordinate, 0, 0),
                        2, processed.Vertices.Length);
                    sources.Add(tex1);
                    idxC++;
                }

                if (doTex2)
                {
                    tex2 = CreateSource(
                        geo.name + "-tex2",
                        (k) => new Vector4(processed.Vertices[k].TextureCoordinateTwo, 0, 0),
                        2, processed.Vertices.Length);
                    sources.Add(tex2);
                    idxC++;
                }

                mesh.source = sources.ToArray();
                var items = new List <object>();
                foreach (var dc in processed.Drawcalls)
                {
                    if (!ex.Materials.Any((x) => x.Name == dc.Material.Name))
                    {
                        ex.Materials.Add(dc.Material);
                    }
                    var trs = new CL.triangles();
                    trs.count    = (ulong)(dc.Indices.Length / 3);
                    trs.material = dc.Material.Name + "-material";
                    List <int> pRefs = new List <int>(dc.Indices.Length * idxC);
                    List <CL.InputLocalOffset> inputs = new List <CL.InputLocalOffset>()
                    {
                        new CL.InputLocalOffset()
                        {
                            semantic = "VERTEX", source = "#" + geo.id + "-vertices", offset = 0
                        }
                    };
                    ulong off = 1;
                    if (normals != null)
                    {
                        inputs.Add(new CL.InputLocalOffset()
                        {
                            semantic = "NORMAL",
                            source   = "#" + normals.id,
                            offset   = off++
                        });
                    }
                    if (colors != null)
                    {
                        inputs.Add(new CL.InputLocalOffset()
                        {
                            semantic = "COLOR",
                            source   = "#" + colors.id,
                            offset   = off++
                        });
                    }
                    if (tex1 != null)
                    {
                        inputs.Add(new CL.InputLocalOffset()
                        {
                            semantic = "TEXCOORD",
                            source   = "#" + tex1.id,
                            offset   = off++
                        });
                    }
                    if (tex2 != null)
                    {
                        inputs.Add(new CL.InputLocalOffset()
                        {
                            semantic = "TEXCOORD",
                            source   = "#" + tex2.id,
                            offset   = off++
                        });
                    }
                    trs.input = inputs.ToArray();
                    for (int i = 0; i < dc.Indices.Length; i++)
                    {
                        for (int j = 0; j < idxC; j++)
                        {
                            pRefs.Add(dc.Indices[i]);
                        }
                    }

                    trs.p = string.Join(" ", pRefs.ToArray());
                    items.Add(trs);
                }

                mesh.Items = items.ToArray();
                ex.Geometries.Add(geo);
            }

            return(ex);
        }