Notifications
Clear all
[Closed] MAXSCRIPT: get peer-vertex normal function
Apr 27, 2010 3:32 pm
I still working on export plugin, and I stuck again…
How I wanna do:
I have mesh to export
I copy them and break some vertices (or all)
Now I wrote function that should return normals peer every vertex of broken object, but shading will be same as in source object.
So after clone, break, get normals, apply normals to copy the copy object should looks identical like original one.
I wrote that, but it looks I geting wrong normals form source object
-- get peer-vertex normals using normals source object
-- not exacly working...
-- by Miran - Miran4@op.pl
-- return average point3 of point3 array
function avg3 point3array =
(
local average = point3 0.0 0.0 0.0
if( point3array.count > 0 ) then
(
for i=1 to point3array.count do
(
average += point3array[i]
)
average /= point3array.count
)
return average
)
-- returns array of face indices which using given vertice's indice
function getFacesByVert object vert =
(
local face
local faces = #()
for i=1 to object.numfaces do
(
face = (getFace object i)
for j=1 to 3 do
(
if( face[j] == vert ) then
(
append faces i
exit
)
)
)
return faces
)
--
function getVertNormalsObj vert_obj normal_obj =
(
local normalMod, localNormalMod = true
-- use already existing normals modifier
for i=1 to normal_obj.modifiers.count do
(
if( (classOf normal_obj.modifiers[i]) == editNormals ) then
(
normalMod = normal_obj.modifiers[i]
localNormalMod = false
exit
)
)
-- create new modifier
if( localNormalMod ) then
(
normalMod = editNormals()
addModifier normal_obj (normalMod)
)
-- for correct working of modifier
select #( normal_obj )
max modify mode
local VertNormals = #()
local faces_vert, vert_normals, face, face_normals
for i=1 to vert_obj.numverts do
(
-- get faces builded on this vert
faces_vert = getFacesByVert vert_obj i
vert_normals = #()
for j=1 to faces_vert.count do
(
-- get this vert corner index in the face
face = getFace vert_obj faces_vert[j]
for k=1 to 3 do
(
if( face[k] == i ) then
(
-- get this face's corner normal
append vert_normals (normalMod.GetNormal (normalMod.GetNormalID faces_vert[j] k))
exit
)
)
)
append VertNormals (avg3 vert_normals)
)
if( localNormalMod ) then
deleteModifier normal_obj normalMod -- remove local created modifier
return VertNormals
)
-- testing interface
rollout VertNormals "VertNormals"
(
button getBtn "Get peer-vertex normals"
on getBtn pressed do
(
if( (selection.count > 0) and ((classOf selection[1]) == Editable_mesh) ) then
(
local broken_mesh = copy selection[1]
broken_mesh.name = "broken"
meshop.breakVerts broken_mesh broken_mesh.verts -- break all verts
local vert_normals = getVertNormalsObj broken_mesh selection[1]
for i=1 to broken_mesh.numverts do
( -- apply calculated peer-vertex normals (visible when render)
setNormal broken_mesh i vert_normals[i]
)
move broken_mesh (point3 (1.5*(broken_mesh.max.x-broken_mesh.min.x)) 0.0 0.0) -- move next to original object
select #( selection[1], broken_mesh ) -- select source and result
max zoomext sel all -- center viewport on selected
)
else
messageBox "No mesh selected!"
)
)
-- create the rollout window and add the rollout
if VertNormalsFloater != undefined do closerolloutfloater VertNormalsFloater
VertNormalsFloater = newRolloutFloater "VertNormals Floater" 200 100
addRollout VertNormals VertNormalsFloater
I checked 10 times and can’t figure out what’s wrong
1 Reply
Apr 27, 2010 3:32 pm
Are you sure that once you break the verts that the vertex ID is still valid? I would look into using meshop.getVertsUsingFace to decide which vertex you need to set to which normal.
-Eric