4/1/2014
Mesh Tutorial - Codea
Discussions
Sign In
Mesh Tutorial
Vega June 2012
Posts: 179
General
Howdy, Stranger!
It looks like you're new here. If you want to get involved, click one of these buttons!
Hello, after I had a little trouble grasping the concepts of how meshes work and how to texture them (and subsequently receiving help from @Andrew_Stacey and @Shrike) I decided to create a tutorial on the basics of meshes. If you have time, please read it over and see where I have failed to explain things clearly, or where I give bad info. Once I get corrections made, I will add it to the wiki: Mesh tutorial In this tutorial, I am going to talk about what meshes are, why you might want to use them, and how to use them in Codea. To complete this tutorial, you probably want to have a basic understanding of Codea and the LUA language. Let's call the level of difficulty "intermediate." What Meshes Are First off, let's talk about what a mesh is. You might think of a mesh as a 3D object, and meshes ARE a basic part of 3D objects. But a mesh could more accurately be defined as as a list of polygons, and more specifically, in the case of Codea, it is a list of triangles. These lists of polygons are very useful for both 2d and 3d game creation. Basically any 2d or 3d shape can be broken up into a bunch of triangles. Generally, the more polygons used, the smoother the shape.
Sign In Apply for Membership
Categories
All Discussions General Code Sharing Questions Shaders Suggestions Bugs Wiki Examples Competition
4,338 1,671 598 1,301 39 331 242 8 110 38
In this Discussion
In modern 3d games, the models are composed of thousands (or tens of thousands) of polygons. To accomplish this feat, the iPad graphics hardware has been designed to manipulate and draw triangles extremely fast, much faster than just drawing pixels to the screen. By using meshes in Codea rather than using the sprite command, you can often drastically speed up the rendering in your game. Your first mesh To create a mesh you simply run the built in mesh function, which returns an empty mesh.
m y M e s h=m e s h ( )
Andrew_Stacey bee Fred John Keebo mpilgrem Reefwing shrike Simeon toadkick Toshio Vega Two Lives Left on Twitter
June 2012 June 2012 June 2012 June 2012 June 2012 June 2012 June 2012 June 2012 June 2012 June 2012 June 2012 June 2012
That mesh is pretty worthless without any triangles, so let's add a triangle to it. Here is a diagram of the first mesh we are going to create:
[Link]
1/11
4/1/2014
Mesh Tutorial - Codea
Two Lives Left on Facebook Codea Wiki Codea Development on Twitter
And here is the code to add that triangle to our mesh:
m y M e s h . v e r t i c e s={ v e c 2 ( 0 , 0 ) , v e c 2 ( 1 0 0 , 0 ) , v e c 2 ( 0 , 1 0 0 ) }
And now we have a functional mesh. Let's make a quick program to test it out:
f u n c t i o ns e t u p ( ) m y M e s h=m e s h ( ) m y M e s h . v e r t i c e s={ v e c 2 ( 0 , 0 ) , v e c 2 ( 1 0 0 , 0 ) , v e c 2 ( 0 , 1 0 0 ) } e n d f u n c t i o nd r a w ( ) b a c k g r o u n d ( 4 0 ,4 0 ,5 0 ) m y M e s h : d r a w ( ) e n d
Run this and you will see our grey triangle appear in the bottom left hand corner of the screen at the origin. The triangle is rendered in grey because we have not given it any color or texture. Let's try giving it color first, and we will graduate to textures later. Coloring Meshes To add color to a mesh you pass a list of colors to the mesh that is equal in length to the number of vertices. This means that for every vertex in the list, you must provide a color. Our mesh currently has 1 triangle and therefore 3 vertices, so we need to give the mesh 3 colors. Here is the updated program with colors added:
f u n c t i o ns e t u p ( ) r e d=c o l o r ( 2 5 5 ,0 ,0 ,2 5 5 ) g r e e n=c o l o r ( 0 ,2 5 5 ,0 ,2 5 5 ) b l u e=c o l o r ( 0 ,0 ,2 5 5 ,2 5 5 ) m y M e s h=m e s h ( ) m y M e s h . v e r t i c e s={ v e c 2 ( 0 , 0 ) , v e c 2 ( 1 0 0 , 0 ) , v e c 2 ( 0 , 1 0 0 ) } m y M e s h . c o l o r s={ r e d ,g r e e n ,b l u e } e n d f u n c t i o nd r a w ( ) b a c k g r o u n d ( 4 0 ,4 0 ,5 0 ) m y M e s h : d r a w ( ) e n d
Now our mesh is rendered in color, and we get a pretty neat gradient effect as the colors are blended between the vertices. You can use this technique to create gradient backgrounds, buttons, etc. Texturing Meshes Texturing meshes works a lot like coloring meshes, but instead of passing a list of colors, we will pass a list of texture coordinates. This is a process called U,V mapping . The x,y coordinate of each vertex is mapped to a coordinate on the texture, which is called the u,v coordinate, since x and y are already in use. (Hence the name U,V mapping) With the U,V coordinates, we use a "unit-square" system, which allows us to specify what portion of the texture we want to use for the triangle, instead of the exact pixels. (Don't worry, this will be explained more clearly as you follow this example.) This is a diagram of the unit square:
[Link]
2/11
4/1/2014
Mesh Tutorial - Codea
If you want to use the full width of the image, you would use 1 as the width. To use half of the width, you would use .50 as the width. Let's take an image from the Codea spritepacks and use it as our texture. First get rid of the color code as we are now texturing instead. Next we want to make a triangle in U,V coordinates that matches our mesh triangle. It will look something like this:
So the texture coordinates for our triangle are going to be: 0,0 1,0 0,1 Can you see how those relate to our vertices? Remember they are: 0,0 100,0 0,100 Here is the updated code:
f u n c t i o ns e t u p ( ) i m g=r e a d I m a g e ( " P l a n e tC u t e : I c o n " )G e tt h ei m a g e m y M e s h=m e s h ( ) m y M e s h . v e r t i c e s={ v e c 2 ( 0 , 0 ) , v e c 2 ( 1 0 0 , 0 ) , v e c 2 ( 0 , 1 0 0 ) } m y M e s h . t e x t u r e=i m gS e tt h ei m a g ea st e x t u r e m y M e s h . t e x C o o r d s={ v e c 2 ( 0 , 0 ) , v e c 2 ( 1 , 0 ) , v e c 2 ( 0 , 1 ) } e n d f u n c t i o nd r a w ( ) b a c k g r o u n d ( 4 0 ,4 0 ,5 0 ) m y M e s h : d r a w ( ) e n d
And if done correctly, we should now have half of the image drawn on our triangle. To see the rest of this image, I would really like to have another triangle to finish the rectangle. So, looking at this image:
[Link]
3/11
4/1/2014
Mesh Tutorial - Codea
This image has been resized to fit in the page. Click to enlarge.
You should see that the triangle we need to add to our mesh has the vertices: 0,100 100,100 100,0 And the texture coordinatesfor that new triangle will be: 0,1 1,1 1,0. Lets add them to our code:
f u n c t i o ns e t u p ( ) i m g=r e a d I m a g e ( " P l a n e tC u t e : I c o n " )G e tt h ei m a g e m y M e s h=m e s h ( ) m y M e s h . v e r t i c e s={ v e c 2 ( 0 , 0 ) , v e c 2 ( 1 0 0 , 0 ) , v e c 2 ( 0 , 1 0 0 ) , v e c 2 ( 0 , 1 0 0 ) , v e c 2 ( 1 0 0 , 1 0 0 ) , v e c 2 ( 1 0 0 , 0 ) } m y M e s h . t e x t u r e=i m gS e tt h ei m a g ea st e x t u r e m y M e s h . t e x C o o r d s={ v e c 2 ( 0 , 0 ) , v e c 2 ( 1 , 0 ) , v e c 2 ( 0 , 1 ) , v e c 2 ( 0 , 1 ) , v e c 2 ( 1 , 1 ) , v e c 2 ( 1 , 0 ) } e n d f u n c t i o nd r a w ( ) b a c k g r o u n d ( 4 0 ,4 0 ,5 0 ) m y M e s h : d r a w ( ) e n d
And Voila, we have our image drawn correctly to a mesh. Now that we have learned how to draw a rectangular texture onto a mesh the hard way, lets look at a slightly simplified way of completing that same task.
Vega June 2012
Posts: 179
The Rectangle Functions It is very common to want to use rectangles in your meshes like we did above. To make it easier on us, Codea has a mesh function called addRect. addRect automatically adds two right(angle) triangles to your mesh that together make a rectangle. It can also automatically create the proper texture coordinates. To replicate the work we did above, we could use:
m y M e s h : a d d R e c t ( 5 0 , 5 0 , 1 0 0 , 1 0 0 )
Much easier, huh? The reason we used 50,50 there instead of 0,0 is because the addRect function creates the mesh CENTERED on the x,y coordinate given. So the following code could do what we did above:
f u n c t i o ns e t u p ( ) i m g=r e a d I m a g e ( " P l a n e tC u t e : I c o n " ) m y M e s h=m e s h ( ) m y M e s h . t e x t u r e=i m g m y M e s h : a d d R e c t ( 5 0 , 5 0 , 1 0 0 , 1 0 0 ) e n d f u n c t i o nd r a w ( ) b a c k g r o u n d ( 4 0 ,4 0 ,5 0 ) m y M e s h : d r a w ( ) e n d
You can also adjust the texture on rectangles. This is especially useful if you want a mesh with one texture stretched over a grid of rectangles. When you create a new rectangle using addRect, the function will return a unique identifier for that rectangle that can be used to modify the texturing (u,v mapping) of the rectangle. You do it like this:
i d x=m y M e s h : a d d R e c t ( 5 0 , 5 0 , 1 0 0 , 1 0 0 ) m y M e s h : s e t R e c t T e x ( i d x ,0 ,0 ,1 ,1 )
[Link]
4/11
4/1/2014
Mesh Tutorial - Codea
The arguments you pass to the setRectTex function are: (index, u, v, width, height) So if I want to only use the bottom-left 1/4th of the image as my texture I would do so like this:
m y M e s h : s e t R e c t T e x ( i d x , 0 , 0 , . 5 0 , . 5 0 )
And if I wanted to use the top left 1/4 of the image I would use:
m y M e s h : s e t R e c t T e x ( i d x , 0 , . 5 0 , . 5 0 , . 5 0 )
When creating rectangular meshes, it is generally much easier to use the rectangle functions. If you are just using meshes to draw sprites quickly on the screen, then using addRect will likely be much more user-friendly than creating a list of vertices and creating a complimentary texture coordinate list. To be continued... In a future tutorial I will cover moving meshes into 3d and using indexed colors. If there are other topics concerning meshes you would like me to explain, please let me know.
shrike June 2012
Posts: 121
Hi, great job @Vega. One more important thing about addRect and other rect functionalities: addRect is just a short way to add 6 vertex (so 2 triangles). If you use addRect to create your mesh, the return value is nothing more then a progressive value and you can use it also to index specific vertex, knowing that rect with idx = i has vertices with idx from (i-1)6 + 1 to (i-1)6 + 6 I hope to remember right (please have a test...) that the rect should be formed in this way:
v 1 = v 4 -v 6 | | | \ \ | | \ |
v 2 v 3 = V 5
Moreover pay attention because if you first set directly the vertex buffer (like in the beginning of your tutorial) and then you call an addRect, the vertex buffer gets cleared and reset, so the addRect set the first 6 vertices and returns 1 as rect idx (or at least is what it seems to me after some try, maybe better to ask to @Simeon or @Andrew_Stacey about that!) Last thing: you can't remove vertices from a mesh, if you really need that, the only way is to clear and set a new vertex buffer.
Reefwing June 2012
Posts: 558
Brilliant - fantastic tutorial @Vega.
Andrew_Stacey June 2012
Posts: 2,075
(Add it to the wiki!) Didn't spot anything wrong. Some ideas for expansion (trying to keep to the same level): 1. You can set a uniform colour all in one go. 2. Order matters - experiment to be sure, but I think the texture has to be set before the texture coords, and a global colour only affects previously declared coords. 3. You can set colours and textures, in which case the colours tint the texture.
Simeon June 2012
Posts: 3,435
Edit: I've added links to the WIki and Issue Tracker at the top of the forums. Hopefully they will be more accessed now. Amazing post, @Vega. Thank you for sharing what you learned. I second adding it to the wiki. You can't remove vertices from a mesh, but you can effectively remove triangles by collapsing their vertices they will still be "in" the mesh, but won't be rendered. If you plan to add and remove a lot of triangles (for example, in a particle system), it's best to allocate a whole bunch of vertices and then arrange them into triangles when you need them, and collapse them when you don't. (Vertices are cheap to pass to the GPU, so this is more cost effective than re-allocating your mesh all the time.)
[Link]
5/11
4/1/2014
Mesh Tutorial - Codea
Vega June 2012
Posts: 179
Thank you for the feedback. I will add a few things and revise a bit and post it to the wiki. I will continue spelling it color, however, as I am from USA. Sorry, people from the rest of the world.
Fred June 2012
Posts: 383
This is extremely well-written, @Vega! You've made sense of it for me, and this will be a great addition to the wiki.
Simeon June 2012
Posts: 3,435
@Vega color is good. I'd rather keep everything consistent with US spelling where possible. Especially as that's the name of the type.
Vega June 2012
Posts: 179
Finally got it added to the Wiki in the tutorials section just below the tutorial on vec2. Not sure if that is the best place, feel free to move it if you like.
Toshio June 2012
Posts: 11
Thank you very much, Vega. I finally can try to mess with mesh. Played with it a lil bit and got the following that lets you add a triangle to a mesh every three points you touch... Kinda... (altho in between it does some weird stuff...)
f u n c t i o ns e t u p ( ) t a p=f a l s e m=m e s h ( ) p t 1=v e c 2 ( 0 , 0 ) p t 2=v e c 2 ( 0 , 0 ) p t 3=v e c 2 ( 0 , 0 ) e n d f u n c t i o nd r a w ( ) i fC u r r e n t T o u c h . s t a t e= =B E G A Na n dn o tt a pt h e n t a p=t r u e e n d i ft a pt h e n i fC u r r e n t T o u c h . s t a t e= =E N D E Dt h e n p t 1=p t 2 p t 2=p t 3 p t 3=v e c 2 ( C u r r e n t T o u c h . x ,C u r r e n t T o u c h . y ) i=m : a d d R e c t ( p t 1 . x ,p t 1 . y ,p t 2 : d i s t ( p t 3 ) , 0 ) m : v e r t e x ( i ,p t 3 ) m : c o l o r ( i ,m a t h . r a n d o m ( 2 5 5 ) ,m a t h . r a n d o m ( 2 5 5 ) ,m a t h . r a n d o m ( 2 5 5 ) ,m a t h . r a n d o m ( 2 5 5 ) ) t a p=f a l s e
e n d e n d b a c k g r o u n d ( 5 3 ,5 3 ,5 3 ,2 5 5 ) m : d r a w ( ) f i l l ( 2 3 1 ,2 3 1 ,4 1 ,2 5 5 ) e l l i p s e ( p t 1 . x ,p t 1 . y ,2 0 ) e l l i p s e ( p t 2 . x ,p t 2 . y ,2 0 ) e l l i p s e ( p t 3 . x ,p t 3 . y ,2 0 ) e n d
[Link]
6/11
4/1/2014
Mesh Tutorial - Codea
bee June 2012
Posts: 333
Great tutorial, @vega. Suggestion: please add screenshot for every given example. That way, the learners would know whether his/her code is correct or not. Thank you. Looking forward for the advance chapters.
Vega June 2012
Posts: 179
That seems to work well, @Toshio. You may have noticed that there is currently no function for addTriangle, which I believe would be a great idea for a future addition. (@Simeon, what do you think?) To add a triangle you would code something like this:
v e r t s=m y M e s h . v e r t i c e s t a b l e . i n s e r t ( v e r t s ,n e w V e c 2 a ) t a b l e . i n s e r t ( v e r t s ,n e w V e c 2 b ) t a b l e . i n s e r t ( v e r t s ,n e w V e c 2 c ) m y M e s h : c l e a r ( ) m y M e s h . v e r t i c e s=v e r t s
That is a little slow however, because you are clearing and recreating the mesh each time you add a triangle.
toadkick June 2012
Posts: 572
Not sure a function to create a triangle would actually be all that useful. For a rect it's great, because you only need to specify x, y, w, and h, but there's really no getting around the fact that you need to specify 3 points for a triangle.
Simeon June 2012
Posts: 3,435
@Vega we thought about it, but as @toadkick mentioned, you would need up to 9 arguments to specify the triangle's vertices. I felt that this was more neatly done over multiple lines. In addition, you would not be able to use addTriangle and addRect reliably together - as addTriangle would break the returned rect indices. Still I agree in principle that there needs to be a better way to add triangles.
mpilgrem June 2012
Posts: 487
Thank you @Vega for a very helpful guide. Drawing the two meshes side by side - the two triangles ( m y M e s h 1 ) and the rectangle ( m y M e s h 2 ) - I noticed that the first was dull and the second bright. Picking up what @Andrew_Stacey explains about colours tinting textures, it seems to me that a default grey tints one mesh, but not the other. (Adding m y M e s h 1 : s e t C o l o r s ( 2 5 5 ,2 5 5 ,2 5 5
,2 5 5 ) after the vertices have been set clears the dullness.)
mpilgrem June 2012
Posts: 487
The . v e r t i c e s syntax for the m e s h ( ) userdata follows the Lua syntax for setting a field of a table:
v e r t e x T a b l e={[ [t a b l eo fv e c 2o rv e c 3] ]} m=m e s h ( ) m [ " v e r t i c e s " ]=v e r t e x T a b l e
I am correct to understand, however, it is more akin to a function ( m : v e r t i c e s ( v e r t e x T a b l e ) ), because to change the vertices you cannot simply update the table referenced (in my example) by v e r t e x T a b l e )?
Vega June 2012
Posts: 179
I'm not sure exactly how it works, but it seems you do have to set the whole table at once. In my. experiments I tried to change a single vertex like
m [ " v e r t i c e s " ] [ 1 ]=v e c 2 ( 2 0 0 , 2 0 0 )
And that does not work. You must set the entire vertices table at once. So you would have to do
[Link]
7/11
4/1/2014
v e r t e x T a b l e [ 1 ]=v e c 2 ( 2 0 0 , 2 0 0 ) m [ " v e r t i c e s " ]=v e r t e x T a b l e
Mesh Tutorial - Codea
Maybe I should take the time to look at the API to see how it really works.
toadkick June 2012
Posts: 572
mesh has a method called vertex() that allows you to set/get the position of a vertex. For example,
m y M e s h : v e r t e x ( 1 ,1 0 0 ,1 0 0 )
will set vertex 1's position to (100,100). Calling it with just the index will return the vertex's position at that index as a vec3. Now, what I'm not sure of at the moment is whether or not you can do that without first adding the verts by assigning them to the .vertices field or by using addRect, but I do know for sure that once the verts are there they can be modified using the vertex() method. There are also color() and texCoord() methods that allow you to modify/get the color/texture coordinates in the same way
mpilgrem June 2012
Posts: 487
The m : v e r t e x ( i n d e x ,. . . ) function reports an error if the index is out of bounds. I was considering changing metatables to get a different behaviour, starting with this experiment:
-R e w i r et h ef u n c t i o n a l i t y . . . l o c a lm m t=g e t m e t a t a b l e ( m e s h ( ) ) l o c a lo l d m e s h n e w i n d e x=m m t [ " _ _ n e w i n d e x " ] m m t [ " _ _ n e w i n d e x " ]=f u n c t i o n ( m 1 ,k 1 ,v 1 ) i fk 1 = = " v e r t i c e s "t h e n l o c a lv m t 1=g e t m e t a t a b l e ( v 1 )o r{ } l o c a lo l d v 1 n e w i n d e x=v m t 1 [ " _ _ n e w i n d e x " ]o rr a w s e t v m t 1 [ " _ _ n e w i n d e x " ]=f u n c t i o n( t 2 ,k 2 ,v 2 ) o l d v 1 n e w i n d e x ( t 2 ,k 2 ,v 2 ) m 1 . c l e a r ( ) m 1 . v e r t i c e s=t 2 e n d s e t m e t a t a b l e ( v 1 ,v m t 1 ) e n d o l d m e s h n e w i n d e x ( m 1 ,k 1 ,v 1 ) r e t u r n e n d
-E x a m p l eo ft h er e w i r e dm e s hu s e r d a t a f u n c t i o ns e t u p ( ) m=m e s h ( ) v e r t e x T a b l e={ v e c 2 ( 0 , 0 ) ,v e c 2 ( 1 0 0 , 0 ) } m . v e r t i c e s=v e r t e x T a b l e p r i n t ( " S i z eo fm e s h : " ,m . s i z e )-O u t p u t s2 v e r t e x T a b l e [ # v e r t e x T a b l e+1 ]=v e c 2 ( 0 ,1 0 0 )-B u tt a b l e . i n s e r t ( )d o e sn o tw o r k . . . p r i n t ( " N e ws i z eo fm e s h : " ,m . s i z e )-O u t p u t s3 e n d f u n c t i o nd r a w ( ) b a c k g r o u n d ( 4 0 ,4 0 ,5 0 ) e n d
but it would need more code to avoid changes to the table forever affecting the mesh userdata that it has now become associated with.
toadkick June 2012
Posts: 572
@mpilgrem: that's what I figured, and it makes sense. [Link]() won't work because under the hood I'm sure that mesh just uses a straightforward memory buffer (don't have the code in front of me at the moment, but I'll bet it's a std::vector). It might be cool to have an addVertex() method to tack on a vertex to the end of the existing buffer, and/or an insertVertex() method that would shift higher indexed vertices up (though that would be a bit expensive). Dunno, I'll have to think about it some more. Not sure that there are enough common use cases to warrant the API
[Link]
8/11
4/1/2014
additions.
Mesh Tutorial - Codea
mpilgrem June 2012
Posts: 487
Continuing my experiments in how Lua can be used to rewire built-in functionality, the following code rewires r a w s
e t, t a b l e . i n s e r t and the m e s h userdata to make m . v e r t i c e s=v e r t e x T a b l e behave like its syntax
implies - linking the table with the mesh userdata.
-R e w i r er a w s e t ( ) l o c a lo l d r a w s e t=r a w s e t r a w s e t=f u n c t i o n( t ,k ,v ) l o c a lr=o l d r a w s e t ( t ,k ,v ) l o c a lm t=g e t m e t a t a b l e ( t )o r{ } l o c a lm m=m t [ " _ _ l i n k e d T o M e s h " ] -I st h et a b l el i n k e dt om e s h e s ? i fm mt h e n -U p d a t ee a c hm e s h f o r_ ,mi np a i r s ( m m )d o m . c l e a r ( ) m . v e r t i c e s=t e n d e n d r e t u r nr e n d -R e w i r et a b l e . i n s e r t ( ) l o c a lo l d t a b l e i n s e r t=t a b l e . i n s e r t t a b l e [ " i n s e r t " ]=f u n c t i o n( t ,a 1 ,a 2 ) i fa 2t h e n o l d t a b l e i n s e r t ( t ,a 1 ,a 2 ) e l s e o l d t a b l e i n s e r t ( t ,a 1 ) e n d l o c a lm t=g e t m e t a t a b l e ( t )o r{ } l o c a lm m=m t [ " _ _ l i n k e d T o M e s h " ] -I st h et a b l el i n k e dt om e s h e s ? i fm mt h e n -U p d a t ee a c hm e s h f o r_ ,mi np a i r s ( m m )d o m . c l e a r ( ) m . v e r t i c e s=t e n d e n d e n d -R e w i r e_ _ n e w i n d e xo fm e s hu s e r d a t a l o c a lm m t=g e t m e t a t a b l e ( m e s h ( ) ) l o c a lo l d m e s h n e w i n d e x=m m t [ " _ _ n e w i n d e x " ] l o c a ll i n k e d V T={ } m m t [ " _ _ n e w i n d e x " ]=f u n c t i o n ( m 1 ,k 1 ,v 1 ) i fk 1 = = " v e r t i c e s "t h e n l o c a lV T=l i n k e d V T [ m 1 ] -I sav e r t e xt a b l el i n k e dt om e s h ? i fV Tt h e n -C l e a rt h et a b l e ' sl i n kt ot h em e s h l o c a lV T m t=g e t m e t a t a b l e ( V T ) V T m t [ " _ _ l i n k e d T o M e s h " ] [ m 1 ]=n i l e n d -L i n kt h ev e r t e xt a b l et ot h em e s h l i n k e d V T [ m 1 ]=v 1 l o c a lv m t 1=g e t m e t a t a b l e ( v 1 )o r{ } v m t 1 [ " _ _ n e w i n d e x " ]=v m t 1 [ " _ _ n e w i n d e x " ]o rr a w s e t -A d dal i n kt h em e s ht ot h et a b l e v m t 1 [ " _ _ l i n k e d T o M e s h " ]=v m t 1 [ " _ _ l i n k e d T o M e s h " ]o r{ } v m t 1 [ " _ _ l i n k e d T o M e s h " ] [ m 1 ]=m 1 s e t m e t a t a b l e ( v 1 ,v m t 1 ) e n d o l d m e s h n e w i n d e x ( m 1 ,k 1 ,v 1 ) r e t u r n e n d
[Link]
9/11
4/1/2014
-E x a m p l eo fu s e f u n c t i o ns e t u p ( ) m 1=m e s h ( ) m 2=m e s h ( ) v e r t e x T a b l e 1={ v e c 2 ( 0 , 0 ) ,v e c 2 ( 1 0 0 , 0 ) ,v e c 2 ( 0 ,1 0 0 ) } -L i n ko n ev e r t e xt a b l et ot w om e s h e s m 1 . v e r t i c e s=v e r t e x T a b l e 1 m 2 . v e r t i c e s=v e r t e x T a b l e 1 p r i n t ( " S i z eo fm e s h1 : " ,m 1 . s i z e )-O u t p u t s3 p r i n t ( " S i z eo fm e s h2 : " ,m 2 . s i z e )-O u t p u t s3 v e r t e x T a b l e 1 [ # v e r t e x T a b l e 1+1 ]=v e c 2 ( 0 ,1 0 0 ) v e r t e x T a b l e 1 [ # v e r t e x T a b l e 1+1 ]=v e c 2 ( 1 0 0 ,1 0 0 ) v e r t e x T a b l e 1 [ # v e r t e x T a b l e 1+1 ]=v e c 2 ( 1 0 0 ,0 ) p r i n t ( " N e ws i z eo fm e s h1 : " ,m 1 . s i z e )-O u t p u t s6 p r i n t ( " N e ws i z eo fm e s h2 : " ,m 2 . s i z e )-O u t p u t s6 v e r t e x T a b l e 2={ } -L i n kan e wv e r t e xt a b l et oo n eo ft h em e s h e s m 1 . v e r t i c e s=v e r t e x T a b l e 2 t a b l e . i n s e r t ( v e r t e x T a b l e 2 ,v e c 2 ( 1 0 0 ,0 ) ) t a b l e . i n s e r t ( v e r t e x T a b l e 2 ,v e c 2 ( 2 0 0 ,0 ) ) t a b l e . i n s e r t ( v e r t e x T a b l e 2 ,v e c 2 ( 1 0 0 ,1 0 0 ) ) p r i n t ( " F i n a ls i z eo fm e s h1 : " ,m 1 . s i z e )-O u t p u t s3 p r i n t ( " F i n a ls i z eo fm e s h2 : " ,m 2 . s i z e )-O u t p u t s6 e n d f u n c t i o nd r a w ( ) b a c k g r o u n d ( 4 0 ,4 0 ,5 0 ) e n d
Mesh Tutorial - Codea
Simeon June 2012
Posts: 3,435
@mpilgrem from a glance at the code, that will clear the mesh every time the vertex table is modified? I think it's a great piece of code especially if you use it to set up a fairly static mesh. But for dynamic meshes you might not want to do that (if they get large).
mpilgrem June 2012
Posts: 487
@Simeon, you're absolutely right, of course. I should have made clear that - currently - extending a general mesh userdata's vertices, once they have been set initially, will involve an inefficient 'reset', no matter what syntactic approach is adopted. I was experimenting with syntax, really - rather than proposing something useful for dynamic situations.
John June 2012
Posts: 306
Looks interesting. What you could do is have a flag that is set when you modify the table, and alter mesh's draw function to copy the vertices over and reset the flag before drawing.
mpilgrem June 2012
Posts: 487
I have added this page to the wiki. My aim was to provide reference-type material to complement @Vega's tutorial, and add to what is set out in the in-app reference.
Vega June 2012
Posts: 179
Good info there, @mpilgrem. I wish I had that when I was learning about Meshes.
Keebo June 2012
Posts: 115
Nice work @Vega and @mpilgrem.
[Link]
10/11
4/1/2014
Learning is getting easier with everyone's help. Thanks a lot.
Mesh Tutorial - Codea
Add a Comment
Powered by Vanilla
[Link]
11/11