I changed the “varying vec” to “in vec” at grass shader script and it shows this:
0(3): error C204: version directive must be first statement and may not be repeated.
Blend file:http://benjamin.vedrenne.free.fr/STOCKAGE/terrain_LOD_physics_grass.rar
Here is the grass shader script:
Grass Shader, made by benj
Feel free to use it, share it, improve it
Have fun
VertexShader = “”"
//WIND SETTINGS-------------------------------*
vec2 windDirection = vec2(-1.0,0.5);
float windStrength = 0.05;
float waveLength = 1.5;
//--------------------------------------------*
// position of the vertex (and fragment) in view space
varying vec4 position;
// getting texture and properties values
uniform sampler2D heightMap;
uniform int uvX;
uniform int uvY;
uniform float height;
uniform int cellNumber;
uniform float windTime;
void main(){
//get the first UV layout and move it accordingly to object position
gl_TexCoord[0].x = gl_MultiTexCoord0.x+float(uvX)/float(cellNumber);
gl_TexCoord[0].y = gl_MultiTexCoord0.y+float(uvY)/float(cellNumber);
// get initial vertex position in world space
vec4 v = gl_Vertex;
// add the wind’s contribution
float windDisplacement = sin(6.2832*(windTime+dot(float(cellNumber)gl_TexCoord[0].xy,normalize(windDirection))));
v.xy += windStrengthgl_MultiTexCoord1.t*normalize(windDirection)*windDisplacement;
// assign height
float heightFactor = texture2D(heightMap, clamp(gl_TexCoord[0].st,0.0,1.0)).a;
v.z += heightFactor*(height);
//deform the mesh
gl_Position = gl_ModelViewProjectionMatrix * v;
//get the vertex position
position = gl_Position;
//get second UV layout for grass color and alpha
gl_TexCoord[1] = gl_MultiTexCoord1;
}
“”"
FragmentShader = “”"
#version 120
//--------------------------------------------------------------------------------------------------**
// VARIABLES TO TWEAK
//Set how much to darken the grass at it’s base(to create an AO-like effect).
//If you’ve already darkened the bottom of your grass texture for just that purpose,
//or just don’t want it darkened, set this to 0.
const float AO = .5;
//Material settings:
vec3 ambientColor = vec3(0.06,0.15,0.2); // Set the ambient color (R,G,B)
const float rimLight = 0.3; // Set the rim lighting effect
//Lighting settings:
vec3 sunColor = vec3(1.0,0.8,0.6); // Set the Sun color (R,G,B)
float sunIntensity = 1.5; // Set the Sun intensity (at least 0.0)
vec3 sunDirection = vec3(-1.0,0.0,0.4); // Set the sun direction (X,Y,Z)
//Grass fading settings :
float GrassDistance = 14.0; // Distance where grass start to fade away, in blender units
float GrassFade = 16.0; // Distance of the Fade, in blender units
//---------------------------------------------------------------------------------------------------**
// get textures
uniform sampler2D heightMap;
uniform sampler2D grass;
uniform sampler2D stencil;
uniform sampler2D lightmap;
//get vertex position
in vec4 position;
void main()
{
// get color and alpha of the grass
vec3 grasscolor = texture2D(grass, gl_TexCoord[1].st).rgb;
float grassAlpha = texture2D(grass, gl_TexCoord[1].st).a;
// get normals of the terrain
vec3 normalGlobal = normalize(vec3(2.0, 2.0, 1.0) * (texture2D(heightMap, gl_TexCoord[0].st).rgb) - vec3(1.0, 1.0, 0.0));
//get lightmap
float lightmap = texture2D(lightmap, gl_TexCoord[0].st).r;
// compute lighting
float diffuseTemp = dot(normalGlobal, normalize(sunDirection)); // compute diffuse lighting
vec3 diffuseRaw = vec3(max(0.0,diffuseTemp)); // returns 0 when negative
if (rimLight != 0.0)
diffuseRaw += rimLight*pow(1.0-abs(diffuseTemp),5.0); // add rim light effect
diffuseRaw *= vec3(sunIntensity * sunColor); // multiply by sun values
diffuseRaw *= vec3(lightmap); // Affect with lightmap
diffuseRaw += ambientColor; // Add ambient color
diffuseRaw *= grasscolor; // Multiply by color
// compute AO
if (AO != 0.0) //This comparison should have no performance overhead since AO is a compile-time const
diffuseRaw = gl_TexCoord[1].tAO+1.0-AO; //If AO is set to 0.0, the compiler should be able to remove this whole “if” block at compile time
//decrease alpha with distance
float z = gl_FragCoord.z / gl_FragCoord.w;
z /= GrassFade;
z -= GrassDistance/GrassFade;
z = clamp(1.0-z,0.0,1.0);
// make grass visible only on grassy areas
vec3 allStencil = texture2D(stencil, gl_TexCoord[0].st).rgb;
float grassStencil = (1.0-(allStencil.r+allStencil.g+allStencil.b));
if (grassStencil < 0.5) {
grassStencil = 0.0;
} else {
grassStencil = 1.0;
}
// Get the final alpha value
float alpha = grassAlphagrassStencilz;
// set final values
gl_FragColor.rgb = diffuseRaw;
gl_FragColor.a = alpha;
}
“”"
Get Mesh and properties values’
import bge
from bge import logic
cont = logic.getCurrentController()
own = cont.owner
mesh = own.meshes[0]
cell = own[‘cell_size’]
player=logic.getCurrentScene().objects[str(own[‘LODcenter’])]
Get object position
uvX=(int(round(player.localPosition[0]/cell)))
uvY=(int(round(player.localPosition[1]/cell)))
Move the object to follow the Player step by step
own.localPosition[0] = uvXcell
own.localPosition[1] = uvYcell
increase time value and keep only floating numbers
own[‘windTime’] += own[‘windSpeed’]
own[‘windTime’] = own[‘windTime’] % 1
#Run the Shader
for mat in mesh.materials:
shader = mat.getShader()
if shader != None:
shader.setSource(VertexShader, FragmentShader, 1)
shader.setSampler(‘heightMap’,0)
shader.setSampler(‘grass’,1)
shader.setSampler(‘stencil’,2)
shader.setSampler(‘lightmap’,3)
shader.setUniform1i(‘uvX’,uvX)
shader.setUniform1i(‘uvY’,uvY)
shader.setUniform1f(‘height’,own[“height”])
shader.setUniform1f(‘windTime’,own[‘windTime’])
shader.setUniform1i(‘cellNumber’,own[“cell_number”])