<p>You can sample the texture in your vertex shader and set the color appropriately. You don't say what system you're using, but for something like an OpenGL vertex shader, it would look something like this (this is a little old school, so it may need updating for modern GL) :</p>

<pre><code>uniform sampler2DRect texture;
varying vec4 color;
void main ()
{
    color  = texture2DRect(texture, gl_MultiTexCoord0.xy);

    vec4    newVertex   = vec4(gl_Vertex.xyz, 1.0);

    gl_Position = gl_ModelViewProjectionMatrix * newVertex;
}
</code></pre>

<p>Your fragment shader would then just set the color of the fragment to the interpolated color coming from the vertex shader. Something like this:</p>

<pre><code>varying vec4 color;
void main()
{
    gl_FragColor = color;
}
</code></pre>

CC BY-SA 3.0
2017-12-26T21:03:41.933Z
Thank you for your response. Since I never worked with openGl, I would need some clarifications about your code. The parameter texture that you've passed to the texture2DRect function is the 2D image associated to texture right ? Can you please give a more complet code having the lines for reading the 3D textured mesh, transferring the color texture to 3D vertices, and saving it to a .ply file ?
javier
CC BY-SA 3.0
2017-12-26T21:53:18.553Z
That's beyond the scope of an answer here, I'm afraid. I suggest you look up a modern beginning OpenGL tutorial. Once you've learned how to put geometry on the screen and add a texture to it, it's pretty much the same for a 3D texture. For 3D Textures you'll use a `sampler3D` instead of a `sampler2DRect` and call `texture3D()` instead of `texture2DRect()`. Good luck!
user1118321
CC BY-SA 3.0
2017-12-27T23:35:56.337Z
@javier What you're requesting is not an algorithm, it's an entire application. We have a finite amount of space in our answers, so it's best to keep your question limited to something specific. Loading a 3D mesh and save a .ply file are totally different problems and we can't possibly answer that without knowing more about the original format and what specific problems you're having with your mesh loading code. You should post separate questions once you've tried to load / save a mesh if you still need help.
Dan
CC BY-SA 3.0
2017-12-23T22:17:43.320Z
2017-12-23T22:17:43.320Z
user1118321
<p>I have a textured 3D mesh. I want to deduce a 3D mesh without texture, but with an RGB vector color on each node of this 3D mesh. The colors have to be picked from the initial texture. Is there a way to do this ? My question isn't related to art work. It's related to 3D mesh processing.</p>

<p>Thanks</p>

CC BY-SA 3.0
2017-12-22T19:04:22.700Z
To put it another way, you want to write code to "bake" the texture into vertex colors, so that the vertex colors approximate the original texture?
Nathan Reed
CC BY-SA 3.0
2017-12-23T09:39:40.143Z
I've reopened your question now that you've clarified it a little, but to make sure you're not disappointed later, I'll say now: if you're looking for an existing piece of software that you can run your mesh through, we won't be able to help. If you're looking for an algorithm that you can use to write some software, we might be able to help.
Dan Hulme
CC BY-SA 3.0
2017-12-26T21:00:48.330Z
@DanHulme YEs, I was looking for an algorithm
javier
<texture><3d><mesh>
How do I replace a mesh's texture with vertex colours?
javier
# How do I replace a mesh's texture with vertex colours?

(Asked by: username_0 on 2017-12-22)

I have a textured 3D mesh. I want to deduce a 3D mesh without texture, but with an RGB vector color on each node of this 3D mesh. The colors have to be picked from the initial texture. Is there a way to do this ? My question isn't related to art work. It's related to 3D mesh processing.

Thanks

**Tags:** texture, 3d, mesh

> **Comments:**
> **username_1**: To put it another way, you want to write code to "bake" the texture into vertex colors, so that the vertex colors approximate the original texture?  
> **username_2**: I've reopened your question now that you've clarified it a little, but to make sure you're not disappointed later, I'll say now: if you're looking for an existing piece of software that you can run your mesh through, we won't be able to help. If you're looking for an algorithm that you can use to write some software, we might be able to help.  
> **username_0**: @username_2 YEs, I was looking for an algorithm  

---

## 1 answers

### Answer by username_3 on 2017-12-23. Score: 1 | Accepted answer

You can sample the texture in your vertex shader and set the color appropriately. You don't say what system you're using, but for something like an OpenGL vertex shader, it would look something like this (this is a little old school, so it may need updating for modern GL) :

```
uniform sampler2DRect texture;
varying vec4 color;
void main ()
{
    color  = texture2DRect(texture, gl_MultiTexCoord0.xy);

    vec4    newVertex   = vec4(gl_Vertex.xyz, 1.0);

    gl_Position = gl_ModelViewProjectionMatrix * newVertex;
}

```

Your fragment shader would then just set the color of the fragment to the interpolated color coming from the vertex shader. Something like this:

```
varying vec4 color;
void main()
{
    gl_FragColor = color;
}

```

> **Comments:**
> **username_0**: Thank you for your response. Since I never worked with openGl, I would need some clarifications about your code. The parameter texture that you've passed to the texture2DRect function is the 2D image associated to texture right ? Can you please give a more complet code having the lines for reading the 3D textured mesh, transferring the color texture to 3D vertices, and saving it to a .ply file ?  
> **username_3**: That's beyond the scope of an answer here, I'm afraid. I suggest you look up a modern beginning OpenGL tutorial. Once you've learned how to put geometry on the screen and add a texture to it, it's pretty much the same for a 3D texture. For 3D Textures you'll use a `sampler3D` instead of a `sampler2DRect` and call `texture3D()` instead of `texture2DRect()`. Good luck!  
> **username_4**: @username_0 What you're requesting is not an algorithm, it's an entire application. We have a finite amount of space in our answers, so it's best to keep your question limited to something specific. Loading a 3D mesh and save a .ply file are totally different problems and we can't possibly answer that without knowing more about the original format and what specific problems you're having with your mesh loading code. You should post separate questions once you've tried to load / save a mesh if you still need help.  

---

