Jump to content

terrain shader question


havenphillip
 Share

Recommended Posts

I'm working on this bumpy terrain shader. I'm getting some cool little hills but the collision is still totally flat. I assumed the collision would change with the shape of the model displacement but apparently not.  How do I adjust collision to match the texture displacement? Even knowing what it's called that I should be looking for would help. I just assume I'm looking for "collision" but I'm not finding answers to this specific problem anywhere. Here's my vertex shader. I don't know if that's helpful. But it would be cool to make little patches of terrain like this because I could use materials on it, which I can't do with the terrain.
 

#version 400
#define MAX_INSTANCES 256

//Uniforms

uniform vec4 materialcolordiffuse;
uniform sampler2D texture0;//diffuse+height map
uniform sampler2D texture3;//height map
uniform mat4 cameramatrix;
uniform mat4 projectioncameramatrix;
uniform mat4 camerainversematrix;
uniform instancematrices { mat4 matrix[MAX_INSTANCES];} entity;

float terrainSize = 4.0; //texture size
float verticalScale = 15;
float verticalScaleFix = 0.8;

//Attributes
in vec3 vertex_position;
in vec4 vertex_color;
in vec2 vertex_texcoords0;
in vec2 vertex_texcoords1;
in vec3 vertex_normal;
in vec3 vertex_tangent;
in vec3 vertex_binormal;

//Outputs
out vec4 ex_vertexposition;
out vec4 ex_color;
out vec2 ex_texcoords0;
out vec2 ex_texcoords1;
out float ex_selectionstate;
out vec3 ex_normal;
out vec3 ex_tangent;
out vec3 ex_binormal;
out float clipdistance0;
out vec3 ex_VertexCameraPosition;
 

void main() {

    mat4 entitymatrix = entity.matrix[gl_InstanceID];
    mat4 entitymatrix_=entitymatrix;
    entitymatrix_[0][3]=0.0;
    entitymatrix_[1][3]=0.0;
    entitymatrix_[2][3]=0.0;
    entitymatrix_[3][3]=1.0;

    vec4 ex_vertexposition = entitymatrix_ * vec4(vertex_position,1.0);
    //entitymatrix[0] = ex_vertexposition;

    //Transform vectors from local to global space
    mat3 nmat = mat3(entitymatrix[0].xyz,entitymatrix[1].xyz,entitymatrix[2].xyz);//40
    ex_normal = (nmat * vertex_normal);    
    ex_tangent = normalize(nmat * vertex_tangent);
    //ex_binormal = normalize(nmat*vertex_binormal);
    ex_binormal = normalize(reflect(-ex_tangent,ex_normal));//cross

    ex_texcoords0 = ex_vertexposition.xz / terrainSize;
    float textureheightvalue = (textureLod(texture0, ex_vertexposition.xz,0).r * verticalScale)-4;
    ex_vertexposition.y += textureheightvalue;

    //from https://www.leadwerks.com/community/blogs/entry/1166-up-part-1/
    vec3 norm = normalize(nmat * ex_normal);
    vec3 up = vec3(0.0, 1.0, 0.0);
    float ang = max(dot(norm, up), 0.0);

    gl_Position = projectioncameramatrix * ex_vertexposition * ang;

    ex_color = vec4(entitymatrix[0][3],entitymatrix[1][3],entitymatrix[2][3],entitymatrix[3][3]);
    ex_color *= vec4(1.0-vertex_color.r,1.0-vertex_color.g,1.0-vertex_color.b,vertex_color.a) * materialcolordiffuse;

 
}



 

little hills.jpg

  • Like 2
Link to comment
Share on other sites

Are you adding tesselation to the terrain shader? If you are just adding an offset in the vertex shader why not just modify the terrain height instead and skip the custom shader?

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

I'm just doing vertex displacement of the y axis on a subdivided model that I imported. I assumed the displacement would affect the model and thus the collision but it just acts like I'm walking on a flat surface. It's really irritating. It seems like a cool idea to me to have a subdivided surface model ready to go, that I can resize and reshape however I want.

You can't put materials on the terrain. That's the gist of it. The ability to do that opens its usefulness up a lot.  For a small patch I'd want to be able to set a heightmap on it and have it pop up, or put puddles on it or whatever. I want to make a shader that allows me to make small patches of terrain with, and generally learn shaders in the process so I can ultimately make more cool-looking stuff. I need the shaders and scripts etc, first. I just read your post about terrain in LE5. You totally get it. It's gonna be lit. But in the meantime if you could point me to which part of which shader handles the collision in the terrain - or any direction toward the answer - that would be much appreciated.

Like something like in this picture I see a heightmap shader on a small square, two box brushes with pom shader, a door,  and maybe two models (since the railing could be used twice). I just don't want to have to make a new model for every single little bump in every scene. That's tedious, not fun for me.





 

terrain patch.jpg

  • Like 1
Link to comment
Share on other sites

I haven't seen Klepto's buoyancy but I know that in The Zone there's a floating leaves shader which is essentially a few leaves on a diffuse/alphamask texture which is put on a box that just wobbles, and the box is put in the water. He might be doing something like that.  It makes sense to me now that the GPU handles the screen pixels, and not the interactions between objects or collisions themselves. I guess that should have been obvious to me. What I probably need to learn is how to use Shape:PolyMesh.

Link to comment
Share on other sites

2 hours ago, ?Yue? said:

With this I can learn to write shaders for leadwerks?


Yes! Do it! I need a shader buddy on here. You could read that if you want. I never did. If you watch all of these (about 30 tutorials) you'll be about where I'm at. I just followed along and wrote the code with them to get it stuck in my memory. I'll help you with whatever I can along the way. These .zip files are the codes from these videos configured to work with Leadwerks. Once you understand how to do that it opens up a lot for you. None for the first set of videos because those are literally Leadwerks tutorials. But if I were you I'd follow along and write the shaders for yourself to get it burned into your memory. Do it, bro! Good luck.

 

I would jump right in here:

https://www.youtube.com/watch?v=ThUhCs3LNXo&list=PLWWTOiYlyyL4ymtjySQVQ4v7anhOZjPww

 



I started there. Then I did several of these:

https://www.youtube.com/watch?v=ZQpE4GPUR5g&list=PL4neAtv21WOmIrTrkNO3xCyrxg4LKkrF7&index=6

 

 

Then I did most of these:

https://www.youtube.com/watch?v=u5HAYVHsasc&list=PLGmrMu-IwbguU_nY2egTFmlg691DN7uE5

 

 

Lewis Lepton.zip aa_shadertoy tutorials.zip

  • Like 3
  • Thanks 1
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.

 Share

×
×
  • Create New...