question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hi,

After haveing done some research I cant seem to figure out how to assign a texture to a mesh. Taking a piece of code given here

using SharpGLTF.Scenes;
using SharpGLTF.Geometry;
using SharpGLTF.Geometry.VertexTypes;
using SharpGLTF.Materials;

// Create a mesh with a triangle.
var mesh = new MeshBuilder<VertexPosition>();

var prim = mesh.UsePrimitive(MaterialBuilder.CreateDefault());
prim.AddTriangle
               (
               new VertexPosition(0, -1, 0),
               new VertexPosition(1, 0, 0),
               new VertexPosition(0, 1, 0)
               );

// Create a scene and add the mesh:

var scene = new SceneBuilder();

mesh.UsePrimitive(material);

scene.AddRigidMesh(mesh, Matrix4x4.Identity);
            
// save it to GLB:

scene.ToGltf2().Save(@"D:\\New folder\\File.gltf");

How can I add an image to this triangle?

I understand that I should use MeshBuilder<VertexPosition, VertexColor1Texture1>(), but what should I modify to insert a texture? MeshBuilder, pimitive, or material?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Mikhail1Demianenkocommented, Jan 16, 2021

Thank you, I finnaly was able to assign a texture. I will leave the code here in case someone needs it

var material = new MaterialBuilder()
                .WithDoubleSide(true)
                .WithMetallicRoughnessShader()
                .WithChannelImage(KnownChannel.BaseColor, "D:\\New folder\\bricks.png");

            var mesh = new MeshBuilder<VertexPosition, VertexTexture1>("Mesh");


            VertexTexture1 cor1 = new VertexTexture1(new Vector2(0f, 0f));
            VertexTexture1 cor2 = new VertexTexture1(new Vector2(0f, 2f));
            VertexTexture1 cor3 = new VertexTexture1(new Vector2(2f, 2f));
            VertexTexture1 cor4 = new VertexTexture1(new Vector2(2f, 0f));
            


            VertexPosition pos1 = new VertexPosition(0, 0, 0);
            VertexPosition pos2 = new VertexPosition(0, 1, 0);
            VertexPosition pos3 = new VertexPosition(1, 1, 0);
            VertexPosition pos4 = new VertexPosition(1, 0, 0);


            VertexBuilder<VertexPosition, VertexTexture1, VertexEmpty> ver1 = new VertexBuilder<VertexPosition, VertexTexture1, VertexEmpty>(pos1, cor2);
            VertexBuilder<VertexPosition, VertexTexture1, VertexEmpty> ver2 = new VertexBuilder<VertexPosition, VertexTexture1, VertexEmpty>(pos2, cor1);
            VertexBuilder<VertexPosition, VertexTexture1, VertexEmpty> ver3 = new VertexBuilder<VertexPosition, VertexTexture1, VertexEmpty>(pos3, cor4);
            VertexBuilder<VertexPosition, VertexTexture1, VertexEmpty> ver4 = new VertexBuilder<VertexPosition, VertexTexture1, VertexEmpty>(pos4, cor3);

            var prim = mesh.UsePrimitive(material);


            prim.AddTriangle(ver1, ver2, ver3);
            prim.AddTriangle(ver1, ver3, ver4);



            var scene = new SharpGLTF.Scenes.SceneBuilder();
            scene.AddRigidMesh(mesh, Matrix4x4.Identity);

            var model = scene.ToGltf2();
            

            model.SaveGLTF(@"D:\\New folder\\File.gltf");
0reactions
Mikhail1Demianenkocommented, Jan 16, 2021

Thank you, I finally was able to assign texture

Read more comments on GitHub >

github_iconTop Results From Across the Web

Textures for 3D, graphic design and Photoshop!
Textures.com is a website that offers digital pictures of all sorts of materials. Sign up for free and download 15 free images every...
Read more >
20000+ Best Free Textures · 100% Free Download
Download and use 20000+ Texture stock photos for free. ✓ Thousands of new images every day ✓ Completely Free to Use ✓ High-quality...
Read more >
Free textures and tutorials for Photoshop and more!
Over 1000 high-resolution textures, Photoshop tutorials, and more. Free for commercial and personal use. Grunge, metal, lens effects, atmosphere, ink, ...
Read more >
ambientCG - CC0 Textures, HDRIs and Models
All textures come with PBR maps and tile seamlessly. Explore All PBR Materials · Wood 041. Accurate Displacement. Photogrammetry is used for many...
Read more >
750+ Best Texture Pictures [HD] | Download Free Images ...
Download the perfect texture pictures. Find over 100+ of the best free texture images. Free for commercial use ✓ No attribution required ✓...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found