Notifications
Clear all

[Closed] Bitmap – Texture Coordinates

Hi! I have the following question. I wrote a script, which takes a random face -> gets the 3 vertices of this face and then takes a random point on that face. On this random point I place a object which looks in the direction of the face normal.Then what I want is to get the texture coordinate of this point and look for the color at this point in a bitmap to rotate it dependent on the color. But I get the wrong colors. Can anyone help me what I am doing wrong? This is the relevant code that I have:


face_index = random 1 number_of_faces		 
 --Get vertices of face:					
 vertices = meshop.getVertsUsingFace obj face_index as array
 vertex1 = meshop.getVert obj vertices[1]
 vertex2 = meshop.getVert obj vertices[2]
 vertex3 = meshop.getVert obj vertices[3]
 --Calculate random point on that face
vec1 = vertex2 - vertex
vec2 = vertex3 - vertex1
x = random 0.0 1.0
y =1 - x
 random_point = vertex1 + (x * vec1 + y * vec2)
--get Map Vertices:
map_face = meshop.getMapFace obj 1 face_index
map_vertex1 = meshop.getMapVert obj 1 map_face[1]
map_vertex2 = meshop.getMapVert obj 1 map_face[2]
map_vertex3 = meshop.getMapVert obj 1 map_face[3]
--Bary Coords
bary_coords = meshop.getBaryCoords obj face_index random_point
 texture_coords = ((bary_coords.x * map_vertex1) + (bary_coords.y * map_vertex2) + (bary_coords.z * map_vertex3))
--get Pixel Color 
pixel_color = getpixels current_bitmap[texture_coords.x*(bitmap_width-1),(1-texture_coords.y) * (bitmap_height - 1)] 1


11 Replies

a quick run looks ok to me… the uvcoord tally with the uvw unwrapper and I use the same pixel lookup formula without issue.

though might want to replace

    vertices = meshop.getVertsUsingFace obj face_index as array
with...
vertices = getface obj	face_index

is

vec1 = vertex2 - vertex

a typo ?

Yes ist was a typo.
But it doesn’t work. Don’t know where the problem is
With this function you recommended, I get a face with four vertices, but I want to work with triangles.

With this function you recommended, I get a face with four vertices, but I want to work with triangles.

i find that difficult to believe as…

getFace
<mesh> <face_index_integer>
Returns the face vertex indexes for the indexed face as a point3. Each component of the point3 value contains a vertex index.

removing the minus one on the width and height give more precise result

getpixels bm [texture_coords.x * (bm.width), (1-texture_coords.y) * (bm.height)] 1

how are you checking the result ? I created a colour noise map in photoshop (with a gradient overlay) mapped to a plane in max. The scene has ambient 100% with a single omni turned off to kill the default lighting. The texture map has filtering turned off and I the create a point at the randomly created position then use the color clipboard util to pick the colour from the map at that point. Everything checks out as far as I can tell.

btw is the random point generation supposed to always be on an edge ?

Hi!
Ok that is right with the getface function…thought that it is like the polyop function that it returns vertices of quads, but you are right.
But it makes no difference for the result
I have a bitmap in the material editor which is mapped on a cube. And I want to rotate the objects which are placed on the faces of the cube dependent on the color on the point, where they are placed, but the result is, that the false objects are rotated.
No, they are not placed on the edges. They are placed everywhere on the faces, because the typo was :
vec2 = vertex3 – vertex1
So I go a random factor along the vector from vertex 1 to vertex 2 and a random factor along the vector from vertex 1 to vertex 3. It’s nothing else then placing a point on a plane

If I get it right, doesn’t that actually place point on a line opposite vertex1? Or what was the typo supposed to be corrected to? To get a point in triangle, one of the methods I’ve seen used most often is:

	local r1 = (random 0. 1.)^.5
 	local r2 = random 0. 1.
 	local pos = (1 - r1) * vertex1 + (r1 * (1 - r2)) * vertex2 + (r1 * r2) * vertex3

I have the three vertices of a face. vertex1, vertex2 and vertex3.
Then I calculate the vector from vertx 1 to vertex2: vec1 = vertex2 – vertex1
and the vector from vertex1 to vertex3: vec2 = vertex3 – vertex1
Then I normalize this vectors with: vec1 = vec1 / length(vec1) and vec2 = vec2 / length(vec2) so that I have the direction with a length of 1
Then under the restriction of: a + b = 1 you can say:
random_point = vertex1 + (avec1) + (bvec2)
This is a point which is on the face of this triangle. So you go from vertex 1 a steps into the direction to vertex2 and from this point(which is on the edge between vertex 1 and vertex 2) you go in the direction of vec2, so that is no longer on the edge. This works fine.

Now the problem is slightly hidden by normalizing the vectors (it would pick all the points on the side of triangle opposite vertex1 only if the distances of vertex2 and vertex3 from vertex1 were equal to 1; btw. you can use normalize function instead) but it is still there. Trick question, what would happen if distances of vertex2 and vertex3 from vertex1 were order(s) of magnitude bigger/smaller?

Hi! I have it know The problem was that I didn’t recognized that there was a crop applied in the bitmap. Now it works. Thanks a lot. I also forgot to normalize the vectors altough I wrote it in my last post.

Page 1 / 2