# Vertex Animation Textures

**URL:** <https://forum.playcanvas.com/t/vertex-animation-textures/27729>\
**Category:** Help & Support\
**Tags:** animation, shaders\
**Created:** [October 3, 2022, 3:36pm UTC](https://forum.playcanvas.com/t/vertex-animation-textures/27729 "2022-10-03T15:36:03Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![steve\_wk](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/s/dc4da7/32.png) [@steve\_wk](https://forum.playcanvas.com/u/steve_wk)\
**Post date:** [October 3, 2022, 3:36pm UTC](https://forum.playcanvas.com/t/vertex-animation-textures/27729/1 "2022-10-03T15:36:03Z")

</div>

I’d like to explore using vertex animation textures in Playcanvas. While I’ve now done a fair amount making custom frag shaders in PC, I’ve yet to do anything on the vertex side. Which shader chunk/s would I need to modify in order to use textures to modify the vertex positions/normals?

---

<div class="post-metadata">

**Author:** ![mvaligursky](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mvaligursky/32/11681_2.png) [@mvaligursky](https://forum.playcanvas.com/u/mvaligursky)\
**Post date:** [October 3, 2022, 3:46pm UTC](https://forum.playcanvas.com/t/vertex-animation-textures/27729/2 "2022-10-03T15:46:19Z")

</div>

This would be the main one to play with:

> <https://github.com/playcanvas/engine/blob/main/src/graphics/program-lib/chunks/common/vert/transform.js>

---

<div class="post-metadata">

**Author:** ![mvaligursky](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mvaligursky/32/11681_2.png) [@mvaligursky](https://forum.playcanvas.com/u/mvaligursky)\
**Post date:** [October 3, 2022, 3:47pm UTC](https://forum.playcanvas.com/t/vertex-animation-textures/27729/3 "2022-10-03T15:47:17Z")

</div>

see MORPHING\_TEXTURE\_BASED in that file … you’d do something similar

---

<div class="post-metadata">

**Author:** ![steve\_wk](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/s/dc4da7/32.png) [@steve\_wk](https://forum.playcanvas.com/u/steve_wk)\
**Post date:** [October 3, 2022, 8:18pm UTC](https://forum.playcanvas.com/t/vertex-animation-textures/27729/4 "2022-10-03T20:18:20Z")

</div>

Ah, fantastic thank you!

Follow up question: how can I bring in a texture at 16bit (half float)? Every format I’m trying it seems to convert it to an 8-bit version. It’s going to be tricky to do vertex animations using 8bit textures!

---

<div class="post-metadata">

**Author:** ![mvaligursky](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mvaligursky/32/11681_2.png) [@mvaligursky](https://forum.playcanvas.com/u/mvaligursky)\
**Post date:** [October 3, 2022, 8:41pm UTC](https://forum.playcanvas.com/t/vertex-animation-textures/27729/5 "2022-10-03T20:41:10Z")

</div>

study this file: [engine/morph.js at main · playcanvas/engine · GitHub](https://github.com/playcanvas/engine/blob/main/src/scene/morph.js)

This uses either float or half float texture to store morph targets, depending on what your system supports.

In the past, I’ve done a VAT implementation using 8bit, and the quality was very good - and it could run about 10k skinned characters on a normal PC. I stored x, y and z offset in rgb, normalized to a bounding box of the skinned mesh. And .w was storing a normal ID, which I then use ti generate the normal on the GPU. It would do 2 texture samples per vertex. One with linear interpolation to get interpolated vertex offset, and one with point sampling to get exact normal index (those cannot be interpolated).

---

<div class="post-metadata">

**Author:** ![steve\_wk](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/s/dc4da7/32.png) [@steve\_wk](https://forum.playcanvas.com/u/steve_wk)\
**Post date:** [October 4, 2022, 5:26pm UTC](https://forum.playcanvas.com/t/vertex-animation-textures/27729/6 "2022-10-04T17:26:30Z")

</div>

Great, this is really helpful, thanks!

I’ve figured out how to encode/decode a 16-bit float to/from two 8-bit RGB textures. I’m not sure I understand how you would calculate the normals as you’re suggesting - do you have a resource which describes that process?

Alternatively I could encode the normals into another texture, in which case which shader chunk would I use to set them directly using a texture? Edit: I just found normal.js which seems to be the place.

---

<div class="post-metadata">

**Author:** ![steve\_wk](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/s/dc4da7/32.png) [@steve\_wk](https://forum.playcanvas.com/u/steve_wk)\
**Post date:** [October 4, 2022, 9:40pm UTC](https://forum.playcanvas.com/t/vertex-animation-textures/27729/7 "2022-10-04T21:40:38Z")

</div>

I’ve hit another wall I’m afraid.

I’m trying to figure out how to actually access the vertex id inside the shader. Looking at how it’s done in **transform.js** , there is this line:

`float vertexId = morph_vertex_id;`

When I search for how morph\_vertex\_id is defined, I find this in **lit-shader.js** :

```javascript
// vertex ids attributes
this.attributes.morph_vertex_id = SEMANTIC_ATTR15;
code += "attribute float morph_vertex_id;\n";

```

So it seems this is being set according to some value which has been put in SEMANTIC\_ATTR15.

Which then brings me to these lines in **morph.js** :

```javascript
// create vertex stream with vertex_id used to map vertex to texture
const formatDesc = [{ semantic: SEMANTIC_ATTR15, components: 1, type: TYPE_FLOAT32 }];
this.vertexBufferIds = new VertexBuffer(this.device, new VertexFormat(this.device, formatDesc), ids.length, BUFFER_STATIC, new Float32Array(ids));

```

So I tried essentially copying this code, replacing **ids.length** with the number of vertices in my mesh, and the **ids** array with an array of all the vertex ids (in my case 0-507).  
But I’m getting errors trying to use SEMANTIC\_ATTR15 and BUFFER\_STATIC because I can’t import things from a module in a Playcanvas script. So now I’m confused about whether I’m going about this in the right way at all.

How can I get the vertex id during the transform vert shader?

---

<div class="post-metadata">

**Author:** ![mvaligursky](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mvaligursky/32/11681_2.png) [@mvaligursky](https://forum.playcanvas.com/u/mvaligursky)\
**Post date:** [October 5, 2022, 8:30am UTC](https://forum.playcanvas.com/t/vertex-animation-textures/27729/8 "2022-10-05T08:30:18Z")

</div>

For the normal generation, I used Fibonacci sphere

![image](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/3X/e/0/e07f3efeb8aaf4c1b8a4ba339ea2533e18d95b78.png)

See the code we have in PlayCanvas: [engine/random.js at d3e1b4a32c3eced83fb464e29aeef53fc9e5704c · playcanvas/engine · GitHub](https://github.com/playcanvas/engine/blob/d3e1b4a32c3eced83fb464e29aeef53fc9e5704c/src/math/random.js#L59-L76)

On the cpu during VAT generation, I’d generate all 255 normals using that code, compare the normal I’m encoding to find out the nearest of those, and store the index in .a of the texture.

During runtime, I’d simply use the index and the same code (running in a shader) to evaluate a normal.

Num points would be 255, start and end 0 and 1, the result is pretty fast to run code.

---

<div class="post-metadata">

**Author:** ![mvaligursky](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mvaligursky/32/11681_2.png) [@mvaligursky](https://forum.playcanvas.com/u/mvaligursky)\
**Post date:** [October 5, 2022, 8:37am UTC](https://forum.playcanvas.com/t/vertex-animation-textures/27729/9 "2022-10-05T08:37:40Z")

</div>

> [@steve\_wk](#):
>
> `SEMANTIC_ATTR15`

those are all exported into pc name space. So this should work:

```javascript
pc.SEMANTIC_ATTR15
pc.BUFFER_STATIC
pc.TYPE_FLOAT32

```

but this bit is tricky ;

` this.attributes.morph_vertex_id = SEMANTIC_ATTR15;`

and I’m not sure I have a solution here. Internally, when we generate the shader, we make it use ATTR15, but this is not at the moment available to you I suspect. This is the missing bit: [Custom attributes on material's shader definitions. · Issue #3485 · playcanvas/engine · GitHub](https://github.com/playcanvas/engine/issues/3485)

As a workaround for now could be to use `gl_VertexID` - but note that this is WebGl 2 only features. See here: [WebGL2 Drawing Without Data](https://webgl2fundamentals.org/webgl/lessons/webgl-drawing-without-data.html)

---

<div class="post-metadata">

**Author:** ![steve\_wk](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/s/dc4da7/32.png) [@steve\_wk](https://forum.playcanvas.com/u/steve_wk)\
**Post date:** [October 5, 2022, 8:46am UTC](https://forum.playcanvas.com/t/vertex-animation-textures/27729/10 "2022-10-05T08:46:40Z")

</div>

I did try using gl\_VertexID, but even though I have ‘Prefer WebGL 2.0’ enabled in the rendering settings I’m getting this error:

```javascript
ASSERT FAILED:
Vertex shader attribute "gl_VertexID" is not mapped to a semantic in shader definition.

```

So are you suggesting I should be able to potentially make the code like this?

```javascript
// create vertex stream with vertex_id used to map vertex to texture
const formatDesc = [{ semantic: pc.SEMANTIC_ATTR15, components: 1, type: TYPE_FLOAT32 }];
this.vertexBufferIds = new VertexBuffer(this.device, new VertexFormat(this.device, formatDesc), ids.length, pc.BUFFER_STATIC, new Float32Array(ids));

```

Essentially using pc.SEMANTIC\_ATTR15 and pc.BUFFER\_STATIC.  
And then potentially pass that to the shader as a parameter?

PS. That Fibonacci sphere is a very cool idea, thanks!

---

<div class="post-metadata">

**Author:** ![mvaligursky](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mvaligursky/32/11681_2.png) [@mvaligursky](https://forum.playcanvas.com/u/mvaligursky)\
**Post date:** [October 5, 2022, 8:54am UTC](https://forum.playcanvas.com/t/vertex-animation-textures/27729/11 "2022-10-05T08:54:46Z")

</div>

> [@steve\_wk](#):
>
> `is not mapped to a semantic in shader definition.`

how did you use `gl\_VertexID’ in the shader? I would not expect this error.

---

<div class="post-metadata">

**Author:** ![steve\_wk](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/s/dc4da7/32.png) [@steve\_wk](https://forum.playcanvas.com/u/steve_wk)\
**Post date:** [October 5, 2022, 8:55am UTC](https://forum.playcanvas.com/t/vertex-animation-textures/27729/12 "2022-10-05T08:55:29Z")

</div>

Ah OK, if I understand correctly I also need to put this at the top of the shader:

`#version 300 es`

However, obviously if I put that at the top of the transform shader, it is not actually at the top of the entire shader which gets generated. So I need to figure out where the start of the vert shader is, which file, and put it there. I think…?

---

<div class="post-metadata">

**Author:** ![mvaligursky](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mvaligursky/32/11681_2.png) [@mvaligursky](https://forum.playcanvas.com/u/mvaligursky)\
**Post date:** [October 5, 2022, 8:57am UTC](https://forum.playcanvas.com/t/vertex-animation-textures/27729/13 "2022-10-05T08:57:36Z")

</div>

Spector JS is your friend … install it as a Chrome plugin. Then you capture your code and see the whole shader generated.

By the way, all shader on GL2 already have that in them.  
`#version 300 es`

---

<div class="post-metadata">

**Author:** ![mvaligursky](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mvaligursky/32/11681_2.png) [@mvaligursky](https://forum.playcanvas.com/u/mvaligursky)\
**Post date:** [October 5, 2022, 8:58am UTC](https://forum.playcanvas.com/t/vertex-animation-textures/27729/14 "2022-10-05T08:58:21Z")

</div>

Your problem is probably that you declare  
`attribute gl\_VertexID’ or something similar, which you should not.

---

<div class="post-metadata">

**Author:** ![steve\_wk](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/s/dc4da7/32.png) [@steve\_wk](https://forum.playcanvas.com/u/steve_wk)\
**Post date:** [October 5, 2022, 9:02am UTC](https://forum.playcanvas.com/t/vertex-animation-textures/27729/15 "2022-10-05T09:02:45Z")

</div>

I was just trying to test whether the value was even pulling through with these lines:

```javascript
float vertexId = float(gl_VertexID);
float vertexCount = 508.0;
float u = vertexId / vertexCount;
vec2 testUV = vec2(u, 0.5);
vec3 vatPos01 = texture2D(vatPositionTexture01, testUV).xyz;

```

Edit: though now I’m wondering if it’s because there isn’t actually a texture in vatPositionTexture01 yet? I need to hook that up.  
Edit 2: Nope, that didn’t make any difference. Seems as soon as I try to hook that value into the uv for a texture lookup it breaks.

---

<div class="post-metadata">

**Author:** ![steve\_wk](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/s/dc4da7/32.png) [@steve\_wk](https://forum.playcanvas.com/u/steve_wk)\
**Post date:** [October 5, 2022, 9:26am UTC](https://forum.playcanvas.com/t/vertex-animation-textures/27729/16 "2022-10-05T09:26:20Z")

</div>

This is the full error I’m getting any time I actually try to use gl\_VertexID to do anything at all:

```javascript
Vertex shader attribute "gl_VertexID" is not mapped to a semantic in shader definition.

[[playcanvas-1.56.0.dbg.js:15808]](https://code.playcanvas.com/playcanvas-1.56.0.dbg.js): Cannot read properties of undefined (reading 'substring')

TypeError: Cannot read properties of undefined (reading 'substring')
at new ShaderInput (https://code.playcanvas.com/playcanvas-1.56.0.dbg.js:15808:13)
at WebglShader.postLink (https://code.playcanvas.com/playcanvas-1.56.0.dbg.js:15976:22)
at WebglGraphicsDevice.setShader (https://code.playcanvas.com/playcanvas-1.56.0.dbg.js:18353:49)
at ForwardRenderer.renderForwardInternal (https://code.playcanvas.com/playcanvas-1.56.0.dbg.js:25292:42)
at ForwardRenderer.renderForward (https://code.playcanvas.com/playcanvas-1.56.0.dbg.js:25426:11)
at ForwardRenderer.renderRenderAction (https://code.playcanvas.com/playcanvas-1.56.0.dbg.js:25948:13)
at ForwardRenderer.renderPassRenderActions (https://code.playcanvas.com/playcanvas-1.56.0.dbg.js:25876:13)
at RenderPass.execute (https://code.playcanvas.com/playcanvas-1.56.0.dbg.js:25807:15)
at RenderPass.render (https://code.playcanvas.com/playcanvas-1.56.0.dbg.js:22604:11)
at FrameGraph.render (https://code.playcanvas.com/playcanvas-1.56.0.dbg.js:28382:24)

```

---

<div class="post-metadata">

**Author:** ![mvaligursky](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mvaligursky/32/11681_2.png) [@mvaligursky](https://forum.playcanvas.com/u/mvaligursky)\
**Post date:** [October 5, 2022, 9:43am UTC](https://forum.playcanvas.com/t/vertex-animation-textures/27729/17 "2022-10-05T09:43:26Z")

</div>

> [@steve\_wk](#):
>
> ```javascript
> ASSERT FAILED:
> Vertex shader attribute "gl_VertexID" is not mapped to a semantic in shader definition.
> 
> ```

I have a fix which we should release as part of 1.57.1 sometimes next week most likely:

> <https://github.com/playcanvas/engine/pull/4695>
>
> Fix the error report and following null access when built in attributes, such as… gl\_VertexID or gl\_InstanceID, are used in the vertex shader:
> 
> reported error:
> \`ASSERT FAILED:
> Vertex shader attribute "gl\_VertexID" is not mapped to a semantic in shader definition.\`

In the meantime, you could build the engine yourself with the fix and use that, that’s pretty easy.

---

<div class="post-metadata">

**Author:** ![steve\_wk](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/s/dc4da7/32.png) [@steve\_wk](https://forum.playcanvas.com/u/steve_wk)\
**Post date:** [October 5, 2022, 9:53am UTC](https://forum.playcanvas.com/t/vertex-animation-textures/27729/18 "2022-10-05T09:53:45Z")

</div>

Fantastic, thank you so much!
