-- function to apply planar mapping to an object
-- direction parameter specifies the axis the mapping is
-- perpendicular to, and can have a value of #x, #y, or #z
--
fn ApplyPlanarMap obj direction =
(local oldcoordsys, normalize_pos
-- in this case, the number of texture vertices is equal to the
-- number of mesh vertices
obj.numtverts=obj.numverts
-- build the texture vertex faces
buildTVFaces obj
-- operate in objects coordinate space. Save original coord system so we
-- can restore later.
oldcoordsys=set coordsys local
-- for each vertex in mesh, get it's position and normalize the position
-- to a range of [0,0,0] to [1,1,1]. This is the planar UVW space.
for v = 1 to obj.numverts do
( normalize_pos=((getvert obj v)-obj.min)/(obj.max-obj.min)
-- flip around position component values based on which direction the
-- planar mapping is perpendicular to
case direction of
(#x:( tmp=normalize_pos.x
normalize_pos.x=normalize_pos.y
normalize_pos.y=normalize_pos.z
normalize_pos.z=tmp
)
#y:( tmp=normalize_pos.y
normalize_pos.y=normalize_pos.z
normalize_pos.z=tmp
)
)
-- set the corresponding texture vertex "position"
settvert obj v normalize_pos
)
-- done "positioning" the texture vertices. Build the texture faces.
-- since in this case there is a 1-to-1 correspondence between mesh and
-- texture vertices, the vertex indices for a mesh face are also the
-- texture vertex indices for the texture faces.
for f = 1 to obj.numfaces do
setTVFace obj f (getface obj f)
-- all done. reset the coordinate system
set coordsys oldcoordsys
-- update the mesh so 3ds Max sees the changes
update obj
)
--
-- test bed. Create a sphere, apply a material, set the diffuse map
-- to a 2x3 tiled checker map, turn on viewport display of the
-- checker texture map, and call the ApplyPlanarMap function
--
g=geosphere()
convertToMesh g
g.material=standard()
g.material.maps[2]=checker()
g.material.maps[2].coordinates.u_tiling=2
g.material.maps[2].coordinates.v_tiling=3
showTextureMap g.material g.material.maps[2] true
ApplyPlanarMap g #x
|