Skip to content

Commit

Permalink
GLTFAsset.loadSkins
Browse files Browse the repository at this point in the history
  • Loading branch information
gecko0307 committed Mar 17, 2024
1 parent 911290e commit 850fde4
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/dagon/resource/gltf/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import dagon.resource.gltf.accessor;
import dagon.resource.gltf.meshprimitive;
import dagon.resource.gltf.mesh;
import dagon.resource.gltf.node;
import dagon.resource.gltf.skin;

Vector3f asVector(JSONValue value)
{
Expand Down Expand Up @@ -141,6 +142,7 @@ class GLTFAsset: Asset, TriangleSet
Array!Texture textures;
Array!Material materials;
Array!GLTFNode nodes;
Array!GLTFSkin skins;
Array!GLTFScene scenes;
Entity rootEntity;

Expand Down Expand Up @@ -168,7 +170,9 @@ class GLTFAsset: Asset, TriangleSet
loadTextures(doc.root);
loadMaterials(doc.root);
loadMeshes(doc.root);
loadAnimations(doc.root);
loadNodes(doc.root);
loadSkins(doc.root);
loadScenes(doc.root);
return true;
}
Expand Down Expand Up @@ -797,6 +801,48 @@ class GLTFAsset: Asset, TriangleSet
rootEntity.updateTransformationTopDown();
}

void loadSkins(JSONValue root)
{
if ("skins" in root.asObject)
{
foreach(i, s; root.asObject["skins"].asArray)
{
auto skin = s.asObject;

GLTFSkin skinObj = New!GLTFSkin(this);

if ("joins" in skin)
{
foreach(ni, n; skin["joins"].asArray)
{
uint nodeIndex = cast(uint)n.asNumber;
if (nodeIndex < nodes.length)
skinObj.joints.insertBack(nodes[nodeIndex]);
}
}

if ("inverseBindMatrices" in skin)
{
GLTFAccessor invBindMatricesAccessor;
uint invBindMatricesAccessorIndex = cast(uint)skin["inverseBindMatrices"].asNumber;
if (invBindMatricesAccessorIndex < accessors.length)
invBindMatricesAccessor = accessors[invBindMatricesAccessorIndex];
else
writeln("Warning: nonexistent accessor ", invBindMatricesAccessorIndex);

skinObj.invBindMatricesAccessor = invBindMatricesAccessor;
}

skins.insertBack(skinObj);
}
}
}

void loadAnimations(JSONValue root)
{
// TODO
}

void loadScenes(JSONValue root)
{
if ("scenes" in root.asObject)
Expand Down Expand Up @@ -890,6 +936,10 @@ class GLTFAsset: Asset, TriangleSet
deleteOwnedObject(no);
nodes.free();

foreach(sko; skins)
deleteOwnedObject(sko);
skins.free();

foreach(sc; scenes)
deleteOwnedObject(sc);
scenes.free();
Expand Down
51 changes: 51 additions & 0 deletions src/dagon/resource/gltf/skin.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright (c) 2024 Timur Gafarov
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
module dagon.resource.gltf.skin;

import std.stdio;

import dlib.core.ownership;
import dlib.container.array;

import dagon.resource.gltf.node;
import dagon.resource.gltf.accessor;

class GLTFSkin: Owner
{
Array!GLTFNode joints;
GLTFAccessor invBindMatricesAccessor;

this(Owner o)
{
super(o);
}

~this()
{
joints.free();
}
}

0 comments on commit 850fde4

Please sign in to comment.