public void CreateAllAnimationTypesScene() { // 3D View 7.1908.9012.0 has an issue displaying off-center meshes with animated morph targets. TestContext.CurrentContext.AttachShowDirLink(); TestContext.CurrentContext.AttachGltfValidatorLinks(); // create two materials var pink = new MaterialBuilder("material1") .WithChannelParam(KnownChannel.BaseColor, new Vector4(1, 0, 1, 1)); var yellow = new MaterialBuilder("material2") .WithChannelParam(KnownChannel.BaseColor, new Vector4(1, 1, 0, 1)); var scene = new SceneBuilder(); var mesh1 = VPOSNRM.CreateCompatibleMesh("shape1"); mesh1.AddCube(MaterialBuilder.CreateDefault(), Matrix4x4.Identity); var inst1 = scene.AddRigidMesh(mesh1, Matrix4x4.Identity); var mesh2 = VPOSNRM.CreateCompatibleMesh("shape2"); mesh2.AddCube(pink, Matrix4x4.Identity); var inst2 = scene.AddRigidMesh(mesh2, Matrix4x4.CreateTranslation(2, 0, 0)); scene.AttachToCurrentTest("static.glb"); scene.AttachToCurrentTest("static.gltf"); var morphBuilder = mesh2.UseMorphTarget(0); morphBuilder.SetVertexDelta(morphBuilder.Positions.ElementAt(0), (Vector3.UnitY, Vector3.Zero)); morphBuilder.SetVertexDelta(morphBuilder.Positions.ElementAt(1), (Vector3.UnitY, Vector3.Zero)); morphBuilder.SetVertexDelta(morphBuilder.Positions.ElementAt(2), (Vector3.UnitY, Vector3.Zero)); morphBuilder.SetVertexDelta(morphBuilder.Positions.ElementAt(3), (Vector3.UnitY, Vector3.Zero)); inst2.Content.UseMorphing().Value = Transforms.SparseWeight8.Create(1); var curve = inst2.Content.UseMorphing().UseTrackBuilder("Default"); curve.SetPoint(0, Transforms.SparseWeight8.Create(0)); curve.SetPoint(1, Transforms.SparseWeight8.Create(1)); curve.SetPoint(2, Transforms.SparseWeight8.Create(0)); var gltf = scene.ToGltf2(); // Assert.AreEqual(1, gltf.LogicalMeshes[1].MorphWeights[0]); scene.AttachToCurrentTest("mopth.glb"); scene.AttachToCurrentTest("mopth.gltf"); }
public void TestCreateEmptyMesh() { // create a scenebuilder with an empty mesh var sb = new SceneBuilder(); sb.AddRigidMesh(VPOSNRM.CreateCompatibleMesh("Empty"), Matrix4x4.Identity); var schema = sb.ToGltf2(); Assert.AreEqual(0, schema.LogicalMeshes.Count, "SceneBuilder should detect empty meshes and remove them."); schema.CreateMesh("Empty2"); var fileName = AttachmentInfo.From("empty.glb").File.FullName; Assert.Throws <SharpGLTF.Validation.SchemaException>(() => schema.SaveGLB(fileName)); }
public void CreateAllAnimationTypesScene() { // 3D View 7.1908.9012.0 has an issue displaying off-center meshes with animated morph targets. TestContext.CurrentContext.AttachGltfValidatorLinks(); // create two materials var pink = new MaterialBuilder("material1") .WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, new Vector4(1, 0, 1, 1)); var yellow = new MaterialBuilder("material2") .WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, new Vector4(1, 1, 0, 1)); var mesh1 = VPOSNRM.CreateCompatibleMesh("shape1"); mesh1.AddCube(pink, Matrix4x4.Identity); var mesh2 = VPOSNRM.CreateCompatibleMesh("shape2"); mesh2.AddCube(yellow, Matrix4x4.Identity); var scene = new SceneBuilder(); var inst1 = scene.AddRigidMesh(mesh1, Matrix4x4.Identity); // meshes intended to support animation must be created using an armature var armature = new NodeBuilder(); armature.LocalTransform = Matrix4x4.CreateTranslation(2, 0, 0); var inst2 = scene.AddRigidMesh(mesh2, armature); scene.AttachToCurrentTest("static.glb"); scene.AttachToCurrentTest("static.gltf"); // up to this point, the scene has two plain unanimated cubes. var morphBuilder = mesh2.UseMorphTarget(0); morphBuilder.SetVertexDelta(morphBuilder.Positions.ElementAt(0), (Vector3.UnitY, Vector3.Zero)); morphBuilder.SetVertexDelta(morphBuilder.Positions.ElementAt(1), (Vector3.UnitY, Vector3.Zero)); morphBuilder.SetVertexDelta(morphBuilder.Positions.ElementAt(2), (Vector3.UnitY, Vector3.Zero)); morphBuilder.SetVertexDelta(morphBuilder.Positions.ElementAt(3), (Vector3.UnitY, Vector3.Zero)); // set default value. inst2.Content.UseMorphing().SetValue(1); // ser animation curve. var curve = inst2.Content.UseMorphing().UseTrackBuilder("Default"); curve.SetPoint(0, true, 0); curve.SetPoint(1, true, 1); curve.SetPoint(2, true, 0); var gltf = scene.ToGltf2(); TestContext.WriteLine(gltf.GetJsonPreview()); var meshIdx = 1; Assert.AreEqual(1, gltf.LogicalMeshes[meshIdx].Primitives[0].MorphTargetsCount); Assert.AreEqual(1, gltf.LogicalMeshes[meshIdx].MorphWeights[0]); Assert.AreEqual(1, gltf.LogicalAnimations.Count); scene.AttachToCurrentTest("mopth.glb"); scene.AttachToCurrentTest("mopth.gltf"); }
public void CreateSceneWithRandomShapes(bool useGpuInstancing) { TestContext.CurrentContext.AttachGltfValidatorLinks(); var rnd = new Random(177); // create materials var materials = Enumerable .Range(0, 10) .Select(idx => new MaterialBuilder() .WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, new Vector4(rnd.NextVector3(), 1))) .ToList(); // create meshes var sphereMeshes = Enumerable .Range(0, 10) .Select(idx => materials[idx]) .Select(mat => { var mesh = VPOSNRM.CreateCompatibleMesh("shape"); #if DEBUG mesh.VertexPreprocessor.SetValidationPreprocessors(); #else mesh.VertexPreprocessor.SetSanitizerPreprocessors(); #endif mesh.AddSphere(mat, 0.5f, Matrix4x4.Identity); mesh.Validate(); return(mesh); }); var cubeMeshes = Enumerable .Range(0, 10) .Select(idx => materials[idx]) .Select(mat => { var mesh = VPOSNRM.CreateCompatibleMesh("shape"); #if DEBUG mesh.VertexPreprocessor.SetValidationPreprocessors(); #else mesh.VertexPreprocessor.SetSanitizerPreprocessors(); #endif mesh.AddCube(mat, Matrix4x4.Identity); mesh.Validate(); return(mesh); }); var meshes = sphereMeshes.Concat(cubeMeshes).ToArray(); // create scene var scene = new SceneBuilder(); for (int i = 0; i < 100; ++i) { var mesh = meshes[rnd.Next(0, 20)]; // create random transform var r = rnd.NextQuaternion(); var t = rnd.NextVector3() * 25; scene.AddRigidMesh(mesh, (r, t)); } // collapse to glTF var gltf = scene.ToGltf2(useGpuInstancing ? SceneBuilderSchema2Settings.WithGpuInstancing : SceneBuilderSchema2Settings.Default); var bounds = Runtime.MeshDecoder.EvaluateBoundingBox(gltf.DefaultScene); // Assert.AreEqual(defaultBounds,instancedBounds); // save the model as GLB gltf.AttachToCurrentTest("shapes.glb"); scene.AttachToCurrentTest("shapes.plotly"); }
public static void AttachToCurrentTest(this Scenes.SceneBuilder scene, string fileName) { var model = scene.ToGltf2(); model.AttachToCurrentTest(fileName); }