Raycast to detect textures of a interior
by Thomas Bang · in Torque Game Engine · 06/23/2009 (5:11 am) · 13 replies
Is it possible to get the name of the texture of a interior with a raycast?
#2
Some time ago, i thought about different footstep sounds onto the terrain and got this idea:
Paint your sounds.
In TGE the terrain has a size of 2048x2048 world units and the heightmap has a size of 256x256 pixel. So each pixel represents a square of 8x8 world units. I guess each terrain polygon (if you ignore z) has a size of 8x8 world units.
Create a new image (2048x2048). Each pixel correlates 1x1 world units.
Check the position of the player (ignore z) and calculate the corresponding pixel of that image and get the RGB-Value of this pixel.
If that pixel is green, play sound x.
If that pixel is brown, player sound y.
And so on...
It is also possible to blend colors. So you can create nice effects.
For example, if a projectile collides with the terrain, you can create a particle effect with 50% flying blades of grass and 50% flying dust.
By the way... this is the reason why i asked the question. If you shoot inside a interior, i want to see different particle effects on the walls.
06/23/2009 (6:43 am)
Thank you. I will look into the source code. Some time ago, i thought about different footstep sounds onto the terrain and got this idea:
Paint your sounds.
In TGE the terrain has a size of 2048x2048 world units and the heightmap has a size of 256x256 pixel. So each pixel represents a square of 8x8 world units. I guess each terrain polygon (if you ignore z) has a size of 8x8 world units.
Create a new image (2048x2048). Each pixel correlates 1x1 world units.
Check the position of the player (ignore z) and calculate the corresponding pixel of that image and get the RGB-Value of this pixel.
If that pixel is green, play sound x.
If that pixel is brown, player sound y.
And so on...
It is also possible to blend colors. So you can create nice effects.
For example, if a projectile collides with the terrain, you can create a particle effect with 50% flying blades of grass and 50% flying dust.
By the way... this is the reason why i asked the question. If you shoot inside a interior, i want to see different particle effects on the walls.
#3
06/23/2009 (9:45 am)
Painting soundmaps would be awesome, propably hard to code too.
#4
this is one of the most brilliant ideas I've heard in a long time,
someone with good coding skills should follow this up
06/23/2009 (10:03 am)
@Thomas,this is one of the most brilliant ideas I've heard in a long time,
someone with good coding skills should follow this up
#5
Is a execution time of 0,20 ms fast enough? ;-)
If you are interested, i can give you the dll and the cc/h-files. For free.
06/23/2009 (12:58 pm)
OK, i could not resist. I wrote a dll where you can get a RGB-value of a given image. Is a execution time of 0,20 ms fast enough? ;-)
If you are interested, i can give you the dll and the cc/h-files. For free.
#6
But if you want C++-Code for your engine...
With this code i get a crash. Dont know why.
06/23/2009 (1:20 pm)
Puh, i like to write DLLs. But if you want C++-Code for your engine...
ConsoleFunction( getTextureRGB, const char*, 4, 4, "get RGB of a given texture")
{
const char* textureName = StringTable->insert( argv[1] );
ColorI cl;
char *returnBuffer = Con::getReturnBuffer(256);
int x = atoi(argv[2]);
int y = atoi(argv[3]);
U32 key = HashPointer(textureName) % TextureDictionary::smHashTableSize;
TextureObject *tex = TextureDictionary::smTable[key];
for(; tex; tex = tex->hashNext)
if(tex->texFileName == textureName)
{
tex->bitmap->getColor(x,y,cl);
U8 r = cl.red;
U8 g = cl.green;
U8 b = cl.blue;
dSprintf(returnBuffer,256,"%g %g %g",r,g,b);
break;
}
return returnBuffer;
}With this code i get a crash. Dont know why.
#7
the code above, is that all that is required? or is there more?
I, for one, would really like to use this.
can you post a link to the files?
06/23/2009 (2:28 pm)
@Thomas,the code above, is that all that is required? or is there more?
I, for one, would really like to use this.
can you post a link to the files?
#8
www.darkware.de/Torque_SoundmapDLL.zip
Example in TorqueScript:
06/24/2009 (3:15 am)
OK, here is the DLL including lib,h and cc. www.darkware.de/Torque_SoundmapDLL.zip
Example in TorqueScript:
echo("Test with the DLL");
if (dw_loadSoundmap("game\data\Soundmaps\soundmap2048.bmp") == true) // image exists?
{
echo("getSoundmapRGB: " @ dw_getSoundmapRGB(1320,234)); // get RGB of pixel x,y
echo("getSoundmapRGB: " @ dw_getSoundmapRGB(1435,246));
echo("getSoundmapRGB: " @ dw_getSoundmapRGB(1472,263));
dw_destroySoundmap(); // i dont need you anymore
}
else
{
echo("Soundmap not found");
}
#9
Instead of using this for sound, how could it be used to affect the player? For example,
if terrain color = white //-walking on snow
then walking speed = .5
if terrain color = black //-walking on pavement
then walking speed = 2
if terrain color = brown //-walking on mud
then walking speed = 1
Might be hard to do if a raycast is needed. Maybe there is a way to use the player's bounding box contact with the terrain?
Just an idea.
Great dll!
Tony
06/24/2009 (4:41 am)
This is a really great idea you have, Thomas! So a projectile hitting wood sounds different than one hitting metal. Brilliant!Instead of using this for sound, how could it be used to affect the player? For example,
if terrain color = white //-walking on snow
then walking speed = .5
if terrain color = black //-walking on pavement
then walking speed = 2
if terrain color = brown //-walking on mud
then walking speed = 1
Might be hard to do if a raycast is needed. Maybe there is a way to use the player's bounding box contact with the terrain?
Just an idea.
Great dll!
Tony
#10
If so (player is walking on the terrain) you have to get the position of the player to detect the corresponding pixel.
Now you get the color of that pixel.
After that, it is your choice what you do next.
Play a footstep sound, spawn a enemy or quit the engine ;-)
It is up to you.
Walking speed of the player: Unfortunataly this is done with a datablock (PlayerData). You can modify the fields of this datablock (maxForwardSpeed, maxBackwardSpeed and maxSideSpeed). But you will get problems with a multiplayer game because all clients need to update their datablock.
06/24/2009 (5:06 am)
You need a raycast to detect if the player is walking on a terrain or a interior. If so (player is walking on the terrain) you have to get the position of the player to detect the corresponding pixel.
Now you get the color of that pixel.
After that, it is your choice what you do next.
Play a footstep sound, spawn a enemy or quit the engine ;-)
It is up to you.
Walking speed of the player: Unfortunataly this is done with a datablock (PlayerData). You can modify the fields of this datablock (maxForwardSpeed, maxBackwardSpeed and maxSideSpeed). But you will get problems with a multiplayer game because all clients need to update their datablock.
#11
I think that might work better than the RGB value because the sound / speed / etc will (probably) be the same for all of one material.
06/24/2009 (7:36 am)
It seems like the best way to do this would be to make sure every object returns a material on raycast, and have the materials themselves linked to sound files, movement speeds, etc. It may be neccesary to create dummy materials for the terrain.I think that might work better than the RGB value because the sound / speed / etc will (probably) be the same for all of one material.
#12
If you want even more detail (like Thomas mentioned about blending particles), there are methods to get the different opacities of terrain textures at a point on the terrain. You pick the two textures with the highest opacities, look up their material properties in the MPM, and use them to create the right particles. I already had a prototype system for creating particles when projectiles collided with different materials - worked a treat with bouncing projectiles. I don't have the code with me any more, but the changes aren't rocket science. I'll probably take some time off during the summer and reimplement them, clean them up and make a resource.
06/25/2009 (1:22 am)
Dan's got the right idea. Like I said, the MaterialPropertyManager class already does this for you. If you look somewhere in TGE's scripts there should be a properties.cs or something similar, with a bunch of calls to addMaterialMapping. The values in this call determine what type the material is for footstep sounds. I plan to expand this function to accomodate different movement speeds (you'd need to update each player's movement speed dynamically in Player::updateMove), and get Projectiles to use the system to create different decals and possibly explosions.If you want even more detail (like Thomas mentioned about blending particles), there are methods to get the different opacities of terrain textures at a point on the terrain. You pick the two textures with the highest opacities, look up their material properties in the MPM, and use them to create the right particles. I already had a prototype system for creating particles when projectiles collided with different materials - worked a treat with bouncing projectiles. I don't have the code with me any more, but the changes aren't rocket science. I'll probably take some time off during the summer and reimplement them, clean them up and make a resource.
#13
So it would be very good, if you could do a resource like this.
When a projectile collides with a interior, we could do a raycast to detect the collided texture.
06/25/2009 (2:00 am)
I am not very good in engine programming. So it would be very good, if you could do a resource like this.
When a projectile collides with a interior, we could do a raycast to detect the collided texture.
Torque 3D Owner Daniel Buckmaster
I think it was necessary for me to make a few minor code changes to get the material properties of Interiors registered properly - it should be easy enough to do a codebase-wide search for RayInfo::material, or to dive right into the Interior::castRay functions.
The MPM is something I've looked into a bit, and I'd love to expand and fix into a proper system for many more material-based effects. Things like unique footstep and projectile decals for different material types, etc.