Beispiel #1
0
        private static Gltf InitializeGlTF(Model model, List <byte> buffer, bool drawEdges = false)
        {
            var gltf  = new Gltf();
            var asset = new Asset();

            asset.Version   = "2.0";
            asset.Generator = "hypar-gltf";

            gltf.Asset = asset;

            var root = new Node();

            var rootTransform = new Transform(model.Transform);

            // Rotate the transform for +Z up.
            rootTransform.Rotate(new Vector3(1, 0, 0), -90.0);
            var m = rootTransform.Matrix;

            root.Matrix = new float[] {
                (float)m.m11, (float)m.m12, (float)m.m13, 0f,
                (float)m.m21, (float)m.m22, (float)m.m23, 0f,
                (float)m.m31, (float)m.m32, (float)m.m33, 0f,
                (float)m.tx, (float)m.ty, (float)m.tz, 1f
            };

            var nodes = new List <glTFLoader.Schema.Node> {
                root
            };
            var meshes = new List <glTFLoader.Schema.Mesh>();

            gltf.Scene = 0;
            var scene = new Scene();

            scene.Nodes = new[] { 0 };
            gltf.Scenes = new[] { scene };

            gltf.ExtensionsUsed = new[] {
                "KHR_materials_pbrSpecularGlossiness",
                "KHR_materials_unlit",
                "KHR_lights_punctual"
            };

            var bufferViews = new List <BufferView>();
            var accessors   = new List <Accessor>();

            var materialsToAdd = model.AllElementsOfType <Material>().ToList();

            if (drawEdges)
            {
                materialsToAdd.Add(BuiltInMaterials.Edges);
            }
            var materials = gltf.AddMaterials(materialsToAdd, buffer, bufferViews);

            var elements = model.Elements.Where(e =>
            {
                return(e.Value is GeometricElement || e.Value is ElementInstance);
            }).Select(e => e.Value).ToList();

            var lights = model.AllElementsOfType <DirectionalLight>().ToList();

            gltf.AddLights(lights, nodes);

            // Lines are stored in a list of lists
            // according to the max available index size.
            var lines     = new List <List <Vector3> >();
            var currLines = new List <Vector3>();

            lines.Add(currLines);

            var meshElementMap = new Dictionary <Guid, List <int> >();

            foreach (var e in elements)
            {
                // Check if we'll overrun the index size
                // for the current line array. If so,
                // create a new line array.
                if (currLines.Count * 2 > ushort.MaxValue)
                {
                    currLines = new List <Vector3>();
                    lines.Add(currLines);
                }

                GetRenderDataForElement(e,
                                        gltf,
                                        materials,
                                        buffer,
                                        bufferViews,
                                        accessors,
                                        meshes,
                                        nodes,
                                        meshElementMap,
                                        currLines,
                                        drawEdges);
            }
            if (buffer.Count == 0 && lights.Count == 0)
            {
                return(null);
            }

            if (drawEdges && lines.Count() > 0)
            {
                foreach (var lineSet in lines)
                {
                    if (lineSet.Count == 0)
                    {
                        continue;
                    }
                    AddLines(GetNextId(), lineSet, gltf, materials[BuiltInMaterials.Edges.Name], buffer, bufferViews, accessors, meshes, nodes, false);
                }
            }

            var buff = new glTFLoader.Schema.Buffer();

            buff.ByteLength  = buffer.Count;
            gltf.Buffers     = new[] { buff };
            gltf.BufferViews = bufferViews.ToArray();
            gltf.Accessors   = accessors.ToArray();
            gltf.Nodes       = nodes.ToArray();
            if (meshes.Count > 0)
            {
                gltf.Meshes = meshes.ToArray();
            }

            return(gltf);
        }