Giter Club home page Giter Club logo

Comments (5)

warrenm avatar warrenm commented on July 29, 2024 1

As GLTFKit2 is a fairly low-level framework, this is tedious but ultimately straightforward. One approach is to declare an Objective-C category on the GLTFPrimitive class that returns packed position data:

@interface GLTFPrimitive (VertexPositionAccess)
- (NSData *_Nullable)copyPackedVertexPositions;
@end

which could be implemented as follows

@implementation GLTFPrimitive (VertexPositionAccess)

- (NSData *)copyPackedVertexPositions {
    GLTFAccessor *positionAccessor = self.attributes[GLTFAttributeSemanticPosition];
    if (positionAccessor == nil) {
        return nil; // No position data, because the primitive didn't contain a position accessor
    }
    const void *sourceBufferBase = positionAccessor.bufferView.buffer.data.bytes;
    const void *sourceBase = sourceBufferBase + positionAccessor.offset + positionAccessor.bufferView.offset;
    if (sourceBase == NULL) {
        return nil; // No position data, because buffer view or buffer is NULL, or because the buffer contains no data
    }
    NSInteger vertexCount = positionAccessor.count;
    NSInteger destinationStride = sizeof(float) * 3;
    NSInteger bufferLength = destinationStride * vertexCount;
    NSInteger sourceStride = positionAccessor.bufferView.stride;
    if (sourceStride == 0) {
        sourceStride = destinationStride;
    }
    if (sourceStride == destinationStride) {
        // Existing data is already packed; just return a data object that acts as a view on it
        return [NSData dataWithBytesNoCopy:(void *)sourceBase length:bufferLength freeWhenDone:NO];
    } else {
        // Existing data is *not* packed; we need to copy it element by element
        void *packedBuffer = malloc(bufferLength);
        for (int i = 0; i < vertexCount; ++i) {
            const void *sourcePtr = sourceBase + i * sourceStride;
            void *destPtr = packedBuffer + i * destinationStride;
            memcpy(destPtr, sourcePtr, destinationStride);
        }
        return [NSData dataWithBytesNoCopy:packedBuffer length:bufferLength freeWhenDone:YES];
    }
}

@end

Back in Swift, assuming you have a reference to a primitive object, you could print out all of its vertices like this:

let vertexCount = primitive.attributes[GLTFAttributeSemantic.position.rawValue]?.count ?? 0
if let positionData = primitive.copyPackedVertexPositions() {
    positionData.withUnsafeBytes { positionPtr in
        for i in 0..<vertexCount {
            let position = positionPtr.baseAddress!
                .advanced(by: MemoryLayout<Float>.stride * 3 * i)
                .assumingMemoryBound(to: Float.self)
            let x = position[0]
            let y = position[1]
            let z = position[2]
            print("\(x) \(y) \(z)")
        }
    }
}

If you need to iterate over triangles in indexed order instead of just the raw vertex list, you can write a similar extension to retrieve the index data from the primitive. This is slightly more complex, because indices can be 8, 16, or 32 bits each, but at least index data is guaranteed to be packed.

from gltfkit2.

warrenm avatar warrenm commented on July 29, 2024

If you've already converted a glTF asset to a SceneKit scene, you can instead retrieve the vertex source of an SCNGeometry using the sources(for:) method, then iterate its data instead, making sure to account for the stride between vertices.

from gltfkit2.

warrenm avatar warrenm commented on July 29, 2024

This issue will be automatically closed in 7 days if no further comment is received.

from gltfkit2.

warrenm avatar warrenm commented on July 29, 2024

Closing due to inactivity.

from gltfkit2.

adellari avatar adellari commented on July 29, 2024

Is this still the best way to get vertex position data?

from gltfkit2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.