vengi-voxel / vengi

free and open source voxel art tools - editor, thumbnailer and format converter
http://vengi-voxel.github.io/vengi/
Other
1.08k stars 90 forks source link

VOXELFORMAT: vox scene graph support should be improved #36

Closed mgerhardy closed 2 years ago

mgerhardy commented 4 years ago

Details about the scenegraph can e.g. be found here:

mgerhardy commented 4 years ago

_t and _r are not handled properly across the whole scenegraph. Loading a complex scene doesn't put the volumes at the right offset or the right rotation. The saving is broken: https://github.com/mgerhardy/engine/blob/master/src/modules/voxelformat/VoxFormat.cpp#L67 as well as the loading https://github.com/mgerhardy/engine/blob/master/src/modules/voxelformat/VoxFormat.cpp#L722

mgerhardy commented 4 years ago

Found this in #dev-talk of MV Community (Discord) (User Eisenwave)

If anybody's trying to figure out how to correctly apply the transformations of Vox chunks to the positions inside, this is how you do it.

i32 div2Floor(i32 n) {
    return (n - (n < 0)) / 2 ;
}

Vec3i32 applyTransform(Matrix3i32 matrix, Vec3i32 translation, Vec3i32 pointInChunk, Vec3i32 chunkSize)
{
    Vec3i32 doublePivot = Vec3i32{chunkSize[0] & ~1, chunkSize[1] & ~1, chunkSize[2] & ~1} + Vec3i32{1, 1, 1}
    Vec3i32 doublePointRelToCenter = pointInChunk + pointInChunk - ;
    Vec3i32 rotated = {
        div2Floor(dot(matrix.row(0), doublePointRelToCenter)),
        div2Floor(dot(matrix.row(1), doublePointRelToCenter)),
        div2Floor(dot(matrix.row(2), doublePointRelToCenter))
    };
    return rotated + translation;
}
mgerhardy commented 4 years ago

https://gist.github.com/Eisenwave/aca18b48fdaea3259894ceb4d8e0b846

mgerhardy commented 2 years ago

The rotation is fixed now.

The scenegraph is still not support though - I misunderstood some of the concepts while implementing it. The problem is, that I've assigned each layer a volume - this is not correct. The layers and objects in the scene graph share the volume with other objects - but have different translation and rotation assigned by the scene graph node properties.

mgerhardy commented 2 years ago

vox2vxm

mgerhardy commented 2 years ago

https://godotengine.org/asset-library/asset/341

mgerhardy commented 2 years ago

Info from mv Discord:

Hey <@!529750511977627648> , so the trick is that the transform stored in _r/_t for each nTRN node is a local_transform (ie. transforms from local coordinates into parent coordinates), not a world_transform (converts from local coordinates into world coordinates), so it's defined relative to the transform for the parent nTRN node. So if you have this node hierarchy:

You find the "world space" transform of nTRN2 by computing the world transform for its parent node, something like so: nTRN0.world_transform = nTRN0.local_transform; // it has no parent, nTRN1.world_transform = nTRN1.local_transform nTRN0.world_transform nTRN2.world_transform = nTRN2.local_transform nTRN1.world_transform

In ogt_vox, this is done (admittedly hard to decipher) and implicitly in recursive calls to generate_instances_for_node:

Hope this helps

mgerhardy commented 2 years ago

c298633a5f322d1017e8493f4d06207cfe9059bb should fix this issue. Feedback welcome.

mgerhardy commented 2 years ago

0fd861322dc87b176f9bd80926a3dc8d839154db should fix the remaining issue