Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Roll your own lighting shader with this code

IgnatzIgnatz Mod
edited January 2014 in General Posts: 5,396

I just shared a project on CC that includes a shader with a lot of lighting effects. Normally, if you do this, you slow down your app because of all the if tests you have to do.

But this project creates special shader code with just the features you want, ie it leaves out the parts you don't want, to make the shader run faster. You can copy this code to your own project.

There is a Notes tab to explain how to use the code, and what the different parameters mean.

(I used Reflector to record this, because it gives you the whole iPad screen, and I played with adding captions on this video, using youTube's editing options).

And before Jmv38 asks, here is an alternative link:


  • Very nice. I particularly like that you are using this to generate a shader with just what you need.

  • SimeonSimeon Admin Mod
    Posts: 5,434

    That's really clever, dynamically building a shader. The results look great too.

  • Posts: 1,595

    Nice, fogs look really good as well, great way of building the right lighting environment!

  • IgnatzIgnatz Mod
    Posts: 5,396

    @spacemonkey - you got me started with shaders - and I still haven't got my head around bump mapping!

  • Posts: 505

    Nice idea. Just a black screen when using Codea beta though. I haven't figured out why..

  • edited January 2014 Posts: 372

    That is really nice... Will be useful.

    I couldn't find it on CC, so just copied from the gist.

  • @Ignatz - Yeah, if I can find the time and spare mental capacity to get back into some Codea I was thinking of looking at what you've built and how bump mapping could be worked into it.

  • Posts: 2,820

    Very nice! Thank you.

  • SimeonSimeon Admin Mod
    edited January 2014 Posts: 5,434

    @tnlogy @Ignatz it does not work in the beta due to a shader compile error — the variable reflect which you define is actually a new GLSL function in OpenGL ES 3.0, and GLSL complains that your use is a redefinition. If you rename it to something else then it works.

    Edit: corrected OpenGL version

  • edited January 2014 Posts: 455

    Hang about, so IOS 7 now gives us OpenGL ES 3.0, and that maybe opens the door to textures in vertex shaders, I HAVE to get Codea back out and try and play with this ;-)

    Dynamicly deformed meshes from a texture... I guess it breaks compatibility with IOS 6, but still.

    Edit: I'm sad again
    Suggests it's only latest gen ipads that support 3.0...

    ReEdit: although the same page in the OpenGL ES 2.0 on Power VR suggests maybe you can now have textures in the vertex... I'll have to have a look.

  • Jmv38Jmv38 Mod
    Posts: 3,297

    @ignatz lol! Thanks. Looks really good!

  • BriarfoxBriarfox Mod
    Posts: 1,542

    @ignatz This is very cool, great idea.

  • IgnatzIgnatz Mod
    Posts: 5,396

    I updated to fix the problem with the name reflect, affecting 2.0 beta versions

    Also it wasn't sharing on CC, fixed now

  • edited January 2014 Posts: 455

    Textures in Vertex shaders do now work, I've only tested on ipad 3rd gen, but it should work on any ipad as long as it's IOS 7.

    Project is on CC called texture driven hieghtfield. Basically it's a flat mesh of 20,000 triangles, and dependant on the texture it will height it based on the "brightness" of the pixel for the vertex. For real dynamic, you can even point the camera at it.

    The lighting is a bit shabby because I used @Ignatz solution from above, but I think I haven't plumbed it in correctly ;-)

  • Posts: 505

    Nice! I'm using perlin noise to make an infinite landscape. should be able to speed it up by saving the height map instead of calculating it for each draw..

  • IgnatzIgnatz Mod
    Posts: 5,396

    @tnlogy - I'm interested in that, would appreciate seeing it if possible ;;)

  • Posts: 1,976

    Aw, I had dynamically built shaders in my projects... You beat me to posting it. :(

  • IgnatzIgnatz Mod
    Posts: 5,396


  • IgnatzIgnatz Mod
    Posts: 5,396

    @tnlogy - thanks, I'll have a look

  • My height field code is a bit rubbish, mainly in terms of the recalculating normals which I only half did as I only had time to prove the concept to myself. But hopefully it gives a good feel for what can be done.

    Textures in vertex shaders are commonly used for height fields, and also for things like waves on water effects and such. It should also be usable for user deformable objects, so you could let a user dent your mesh by taping it by just updating a texture for their touch.

  • Posts: 505

    I guess you could save the normal as RGB for each pixel so save some GPU? Is there a list of new features for GLSL in iOS 7?

  • IgnatzIgnatz Mod
    Posts: 5,396

    @tnlogy - I modified your shader to take a texture. There are two issues I'd like to fix. One is that the furthest vertices flicker, and the other is that additional "tiles" are added periodically as you move forward, and it is very obvious when it happens. I'd like it to be more subtle.

  • Posts: 2,820

    I finally got a chance to try it. The printing the shader option doesn't seem to be working (I'm running the beta). Other than that, it seems very useful.

  • Posts: 505

    Cool, looking forward to seeing your code. :)

    Yes, I've seen those issues as well, haven't had time to fix them though, was thinking about adding a fog to solve it. I don't know if my light shader code is so elegant either. :)

    Did you see any speed improvements?

  • @tnlogy - my understanding is that IOS 7 introduces OpenGL ES 3.0 which has some new features, and also includes a bunch of capabilities which (like compressed textures) which were optional in the previous version.

    HOWEVER, OpenGL ES 3.0 is only usable on the latest "Air" versions of Ipads (and I'm not sure how Codea works with this), however IOS 7 also made available some capabilities to OpenGL ES 2.0 that were previously disabled, the only one I've used/looked into being textures in the vertex shaders.

Sign In or Register to comment.