I was finally able to get rid of the errors but the way in which I did it causes the shader to not function. The result is a black screen.
@pspeed,
I agree the errors were pretty straightforward. As you know I was more interested in experimenting with Lemur and since it’s not compatible with 3.0.10 I decided to upgrade to 3.1.
The first class array error was because of this line:
uniform float lightTable[] = float[16](0.05f, 0.067f, 0.085f, 0.106f, 0.129f, 0.156f, 0.186f, 0.221f, 0.261f, 0.309f, 0.367f, 0.437f, 0.525f, 0.638f, 0.789f, 1.0f);
Apparently you are supposed to declare and initialize like so(note: i’ve reverted my changes so I’m going off of memory)
uniform float lightTable[16] = float[](0.05, 0.067, 0.085, 0.106, 0.129, 0.156, 0.186, 0.221, 0.261, 0.309, 0.367, 0.437, 0.525, 0.638, 0.789, 1.0);
I also found the implicit cast and I’ve already changed that so it won’t be in the following shader code. The part that I’m still unsure of is perhaps the most important because I’m pretty sure my solution(or attempted solutions) is what caused my scene to go dark.
In my vertex shader I’m calculating color for a specific vertex of my blocks. I only wanted to do this calculation for each vertex which is why I don’t do that calculation in my fragment shader. At the time it seemed like a good idea to just set the inColor rgba values. Apparently that’s a no no. I’m just not sure how to pass that variable along to my fragment shader. I have a varying light variable that is the result of the calculation and I thought my fragment shader would be able to access that. So I accessed it and basically set the rgba of the gl_FragColor variable in my fragment shader instead. I tried a couple of other things like adding “out” after varying in my vertex shader and “in” after varying in my fragment shader’s declaration of the light variable but still no luck.
I realize this is pretty basic stuff but it’s pretty hard to debug a shader because it’s all or nothing. One little bug and the screen is black or it blows up. You can’t System.out the variable values either. Any advice on how you guys do this would be great.
Here’s the shaders as they are in 3.0.10. Please note that a couple of the syntax errors I listed before have been fixed. The main one that hasn’t is the attribute assignment problem.
There’s probably a lot of fluff that I don’t need in these since I think I copied from an existing material.
Thanks for the replies thus far!
Blocks.frag
#if defined(HAS_GLOWMAP) || defined(HAS_COLORMAP) || (defined(HAS_LIGHTMAP) && !defined(SEPARATE_TEXCOORD))
#define NEED_TEXCOORD1
#endif
#if defined(DISCARD_ALPHA)
uniform float m_AlphaDiscardThreshold;
#endif
uniform vec4 m_Color;
uniform sampler2D m_ColorMap;
uniform sampler2D m_LightMap;
uniform float m_dayNightLighting;
varying vec2 texCoord1;
varying vec2 texCoord2;
varying vec4 vertColor;
varying float light;
attribute vec3 inNormal;
void main(){
vec4 color = vec4(1.0);
#ifdef HAS_COLORMAP
color *= texture2D(m_ColorMap, texCoord1);
#endif
#ifdef HAS_COLOR
color *= m_Color;
#endif
#ifdef HAS_VERTEXCOLOR
color *= vertColor;
#endif
#ifdef HAS_LIGHTMAP
#ifdef SEPARATE_TEXCOORD
color.rgb *= texture2D(m_LightMap, texCoord2).rgb;
#else
color.rgb *= texture2D(m_LightMap, texCoord1).rgb;
#endif
#endif
#if defined(DISCARD_ALPHA)
if(color.a < m_AlphaDiscardThreshold){
discard;
}
#endif
gl_FragColor = color;
}
Blocks.vert
#import "Common/ShaderLib/Skinning.glsllib"
uniform mat4 g_WorldViewProjectionMatrix;
attribute vec3 inPosition;
#if defined(HAS_COLORMAP) || (defined(HAS_LIGHTMAP) && !defined(SEPARATE_TEXCOORD))
#define NEED_TEXCOORD1
#endif
attribute vec2 inTexCoord;
attribute vec2 inTexCoord2;
attribute vec4 inColor;
attribute vec3 inNormal;
varying vec2 texCoord1;
varying vec2 texCoord2;
varying float light;
varying vec4 vertColor;
uniform float m_dayNightLighting;
uniform float lightTable[] = float[16](0.05f, 0.067f, 0.085f, 0.106f, 0.129f, 0.156f, 0.186f, 0.221f, 0.261f, 0.309f, 0.367f, 0.437f, 0.525f, 0.638f, 0.789f, 1.0f);
void main()
{
float skyLight = lightTable[int(inColor.a)] * m_dayNightLighting;
float blockLight = lightTable[int(inColor.g)];
light = max(blockLight, skyLight);
if(inNormal.x != 0)
{
light*=0.8;
}
else if(inNormal.z != 0)
{
light*=0.6;
}
else if(inNormal.y == -1)
{
light*=0.5;
}
inColor.r = light;
inColor.g = light;
inColor.b = light;
inColor.a = 1.0;
#ifdef NEED_TEXCOORD1
texCoord1 = inTexCoord;
#endif
#ifdef SEPARATE_TEXCOORD
texCoord2 = inTexCoord2;
#endif
#ifdef HAS_VERTEXCOLOR
vertColor = inColor;
#endif
vec4 modelSpacePos = vec4(inPosition, 1.0);
#ifdef NUM_BONES
Skinning_Compute(modelSpacePos);
#endif
gl_Position = g_WorldViewProjectionMatrix * modelSpacePos;
}
Blocks.j3md
MaterialDef Blocks {
MaterialParameters {
Float dayNightLighting;
Texture2D ColorMap
Texture2D LightMap
Color Color (Color)
Boolean VertexColor (UseVertexColor)
Boolean SeparateTexCoord
// Texture of the glowing parts of the material
Texture2D GlowMap
// The glow color of the object
Color GlowColor
// For hardware skinning
Int NumberOfBones
Matrix4Array BoneMatrices
// Alpha threshold for fragment discarding
Float AlphaDiscardThreshold (AlphaTestFallOff)
//Shadows
Int FilterMode
Boolean HardwareShadows
Texture2D ShadowMap0
Texture2D ShadowMap1
Texture2D ShadowMap2
Texture2D ShadowMap3
//pointLights
Texture2D ShadowMap4
Texture2D ShadowMap5
Float ShadowIntensity
Vector4 Splits
Vector2 FadeInfo
Matrix4 LightViewProjectionMatrix0
Matrix4 LightViewProjectionMatrix1
Matrix4 LightViewProjectionMatrix2
Matrix4 LightViewProjectionMatrix3
//pointLight
Matrix4 LightViewProjectionMatrix4
Matrix4 LightViewProjectionMatrix5
Vector3 LightPos
Float PCFEdge
Float ShadowMapSize
}
Technique {
VertexShader GLSL100: MatDefs/Blocks.vert
FragmentShader GLSL100: MatDefs/Blocks.frag
WorldParameters {
WorldViewProjectionMatrix
}
Defines {
SEPARATE_TEXCOORD : SeparateTexCoord
HAS_COLORMAP : ColorMap
HAS_LIGHTMAP : LightMap
HAS_VERTEXCOLOR : VertexColor
HAS_COLOR : Color
NUM_BONES : NumberOfBones
DISCARD_ALPHA : AlphaDiscardThreshold
}
}
Technique {
}
Technique PreNormalPass {
VertexShader GLSL100 : Common/MatDefs/SSAO/normal.vert
FragmentShader GLSL100 : Common/MatDefs/SSAO/normal.frag
WorldParameters {
WorldViewProjectionMatrix
WorldViewMatrix
NormalMatrix
}
Defines {
NUM_BONES : NumberOfBones
}
}
Technique PreShadow {
VertexShader GLSL100 : Common/MatDefs/Shadow/PreShadow.vert
FragmentShader GLSL100 : Common/MatDefs/Shadow/PreShadow.frag
WorldParameters {
WorldViewProjectionMatrix
WorldViewMatrix
}
Defines {
COLOR_MAP : ColorMap
DISCARD_ALPHA : AlphaDiscardThreshold
NUM_BONES : NumberOfBones
}
ForcedRenderState {
FaceCull Off
DepthTest On
DepthWrite On
PolyOffset 5 3
ColorWrite Off
}
}
Technique PostShadow15{
VertexShader GLSL150: Common/MatDefs/Shadow/PostShadow.vert
FragmentShader GLSL150: Common/MatDefs/Shadow/PostShadow15.frag
WorldParameters {
WorldViewProjectionMatrix
WorldMatrix
}
Defines {
HARDWARE_SHADOWS : HardwareShadows
FILTER_MODE : FilterMode
PCFEDGE : PCFEdge
DISCARD_ALPHA : AlphaDiscardThreshold
COLOR_MAP : ColorMap
SHADOWMAP_SIZE : ShadowMapSize
FADE : FadeInfo
PSSM : Splits
POINTLIGHT : LightViewProjectionMatrix5
NUM_BONES : NumberOfBones
}
ForcedRenderState {
Blend Modulate
DepthWrite Off
PolyOffset -0.1 0
}
}
Technique PostShadow{
VertexShader GLSL100: Common/MatDefs/Shadow/PostShadow.vert
FragmentShader GLSL100: Common/MatDefs/Shadow/PostShadow.frag
WorldParameters {
WorldViewProjectionMatrix
WorldMatrix
}
Defines {
HARDWARE_SHADOWS : HardwareShadows
FILTER_MODE : FilterMode
PCFEDGE : PCFEdge
DISCARD_ALPHA : AlphaDiscardThreshold
COLOR_MAP : ColorMap
SHADOWMAP_SIZE : ShadowMapSize
FADE : FadeInfo
PSSM : Splits
POINTLIGHT : LightViewProjectionMatrix5
NUM_BONES : NumberOfBones
}
ForcedRenderState {
Blend Modulate
DepthWrite Off
PolyOffset -0.1 0
}
}
Technique Glow {
VertexShader GLSL100: MatDefs/Blocks.vert
FragmentShader GLSL100: Common/MatDefs/Light/Glow.frag
WorldParameters {
WorldViewProjectionMatrix
}
Defines {
NEED_TEXCOORD1
HAS_GLOWMAP : GlowMap
HAS_GLOWCOLOR : GlowColor
NUM_BONES : NumberOfBones
}
}
}
ChunkMaterial.java
public class ChunkMaterial extends Material
{
public ChunkMaterial(AssetManager assetManager, String blockTextureFilePath)
{
super(assetManager, "MatDefs/Blocks.j3md");
Texture texture = assetManager.loadTexture(blockTextureFilePath);
texture.setMagFilter(Texture.MagFilter.Nearest);
texture.setMinFilter(Texture.MinFilter.NearestNoMipMaps);
setTexture("ColorMap", texture);
setBoolean("VertexColor", true);
getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
setFloat("dayNightLighting", 1.0f);
setFloat("AlphaDiscardThreshold", 0.1f);
}
}