PickedNX question
Blitz3D Forums/Blitz3D Programming/PickedNX question
| ||
If I have a shaded terrain with shady areas and bright areas, I wanted to use EntityColor to darken the grass quads on the shady places. I was using PickedNX etc. to determine if a grass quad should be shaded. I suspect I'm doing something wrong here, maybe somebody can help me? What I do right now is:pnx#=pickednx() pnz#=pickednz() pn_shade#=(pnx-pnz)-.2 if pn_shade<0 then shade=255.0-(pn_shade*128.0) limit shade to 0 to 255 use shade for rgb of entitycolor else shade=255 endif I have that vague guess I rather should use Atan2 with pnx and pnz or so, then add the light yaw as an offset and see if the angle is within a certain range, any ideas? |
| ||
Could you do something like this perhaps? get the pickednx,ny,nz as usual. Perform a dot product between the picked normal vector and the vector from light source to your point on the terrain. This will allow you to find the angle between the two vectors quite simply. Depending upon the angle you can then brighten/shade accordingly. |
| ||
Thanks. the dot product may be a furter step. Currently I got to learn how to get the "yaw angle" from pickednx and pickednz. It's more like I'm kind of "unfamilar" with PickedNX :o) EDIT basicly my question is: how to determine a normals brightness using pickedNXYZ, asssuming there's a light rotated something like 30,0,0. |
| ||
Have you tried VectorYaw()? |
| ||
Just using vectoryaw won't give you the correct information. As Matty suggests, something like this will work. I'm pretty sure LdotN will be between 0.0 and 1.0 if > 0.tformnormal 0,0,1,LIGHT, 1 Lx# = tformedx() Ly# = tformedy() Lz# = tformedz() LdotN# = Lx * PickedNx() + Ly * PickedNy() + Lz * PickedNz() If LdotN > 0 Shade# = 255.0 * LdotN else Shade# = 0 Endif |
| ||
Thanks a lot. For some reason I still can't make this work as desired. It kind of works, but then again there are some places where it's lit wrong, for no obvious reason, as if PickedNX returned fancy values. The sooner or later I'm gonna fix this. Just got to set up an isolated test enviroment, using a simple lit sphere and some linepicks from above it. |
| ||
How about another approach? If this isn't dynamic, and is only to be done once, why not do line picks from the lights location, to the entity, making sure the terrain has a pickmode. If you PickedEntity() is the grass quad, then you know not to shade it. |
| ||
Wow, that's a great idea, it will even include simple lightmapping for the trees and things. One problem: shading of meshes is smoother than these one level shades. Maybe we could use several pivots with an offset to obtain the amount of reflection. And how about a 3d grid with light info, so a character can be darkened when he's walking in the shade? Could easily be modified to sneak-in-the-dark stuff. Edit - uh, when the light is a directional one (as sunlight usually is) the the method must be modified lightly. Instead of the light position, a fixed xyz offset must be used, that is calcualted from the "sun" rotation. |
| ||
After all I went back to the initial guess, using Atan2 with nx and nz, add the light yaw and wrap in 360. Get the amount of "shadyness", eg, by using SIN with the angle, then multiply it with ny. Only thing that was consufing was: in one test with a sphere PickedNX() returned values from 0.0 (flat) to 1.0 (steap), in an other app, it returned 1.0(flat) to 0.0(steap). Probably it was only a bug in my code somewhere. Thanks for your help! |