vpenades / SharpGLTF

glTF reader and writer for .NET Standard
MIT License
467 stars 75 forks source link

Creating 1 primitive with multiple vertices with different color #90

Closed Mouldi closed 3 years ago

Mouldi commented 3 years ago

Hi, Love the library, Great work!

Is there a way to create one primitive with multiple vertices but not the same material (vertex color)?

In other words, is it possible to change the material on different vertices while creating a primitive? If so, can you please explain how it's done?

Cheers

vpenades commented 3 years ago

This:

var mesh = new MeshBuilder<VertexPositionNormal,VertexColor1,VertexEmpty>();
Mouldi commented 3 years ago

and how do I add different vertices to the mesh? i will be forced to use : PrimitiveBuilder<MaterialBuilder, VERTEX, VertexColor1, VertexEmpty> prim = mesh.UsePrimitive(mat); and if the material changes, I have to call mesh.UsePrimitive(mat); again. but this will create another primitive in the underline structure. the way primitives are stored as keys and the key is <mat, primtivetype> ===> if I change the material, I create another key, hence multiple primitives by mesh! Is there a way to keep the same primitive but change the material?

vpenades commented 3 years ago

glTF Primitives exist precisely to change the materials of a mesh.

If you have a mesh that uses 5 materials, you will end with a mesh with 5 primitives. This is how glTF and graphics engines work and there's no way around this.

the UsePrimitive method reuses a primitive when the material already exists, so if you call it multiple times to switch between 2 materials, you will end with a mesh with 2 primitive-materials.

Mouldi commented 3 years ago

ok, thank you. last question: how can I account for the alpha when I use VERTEXCOLOR vc1 = new VERTEXCOLOR(vpos, vcolor); to create my primitives? the primitive loses the transparency when I render the model.

vpenades commented 3 years ago

You have to enable alpha transparency in the material: AlphaMode = Blend

Mouldi commented 3 years ago

Perfect thank you