Loop through a texture's pixels - a better way?
by Shawn Kennedy · in Torque 3D Professional · 03/30/2011 (6:43 am) · 4 replies
Hello,
I'm currently trying to do the following:
1) Loop through a texture's pixels.
2) Check each pixel's color.
3) If the color is white, grab the UV.
4) Use the UV to lookup a pixel in another texture.
5) If that pixel has any red, do something.
My first, rough, implementation of it, was to allocate GBitmaps, and then use GFXTexHandle->copyToBmp to get a texture that I can loop through using getWidth, getHeight, and getColor. However, this is a huge hit to performance with the copyToBmp, and this is something that I'll need to do every, or almost every, frame.
I could cut out one of the copyToBmps by using a shader to specify the specific pixels (UVs) where I need to perform step 5, but I'd still have to do one copyToBmp to check that new texture that I made.
I'm looking for any ideas / creative solutions / inspiration. Is there a super cool way to do this?
Thanks,
I'm currently trying to do the following:
1) Loop through a texture's pixels.
2) Check each pixel's color.
3) If the color is white, grab the UV.
4) Use the UV to lookup a pixel in another texture.
5) If that pixel has any red, do something.
My first, rough, implementation of it, was to allocate GBitmaps, and then use GFXTexHandle->copyToBmp to get a texture that I can loop through using getWidth, getHeight, and getColor. However, this is a huge hit to performance with the copyToBmp, and this is something that I'll need to do every, or almost every, frame.
I could cut out one of the copyToBmps by using a shader to specify the specific pixels (UVs) where I need to perform step 5, but I'd still have to do one copyToBmp to check that new texture that I made.
I'm looking for any ideas / creative solutions / inspiration. Is there a super cool way to do this?
Thanks,
#2
Thanks for your time. The part that makes this tricky for me, I believe, is the last part. I'm working on a "drip" simulation. As in, water drops sliding down a window.
So, the final step, "do something" is to create a Drip, which has it's own physics and everything, with a position relative to the UV that I found in the other steps.
So basically I need a way to get the positions (UVs) where I need to place drips for this frame, without doing the gpu->cpu stuff...
03/30/2011 (8:14 am)
Manoel,Thanks for your time. The part that makes this tricky for me, I believe, is the last part. I'm working on a "drip" simulation. As in, water drops sliding down a window.
So, the final step, "do something" is to create a Drip, which has it's own physics and everything, with a position relative to the UV that I found in the other steps.
So basically I need a way to get the positions (UVs) where I need to place drips for this frame, without doing the gpu->cpu stuff...
#3
- Place a water droplet into the buffer into an arbitrary position
- Have it slide down with some physics
This can be done 100% with shaders and it's very likely it can be done with a PostEffect without any source code changes. I'm sure I saw a couple articles on doing such effects (with droplet trails and stuff).
It would go more or less like this:
- A buffer stores the droplet simulation data. A RGBA8 buffer storing water amount and a 2D velocity vector for each pixel should do it.
- You have a series of post effects that:
1) Copy the droplet simulation buffer
2) Read the copy and advance the simulation, writing into the droplet buffer
3) Read the droplet buffer and generate a normal/specular map that will be used to display the droplets.
To advance the simulation, the shader needs to:
- Check its current velocity vector and see how much water it will "lose" (the water will move to another fragment).
- Check the neighbor fragments and their velocity vectors and see how much water it will "gain" (water from other fragments that will move into the current fragment).
03/31/2011 (6:56 am)
If I understand, you want to:- Place a water droplet into the buffer into an arbitrary position
- Have it slide down with some physics
This can be done 100% with shaders and it's very likely it can be done with a PostEffect without any source code changes. I'm sure I saw a couple articles on doing such effects (with droplet trails and stuff).
It would go more or less like this:
- A buffer stores the droplet simulation data. A RGBA8 buffer storing water amount and a 2D velocity vector for each pixel should do it.
- You have a series of post effects that:
1) Copy the droplet simulation buffer
2) Read the copy and advance the simulation, writing into the droplet buffer
3) Read the droplet buffer and generate a normal/specular map that will be used to display the droplets.
To advance the simulation, the shader needs to:
- Check its current velocity vector and see how much water it will "lose" (the water will move to another fragment).
- Check the neighbor fragments and their velocity vectors and see how much water it will "gain" (water from other fragments that will move into the current fragment).
#4
Thanks again for your input. I guess now would be a good time to mention that I'm fairly new to shaders and have zero experience with Torque's PostEffects. I would like to look into a solution along the lines of what you outlined, however.
Can you point me to any good resources that would help me wrap my head around what you are explaining?
Thanks.
03/31/2011 (7:12 am)
Manoel,Thanks again for your input. I guess now would be a good time to mention that I'm fairly new to shaders and have zero experience with Torque's PostEffects. I would like to look into a solution along the lines of what you outlined, however.
Can you point me to any good resources that would help me wrap my head around what you are explaining?
Thanks.
Associate Manoel Neto
Default Studio Name
Can you explain exactly what you want to accomplish? So far, all the steps you listed can be done using a PostEffect that saves the result into a named render target, which you can then use into other post effects or even materials, without ever leaving the GPU.