Ejemplo n.º 1
0
        public static MonoGameDeviceContent <MonoGameModelTemplate> CreateDeviceModel(GraphicsDevice device, Schema2.ModelRoot srcModel, LoaderContext context = null)
        {
            if (context == null)
            {
                context = new BasicEffectsLoaderContext(device);
            }

            context.Reset();

            var options = new Runtime.RuntimeOptions {
                IsolateMemory = true
            };

            var templates = srcModel.LogicalScenes
                            .Select(item => SceneTemplate.Create(item, options))
                            .ToArray();

            var srcMeshes = templates
                            .SelectMany(item => item.LogicalMeshIds)
                            .Distinct()
                            .Select(idx => srcModel.LogicalMeshes[idx]);

            foreach (var srcMesh in srcMeshes)
            {
                context._WriteMesh(srcMesh);
            }

            var dstMeshes = context.CreateRuntimeModels();

            var mdl = new MonoGameModelTemplate(templates, srcModel.DefaultScene.LogicalIndex, dstMeshes);

            return(new MonoGameDeviceContent <MonoGameModelTemplate>(mdl, context.Disposables.ToArray()));
        }
Ejemplo n.º 2
0
        private static (SceneTemplate, WeakReference <Schema2.ModelRoot>) LoadModelTemplate(string path)
        {
            var model = Schema2.ModelRoot.Load(path);

            var template = SceneTemplate.Create(model.DefaultScene, true);

            return(template, new WeakReference <Schema2.ModelRoot>(model));
        }
Ejemplo n.º 3
0
        private static (SceneTemplate, WeakReference <Schema2.ModelRoot>) LoadModelTemplate(string path)
        {
            var model = Schema2.ModelRoot.Load(path);

            var options = new Runtime.RuntimeOptions {
                IsolateMemory = true
            };

            var template = SceneTemplate.Create(model.DefaultScene, options);

            return(template, new WeakReference <Schema2.ModelRoot>(model));
        }
Ejemplo n.º 4
0
        public static void TestMeshDecoding()
        {
            var modelPath = TestFiles.GetSampleModelsPaths()
                            .FirstOrDefault(item => item.Contains("BrainStem.glb"));

            var model = Schema2.ModelRoot.Load(modelPath);

            model.AttachToCurrentTest("reference.plotly");


            var scene = model.DefaultScene;

            var decodedMeshes = scene.LogicalParent.LogicalMeshes.Decode();
            var sceneTemplate = SceneTemplate.Create(scene);
            var sceneInstance = sceneTemplate.CreateInstance();

            var duration = sceneInstance.Armature.AnimationTracks[0].Duration;

            sceneInstance.Armature.SetAnimationFrame(0, duration / 2);

            IEnumerable <(Vector3, Vector3, Vector3, int)> evaluateTriangles(DrawableInstance inst)
            {
                var mesh = decodedMeshes[inst.Template.LogicalMeshIndex];

                foreach (var prim in mesh.Primitives)
                {
                    foreach (var(idxA, idxB, idxC) in prim.TriangleIndices)
                    {
                        var posA = prim.GetPosition(idxA, inst.Transform);
                        var posB = prim.GetPosition(idxB, inst.Transform);
                        var posC = prim.GetPosition(idxC, inst.Transform);

                        yield return(posA, posB, posC, 0xb0b0b0);
                    }
                }
            }

            var worldTriangles = sceneInstance.SelectMany(item => evaluateTriangles(item));

            var scenePlot = new PlotlyScene();

            scenePlot.AppendTriangles(worldTriangles, c => c);

            scenePlot
            .ToHtml()
            .AttachToCurrentTest("result.html", (content, finfo) => System.IO.File.WriteAllText(finfo.FullName, content));
        }
Ejemplo n.º 5
0
        public static MonoGameDeviceContent <MonoGameModelTemplate> CreateDeviceModel(GraphicsDevice device, Schema2.ModelRoot srcModel)
        {
            srcModel.FixTextureSampler();

            var templates = srcModel.LogicalScenes
                            .Select(item => SceneTemplate.Create(item, true))
                            .ToArray();

            var context = new LoaderContext(device);

            var meshes = templates
                         .SelectMany(item => item.LogicalMeshIds)
                         .ToDictionary(k => k, k => context.CreateMesh(srcModel.LogicalMeshes[k]));

            var mdl = new MonoGameModelTemplate(templates, srcModel.DefaultScene.LogicalIndex, meshes);

            return(new MonoGameDeviceContent <MonoGameModelTemplate>(mdl, context.Disposables.ToArray()));
        }
Ejemplo n.º 6
0
        public static void TestMeshDecodingBounds()
        {
            var modelPath = TestFiles.GetSampleModelsPaths()
                            .FirstOrDefault(item => item.Contains("BrainStem.glb"));

            var model = Schema2.ModelRoot.Load(modelPath);

            var(center, radius) = model.DefaultScene.EvaluateBoundingSphere(0.25f);

            var sceneTemplate = SceneTemplate.Create(model.DefaultScene);
            var sceneInstance = sceneTemplate.CreateInstance();

            sceneInstance.Armature.SetAnimationFrame(0, 0.1f);

            var vertices = sceneInstance.GetWorldVertices(model.LogicalMeshes.Decode()).ToList();

            foreach (var p in vertices)
            {
                var d = (p - center).Length();
                Assert.LessOrEqual(d, radius + 0.0001f);
            }
        }