TDInstance GLSL

Hello !!
Maybe someone can explain how to change geo instance transformations in GLSL?
I find out for translation I can use :

newPos = TDInstanceDeform(pos) ;
vec4 worldSpacePos = TDDeform(P+newPos.xyz);

But how I can change rotation and scale? How to use TDInstanceMat() ?

Hi there,

TDDeform() does this all for you. It calls TDInstanceDeform() (the function that uses the instanced data set in the geometry COMP to deform the vertices)

If you’re supplying your transform/rotation & scale data in another way (for example through a sampler of the shader), you would need to do it yourself, something like this:

int index = TDInstanceID();
vec3 instancePosition = SomeFunctionSamplesThePosition(index);
vec3 instanceRotation = SomeFunctionSamplesTheRotationInDegrees(index);
vec3 instanceScale = SomeFunctionSamplesTheScale(index);

// Create a rotation matrix by multiplying: 
//   Z rotation * Y rotation * X rotation
mat3 rotationMatrix = TDRotateOnAxis(radians(instanceRotation.z), vec3(0,0,1)) *
  TDRotateOnAxis(radians(instanceRotation.y), vec3(0,1,0)) *
  TDRotateOnAxis(radians(instanceRotation.x), vec3(1,0,0));

// First scaling, then rotating, then translating.
vec3 sopSpacePosition = P * instanceScale; // scaling in sopspace
vec3 rotatedPosition = rotationMatrtix * sopSpacePosition;
vec3 translatedPosition = rotatedPosition + instancePosition;

// Still calling TDDeform to  deform the position according to 
// the parent geometry. 
//(So all instances can be scaled / rotated / transformed as well)
vec4 worldSpacePos = TDDeform(translatedPosition);

(btw didnt tested to code, so I’m sure there are some typos :slight_smile:)

Some information about matrices can be found here:
opengl-tutorial.org/beginner … -matrices/

Hope this helps
Cheers,
Tim

1 Like