Notifications
Clear all

[Closed] Align to a Quad?

How about :

You take the normal of the polygon
You normalise it
You use this vector as an axis
and you calculate the other 2 axis by using the cross product and the up vector
you then get the position of the normal of the face
and then you construct a new matrix with these 3 vectors for the x, y and z axis and then the position for the 4the row
And then you use this matrix as the transform matrix of your object

1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

all these above is:


  pretranslate (matrixfromnormal faceNormal) faceCenter
  

the question is how to get front-vector (or side-vector) to make a matrix if faceNormal parallel to up-vector?
there is no any general solution for this. you have to make you own rule. I use the normalized vector from first face corner to second.
so my matrix looks:


 matrix3 vector12 (cross vector12 faceNormal) faceNormal faceCenter
 

to be absolutely safe before making the matrix you have to recalculate the front-vector in my case using side-vector and faceNormal

Those last two posts made me sad…a very sad panda indeed, never realised it would be so difficult to achieve something that ‘appears’ so simple :’(

I knew that pesky Matrix jazz would come up at some point, I couldnt avoid it forever. I dont really understand the solution you guys came to but I will research it and try to get my head around it

Cheers, K

I might mess with this again today. Spent days trying to wrap my head around and got somewhere before. But using this new info, I might be able to do it

Out of interest, would it be easier/possible to have an object in the scene say above the box (like a reference point) that the chosen axis of the object would constrain/point to?

K

here is a little snippet to play with:


try(destroydialog alignfaceDialog) catch()
rollout alignfaceDialog "Align To Face by denisT" width:210
(
	spinner seed_sp "Seed: " range:[0,1e9,10] type:#integer fieldwidth:52 align:#right

	edittext world_lb "World Up:" text:" 0, 0, 1" fieldwidth:62 readonly:on align:#right
	edittext front_lb "Front:" text:" 1, 0, 0" fieldwidth:62 readonly:on align:#right
	
	button face_verts "Normal and Vert Vector" width:190
	button up_front "World Up and Front" width:190
	button up_verts "World Up and Vert Vector" width:190
	button face_front "Normal and Front" width:190
	
	fn scatterNodes ground brush method: up:[0,0,1] front:[1,0,0] = if iskindof ground Editable_Mesh and isvalidnode brush do
	(
		faces = ground.faces as bitarray
		done = #{} 
		for f in faces where not done[f] collect
		(
			polys = meshop.getpolysusingface ground f
			ground.selectedverts = meshop.getvertsusingface ground polys
			done += polys

			center = (averageSelVertCenter ground)*ground.objecttransform
			normal = normalize (averageSelVertNormal ground)
			
			tm = case method of
			(
				#face_verts: 
				(
					face = (polys as array)[1]
					vv = getface ground face
					front = normalize ((getVert ground vv[2]) - (getVert ground vv[1]))
					side = normalize (cross front normal)
					_front = normalize (cross normal side)
					matrix3 _front side normal center
				)
				#up_front: 
				(
					side = normalize (cross front normal)
					matrix3 front side up center
				)
				#up_verts: 
				(
					face = (polys as array)[1]
					vv = getface ground face
					front = normalize ((getVert ground vv[2]) - (getVert ground vv[1]))
					side = normalize (cross front up)
					_front = normalize (cross up side)
					matrix3 _front side up center
				)
				#face_front: 
				(
					side = normalize (cross front normal)
					_front = normalize (cross normal side)
					matrix3 _front side normal center
				)
				default: 
				(
					translate (matrixfromnormal normal) center
				)
			)
				
			b = copy brush wirecolor:green
			b.transform = tm
			b
		)
	)

	fn create method: =
	(
		delete objects
		brush = 
		(
			top = box widthsegs:1 lengthsegs:1 heightsegs:1 width:6 length:0.1 height:4 pos:[3,0,-24.5]
			top.mat = standard diffuse:red
			bot = Cylinder name:"flag" smooth:on heightsegs:1 capsegs:1 sides:24 height:12 radius:0.2 pos:[0,0,-32] wirecolor:red
			bot.mat = standard diffuse:yellow
			attach (bot = converttomesh bot) top
			bot
		)
		ground = mesh name:"ground" length:100 width:100 lengthsegs:10 widthsegs:10 wirecolor:green pos:[-50,-50,0]
		ground.mat = standard diffuse:green
		addmodifier ground (Noisemodifier fractal:on strength:[40,40,25] seed:seed_sp.value)
		scatterNodes ground brush method:method
	)
	
	on face_verts pressed do create method:#face_verts
	on up_front pressed do create method:#up_front
	on up_verts pressed do create method:#up_verts
	on face_front pressed do create method:#face_front
)
createdialog alignfaceDialog

no optimization, no beautification … just a basic idea only.

Thank you very much Denis for taking the time to compile this, I am going to try and use this as a reliable starting point on which to hopefully experiment and develop.

Hopefully I can also start to understand and utilise the matrix math Although I sucked at math in school), I am not familiar with things such a vector front, side and cross etc etc so I will make it a priority to learn those concepts.

Denis, I am specifically interested in automating distribution of modular assets over surfaces to help environment artists make the most of their assets. From your experience am i going about it the right way, the direction we have been exploring?

Thanks again

K

Page 2 / 2