Jump to content

Foliage Shader


Dinkelborg
 Share

Recommended Posts

Hi,

 

I'm trying to write a foliage shader of some sort,

but I'm completely new to GLSL and would need some help understanding how to achieve the desired behavior. 

 

For now all the 'plants' should do is bend away from the player. The player is simplified to a sphere and for each vertex I do the following:

 

Vertex Shader Code

precision highp float;// Attributesattribute vec3 position;attribute vec3 normal;// Uniformsuniform mat4 worldViewProjection;uniform mat4 world;uniform mat4 view;uniform mat4 projection;uniform vec3 playerPos;// Varyingvarying vec3 vNormal;void main(void) {vec4 v = world * vec4(position,1);vec4 d = v - vec4(playerPos,1);float l = length(d);if (l <= 1.0){v = v*(normalize(-d));}gl_Position = projection * view * v;vNormal = normal;}

So what I do is to get the vertex's world-position by multiplying it with the world matrix, then I get the distance-vector between the vertex-position and the player position and then measure the distance by getting its length. Now, if the distance length is smaller than 1 I am trying to move the vector one step into the opposite direction of the distance-vector... 

 

So for now I was expecting the Grass / Bamboo to kind of bend away from the player by building a curve or so because the most up vertices might be further away than 1 and so they would stay where they are, that would have been fine for now and I could have searched a way to fix it later, but I guess I'm calculating the 'moving' of the vertex wrong here is the scene as it is right now: http://analogmadness.com/Demo/FoliageShader/
 

 

post-11235-0-71060700-1421245819_thumb.p

Link to comment
Share on other sites

I think that the step calculation "v = v*(normalize(-d));" is where the problem is based, at the beginning I'm converting v into world-space, could it be that I have to convert d into another space too?

And in what 'space' is the uniform playerPos handed over? In my Update function I set the value to player.position, where player is the sphere that is moving in the scene.

 

Link to comment
Share on other sites

Hey,
 
Your shader seems to work outside of the "l < 1.0" special case, so I agree with you: there's probably something wrong with the modification of v.
 
Considering what you're trying to achieve, a vector multiplication (v = v*d) is not a good idea.
What you need is a vector addition.
 
Note: I'll call the player radius 'R', to avoid misunderstanding. In your example, R=1;
 
d is the vector going from the player to the current vertex. This is the direction in which the current vertex must be moved. Also, if length(d) < R, we want to move the current vertex further by an amount of R-length(d).
 
You could try this, see if it works:

float R = 1.0;if (l <= R){v += normalize(d)*(R-l);}
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...