public List <Vultaik.Image> LoadTexture() { if (gltf.Images is null) { return(Array.Empty <Vultaik.Image>().ToList()); } List <Vultaik.Image> textures = new(); foreach (Schema.Image img in gltf.Images) { Vultaik.Image texture = null; string imgName = img.Name; if (img.BufferView is not null) { //load image from gltf buffer view Schema.BufferView bv = gltf.BufferViews[(int)img.BufferView]; EnsureBufferIsLoaded(bv.Buffer); } else if (img.Uri.StartsWith("data:", StringComparison.Ordinal)) { //load base64 encoded image texture = ImageFile.Load2DFromBytes(_device, LoadDataUri(img)); } else { texture = ImageFile.Load2DFromFile(_device, Path.Combine(baseDirectory, img.Uri)); imgName += ";" + img.Uri; } if (texture is not null) { textures.Add(texture); } } return(textures); }
public unsafe List <Mesh> LoadMeshes() { GetVertexCount(out var vCount, out var iCount, out var idxType); IndexType = idxType; int vertexByteSize = Marshal.SizeOf <TVertex>(); ulong vertSize = (ulong)((int)vCount * vertexByteSize); ulong indexSize = (ulong)iCount * (idxType is VkIndexType.Uint16 ? 2ul : 4ul); int autoNamedMesh = 1; int vertexCount = 0, indexCount = 0; VertexBuffer = new(_device, new() { BufferFlags = BufferFlags.VertexBuffer, Usage = ResourceUsage.CPU_To_GPU, SizeInBytes = (int)vertSize }); IndexBuffer = new(_device, new() { BufferFlags = BufferFlags.IndexBuffer, Usage = ResourceUsage.CPU_To_GPU, SizeInBytes = (int)indexSize, }); Meshes = new(); IEnumerable <PropertyInfo> propertyInfos = typeof(TVertex).GetTypeInfo().GetRuntimeProperties(); byte *stagVertPtrInit = (byte *)VertexBuffer.Map(); byte *stagIdxPtrInit = (byte *)(IndexBuffer.Map()); byte *stagVertPtr = stagVertPtrInit; byte *stagIdxPtr = stagIdxPtrInit; foreach (Schema.Mesh mesh in gltf.Meshes) { string meshName = mesh.Name; if (string.IsNullOrEmpty(meshName)) { meshName = "mesh_" + autoNamedMesh.ToString(); autoNamedMesh++; } Mesh m = new Mesh { Name = meshName }; foreach (Schema.MeshPrimitive p in mesh.Primitives) { Schema.Accessor AccPos = null, AccNorm = null, AccUv = null, AccUv1 = null, AccColor = null, AccTan = null, Accjoint = null, Accweights = null; if (p.Attributes.TryGetValue("POSITION", out int accessorIdx)) { AccPos = gltf.Accessors[accessorIdx]; EnsureBufferIsLoaded(gltf.BufferViews[(int)AccPos.BufferView].Buffer); } if (p.Attributes.TryGetValue("NORMAL", out accessorIdx)) { AccNorm = gltf.Accessors[accessorIdx]; EnsureBufferIsLoaded(gltf.BufferViews[(int)AccNorm.BufferView].Buffer); } if (p.Attributes.TryGetValue("TEXCOORD_0", out accessorIdx)) { AccUv = gltf.Accessors[accessorIdx]; EnsureBufferIsLoaded(gltf.BufferViews[(int)AccUv.BufferView].Buffer); } if (p.Attributes.TryGetValue("TEXCOORD_1", out accessorIdx)) { AccUv1 = gltf.Accessors[accessorIdx]; EnsureBufferIsLoaded(gltf.BufferViews[(int)AccUv1.BufferView].Buffer); } if (p.Attributes.TryGetValue("COLOR_0", out accessorIdx)) { AccColor = gltf.Accessors[accessorIdx]; EnsureBufferIsLoaded(gltf.BufferViews[(int)AccColor.BufferView].Buffer); ColorType = AccColor.Type is GltfType.Vec3 ? ColorType.Vec3 : ColorType.Vec4; } if (p.Attributes.TryGetValue("TANGENT", out accessorIdx)) { AccTan = gltf.Accessors[accessorIdx]; EnsureBufferIsLoaded(gltf.BufferViews[(int)AccTan.BufferView].Buffer); } if (Options.Skinning) { if (p.Attributes.TryGetValue("JOINTS_0", out accessorIdx)) { Accjoint = gltf.Accessors[accessorIdx]; EnsureBufferIsLoaded(gltf.BufferViews[(int)Accjoint.BufferView].Buffer); } if (p.Attributes.TryGetValue("WEIGHTS_0", out accessorIdx)) { Accweights = gltf.Accessors[accessorIdx]; EnsureBufferIsLoaded(gltf.BufferViews[(int)Accweights.BufferView].Buffer); } } Primitive prim = new() { FirstIndex = indexCount, FirstVertex = vertexCount, VertexCount = AccPos.Count, Material = p.Material ?? 0 }; //prim.BoundingBox.Min.ImportFloatArray(AccPos.Min); //prim.BoundingBox.Max.ImportFloatArray(AccPos.Max); //prim.BoundingBox.IsValid = true; //Interleaving vertices byte *inPosPtr = null; byte *inNormPtr = null; byte *inUvPtr = null; byte *inUv1Ptr = null; byte *inColorPtr = null; byte *inTanPtr = null; byte *inJointPtr = null; byte *inWeightsPtr = null; Schema.BufferView bv = gltf.BufferViews[(int)AccPos.BufferView !]; inPosPtr = (byte *)bufferHandles[bv.Buffer].AddrOfPinnedObject().ToPointer(); inPosPtr += AccPos.ByteOffset + bv.ByteOffset; if (AccNorm is not null) { bv = gltf.BufferViews[(int)AccNorm.BufferView !];