コード例 #1
0
            CL.instance_material[] GetMaterials(CL.geometry g)
            {
                var materials = new List <CL.instance_material>();

                foreach (var item in ((CL.mesh)g.Item).Items)
                {
                    string matref = ((CL.triangles)item).material;
                    if (!materials.Any((m) => m.symbol == matref))
                    {
                        materials.Add(new CL.instance_material()
                        {
                            symbol = matref,
                            target = "#" + matref
                        });
                    }
                }

                return(materials.ToArray());
            }
コード例 #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);
        }