--imageDistributer1.27 Final by Snow 2002.9.3
--Image Distributer is a image based model scatter tools. it's allow
-- u scatter your model base on a picture. see the attached avi for example.
--Test Works On: 3ds max5.x, 3ds max4.x
--history:1.27 bug fix and color filter change 11/20/2002
--        1.26 suuport scatter seperatly on X, Y axis. also bug fix, interface change and some comments added.10/20/2002
--		  1.25 support max4 now  10/17/2002
--        1.24 undo enabled now, after select map it will auto apply to material editer and bug fix 10/16/2002
--		  1.23 now could clone multiobject(with Hierarchy), also some performance enhance10/14/2002
--		  1.21 progress bar and cancel enable.
--		  1.2 allow u scatter objects on target objects' vertex pos 9/12/2002
--        1.1 final auto size calculation 9/11/2002
--		  1.0 Final Speed increased,thanks for "interface", all texturemap support! 9/10/2002
--      to do:texture internal update on 1.3

--init varibles
escapeEnable=true
imageSelect=undefined
meshDis=undefined
imageCon=undefined
imageWidth=100
imageHeight=100
targetObj=undefined
copyChildren=True

modelDisX=undefined
modelDisY=undefined
colorScale=127

--main function do the thing:interface: imgDis meshObj(the mesh u want to scatter) tarObj(if not undefined will scatter to the object
--imageObj: the image based on

fn imgDis meshObj tarObj imageObj =
( 	
	clearundobuffer();
	undo "MeshScatter" on(
	imgW=imageObj.width-1;
	imgH=imageObj.height-1;
    progressstart "Scatter On The Way...";
	timeS=timeStamp();
	offsetPos=[0,0,0];
	for i=0 to imgW do 
	(	
	 if not progressUpdate(100.0*(i+1)/imgW;) do exit;
	 for f=0 to imgH do
		(
		dd=getPixels imageObj [i,f] 1;
		avdd=(dd[1].r+dd[1].g+dd[1].b)/3;
		kk=i*imgW+f+1;
		if avdd>colorScale do 
				(
				if tarObj==undefined then
				 	offsetPos=[i*modelDisX,(imgH-f)*modelDisY,0]
				 else
				 	if kk<=tarObj.numVerts do offsetPos=tarObj.verts[kk].pos
				--using interface make it really fast.
				maxops.clonenodes #(meshObj) expandHierarchy:copyChildren clonetype:#instance newnodes:&mymesh offset:offsetPos;
				mymesh[1].wirecolor=dd[1]
				)
		)
	)
	
	timeE=timeStamp();
	timeTake="Total take(Sec):" +((timeE-timeS)/1000) as string;
       pushPrompt timeTake;
    progressEnd();
	)

)

--fn maxof vals = (local v=vals[1]; for v1 in vals do (if v1 > v do v=v1);v)

rollout ImgDisRollout "ImageDistributer" width:140 height:364
(
	--create interface using VisualScript Editor
	mapButton btn1 "Image" pos:[26,156] width:80 height:24
	button btn2 "go" pos:[11,331] width:49 height:26 enabled:false
	pickbutton btn3 "Object" pos:[27,29] width:80 height:23
	GroupBox grp1 "Image Distributer" pos:[5,6] width:130 height:320
	button btn4 "about" pos:[80,332] width:48 height:24
	edittext edt3 "Width" pos:[24,189] width:88 height:16
	edittext edt1 "Height" pos:[19,218] width:93 height:16
	button btn5 "preview" pos:[45,245] width:48 height:16 enabled:false
	slider sld1 "ColorFilter:127" pos:[16,269] width:104 height:44 enabled:false range:[0,255,1] type:#integer orient:#horizontal ticks:10 
	edittext edt5 "distanceX" pos:[13,79] width:110 height:16
	
	
	pickbutton btn6 " tagObject(optional) " pos:[15,129] width:110 height:21 message:"if u want scatter to another object, click it." toolTip:"pick the target object you want to scatter to"
	checkbox chk1 "Hierarchy" pos:[18,57] width:96 height:18 checked:true
	edittext edt4 "distanceY" pos:[11,103] width:112 height:16
	on btn1 picked texmap do
	(	
		if texmap!=undefined do
		(matNum=sceneMaterials.count+1;
		medit.PutMtlToMtlEditor texmap matNum;
		if (classof texmap as string)=="Bitmaptexture" do
		(
		if texmap.bitmap!=undefined do 
		(imageWidth=texmap.bitmap.width;
		imageHeight=texmap.bitmap.height)
		)
	
	imageSelect=renderMap texmap size:[imageWidth,imageHeight] filter:on scale:200;
	btn2.enabled=true;
	btn5.enabled=true;
	sld1.enabled=true;
	btn1.text=classof texmap as string;
	edt3.text=imageSelect.width as string;
	edt1.text=imageSelect.Height as string;
	sld1.value=127
	    )
	)
	on btn2 pressed do
	(
	 if meshDis!=undefined do
	 (
	 imageCon=bitmap imageWidth imageHeight;
	 copy imageSelect imageCon;
	 imgDis meshDis targetObj imageCon
	 )
	 )
	on btn3 picked obj do
	(meshDis=obj;
	 modelDisX=floor((meshDis.max.x-meshDis.min.x)*100)/100;
	 modelDisY=floor((meshDis.max.y-meshDis.min.y)*100)/100;
	 edt5.text=modelDisX as string;
	 edt4.text=modelDisY as string;
	 btn3.text=obj.name
	 if meshDis.children.count==0 then
	 	(copyChildren=false;
		 chk1.checked=false)
	 else (copyChildren=true;
		 chk1.checked=true)
	 )
	on btn4 pressed do
	(
	messagebox "ImageDistributer1.27 9/3/2002-11/20/2002,ZhangYang, Email:yang.zhang@autodesk.com"
	)
	on edt3 entered text do
	(
	imageWidth=text as integer
	)
	on edt1 entered text do
	(
	imageHeight=text as integer
	)
	on btn5 pressed do
	(
	imageCon=bitmap imageWidth imageHeight;
	copy imageSelect imageCon;
	if imageSelect!=undefined do display imageCon
	)
	on sld1 changed sldV do
	( sld1.caption="ColorFilter:"+sldV as string
	  colorScale=sldV
	 )
	on edt5 entered text do
	(
	modelDisX=text as integer
	)
	on btn6 picked obj do
	(targetObj=obj.mesh;
	 btn6.text=obj.name
	 )
	on chk1 changed state do
		copyChildren=state
	on edt4 entered text do
	(
	modelDisY=text as integer
	)
)

rolloutSFloater = newRolloutFloater "ImgDisRollout" 150 390
addRollout ImgDisRollout rolloutSFloater
