[Closed] Bake bitmap on vertex color
Hello,
I try to transfer the color of an external bitmap on the vertex color channel of a mesh.
I’m near the solution but I have a bug I cannot fix by myself. When the plane that I use for the sample has not the same number of length and width segments, the result is offset.
Here is my code:
(
fn GetArrayOfMapVerts2VertsMESH node channel &mapVerts2Verts =
(
mapVerts2Verts = #()
local fnGetMapface = meshOp.getMapFace -- cache function for faster execution
local numMapFaces = meshOp.getNumMapFaces node channel
for f = 1 to numMapFaces do
(
local mapFace = fnGetMapface node channel f
mapFace = #(mapFace.x as integer,mapFace.y as integer,mapFace.z as integer)
local meshFace = getFace node f
meshFace = #(meshFace.x as integer,meshFace.y as integer,meshFace.z as integer)
for mv=1 to mapFace.count do
(
local mapVert = mapFace[mv]
if mapVerts2Verts[mapVert] == undefined then mapVerts2Verts[mapVert] = #(meshFace[mv])
else appendifunique mapVerts2Verts[mapVert] meshFace[mv]
)
)
while (mapVerts2Verts[1] == undefined) do deleteItem mapVerts2Verts 1 -- clean starting undefined items
OK
)
function BakeTextureOnVertexColor texFilename node channel =
(
if ( node == undefined ) do return false
local b = openBitmap texFilename
if (b == undefined) do return false
local texWidth = b.width
local texHeight = b.height
-- display b
with undo off
(
try
(
local mesh = convertToMesh node
-- get colors from bitmap
local mapVerts2Verts = #()
GetArrayOfMapVerts2VertsMESH node 1 &mapVerts2Verts
local uvPositions = #()
local vertexColors = #()
local fnGetMapVert = meshop.getMapVert -- cache function for faster execution
for i=1 to mapVerts2Verts.count do
(
local uvpos = fnGetMapVert node channel i
local texPixelCoords = point2 (floor (uvpos[1]*(texWidth-1))) (floor (uvpos[2]*(texHeight-1)))
append vertexColors ( if (texPixelCoords != undefined) then ((getPixels b texPixelCoords 1)[1]) else undefined )
)
-- set vertex colors
setNumCPVVerts mesh mesh.numverts
defaultVCFaces mesh
for i=1 to mapVerts2Verts.count do
(
for v in mapVerts2Verts[i] do if (vertexColors[i]!=undefined) do setVertColor mesh v vertexColors[i]
-- progressUpdate ((i/mapVerts2Verts.count) * 0.5 + 0.5)
)
)
catch ( print (getCurrentException()) )
convertToPoly mesh
)
close b
free b
OK
)
clearListener()
delete objects
local obj = convertToMesh (plane width:100 length:100 widthsegs:100 lengthsegs:10)
obj.showVertexColors = on
BakeTextureOnVertexColor @"C:\Users\xxxxxx\Desktop mp.jpg" obj 1
)
If you change te yellow parts, you will see the bug.
Many thanks for any help on this.
PS: the function GetArrayOfMapVerts2VertsMESH comes from this post. I slighlty modified it because I had useless undefined values at the beginning of the resulting array.
I found the bug.
Actually when you write this line…
plane width:100 length:100 widthsegs:100 lengthsegs:10
… the mesh does NOT update the uvs according the number of length and width segments. UVs are like if there was only 2 segments.
Adding a UVW_Unwrap() modifier then collapsing the stack update the UVS as they are expected to be.