Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get vertex data from a primitive #80

Closed
zw20045500 opened this issue Nov 4, 2022 · 1 comment
Closed

How to get vertex data from a primitive #80

zw20045500 opened this issue Nov 4, 2022 · 1 comment

Comments

@zw20045500
Copy link

zw20045500 commented Nov 4, 2022

Hello, I don't know how get vertex data , can you give a example code, the following code is mine. thank you

List<MeshModel> meshModels =  gltfModel.getMeshModels();
        for(MeshModel meshModel : meshModels ) {
            if(meshModel.getName().equals("M_head")) {
                for (MeshPrimitiveModel meshPrimitiveModel:meshModel.getMeshPrimitiveModels()) {
                    if (meshPrimitiveModel.getMaterialModel().getName().equals("M_Head_mat")) {
                        //gltfModel.getAccessorModels().
                        meshPrimitiveModel.getAttributes().forEach((k,v)->{
                            if (k.equals("POSITION")) {
                                for (int p=0; p<v.getCount();p++){
                                    ByteBuffer data_byte = v.getBufferViewModel().getBufferModel().getBufferData();
                                    int index = v.getBufferViewModel().getByteOffset() + v.getByteOffset() + p * 12;
                                    // how get info from the data_byte
                                }
                            }
                        });
                    }
                }
            }
       }
@javagl
Copy link
Owner

javagl commented Nov 5, 2022

Note that when you directly access the data from the accessor, you really only access the raw data. Accessing the data in a form that takes animations, skinning, or morphing into account is not that simple. (The model already does take sparse accessors into account, so you don't have to worry about that, at least).

However, the general way for accessing the accessor data is via the AccessorData interface (hence the name...). You can obtain the AccessoData from the AccessorModel, safely cast it to the target type depending on the component type (e.g. toAccessorFloatData for float positions), and then access the data elements as shown in this example:

package de.javagl.jgltf.issues;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import de.javagl.jgltf.model.AccessorData;
import de.javagl.jgltf.model.AccessorFloatData;
import de.javagl.jgltf.model.AccessorModel;
import de.javagl.jgltf.model.GltfModel;
import de.javagl.jgltf.model.MeshModel;
import de.javagl.jgltf.model.MeshPrimitiveModel;
import de.javagl.jgltf.model.io.GltfModelReader;

public class JgltfIssue80_VertexDataFromPrimitive
{
    public static void main(String[] args)
        throws URISyntaxException, IOException
    {
        URI uri = new URI("https://raw.githubusercontent.com/KhronosGroup/"
            + "glTF-Sample-Models/master/2.0/Duck/glTF-Embedded/Duck.gltf");
        GltfModelReader gltfModelReader = new GltfModelReader();

        System.out.println("Reading from " + uri);
        GltfModel gltfModel = gltfModelReader.read(uri);

        System.out.println(gltfModel);

        List<MeshModel> meshModels = gltfModel.getMeshModels();
        for (MeshModel meshModel : meshModels)
        {
            for (MeshPrimitiveModel meshPrimitiveModel : meshModel
                .getMeshPrimitiveModels())
            {
                AccessorModel accessorModel =
                    meshPrimitiveModel.getAttributes().get("POSITION");
                AccessorData accessorData = accessorModel.getAccessorData();
                AccessorFloatData accessorFloatData =
                    (AccessorFloatData) accessorData;
                int n = accessorFloatData.getNumElements();
                for (int i = 0; i < n; i++)
                {
                    float x = accessorFloatData.get(i, 0);
                    float y = accessorFloatData.get(i, 1);
                    float z = accessorFloatData.get(i, 2);
                    System.out.println(
                        "Position " + i + " is " + x + " " + y + " " + z);
                }
            }
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants