Notifications
Clear all

[Closed] Custom Mesh: Window

Hi everyone

I wrote my first custom mesh plugin, a window suitable for Argumented Reality projects.
The plugin is working so far, but also it´s a really simple object the code is kinda complex/long.
So i wanted to ask if my workflow/code is ok or if i made it over-complicated?

Would be nice, if you could give me some input on how to improve the code or an insight on how you would write such a plugin.
I don´t care too much about the performance for now. I know i don´t have to build the mesh every time from scratch if i only move vertices.
What bothers me is, that it takes a lot of time and effort to extend the mesh.

For example:
I want to create a sill to the window. So basically just a box on the lower end.
If i create a box using “createinstance box” and combine the objects with “mesh = mesh + sill.mesh” it screws up the hidden edges.
So i have to set the vertices and create the faces by hand?

Looking forward to your comments.
Thanks a lot.

Here´s the code.


 plugin simpleObject WindowExt
     	name:"WindowExt"
     	classid:#(0x6add5341, 0x51246801)
     	category:"Standard Primitives"
     (
     	local firstClickPoint, secondClickPoint, thirdClickPoint 
     	local xVector,yVector,zVector --predeclare the 3 axes to keep the visible in all handlers
		local vert_array, face_array, edge_vis
		 parameters main rollout:window_params
		(
		pWidth type:#float ui:pWidth default:50
		pLength type:#float ui:pLength default:50
		pHeight type:#float ui:pHeight default:0
			
		frameWidth type:#float ui:frameWidth default:5
		frameDepth type:#float ui:frameDepth default:12
			
		railWidth type:#float ui:railWidth default:6
		railDepth type:#float ui:railDepth default:12
		
		)
		rollout window_params "Window"
		(
			spinner pLength"Length "range:[-1000,1000,0]
			spinner pWidth"Width "range:[-1000,1000,0]
			spinner pHeight "Height "range:[0,1000,1]
				
			spinner frameWidth "Frame Width "range:[0,1000,1]
			spinner frameDepth "Frame Depth "range:[0,1000,1]
				
			spinner railWidth "Rail Width "range:[0,1000,1]
			spinner railDepth "Rail Depth "range:[0,1000,1]
		)
		
		/* ##### FN ##### */
		fn makequad p1 p2 p3 p4=
		(
			append face_array [p1,p2,p3]
			append edge_vis #(false,true,false)
			append face_array [p1,p3,p4]
			append edge_vis #(false,true,true)
		)
		fn setEdgeVisibilty =
		(
			for f = 1 to face_array.count do --Edge Visibility
			(	
				setEdgeVis mesh f 1 edge_vis[f][1]
				setEdgeVis mesh f 2 edge_vis[f][2]
				setEdgeVis mesh f 3 edge_vis[f][3]
			)
		)
		fn buildWindow =
		(
				
			append vert_array	 [0,0,0]
			append vert_array	 [pLength,0,0]
			append vert_array	 [pLength,0,pHeight]
			append vert_array	 [0,0,pHeight]
			
			append vert_array	 [0+frameWidth,0,0+frameWidth]
			append vert_array	 [pLength-frameWidth,0,0+frameWidth]
			append vert_array	 [pLength-frameWidth,0,pHeight-frameWidth]
			append vert_array	 [0+frameWidth,0,pHeight-frameWidth]
			
			append vert_array	 [0+frameWidth,0+frameDepth,0+frameWidth]
			append vert_array	 [pLength-frameWidth,0+frameDepth,0+frameWidth]
			append vert_array	 [pLength-frameWidth,0+frameDepth,pHeight-frameWidth]
			append vert_array	 [0+frameWidth,0+frameDepth,pHeight-frameWidth]

			--front
			makequad  1 2 6 5 
			makequad 3 7 6 2
			makequad 4 8 7 3
			makequad 1 5 8 4
			--inner
			makequad 6 10 9 5
			makequad 7 11 10 6
			makequad 8 12 11 7
			makequad 9 12 8 5
			
			
			-- Frame
			rfWidth = railWidth + frameWidth
			append vert_array	 [0+rfWidth,0+frameDepth,0+rfWidth]
			append vert_array	 [pLength-rfWidth,0+frameDepth,0+rfWidth]
			append vert_array	 [pLength-rfWidth,0+frameDepth,pHeight-rfWidth]
			append vert_array	 [0+rfWidth,0+frameDepth,pHeight-rfWidth]
			
			rfDepth = railDepth + frameDepth
			append vert_array	 [0+rfWidth,0+rfDepth,0+rfWidth]
			append vert_array	 [pLength-rfWidth,0+rfDepth,0+rfWidth]
			append vert_array	 [pLength-rfWidth,0+rfDepth,pHeight-rfWidth]
			append vert_array	 [0+rfWidth,0+rfDepth,pHeight-rfWidth]
			
			--front 2
			makequad  10 14 13 9
			makequad 11 15 14 10
			makequad 12 16 15 11
			makequad 9 13 16 12
			
			--inner 2
			makequad  14 18 17 13
			makequad  20 16 15 19
			makequad 19 15 14 18
			makequad 16 13 17 20
				
			--glass
			makequad 20 17 18 19
			
		)
		fn buildSill =
		(
			return createinstance box
			
		)
		 /* ##### BUILD MESH ##### */
		 on buildMesh do
		(
			vert_array = #()
			face_array = #()
			edge_vis = #()
			
			buildWindow()
			--buildSill()
			
			setMesh mesh verts:vert_array faces:face_array
			setEdgeVisibilty()
			 
			--mesh = mesh + sill.mesh

		)--end buildMesh
		
		
		/* ##### CREATE #####*/
		tool create
     	(
     		on mousePoint click do
     		(
     			case click of
     			(
     				1: nodeTM.translation = firstClickPoint = gridPoint
     				2: secondClickPoint = gridPoint
     				3: thirdClickPoint = gridPoint
     				4: #stop
     			)
     		)	
     		on mouseMove click do
     		(
     			case click of
     			(	
     				2: (
     					xVector = normalize (gridPoint - firstClickPoint) --X is defined by the first click and the current mouse position
     					zVector = nodeTM.row3 --Z is defined by the local Z of the box
     					yVector = normalize (cross zVector xVector) --Y is the cross product of Z and X
     					nodeTM = matrix3 xVector yVector zVector firstClickPoint
     					pLength =  length (gridPoint - firstClickPoint) --the width is the distance from first click to mouse
     					pWidth = pHeight = 0 --the length and height will be zero for now
     				)
     				3: (
     					local theLengthVector = gridPoint-secondClickPoint --this is the vector from the second click to the current mouse position
						pWidth = abs (dot theLengthVector yVector) --set the length of the box to the length of the projection of the above vector on the y axis of the box
     					local theMulti = if dot (normalize theLengthVector) yVector < 0 then -1 else 1 --depending on the sign of the dot product, flip the sign of the multiplier
						pWidth = pWidth*theMulti
						--nodeTM = matrix3 xVector yVector zVector (firstClickPoint+yVector)
					)
						4: (
							pHeight = gridDist.z  --set the height to the Z distance to the grid
						)
     			)	
     		)
     	)	

     )



2 Replies

Thanks for the link.
The PicFrame is a really nice plugin and way more advanced than mine.
I´m sure i will learn a lot from it.