I’m getting back into Codea and started to play with 3D. I thought I’d share this code for parametric surfaces. Comes with a demo Main and a simple lighting shader.
Probably a good idea to remove the AutoGist bits before running
Wow that looks really nice, almost look like jelly tots, well done
thank you for sharing. it’s great.
^:)^ ^:)^ ^:)^ ^:)^
Works fine on ipad1, ios5.1
Whoa, you’re right. Just updated Codea on my old iPad 1 to try it out. Takes a good bit longer on startup to create the meshes, but one could make divx and divy smaller in ParaSurf.init to reduce default mesh resolution.
@Damny could you add an option so one can see the vertex? A knid of ‘unsmooth’, or random face colors to see the faces?
@Jmv38 updated the gist with a quick hack that adds a “flat” attribute. If you set it before calling paramesh(), it’ll generate face normals that make the edges kind of visible with the lighting shader.
Random face colors should be easy enough to set after you have the mesh, mesh.size is the number of vertices, make a table that has this many colors in it with every three colors being the same random one.
I actually have a shader that can render as wireframe, but I suspect it will not work on your iPad 1. And it also needs some color manipulation…
@damny - I would love to learn how to include normals and diffuse light in shaders. What is the best way to learn the basics?
@Ignatz uh, looking at existing code is probably a good way Also any book on 3d programming, since normals are fairly essential. Unfortunately I don’t know one with examples that translate directly to Codea…
It’s actually not very complicated though. Normals are basically vectors that point outside/away from your mesh, and each vertex can have one of them. If you have direction vectors for two sides of a triangle, you can calculate the normal vector through the shared vertex as the cross product of the two direction vectors. That’s what the parametric surface code does.
In the vertex shader, you can access the normals stored in the mesh even though the editor doesn’t seem to know about it. If you pass them on to the fragment shader in a varying variable, they’ll get interpolated between vertices, so the fragment shader gets smooth normals. That’s why my previous change that just sets all three normals for each triangle to the same vector makes the edges visible, it simply prevents this smooth interpolation.
In the fragment shader you can then get some simple lighting by multiplying your color or light color with the dot product of the normal and the light direction. The dot product of two vectors will be closer to 1 with smaller angles between the vectors, so the intensity of the light will be greater when a fragment directly faces your light source. The shader I included in this example does some extra stuff like a specular highlight and some weird silhouette glow that I liked; the variable df is the diffuse light intensity according to this dot product.
@damny - thanks!
Would you agree that while Codea can do demos like yours above, it’s unrealistic to expect to able to go much further and include these kind of lighting effects in a game, because the iPad doesn’t have sufficient power? I say this because I’ve seen a lot of impressive small demos but nothing approaching a game, and my own efforts suggest it is difficult to achieve 3D scenarios at a decent frame rate.
I don’t know. I wrote this code for a modeler I’ve been playing with, and it’s certainly powerful enough to have a bunch of them on screen and let me move, rotate, scale and so on in real time. I suppose for an actual game you wouldn’t necessarily have to use meshes with this many triangles, on the other hand you would want to use textures…
I initially started working on a puzzle game where you push around objects made out of cubes and it was fast enough for that, but because it uses predominantly cubes it actually draws much less triangles than this demo does. I’ll find out once I make some models for it for the player character and some objects
Oooooooooooooo… Aaaaaaaaaahhhhhhhhh… I’m very impressed!! :-bd
@damny thanks for the hack! That was exactly what i wanted! Works great too.