Notifications
Clear all
[Closed] get the pixel color from a texture
Jul 31, 2008 11:56 am
fn averagecolor_fn arraycolor =
(
redvalue = 0
bluevalue = 0
greenvalue = 0
for a in arraycolor do
(
redvalue += a.red
bluevalue += a.blue
greenvalue += a.green
)
--divide the channels
redvalue = redvalue/arraycolor.count
bluevalue = bluevalue/arraycolor.count
greenvalue = greenvalue/arraycolor.count
return (color redvalue greenvalue bluevalue)
)
fn getcolorface_fn obj vertexnumber=
(
xVertIndex = vertexnumber --first selected vertex
theFaces = (meshop.getFacesUsingVert obj xVertIndex) as array --all faces using that vert.
theMeshFace = getFace obj theFaces[1] --first face using the vertex
theMapFace = meshop.getMapFace obj 1 theFaces[1] --the corresponding map face
--get the map vert from the same position in the face:
if theMeshFace.x == xVertIndex do theMapVert = theMapFace.x
if theMeshFace.y == xVertIndex do theMapVert = theMapFace.y
if theMeshFace.z == xVertIndex do theMapVert = theMapFace.z
-- get the REAL map vert. There could be more - one for each face using the mesh vertex!
xPoint = meshop.getMapVert obj 1 theMapVert
xBitmap = obj.material.diffuseMap.bitmap --get the bitmap
--get the correct pixel, taking into account origin and bitmap size:
xColor = getpixels xBitmap [xBitmap.width*xPoint.x, xBitmap.height - xBitmap.height*xPoint.y] 1
xcolor[1]
)
fn getmaterialcolor_fn obj facenumber mat numbervertex=
(
if classof mat == VRayMtl then--vray then
(
--not suported yet
return (color 0 0 0)
)
if classof mat == Standardmaterial then
(
if mat.diffusemap == undefined
then --no map in the slot
( return mat.diffuse)
else
(
--get the vertex from the difffusemaps
getcolorface_fn obj numbervertex
)
)
)
fn getfaceColor_fn obj facenumber numbervertex=
(
--look in the material for the id
themat =obj.material
if classof themat == Multimaterial then --multisub
(
--get the id
matid = getFaceMatID obj facenumber
--runthe fucntion for the material color
getmaterialcolor_fn obj facenumber themat[matid]
)
else --no nultisubs
(
getmaterialcolor_fn obj facenumber themat numbervertex
)--find the diffuse
)
fn getvertexcolor_fn obj numbervertex =
(
--find the faces aorund
facesBitArray = meshop.getFacesUsingVert obj numbervertex
FacesArray=facesBitArray as array
--
colorArray = #()
for i= 1 to FacesArray.count do
(
--find the id of the face
append colorarray (getfaceColor_fn obj FacesArray[i] numbervertex)
)
--promediate face color
return (averagecolor_fn colorArray)
)--find the id and the materials
this work work kind of ok when i try a single vertex.
you have to have a object that is an edit mesh with a texture applaied to the diffuse channel.
getvertexcolor_fn $ 264
when i try to get the color in a array give em and error
colorarray = #()
VertsWorkObject = (getNumVerts $)
for i=1 to VertsWorkObject do
(
append colorarray (getvertexcolor_fn $ i)
)
colorarray
i think the mistake is i on my fucntion to get the color for the pixel , i used on posted by bobo probably the issue is i dont understand it properly , sometimes return color and sometimes not , making my average function to fail.
xVertIndex = vertexnumber --first selected vertex
theFaces = (meshop.getFacesUsingVert obj xVertIndex) as array --all faces using that vert.
theMeshFace = getFace obj theFaces[1] --first face using the vertex
theMapFace = meshop.getMapFace obj 1 theFaces[1] --the corresponding map face
--get the map vert from the same position in the face:
if theMeshFace.x == xVertIndex do theMapVert = theMapFace.x
if theMeshFace.y == xVertIndex do theMapVert = theMapFace.y
if theMeshFace.z == xVertIndex do theMapVert = theMapFace.z
-- get the REAL map vert. There could be more - one for each face using the mesh vertex!
xPoint = meshop.getMapVert obj 1 theMapVert
xBitmap = obj.material.diffuseMap.bitmap --get the bitmap
--get the correct pixel, taking into account origin and bitmap size:
xColor = getpixels xBitmap [xBitmap.width*xPoint.x, xBitmap.height - xBitmap.height*xPoint.y] 1
xcolor[1]
any help will be much apreciated.