<p>I copied my answer for the same OP at <a href="https://github.com/godotengine/godot/issues/19473" rel="nofollow noreferrer">https://github.com/godotengine/godot/issues/19473</a>. But this question differs slightly to the original issue report.</p>
<p>In case you want to manually add support for VERTEX_ID, these are the changes I had to make in <code>3.2.3-stable</code> for <code>spatial</code> shaders. I wanted to use the vertex ID for creating procedural animations.</p>
<p>In <code>drivers/gles3/shader_compiler_gles3.cpp</code> in the function <code>ShaderCompilerGLES3::ShaderCompilerGLES3()</code> :</p>
<pre><code>actions[VS::SHADER_SPATIAL].renames[&quot;VERTEX_ID&quot;] = &quot;gl_VertexID&quot;;
</code></pre>
<p>and <code>servers/visual/shader_types.cpp</code> in the function <code>ShaderTypes::ShaderTypes()</code>:</p>
<pre><code>shader_modes[VS::SHADER_SPATIAL].functions[&quot;vertex&quot;].built_ins[&quot;VERTEX_ID&quot;] = constt(ShaderLanguage::TYPE_INT);
</code></pre>
<p>Then compile the source and it should work. Some other primitives could probably be added in a similar fashion but I have not tested that yet.</p>
<p>One caveat is that the vertex IDs reported by Godot are very different from those reported by other tools such as Blender. This is because vertices are duplicated and in a different order. Godot also only gives you two UV channels to work with. Using UV coordinates also means having a temporal component will require significantly more memory to implement as UVs will typically map sparsely onto a 2D texture, if the data is only used in the vertex shader.</p>
<p>I have a primitive tool working that can map the vertex animation texture generated in blender to one usable by Godot into another grayscale texture (n by 1 texture, where n is number of vertices in Godot). This saves texture memory as no data is duplicated for duplicated vertices. I first need to implement an octree before it will be usable for real-world meshes and not just a set of simple deforming cubes. Once I have that done in my spare time I will release it for free.</p>

