Haydenmango Posted July 12, 2014 Share Posted July 12, 2014 Hi everyone. I was wondering if someone could take a look at these modified versions of Shadmars Vegetation+Diffuse+Shadowmask and Shadow+Vegetation shaders to check for any errors. Also I was wondering if there is a way I can make the vegetation sway only when it is visible and/or within a certain distance from the camera. That could be really useful (especially if it increases my fps). Anyways here they are. They compile without any errors and seem to work fine. Feel free to use them in any of your own projects at your own risk until they get a stamp of approval. Vegetation+Diffuse+Shadowmask Shader-- Fragment- #version 400 #define BFN_ENABLED 1 //Uniforms uniform sampler2D texture0;//diffuse map uniform sampler2D texture1;//normal map uniform vec4 materialcolorspecular; uniform vec4 lighting_ambient; uniform samplerCube texture15; uniform vec2 camerarange; uniform vec2 buffersize; uniform float camerazoom; //Inputs in vec2 ex_texcoords0; in vec4 ex_color; in float ex_selectionstate; in vec3 ex_VertexCameraPosition; in vec3 ex_normal; in vec3 ex_tangent; in vec3 ex_binormal; //Outputs out vec4 fragData0; out vec4 fragData1; out vec4 fragData2; out vec4 fragData3; float DepthToZPosition(in float depth) { return camerarange.x / (camerarange.y - depth * (camerarange.y - camerarange.x)) * camerarange.y; } void main(void) { vec4 outcolor = ex_color; outcolor *= texture(texture0,ex_texcoords0); if (outcolor.a < .63) discard; vec4 color_specular = materialcolorspecular; vec3 screencoord = vec3(((gl_FragCoord.x/buffersize.x)-0.5) * 2.0 * (buffersize.x/buffersize.y),((-gl_FragCoord.y/buffersize.y)+0.5) * 2.0,DepthToZPosition( gl_FragCoord.z )); screencoord.x *= screencoord.z / camerazoom; screencoord.y *= -screencoord.z / camerazoom; vec3 nscreencoord = normalize(screencoord); vec3 normal = ex_normal; normal = texture(texture1,ex_texcoords0).xyz * 2.0 - 1.0; float ao = normal.z; normal = ex_tangent*normal.x + ex_binormal*normal.y + ex_normal*normal.z; normal=normalize(normal); vec4 lighting_diffuse = vec4(0); vec4 lighting_specular = vec4(0); float attenuation=1.0; vec3 lightdir; vec3 lightreflection; int i; float anglecos; float diffspotangle; float denom; fragData0 = outcolor; #if BFN_ENABLED==1 fragData1 = texture(texture15,normalize(vec3(normal.x,-normal.y,normal.z))); #else fragData1 = vec4(normalize(normal)*0.5+0.5,fragData0.a); #endif fragData1.a = color_specular.r * 0.299 + color_specular.g * 0.587 + color_specular.b * 0.114; int materialflags=1; if (ex_selectionstate>0.0) materialflags += 2; fragData2 = vec4(0.0,0.0,0.0,materialflags/255.0); } -Vertex #version 400 #define MAX_INSTANCES 256 //Uniforms uniform vec4 materialcolordiffuse; uniform mat4 projectioncameramatrix; uniform mat4 camerainversematrix; uniform instancematrices { mat4 matrix[MAX_INSTANCES];} entity; uniform float currenttime;//Attributes in vec3 vertex_position; in vec4 vertex_color; in vec2 vertex_texcoords0; in vec3 vertex_normal; in vec3 vertex_binormal; in vec3 vertex_tangent; //Outputs out vec4 ex_color; out vec2 ex_texcoords0; out float ex_selectionstate; out vec3 ex_VertexCameraPosition; out vec3 ex_normal; out vec3 ex_tangent; out vec3 ex_binormal;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; //SWAY float seed = mod(currenttime / 100.0 * 0.25,360.0); seed += entitymatrix_[3].x*33.0 + entitymatrix_[3].y*67.8 + entitymatrix_[3].z*123.5; seed += vertex_position.x + vertex_position.y + vertex_position.z; vec4 movement = vec4( vec3( 0.02 * (sin(seed)+0.025*cos(seed*5.2+3.2)) ),0.0); vec4 modelvertexposition = entitymatrix_ * vec4(vertex_position+movement.xyz,1.0); ex_VertexCameraPosition = vec3(camerainversematrix * modelvertexposition); gl_Position = projectioncameramatrix * modelvertexposition; mat3 nmat = mat3(camerainversematrix[0].xyz,camerainversematrix[1].xyz,camerainversematrix[2].xyz); nmat = nmat * mat3(entitymatrix[0].xyz,entitymatrix[1].xyz,entitymatrix[2].xyz); ex_normal = normalize(nmat * vertex_normal); ex_tangent = normalize(nmat * vertex_tangent); ex_binormal = normalize(nmat * vertex_binormal); ex_texcoords0 = vertex_texcoords0; ex_color = vec4(entitymatrix[0][3],entitymatrix[1][3],entitymatrix[2][3],entitymatrix[3][3]); //If an object is selected, 10 is subtracted from the alpha color. //This is a bit of a hack that packs a per-object boolean into the alpha value. ex_selectionstate = 0.0; if (ex_color.a<-5.0) { ex_color.a += 10.0; ex_selectionstate = 1.0; } ex_color *= vec4(1.0-vertex_color.r,1.0-vertex_color.g,1.0-vertex_color.b,vertex_color.a) * materialcolordiffuse; } Shadow+Vegetation Shader-- Fragment- #version 400 uniform sampler2D texture4;in vec2 ex_texcoords0; void main() { if (texture(texture4,ex_texcoords0).a < 0.8) discard; } Vertex- #version 400 #define MAX_INSTANCES 256 //Uniforms //uniform mat4 entitymatrix; uniform mat4 projectioncameramatrix; uniform instancematrices { mat4 matrix[MAX_INSTANCES];} entity; uniform float currenttime; //Attributes in vec3 vertex_position; in vec2 vertex_texcoords0; out vec2 ex_texcoords0; 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; //SWAY float seed = mod(currenttime / 100.0 * 0.25,360.0); seed += entitymatrix_[3].x*33.0 + entitymatrix_[3].y*67.8 + entitymatrix_[3].z*123.5; seed += vertex_position.x + vertex_position.y + vertex_position.z; vec4 movement = vec4( vec3( 0.01 * (sin(seed)+0.025*cos(seed*5.2+3.2)) ),0.0); ex_texcoords0 = vertex_texcoords0; vec4 modelvertexposition = entitymatrix_ * vec4(vertex_position+movement.xyz,1.0); gl_Position = projectioncameramatrix * modelvertexposition; } Quote Check out my game Rogue Snowboarding- https://haydenmango.itch.io/roguesnowboarding Link to comment Share on other sites More sharing options...
Recommended Posts
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.