Hello Monkeys !
Right now I’m working on a really simple grass system with chunk. Im on the early stage of it and i’m facing a problem. I created a custom shader for my grass which is just random thing found on the internet until I finally reach something that was decent.
Here is the Shader Code (Warning : your eyes might turn red)
Fragment
vec3 minGrass = vec3(0, 0, 0);
void main(){
outColor = texture2D(texture,texCoord);
if(outColor.a < 0.2){
discard;
}
//outColor.a = 0.5-(0.01*camDist);
vec3 finalDiffuse = diffuseLight;
finalDiffuse += vec3(1,1,1);
finalDiffuse = vec3(mix(minGrass, finalDiffuse.rgb, texCoord.y));
outColor *= vec4(finalDiffuse,1.0);
}
Vertex
uniform vec4 g_LightPosition;
uniform vec4 g_LightColor;
uniform vec4 g_LightDirection;
vec3 toDirection(vec4 direction){
return vec3(direction.x, direction.y, direction.z);
}
void main(){
projPosition = worldViewProjectionMatrix * vec4(modelPosition,1.0);
camDist = projPosition.z;
vec4 worldPos = worldMatrix*vec4(modelPosition,1.0);
vec3 l = -toDirection(g_LightPosition);
vec3 n = normalMatrix*modelNormal;
float cosTheta = clamp( dot( n,l ), 0,1 );
diffuseLight = toDirection(g_LightColor) * cosTheta;
}
And here is the problem i’m facing after batching all this grass Quad
Without BatchNode.batch()
With BatchNode.batch()
Here is the Code used to create GrassQuad material
public void createGrassMaterial(AssetManager assetManager){
grassMat = new Material(assetManager, "MatDefs/testmat.j3md");
grassMat.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha);
grassMat.getAdditionalRenderState().setFaceCullMode(RenderState.FaceCullMode.Off);
grassMat.getAdditionalRenderState().setDepthWrite(true);
grassMat.getAdditionalRenderState().setDepthTest(true);
grassMat.getAdditionalRenderState().setAlphaFallOff(0.1f);
grassMat.setTransparent(true);
grassMat.setTextureParam("ColorMap", VarType.Texture2D, assetManager.loadTexture("Textures/grass.png"));
}
Why do the grass transparency is messed up after batching them ?
Like always ask me if you want further details or anything that would help.
Thanks, Stomrage !