This has a recursive and a smooth lighting option. The smooth lighting will keep your vertices from harshly flashing from one color to another. This function doesn't account for range, right now:
Function LightMesh_(mesh,r,g,b,range#=0,x#=0,y#=0,z#=0,smooth=0,rec=0)
If EntityClass(mesh)="Mesh"
Normalize x,y,z
dx#=VectorX()
dy#=VectorY()
dz#=VectorZ()
For s=1 To CountSurfaces(mesh)
surf=GetSurface(mesh,s)
For v=0 To CountVertices(surf)-1
intensity#=DotProduct(dx,dy,dz,VertexNX(surf,v),VertexNY(surf,v),VertexNZ(surf,v))
If intensity>0
If intensity>1 intensity=1
vr=VertexRed(surf,v)
vg=VertexGreen(surf,v)
vb=VertexBlue(surf,v)
If smooth
If vr<r
vr=vr+smooth
If vr>r*intensity vr=r*intensity
EndIf
If vg<g
vg=vg+smooth
If vg>g*intensity vg=g*intensity
EndIf
If vb<b
vb=vb+smooth
If vb>b*intensity vb=b*intensity
EndIf
If vr<AMBIENTRED vr=AMBIENTRED
If vg<AMBIENTGREEN vg=AMBIENTGREEN
If vb<AMBIENTBLUE vb=AMBIENTBLUE
Else
vr=vr+r*intensity
vg=vg+g*intensity
vb=vb+b*intensity
If vr>255 vr=255
If vg>255 vg=255
If vb>255 vb=255
EndIf
VertexTexCoords surf,v,vr/255.0,vg/255.0,0,1
VertexColor surf,v,vr,vg,vb,vb/255.0
EndIf
Next
Next
EndIf
If rec
For c=1 To CountChildren(mesh)
TFormVector x,y,z,mesh,GetChild(mesh,c)
LightMesh_(GetChild(mesh,c),r,g,b,range#,TFormedX(),TFormedY(),TFormedZ(),smooth,1)
Next
EndIf
End Function
Function LightMesh_(mesh,r,g,b,range#=0,x#=0,y#=0,z#=0,smooth=0,rec=0)
If EntityClass(mesh)="Mesh"
Normalize x,y,z
dx#=VectorX()
dy#=VectorY()
dz#=VectorZ()
For s=1 To CountSurfaces(mesh)
surf=GetSurface(mesh,s)
For v=0 To CountVertices(surf)-1
intensity#=DotProduct(dx,dy,dz,VertexNX(surf,v),VertexNY(surf,v),VertexNZ(surf,v))
If intensity>0
If intensity>1 intensity=1
vr=VertexRed(surf,v)
vg=VertexGreen(surf,v)
vb=VertexBlue(surf,v)
If smooth
If vr<r
vr=vr+smooth
If vr>r*intensity vr=r*intensity
EndIf
If vg<g
vg=vg+smooth
If vg>g*intensity vg=g*intensity
EndIf
If vb<b
vb=vb+smooth
If vb>b*intensity vb=b*intensity
EndIf
If vr<AMBIENTRED vr=AMBIENTRED
If vg<AMBIENTGREEN vg=AMBIENTGREEN
If vb<AMBIENTBLUE vb=AMBIENTBLUE
Else
vr=vr+r*intensity
vg=vg+g*intensity
vb=vb+b*intensity
If vr>255 vr=255
If vg>255 vg=255
If vb>255 vb=255
EndIf
VertexTexCoords surf,v,vr/255.0,vg/255.0,0,1
VertexColor surf,v,vr,vg,vb,vb/255.0
EndIf
Next
Next
EndIf
If rec
For c=1 To CountChildren(mesh)
TFormVector x,y,z,mesh,GetChild(mesh,c)
LightMesh_(GetChild(mesh,c),r,g,b,range#,TFormedX(),TFormedY(),TFormedZ(),smooth,1)
Next
EndIf
End Function