CC BY-SA 4.0
2020-10-01T15:01:09.523Z
2020-10-01T15:10:46.277Z
2020-10-01T15:10:46.277Z
Johann
<p>In the godot game engine (using opengl) it is not possible to define custom shader attributes.<br>
A workaround is creating a sampler2D texture uniform instead and use vertexId (or in fact UV since vertexID also isn't exposed) to get the per vertex attributes.  </p>

<p>I am wondering are there any downsides to this approach and/or caveats one should keep in mind ?</p>

CC BY-SA 4.0
2020-02-08T06:22:41.280Z
Wouldn't it make more sense to just switch to a game engine that isn't horribly limiting your ability to do what you need to do?
Nicol Bolas
CC BY-SA 4.0
2020-02-08T18:10:37.490Z
Sadly this is out of question. Besides godot gives me a lot of useful stuff.
SleepProgger
CC BY-SA 4.0
2020-02-08T18:28:36.140Z
"*Besides godot gives me a lot of useful stuff.*" If it's not even allowing you to use basic aspects of a shader like `gl_VertexID`, then I would seriously contest that statement. I'm fairly sure that other engines can provide whatever "lot of useful stuff" that Godot does without Godot's pointless limitations. For example, my suggestion would be to use a buffer texture or an SSBO for such "attributes", but given that it doesn't even allow you to use `gl_VertexID`, I would be surprised if it let you use those features.
Nicol Bolas
CC BY-SA 4.0
2020-02-08T18:40:04.087Z
Godot allows the use of texture uniforms (thus the question...) and i can abuse UV and other existing attributes to smuggle the vertexID in. It isn't nice, but it works. BUT this question is not if godot is a good engine to use, but if there are technical concerns regarding that approach and what the downsides and caveats to this technique would be.
SleepProgger
CC BY-SA 4.0
2020-02-08T18:47:52.930Z
The problem is that I don't know what Godot will *allow* you to do, so I can't advise you. For example, I know how to make the texture-based idea work in a way that offers most if not all of the features of a vertex attribute. But I don't know if Godot will actually let you do those things needed to make it work. Again, if it's not letting you touch basic OpenGL functionality like `gl_VertexID`, who knows? Does it allow you to use floating-point image formats? What about integer textures? What about the GLSL functions for doing integer normalization? What about `texelFetch`? Etc.
Nicol Bolas
<opengl><glsl>
Shader sampler2D uniforms instead of attributes
SleepProgger
# Shader sampler2D uniforms instead of attributes

(Asked by: username_0 on 2020-02-07)

In the godot game engine (using opengl) it is not possible to define custom shader attributes.  
A workaround is creating a sampler2D texture uniform instead and use vertexId (or in fact UV since vertexID also isn't exposed) to get the per vertex attributes.

I am wondering are there any downsides to this approach and/or caveats one should keep in mind ?

**Tags:** opengl, glsl

> **Comments:**
> **username_1**: Wouldn't it make more sense to just switch to a game engine that isn't horribly limiting your ability to do what you need to do?  
> **username_0**: Sadly this is out of question. Besides godot gives me a lot of useful stuff.  
> **username_1**: "*Besides godot gives me a lot of useful stuff.*" If it's not even allowing you to use basic aspects of a shader like `gl_VertexID`, then I would seriously contest that statement. I'm fairly sure that other engines can provide whatever "lot of useful stuff" that Godot does without Godot's pointless limitations. For example, my suggestion would be to use a buffer texture or an SSBO for such "attributes", but given that it doesn't even allow you to use `gl_VertexID`, I would be surprised if it let you use those features.  
> **username_0**: Godot allows the use of texture uniforms (thus the question...) and i can abuse UV and other existing attributes to smuggle the vertexID in. It isn't nice, but it works. BUT this question is not if godot is a good engine to use, but if there are technical concerns regarding that approach and what the downsides and caveats to this technique would be.  
> **username_1**: The problem is that I don't know what Godot will *allow* you to do, so I can't advise you. For example, I know how to make the texture-based idea work in a way that offers most if not all of the features of a vertex attribute. But I don't know if Godot will actually let you do those things needed to make it work. Again, if it's not letting you touch basic OpenGL functionality like `gl_VertexID`, who knows? Does it allow you to use floating-point image formats? What about integer textures? What about the GLSL functions for doing integer normalization? What about `texelFetch`? Etc.  

---

## 1 answers

### Answer by username_2 on 2020-10-01. Score: 2

I copied my answer for the same OP at <https://github.com/godotengine/godot/issues/19473>. But this question differs slightly to the original issue report.

In case you want to manually add support for VERTEX\_ID, these are the changes I had to make in `3.2.3-stable` for `spatial` shaders. I wanted to use the vertex ID for creating procedural animations.

In `drivers/gles3/shader_compiler_gles3.cpp` in the function `ShaderCompilerGLES3::ShaderCompilerGLES3()` :

```
actions[VS::SHADER_SPATIAL].renames["VERTEX_ID"] = "gl_VertexID";

```

and `servers/visual/shader_types.cpp` in the function `ShaderTypes::ShaderTypes()`:

```
shader_modes[VS::SHADER_SPATIAL].functions["vertex"].built_ins["VERTEX_ID"] = constt(ShaderLanguage::TYPE_INT);

```

Then compile the source and it should work. Some other primitives could probably be added in a similar fashion but I have not tested that yet.

One caveat is that the vertex IDs reported by Godot are very different from those reported by other tools such as Blender. This is because vertices are duplicated and in a different order. Godot also only gives you two UV channels to work with. Using UV coordinates also means having a temporal component will require significantly more memory to implement as UVs will typically map sparsely onto a 2D texture, if the data is only used in the vertex shader.

I have a primitive tool working that can map the vertex animation texture generated in blender to one usable by Godot into another grayscale texture (n by 1 texture, where n is number of vertices in Godot). This saves texture memory as no data is duplicated for duplicated vertices. I first need to implement an octree before it will be usable for real-world meshes and not just a set of simple deforming cubes. Once I have that done in my spare time I will release it for free.


---

