PDA

View Full Version : texturemap access from maxscript?


logotomie
06-15-2005, 07:50 AM
Hi guys,

i am fairly new to maxscript and in order to learn more about it i started a little scripting project. I want to script a "hair geometry creator". currently it just creates random triangle spikes on the surface of another object. Now i want to control the growth of the triangles with a texture. I got the uv coordinates, got the diffuse map of the object but i cant find any function that returns the color of the map at a given uv coordinate.
after some hours of reading through the help, the forum and through scripts from the web i only found the following possibilities:

-use vertex color
-render texture to a bitmap and access the pixels. (This means finite resolution and loosing tiling info and restricting to 2 dimensions)

any other ways? Is there no generic texturemap access?
Thanks for any info on this!
ciao
Jan

Bobo
06-15-2005, 04:12 PM
Hi guys,

i am fairly new to maxscript and in order to learn more about it i started a little scripting project. I want to script a "hair geometry creator". currently it just creates random triangle spikes on the surface of another object. Now i want to control the growth of the triangles with a texture. I got the uv coordinates, got the diffuse map of the object but i cant find any function that returns the color of the map at a given uv coordinate.
after some hours of reading through the help, the forum and through scripts from the web i only found the following possibilities:

-use vertex color
-render texture to a bitmap and access the pixels. (This means finite resolution and loosing tiling info and restricting to 2 dimensions)

any other ways? Is there no generic texturemap access?
Thanks for any info on this!
ciao
Jan

Of course there is.
You need the following:
1. Get the Barycentric coordinates of the point on the face, using
meshop.getBaryCoords <Mesh mesh> <int faceIndex> <point3 pos>

2. Get the UV coordinates of that face's vertices

3. Calculate the UV coordinates of your point using the data from 1. and 2.

4. Using the UV coordinates and the bitmap used in the texture, grab the color of the pixel corresponding to those UV coordinates.

While not directly related, you can take a look at this tutorial:
http://www.scriptspot.com/bobo/mxs5/pflow/pflow__inherit_UV.htm

It takes the barycentric coordinates using intersectRay, but the rest is rather close to what you need.

logotomie
06-15-2005, 04:55 PM
Hi Bobo, many thanks for the reply.

Unfortunately it does not completely satisfy me :-) The tip with barycentric coordinates is cool, until now i just interpolated between the 3 uv coordinates myself.

direct access to the bitmap is also interesting. But i still loose tiling/mirroring and all the modifications done via the output parameters. And i still cant access other textures like Noise, Cellular and the like...

Bobo
06-15-2005, 10:52 PM
Hi Bobo, many thanks for the reply.

Unfortunately it does not completely satisfy me :-) The tip with barycentric coordinates is cool, until now i just interpolated between the 3 uv coordinates myself.

direct access to the bitmap is also interesting. But i still loose tiling/mirroring and all the modifications done via the output parameters. And i still cant access other textures like Noise, Cellular and the like...

Ok, so you can still go with vertex colors ;)

Seriously, all procedural maps can be rendered into a bitmap when needed (see RenderMap function). You can render the top level map in the channel you want to use to control your hair and get its texels instead. All output paramters are taken into account when performing this rendering, so tiling, rotation, mirror, offset should work as desired.

logotomie
06-22-2005, 02:54 PM
ok. not a beautifull way but it will do.

anyway, thanks very much again bobo. (A lot of helpfull answers in this forum seem to be from you) If you are interested here is what my script currently generates. In general maxscript is faster then i thought, I generated 20000 strands/200000polys within 1 minute.
http://www.janjordan.de/images/hairtest.jpghttp://www.janjordan.de/images/hairtest1.jpg

cgpainter
06-23-2005, 07:10 PM
My question is a bit less advanced then yours... I just switched from Rhino to 3Ds Max, and I cant figure out how in the heck to texture a bitmap onto a 3D object... So I supsoe mine is somewhat the reverse of your own....

anyway, I created a bitmap meterial, and it wont apply to anything... Why is this?

logotomie
06-23-2005, 07:47 PM
Hmmm. does your object have texture coordinates? In general i would advise you to follow one of the tutorials step by step...
maybe this helps:
http://www.max-realms.com/modules/tutorials/assign_texture.php

mahmoudcg
09-17-2005, 02:50 AM
Ok, so you can still go with vertex colors ;)

