How to use multi-processing in touchdesigner?

Touch is largely single threaded, and using multi-threading in python requires that you work only with native python objects.

If you doing work on a grid for realtime deformation you might instead consider doing this in a vertex shader, or do the work in a compute shader then copying it back to the CPU (though I don’t think I would recommend this unless absolutely necessary).

Multi-threading:
github.com/raganmd/touchdesigne … -threading

Vertex Shaders:
nvoid.gitbooks.io/introduction- … in-3D.html
matthewragan.com/make-some-nois … hdesigner/

future versions of touchdesigner will have a process comp that allows that.

Meanwhile you can open a separate TD window, it will run on its own core. You can share you data with touchin-touchout operators

Another idea; if you dont need to compute on EVERY frame, put a part of your network in a COMP, and ‘add time component’ to that comp; set its own timeline’s framerate to something acceptable (like 15 or 30…or less) instead of 60

Thank you so much for your help! I am now working on a project, using realsense to capture the depth data which would affect the objects on the canvas to scale up and down.

I use the geo instancing to place many images on the canvas at the point positions on a grid and then get the points on the human figure with a trace node. A nested for loop is used to check whether the objects are close enough to any point on the human figure. If yes then scale down the object.

The for loop is too heavy so the fps is around 10-20 depends on the number of the points that the for loop goes through. As I am modifying the scale value in a table in every frame, I am not sure if multi threading could be used in this case.

Do you have any advice on this case? Thank you so much!

geo1.PNG

geo2.PNG

Thank you for your advice! It seems hard to separate the execute node to two separate TD. :cry: Separating the execute and rendering to two TD didn’t help a lot. :cry:

I’d look at a vertex shader for this kind of application. You can sample from your realsense texture and use a threshold value to scale your instances - if I’m understanding what you’re up to. Shouldn’t be too many lines of code to make it work. :slight_smile:

I watched the videos you made about vertex shader. They are really helpful! Thank you so much! I am totally new to TD. I failed to find out how to use vertex shader to do what I want in my case. :frowning:

In my case, every instance has their own texture. I want to scale each of them up and down when they are very close to the human figure that is traced from the realsense depth input. I am not using a single texture for the whole geometry. Is vertex shader a proper method for this case? How to do it? (so sorry that i am really new to shader stuff and td :cry:

Here’s a nudge to get you started. This doesn’t address your instance texturing, but it does show how you can set instance scale through a vertex shader by sampling another texture.

Hope this gets you moving in the right direction.
base_scale_from_alpha.tox (4.82 KB)

Thank you soooooooo much for your big big help! I need some time to figure out how it works. Thanks again!! :smiley: :astonished:

Hi @raganmd, sorry for bothering you again.
I am working on creating instances with different textures by using glsl, which means I have several colormaps. I figure out how to use one single colormap for rendering the instances but failed to find out how to randomly assign different textures to the instances. Could you please give a little hint?
Thanks so so much!

Took me a minute to carve out some time to make an example. Take a look here to see if this keeps you moving:

base_scale_from_alpha_texture.tox (5.31 KB)

Hi Matthew, thank you so so so much for your help!! It perfectly solved the problem! so so so happy to have your help! :smiley:
Thanks again!

Hi Matthew, sorry to bother you again. :stuck_out_tongue: I am trying to set the pivot point of the instance but when I assigned a value to Pivot X/Y/Z under the instance tab in the geometry, the scaling and rotating function in the vertex shader script turned not working. What’s the problem with this issue? :open_mouth:

Can you post a simple example that replicates the issue? Ideally one that shows it happening with the custom GLSL material and one that shows the expected behavior with a phong or constant. That will make it a lot easier for me to help diagnose what’s happening. :slight_smile:

Attached please find the project file. I want the instances to scale up and down around the pivot point. Under the Instance tab in geo1, I assigned pivot x/y/z which made the functions of reading the sampler, scaling and rotating in the glsl script not working anymore. Once I removed the pivot x/y/z, all things work. I must do sth wrong but I can’t figure it out. :cry:
Thank you so much!

pivot_problem.7.toe (9.58 KB)

Looks like you’ve changed around some pieces in the Vertex shader. You’ve got a mixed approach which might be part of what’s happening here.

P is the vertex position, which is not the same as the instance position. The instance is made up of some number of verts, so by doing some math to the individual verts rather than the instance you’ll get results like this (at least, that’s what I think I’m seeing here).

Can you say a little more about what you’re doing in the vertex shader and what the expected result should look like?

Initially I rotate all the instances into a random angle and scale them into a random size.

[code]
vec2 samplerLookup = TDInstanceTranslate().xy / (u_inst_grid/2);
samplerLookup = (samplerLookup / 2) + 0.5;

float angle = 360 * random(samplerLookup);
float posOffset = random(samplerLookup) * 0.08;

vec2 rot = vec2(P.x, P.y);
rot = rotate(rot, angle);
vec3 new_p = vec3(rot.x - posOffset, rot.y-posOffset, P.z);[/code]

I am getting the depth data from the depth camera into a top, and in the vertex shader, I have a sampler2D ‘scaleRef’ which is this top.
In the following code, my purpose is to scale down the instances which locate in the white part of the top as the pic shows below. So I grab the r value in the top and change the scale as the code shows. dunno how to make them scale down around the pivot point. :frowning: hope I made everything clear.

float scale = startscale - texture(s_scaleRef, samplerLookup).r * startscale;
new_p = new_p * scale;

vec4 worldSpacePos =TDDeform(new_p);
gl_Position = TDWorldToProj(worldSpacePos);

I was looking at this a little closer this morning and had a few questions.

Is there a reason to do the rotation in the vertex shader rather than in the instances parameters? I think there’s a little more at play in shifting the pivot point and how that plays into some of the other parameters. I think the simplest solution would be to add another column to your DAT (or a channel to your CHOP), and then pull in the rotation that way.

Now I add a new column to rotate the instance in the instances params. But then the function of scaling up and down as below doesn’t work. The situation is the same as it when I add the pivot xyz in the instances params. Is it that because transformation on instances cannot work when I add instances params in geo and also do the transformation in the material at the same time? (It’s my first time to play with glsl :frowning: such a big challenge to add the pivot point

float startscale = 1.5 + random(samplerLookup)*2;
float scale = startscale - texture(s_scaleRef, samplerLookup).r * startscale;
vec3 new_p = P * scale;
vec4 worldSpacePos =TDDeform(new_p);

Huh - that’s frustrating.

Still trying to get a simple functional example for you.

Hang in there GLSL is a hard one.

yea hard for beginners to make it work :frowning:
Thank you so so much! :smiley: