About Store Forum Documentation Contact
Donations:
327$/mo



Post Reply 
Color of mesh texture hit by ray
Author Message
Biga Offline
Member

Post: #1
Color of mesh texture hit by ray
there is any possibility to query the texture color where ray hits the object? sth like this:
Code:
if (cp.otherCollider.Raycast(ray, out hit, rayLength))
        {
            Texture2D tex = (Texture2D)cp.otherCollider.renderer.material.mainTexture;
            C = tex.GetPixelBilinear(hit.textureCoord.x, hit.textureCoord.y);
        }

for now I can access the following info, right?

Code:
PhysHit phys_hit;
      
      if(Physics.ray(pos, dir*D.viewRange(), &phys_hit)) // if ray-test hit something
      {
         lastClick = phys_hit.plane.pos;

+ I can get the actor from phys_hit.plane.obj.
05-05-2014 11:01 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #2
RE: Color of mesh texture hit by ray
You'll need to look into following classes:
Mesh, Material, Image
05-06-2014 07:54 AM
Find all posts by this user Quote this message in a reply
Biga Offline
Member

Post: #3
RE: Color of mesh texture hit by ray
thx, so it is possible, that a good news smile
we plan btw a province selection, where I can map provinces by the color of selected point of click.
I thought I somehow get these coordinates mapped to 2D plane and reading from the province map texture (which should be hidden, just for identifying provinces).
ok, Im on it smile
05-06-2014 09:16 AM
Find all posts by this user Quote this message in a reply
Biga Offline
Member

Post: #4
RE: Color of mesh texture hit by ray
btw did you mean the technique you described here?

"you can grab the index of mesh part, face index,
compare it to the face triangle vertex positions,
calculate barycentric coordinates of 'hit_pos' on the triangle"

http://www.esenthel.com/community/showth...ht=physhit
05-06-2014 10:29 AM
Find all posts by this user Quote this message in a reply
Biga Offline
Member

Post: #5
RE: Color of mesh texture hit by ray
not too big success...
I started from hit_pos data and using a method which transforms spherical coordinates to rectangular ones knowing my image map x,y size...
x offset is almost ok, but y varies around one value and doesnt change too much...
so I know the hit x,y,z on mesh
I have the map in an Image
what I have to do with material? as I want use an another image for map, that isnt a rendered texture (just a color map).
any idea how to coninue?

thanks,
Gabor
05-07-2014 11:09 AM
Find all posts by this user Quote this message in a reply
Biga Offline
Member

Post: #6
RE: Color of mesh texture hit by ray
Im using this method for getting image coordinates, it is a wrong direction?

I have the geosphere with a large color map.
I have an Image with the same proportions for province map. (I dont want show this, it is only for inner mapping.)
I would like get back a colorcode from Image for that position.

PHP Code:
map(Key("9EDCF4FF")).name "Australia";
   
map(Key("9E70F8FF")).name "Turkey";
   
map(Key("D6F4A8FF")).name "Italy";
   
map(Key("6CF8A6FF")).name "Russia";
   
map(Key("FD27AFFF")).name "USA";
   ... 
PHP Code:
void TransformfromSphericaltoRectangularCoordinates(float xcoordfloat ycoordfloat zcoordinttargetxinttargety)
{
   
float norm Sqrt(xcoord xcoord ycoord ycoord zcoord zcoord);

   
xcoord /= norm;
   
ycoord /= norm;
   
zcoord /= norm;

   
float s Sqrt(xcoord xcoord ycoord ycoord);

   
// next stop spherical coordinates

   
float theta Acos(zcoord);
   
float phi Angle(Vec2(xcoordycoord));
   

   
// that's nice.  Now we want cylindrical coordinates.

   
targety int(theta pi 10800 0.5) % 10800
   
targetx int((phi pi) / (2*pi) * 21600 0.5) % 21600



PHP Code:
if(Physics.ray(posdir*D.viewRange(), &phys_hit)) // if ray-test hit something
      
{
         
lastClick phys_hit.plane.pos;
         
int x=0;
         
int y=0;
         
TransformfromSphericaltoRectangularCoordinates(lastClick.xlastClick.ylastClick.zxy); 

but coordinates are wrong....

PHP Code:
Color clr Imageptr->colorxy);
         
Str s1 Replace(clr.asTextHex(), "0x000000000""");
         
selectedcolor TextFlt(x) + "/" TextFlt(y) + "Province: " map(Key(s1)).name

problem is maybe with texture offset?

or do I have to create a new material based upon this color map and use on a hidden mesh with same size of my visible planet?
05-08-2014 02:30 PM
Find all posts by this user Quote this message in a reply
Rubeus Offline
Member

Post: #7
RE: Color of mesh texture hit by ray
It might be that the UV coords are calculated on a per-point basis to prevent textures from getting all bunched up at the poles and stretched at the equator. If this is the case, you can't use a linear measurement to figure out your UV, but you may be able to use that code if you use an Icosahedron rather than a standard sphere.
05-08-2014 06:17 PM
Find all posts by this user Quote this message in a reply
Biga Offline
Member

Post: #8
RE: Color of mesh texture hit by ray
PHP Code:
Color clr TerrainMaterial.base_0->color3D(lastClick.xlastClick.ylastClick.z); 

Tried it too but Im getting 0 color for any coordinates...
Does the engine set base_0 texture automatically with RGB or do I need set it from code?
My plan was to add a 2nd material as province map and getting it its color from hit coordinates.
05-11-2014 11:58 AM
Find all posts by this user Quote this message in a reply
Rubeus Offline
Member

Post: #9
RE: Color of mesh texture hit by ray
The base_0 texture should be the RGBA main texture, base_1 is the specular/normal/glow/ etc.
Color3D doesn't translate world or local cords to a color on the texture, if that is what you are thinking. I think you want to use ->color().
Make sure, also, that you are locking base_0 before you read from it. Reading from images/textures that are locked will always return 0.
05-11-2014 06:33 PM
Find all posts by this user Quote this message in a reply
Biga Offline
Member

Post: #10
RE: Color of mesh texture hit by ray
thx. tried color() and locking as well.
also removed texMove from source, and created proper textures with Gimp move offset tool, so Im getting now the texture synced with mesh relief.
color3d has x,y,z while the color only x,y parameters I assume the local texture coordinates... but exactly this is what I dont know, only hit plane position on mesh.
I have read about Sweep, maybe I have use this method, but didnt find an example/tutorial and parameter list looks very long :(
05-12-2014 04:40 PM
Find all posts by this user Quote this message in a reply
para Offline
Member

Post: #11
RE: Color of mesh texture hit by ray
I you want to go this path a SpherePosToImgCoord() function would come in handy, to make one, you would need to apply the inverse formula that was used for UV mapping the image on the sphere, what kind of sphere it is doesn't matter that much, only that you match UV mapping method (reversed). (Might be better if you UV the mesh yourself, so you don't have to fiddle with any offsets and such)
For example Finding UV on a sphere.

Alternatively, you can also get/interpolate the UV's from the triangle/quad vertices, since each vertex has uv coordinates, and the triangle or quad is a flat surface it's easy to calculate. Another option to consider is to just get the index of quad/triangle and use that to look up to which province it belongs if you don't need that much precision and have enough detailed mesh, saves bit of texture memory and some calculations. Either way, you should do this on the base mesh in system memory.

Once you have the UV's, getting image pixel coordinates is even easier ( x/y = img_width/height * u/v ).

Also, I suggest you to make this province map a lower resolution, like 512x256, export it as single channel 8bit raw file, read it into a byte array on system ram rather than gpu texture, having to lock it all the time, wasting precious space..
05-12-2014 07:28 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Biga Offline
Member

Post: #12
RE: Color of mesh texture hit by ray
Para, thanks a lot for useful comment, I moved to direction getting the triangles.
But now Im getting negative UVs in some cases, but Im not sure Im using the proper methods...
The theory based on your comment - if I understood it correctly:
1. Getting the triangle of hit
2. Getting vertices and their UVs
3. Create barymetric UV
4. Getting coords and colors from UV and height/width


PHP Code:
Earth.mesh->setBase(); 
...

PHP Code:
if(Physics.ray(posdir*D.viewRange(), &phys_hit)) // if ray-test hit something
      
{
            
// get triangle vertices
            
VecI vertices Earth.mesh->parts(0).base.tri.ind(phys_hit.face);
            
            
// get UVs
            
Vec2 UV1 Earth.mesh->parts(0).base.vtx.tex0(vertices.x);
            
Vec2 UV2 Earth.mesh->parts(0).base.vtx.tex0(vertices.y);
            
Vec2 UV3 Earth.mesh->parts(0).base.vtx.tex0(vertices.z);
            
            
// barymetric            
            
Vec2 UV0 Lerp(Lerp(UV1UV20.5), UV30.5);
            
            
// calculate x, y and color
            
int nX ProvinceMap.w() * UV0.x;
            
int nY ProvinceMap.h() * UV0.y;
            
Color clr ProvinceMap->colornXnY); 
05-14-2014 10:40 AM
Find all posts by this user Quote this message in a reply
Biga Offline
Member

Post: #13
RE: Color of mesh texture hit by ray
Quote:Once you have the UV's, getting image pixel coordinates is even easier ( x/y = img_width/height * u/v ).

hm really stuck here... we are getting bad UVs, even negative ones from this:

PHP Code:
Vec2 UV1 Earth.mesh->parts(0).base.vtx.tex0(vertices.x); 

how it is possible?
and UVs arent continous as I see in addition to negatives.
where is the error in code can anyone help to point out?

I clicked 6 times to Australia territory from east to west, ca horizontally and I would expect continous UVs to main texture.

This is a Log I made from click data:
it is not continous rather jumping, see UV1.x:
0.003, 0, 0.2, 0.018, -0.06, 0.015

hit: vertice indexes: UV1: UV2: UV3:

2767: vertices: 1652, 1659, 1653 UV1: 0.003/0.416 UV2: 0.004/0.413UV3: 0.005/0.416
32: vertices: 26, 19, 27 UV1: 0.000/0.998 UV2: 0.200/0.998UV3: 0.100/1.000
38: vertices: 30, 31, 21 UV1: 0.200/0.993 UV2: 0.245/0.992UV3: 0.262/0.994
110: vertices: 75, 76, 70 UV1: 0.018/0.980 UV2: 0.035/0.979UV3: 0.039/0.982
315: vertices: 193, 199, 194 UV1: -0.006/0.936 UV2: -0.005/0.933UV3: 0.000/0.934
354: vertices: 215, 222, 216 UV1: 0.015/0.927 UV2: 0.019/0.926UV3: 0.020/0.928
(This post was last modified: 05-16-2014 10:17 AM by Biga.)
05-16-2014 10:16 AM
Find all posts by this user Quote this message in a reply
Biga Offline
Member

Post: #14
RE: Color of mesh texture hit by ray
face/triangle index (phys_hit.face) getting from physical hit seems not the original mesh face but the one for physical check.
the vertices we get from this face seems not the correct ones where the hit happened/assumed on mesh visible.
without correct vertices I cant access to UVs... :(
05-18-2014 10:15 PM
Find all posts by this user Quote this message in a reply
Rubeus Offline
Member

Post: #15
RE: Color of mesh texture hit by ray
You can create a physBody.part from a static mesh. You might be able do that, feeding it your object's mesh. I don't know for sure that this will work, but it might be worth a try.
Otherwise, you could just do the calculations yourself instead of via the physics engine.
05-18-2014 11:10 PM
Find all posts by this user Quote this message in a reply
Post Reply