Seriously, all procedural maps can be rendered into a bitmap when needed (see RenderMap function). You can render the top level map in the channel you want to use to control your hair and get its texels instead. All output paramters are taken into account when performing this rendering, so tiling, rotation, mirror, offset should work as desired.

hi guys
i want to make the same thing.. uv based size
i have a cold drink bottle, water droplets condensed on it, however, droplets should vary in size and i want to control size by a texture, i need help in some points:

-are the uv coordinats of a texture vertex equivalent to pixel offset in a bitmap ?
that is, is the uv coor(5,21) equvalent to pixel at row 5, column 21 in the texture bitmap? or how can i access pixels based on uv coordinates?

- the call meshop.getMapVert returns a texture vertex as a point3 , how can i access pixels of a bitmap file with its 3 component values

- if i use a procedural map in texturing, the call to RenderMap takes the size of the output bitmap as arguments or outputs a default size 200*200 bitmap, now the texture size of the map to which the object is mapped to is changed, will these changes be reflected to the uv coordinates or will the uv coordinated now point to some other place according to the new bitmap size?

thnx for your concern

Bobo
09-17-2005, 06:52 AM
hi guys
i want to make the same thing.. uv based size
i have a cold drink bottle, water droplets condensed on it, however, droplets should vary in size and i want to control size by a texture, i need help in some points:

-are the uv coordinats of a texture vertex equivalent to pixel offset in a bitmap ?
that is, is the uv coor(5,21) equvalent to pixel at row 5, column 21 in the texture bitmap? or how can i access pixels based on uv coordinates?


No, UV coordinates are normalized coordinates. This means that in order to place a complete bitmap in UV space, you need UV coordinates between 0.0 and 1.0.

For example, in the simplest example of a plane mapped with a bitmap, if you have an image that is 200x100 pixels, the lower left vertex of the plane has UVW coordinates [0,0,0], the lower right has [1,0,0], the upper left has [0,1,0] and the upper right has [1,1,0], then the lower left corner of the bitmap (pixel 1,1) will be placed at the lower left vertex and the upper right corner of the bitmap (pixel 200,100) will be placed at the upper right vertex, resulting in a perfectly mapped plane.
Knowing the size of the bitmap and the UV coordinates, you can calculate the location of any pixel in the bitmap. For example, if you have a UV coordinate of [0.5,0.5,0.0], you just multiply the width of the bitmap by the U value and the height of the bitmap by the V value and you will get the pixel coordinates (remember, pixel 0,0 is the lower left corner, NOT the upper left. You might have to swap the direction if you want it to be the upper left). In the example above, 200*0.5 and 100*0.5 will result in pixel 100,50 which is exactly the center of the bitmap.


- the call meshop.getMapVert returns a texture vertex as a point3 , how can i access pixels of a bitmap file with its 3 component values


The 3rd component is normally not used when working with 2D bitmaps. It is needed when applying procedural 3D maps using UVW coordinates, or to project the bitmap along a different axis in the UVW space (like in the UW and VW plane). Generally, the first two components are what you need to work with bitmap textures.


- if i use a procedural map in texturing, the call to RenderMap takes the size of the output bitmap as arguments or outputs a default size 200*200 bitmap, now the texture size of the map to which the object is mapped to is changed, will these changes be reflected to the uv coordinates or will the uv coordinated now point to some other place according to the new bitmap size?


No, as explained above, UVW space is normalized between 0 and 1, so the bitmap size does not matter. If you would replace the 200x100 bitmap from my example with one that is 800x600, the center pixel 400,300 will still land exactly at the UV coordinate 0.5,0.5 in the case of a simple textured plane - Max will just multiply the dimensions of the bitmap by the UV coordinates and place the correct pixel at the right coordinate.

Note that the same channels can be used to store Vertex Colors (this time, COLOR space is expressed with values between 0 and 1, where the UVW axes correspond to the RGB colors. UVW of [1.0,0.5,0.0] would correspond to orange...)

You can visualize all this easily in the Unwrap_UVW modifier - select a texture vertex and watch its UVW coordinates in the spinners below the editor field.

