My Grass simulation with vertex shaders

@spacemonkey

You were describing a method to do 3d picking on a plane in this post.
Following your idea, here are the functions, it’s working good for me, now I can touch the grass;)

Function uvTex create the texture. It take a little bit, loading a png would be quicker but i prefered sharing the creation code. The blue channel enable better precision.

Function getUV return the UV for a given picture (context)
Hope it will help!


function uvTex()
    local tex=image(1024,1024)
    setContext(tex)
    for i=1,1024 do
        local r=math.fmod(i,256)
        for j=1,1024 do
            g=math.fmod(j,256)
            b=(math.floor(i/256)+4*math.floor(j/256))*16
            col=color(r,g,b,255)
            tex:set(i,j,col)
        end
    end
    return tex
end

function col2uv(r,g,b)
    local xb=math.fmod(b/16,4)
    local yb=math.floor(b/64)
    local U=(xb*256+r)/1024
    local V=(yb*256+g)/1024
    return vec2(U,V)
end

function getUV(ima)
    local x=CurrentTouch.x
    local y=CurrentTouch.y
    local v=vec2(0,0)
    if (x<1 or x>=ima.width or y<1 or y>ima.height) then 
    return v
    else
        r,g,b=ima:get(x,y)
        if (r==0 and g==0 and b==0) then 
            return v
        else
            v=col2uv(r,g,b)
            return v
        end
    end
end

Updated my version, tweaked colors and noise levels. Also added touch, not quite with @Bendiben’s code. Added trails fade in and out and 20 touch points.

https://gist.github.com/sp4cemonkey/5208579

Looks really nice! Fun to follow the development! :slight_smile:

Another minor tweak added. I noticed I had missed the weighting for the touch pressure so the blades weren’t bending, I also tweaked some of the other settings.

I started adding defuse and ambient lighting, which makes it feel a bit more textured, but currently this isn’t changing lighting based on deformation, just lighting based on blade angle to the light.

@spacemonkey - This is amazing! This looks almost like @Bendiben’s, so your almost there… Keep going!

Gist updated with what I think will be my final version for at least a while. It now adjusts the surface normals as the grass bends. It’s definitely not 100% correct in terms of the maths, conceptually it doesn’t worry about the grass bending as it should, and technically I’m sure it’s plain wrong, but it gives a nice effect. The “wind” wobbling causes shadows to run through the grass which I like.

what I’d like to figure out is how to make the grass bend in the higher part of the blade instead of at the root or “scalp”. I think it parts way too wide.

I agree about the math not mattering, just the effect, so that’s where my focus is. I tries to change some params, but they didn’t seem to bend higher in the blade.

.@aciolino The grass has only 2 sections, the bottom half and the top half. The top half bends at maximum (1.0 wieght) and the bottom half bend based on midWeight ← line 11 in the code.

If you change this to midWeight = 0.2 you get maybe what you want. It could potentially be enhanced by added more triangles to give the grass more bend points, but this would slow rendering down as there would be more triangles to render…

@Spacemonkey: really nice! Ligtht, the wave move and multi touch/trail are great!
I wanted to give a try and learn but It’s crashing codea
I tried to reduced the number of blade and free the ipad memory but still crash. Any advice?

.@Bendiben, does it crash immediately? I find sometimes once I’ve been editing and running and editing lots it’ll crash which is probably memory, and someone on the forum talked about forcing garbage collection…

But if it just crashes out of Codea immediately when you run it I doubt it’s that. In the past I’ve noticed the vector stuff in Codea can be fragile, so accidentally setting a vector value to null used to crash it, not sure about latest version.

@spacemonkey, yes it crashes as soon as push the play button. (ipad2). All my apps have been killed in the ipad memory before…

I have 2 questions for you: I notice that in your shader you change from world space to object space by using matrix transformation. As a newcommer in the shader world I would love to understand the benefits of such transformations (and not to use it without understanding). Could you give some explanations or advice me to read specific topics?

I’m currently also trying to make billboards within shader. I tried several method that doesn’t work well. I guess matrix transformation is one of the key. Do you have a clue?

Thanks a lot
PS:are you a professional 3d developer?

.@Bendiben,

On your crashing… when I had this it’s a pain to debug. You probably need to work through commenting out lumps of code until it doesn’t crash and then putting it back in till it crashes to identify the bad code. Then a bunch of prints just before the crash to figure out what variables aren’t what you assume. I know it’s a professional project thing, but I would be happy to take a look if you want.

As to the transforms to object space. In this project they are unecessary because our object space and world space match, but if I had done a translate, scale or rotate call before drawing the mesh then they wouldn’t match. For my lighting I go for an inverse model matrix to bring the light and camera positions from world space to object space, you can do it the other way bringing the normals from object space to world space, but on another project I did texture based bump mapping, and you need to work in object space for that.

The matrixes and spaces in a nutshell (I think) are object space → world space → eye space → screen space. To move up the chain you apply the model matrix to move from object to world, the view matrix to move world to eye and the projection matrix to move eye to screen. But unless you have done translate/scale/rotate functions, the model matrix is an identity so object and world space match.

As to what I do, I manage developers on enterprise business systems projects, technically I’m more of a db programmer but I’ve dabbled in everything and only got into this 3d stuff when Codea 1.5 came out and exposed it to me.

Hi, thanks for your explanation about the matrix.
I was not clear about the crash: it concern your code from github… I don’t understand why it’s running on your ipad, on Aciolino ipad but not on mine… Mysteries of code…

Ah, that makes more sense, some people have reported issues copying code out of gists where it encodes characters like < > etc and maybe screws up something to do with - signs as in comments --whatever… like an html encoding issue or something.

@Bendiben Re matrices, I’ve written a post trying to explain them for Codea: http://loopspace.mathforge.org/discussion/13/matrices-in-codea

.@Bendiben I’ve copied it onto a pastebin, maybe that will work better…
http://pastebin.com/fCXsz2Dw

Let me know if it works when copied from there.