Skip to content

How to add normals and UV data? #3

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

Open
dev20240517 opened this issue Sep 25, 2024 · 5 comments
Open

How to add normals and UV data? #3

dev20240517 opened this issue Sep 25, 2024 · 5 comments

Comments

@dev20240517
Copy link

var mesh = new DracoMesh();
var attrPos = PointAttribute.Wrap(AttributeType.Position, controlPoints);
mesh.AddAttribute(attrPos);
mesh.Indices.AddRange(indices);

How to add normals and UV data?
Use the following method to compress errors.

var attrNormal = PointAttribute.Wrap(AttributeType.Normal,1, normals.ToArray());
mesh.AddAttribute(attrNormal);

@lexchou-aspose
Copy link
Member

Here's an example to add normal data:

        Vector3[] normals = new Vector3[]
        {
                new Vector3( -1, 0, 0f),
                new Vector3( 1, 0, 0f),
                new Vector3( 0, 1, 0.0f),
                new Vector3( 0, 1, 0f),
                new Vector3( 0, 0, 1.0f),
                new Vector3( 0, 0, 1.0f),
                new Vector3( 0, 0, -1.0f),
                new Vector3( 0, 0, -1.0f)
        };
        Vector2[] uv = new Vector2[]
        {
                new Vector2( -1, 0f),
                new Vector2( 1, 0f),
                new Vector2( 0, 1),
                new Vector2( 0, 1),
                new Vector2( 0, 1.0f),
                new Vector2( 0, 1.0f),
                new Vector2( 0, -1.0f),
                new Vector2( 0, -1.0f)
        };


        var attrNormal = PointAttribute.Wrap(AttributeType.Normal, normals);
        mesh.AddAttribute(attrNormal);

        var attrUV = PointAttribute.Wrap(AttributeType.TexCoord, uv);
        mesh.AddAttribute(attrUV);

In the default case it's identity mapping, which means the number of normals/UV should be the same to the position data.

If you want to manually map the entry to specified position point, you can use following code:

        attrUV.SetExplicitMapping(controlPoints.Length);
        attrNormal.SetExplicitMapping(controlPoints.Length);
        for (int i = 0; i < controlPoints.Length; i++)
        {
            attrUV.SetPointMapEntry(i, 0);
            attrNormal.SetPointMapEntry(i, 0);
        }

@dev20240517
Copy link
Author

var mesh = new DracoMesh();
var attrPos = PointAttribute.Wrap(AttributeType.Position, controlPoints);
mesh.AddAttribute(attrPos);
mesh.Indices.AddRange(indices);

var attrNormal = PointAttribute.Wrap(AttributeType.Normal, normals);
mesh.AddAttribute(attrNormal);

var drcBytes = FileFormat.Drako.Draco.Encode(mesh, encodeOptions);

Why is adding normal compression after adding vertices and indexes incorrect?
Is there any other way to add vertices, indexes, and normal at the same time?

System.IndexOutOfRangeException
Snipaste_2024-09-25_11-25-47

@lexchou-aspose
Copy link
Member

Can you provide full code sample to reproduce this?

I have updated the test case with a working code.

@dev20240517
Copy link
Author

I have found the cause of the problem.
NumPoints were not assigned a value, and I thought it would automatically be based on the passed AttributeType Position calculation;
I have now assigned NumPoints and the compression has been successful;
Thank you very much for your patient answer!!!

@lexchou-aspose
Copy link
Member

Good to hear that.

This strange unnecessary requirement was inherited from the original draco implementation.

I'll update this behavior in the next version.

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