The Tiling in the UVW_Map modifier multiplies the values and thus causes the bitmap to change its location on the object.
For example, if you would set the U tiling to 2.0 and the V tiling to 3.0, the UVW values we mentioned above would be multiplied by these values, resulting in lower left UVW coordinate of [0,0,0] but upper right coordinate of [2.0,3.0,0.0]. Obviously, the upper right pixel of the bitmap would still appear at [1.0,1.0,0.0], which in this case is at the center of the width of the plane and 1/3 of the height measured from below. Then it keeps on mapping on the rest of the plane, resulting in 2 bitmaps horizontally and 3 vertically...

The Tiling, Rotation and Offset values in the Bitmap Texture UI correspond to a Transformation Matrix to transform the bitmap inside the UV space. The difference to the above example is that the center of the UV coordinates (0.5,0.5) is considered the center of the transformation, not the [0,0] bottom left point. Changing the tiling affects the bitmap symetrically relatively to the center, and using W rotation rotates about the center, too.

Hope this helps...

Cheers,
Bobo

mahmoudcg
09-17-2005, 03:44 PM
thanx a lot Bobo
i'll implement this today and post the result

Rivendale
09-17-2005, 11:18 PM
While we're talking about texturemaps...Is there some way I can trick max into showing textures in the viewport in higher resolution than max normally allows in the configure driver settings? Might not be possible but I though I'd throw it out here anyway.

CML

Rivendale
09-18-2005, 12:47 AM
Nevermind, I don't think my last request is very necessary. Setting the graphics driver Texel lookup to "Nearest" seemed to be good enough.

Another thing I'm interested in...mainly directed at Bobo I guess:
Is it possible to set the viewport graphics window to a bitmap. I can get the viewport with gw.getViewportDib() but is there a way to set it? (I don't mean the viewport backround but the actual graphics window.) If not with maxscript, what other way could it be done?

CML

mahmoudcg
09-27-2005, 08:25 PM
hi guys
i did the particle scale from map.i know i'm late, but i had to finish somework first

here's the result
http://xs48.xs.to/pics/05392/dropletsrender.jpg

here's the map used to guide particle scale
http://xs48.xs.to/pics/05392/dropletsmap.jpg

the particles...
http://xs48.xs.to/pics/05392/dropletsparticles.jpg

the code of the script operator...
---------------------------------------------
---------------------------------------------
--this code assumes the scale map is rendered to a bmp file using RenderMap
---------------------------------------------
on ChannelsUsed pCont do
(
pCont.usePosition = true
pCont.useScale = true
pCont.useSpeed = true
)

on Init pCont do
(
global mapchannel = 5 -- revise this
global bottle = $Box09test -- revise this
global bmpfile = openBitmap "C:\\7up.bmp" --revise this
global xfactor = bmpfile.width
global yfactor = bmpfile.height
)

on Proceed pCont do
(
count = pcont.numParticles()


for i in 1 to count do
(
pcont.particleIndex=i
if (pcont.particleAge==0) do
(
pvel = pcont.particleSpeed
pos = pcont.particlePosition + pvel
pray = ray pos -pvel
arr = intersectRayEx bottle pray
if(arr != undefined) then
(

tf = meshop.getmapface bottle mapchannel arr[2]
tv1= meshop.getmapvert bottle mapchannel tf.x
tv2= meshop.getmapvert bottle mapchannel tf.y
tv3= meshop.getmapvert bottle mapchannel tf.z

tv = tv1*arr[3].x + tv2*arr[3].y + tv3*arr[3].z

if(tv.x >1)then
(
tv.x = tv.x-1
)
else if(tv.x<0)then
(
tv.x = tv.x+1
)

if(tv.y >1)then
(
tv.y = tv.y-1
)
else if(tv.y<0)then
(
tv.y = tv.y+1
)

co=getpixels bmpfile [tv.x*xfactor,(1-tv.y)*yfactor] 1

pcont.particlescale = (co[1].r/255)+0.2
--print "done"
--print pvel as string

)
else
(
--print ((pcont.particlePosition as string) +"and" + (pos as string))
--print "left"
)
)
)

)

on Release pCont do
(
close bmpfile
)
--------------------------------------
--------------------------------------
and finally a blobmesh with the particle system as the blobobject

the code is much inspired by or a modeification of Bobo's Inherit Texel Color From Emitter (http://www.scriptspot.com/bobo/mxs5/pflow/pflow__inherit_UV.htm)
the modifications are based on the above explanations (also from Bobo :))

if there's anything wrong in the code, please tell
thanks a lot guys, you helped me do this (specially BOBO) :):):)

CGTalk Moderation
09-27-2005, 08:25 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.