Notifications
Clear all

[Closed] UVW Coordinates to Bitmap Coordinates & back

Does anybody know how to convert uvw coordinates to bitmap coordinates (and back if possible)? This is kinda killin me

2 Replies

to convert from uvw space to bitmap space you first need to ignore the ‘w’ axis since uvw space is 3D and bitmap space 2D.
so you need a conversion function f : [0,1]x[0,1] -> [0,W]x[0,H] where ‘W’ is the width of the bitmap in pixels and ‘H’ is the height in pixels. remember that the [0,0] in uv space corresponds to the lower left side of the image but the [0,0] in bitmap space is the upper left corner. so we get:

f(u,v) = (u*W, (1-v)*H)

now we still need to take care of the tileability of bitmaps. For instance uv coordinates [0.1,0] and [1.1,0] corresponds to the same bitmap pixel.
We modify our function using the mod function as follows:

f(u,v) = ((mod u 1)*W, (mod (1 - v) 1)*H)

now to invert the function f we need a function f^-1 : [0,W]x[0,H] -> [0,1]x[0,1]
we can ignore the 2nd version of f and invert the 1st version without the mod, so:

f^-1(x,y) = (x/W,1 - y/H)

hope this helps a bit

Thanks a lot man! I’m going to give it another try Ive been at this on and off for months now and just am having a hard time grasping the math to convert between the 3d points(verts or facecenters) to bitmap and back.

Here is what I have as of now that i am calling a partial success. I’m only getting Red Pixel colors back even though i have Red, Green, and blue available in my bitmap. I think I’m collecting the texture vert color’s wrong or converting the uvw coords to bitmap coords wrong.

--NATURAL LANGUAGE
---------------------------------------------------
-- loop through all mesh vertx
-- get connected faces
-- loop through all connected faces and get coresponding texture faces
-- get mapverts from texture faces
-- find vert indicies in map verts that coresponds to current master loop vertex
--get coords of of texture verts by using collected indicies from above
--convert coords of vertex into bitmap coords
--get pixel color at that coordinate
--if blue collect master loop vertex integer into array
---------------------------------------------------

--Variable Declerations
theMesh = $
MapChannel = 1
subdivs=256
theTextureMap = renderMap theMesh.material.diffuseMap size:[subdivs,subdivs] filter:on display:false

TargetFacesTexVerts= #()
TargetFacesTexVertCoords = #()
VertIndicies = #()
TexVertColors = #()
BlueVerts = #()
GreenVerts = #()
RedVerts = #()

-- Loop through all mesh verts
for v = 1 to theMesh.numverts do
(
--Get corresponding shared faces
TargetFaces = polyop.getFacesUsingVert theMesh v as array --get faces that reference the current vertex

-- get corresponding face verts then get corresponding texture vert indecies
for f = 1 to TargetFaces.count do
(
FaceVerts = polyop.getFaceVerts theMesh TargetFaces[f] -- get mesh face verts
append VertIndicies (finditem FaceVerts v) -- get face index corresponding to primary vert index.

FaceTexVerts = polyOp.getMapface theMesh MapChannel f -- get single texture face verts
append TargetFacesTexVerts FaceTexVerts
)
-- Get coordinates of each respective vertex
for c = 1 to TargetFacesTexVerts.count do
(
append TargetFacesTexVertCoords (polyop.getmapvert theMesh MapChannel TargetFacesTexVerts[C][VertIndicies[C]])
)

for b = 1 to TargetFacesTexVertCoords.count do
(
--convert vertex coords to bitmap coords
--textureCoords = [ (uvCoords.x)*(thisTexture.width-1) , thisTexture.height-(uvCoords.y)*(thisTexture.height-1) ]
BitmapCoords = [ (TargetFacesTexVertCoords[b][1])*(theTextureMap.width-1) , theTextureMap.height-(TargetFacesTexVertCoords[b][2])*(theTextureMap.height-1) ]
--print Bitmapcoords
append TexVertColors (getPixels theTextureMap BitmapCoords 1)--[1]
--print TexVertColors
)
--Average Vertex Color
Avg = TexVertColors[1][1]
for A = 2 to TexVertColors.count do
(
Avg = Avg + TexVertColors[A][1] as point3
)
--Get average vert color from all texture vert colors collected
AverageColor = (avg / TexVertColors.count) as point3

--Look for Primarily blue texture verts
if AverageColor[3] > AverageColor[1] and AverageColor[3] > AverageColor[2]
then
(
append BlueVerts v
Print ("BlueVerts: " + BlueVerts[v] as string)
)
if AverageColor[2] > AverageColor[1] and AverageColor[2] > AverageColor[3]
then
(
append GreenVerts v
Print ( "Green: " + GreenVerts[v] as string)
)
if AverageColor[1] > AverageColor[2] and AverageColor[1] > AverageColor[3]
then
(
append RedVerts v
Print ("Red: " + RedVerts[v] as string)
)

-- reset array's for next loop
TargetFacesTexVerts= #()
TargetFacesTexVertCoords = #()
VertIndicies = #()
TexVertColors = #()

)