# Vertex Displacement in GLSL

Hi guys! I’m new to touchdesigner and I’m trying to displace single vertices of an instanced Comp Geo. But I figured that what I’m affecting actually are the vertices of the geometry that is instancing the COMP.
So : how can i affect the position value of the vertices of the geometry resulting from the instancing??

I post the GLSL code that i wrote in th vertex shader (not all the code, just the uniforms and the void):

uniform vec3 uPos;
uniform sampler2D noise;

void main()
{
vec3 noiseVal = texture(noise, uv[0].st).rgb;
vec3 myPos= P*noiseVal;

``````vec4 worldSpacePos = TDDeform(myPos);
vec3 uvUnwrapCoord = TDInstanceTexCoord(TDUVUnwrapCoord());
gl_Position = TDWorldToProj(worldSpacePos, uvUnwrapCoord);
``````

``````vec3 noiseVal = texture(noise, uv[0].st).rgb;
``````

`uv[0].st` is probably not what you want here. That’s most likely UV information from the SOP that’s being instanced. I think instead you should find a coordinate based on the gl_InstanceID. Get the width in pixels of your noise texture as a uniform uNoiseWidth

``````ivec2 coord = ivec2(gl_InstanceID%uNoiseWidth,gl_InstanceID/uNoiseWidth);
vec3 noiseVal = texelFetch(noise, coord, 0).rgb;
``````

Alternatively, TouchDesigner has a new feature for instancing from TOPs without needing to write GLSL. Release Notes - Derivative (Geometry Instancing With TOPs)

Thank you very much David for your quick answer! It works now.
I’m wondering if you can explain me how works specifically :
1 -the division of gl_InstanceID by uNoiseWidth.
2- the texelfetch function.

the first question is because i do not understand why/how i’m sending a floating number into an integer vector.
For the texelfetch i don’t understand what the third argument stand for.

Thank you very much!

Woops I should have said that uNoiseWidth needs to be an integer.

``````uniform int uNoiseWidth;
``````

The division comes from the concept of a divisor and a remainder. Consider the task in the opposite direction: Based on a 2D coordinate, find the 1D ID. You’re given a pixel coordinate as an integer pair starting at (0,0) and ending at (width-1,height-1). Now give a unique single integer ID starting at 0 to each pixel starting in the lower left, moving rightward, then the second row, then the third row etc onward to the top right corner. So if I say give me the ID of the pixel at coordinate (X, Y), what you need to do is Y*width+X. For every Y rows you are upward, you need to add the width to your integer counter, and then lastly add the X coordinate. Doing the whole thing in reverse is division with remainder. If I have a single integer, how many rows can I fit in (the Y value). The remainder is the X value. Y = ID/width (no remainder). X = ID % width.

The third value in texelFetch is LOD, level of detail, which has to do with mip-maps. Generally you just want it to be 0. When writing shaders I use texelFetch when I know I want to access a very specific value. There’s no interpolation of nearby pixels that can happen with texture() and linear interpolation or mipmaps.

1 Like

Ok, now it’s everything more clear,thank you so much!
I hope this conversation help someone else to understand better this process.
Have a nice life, than you again!