Hi guys!
I’m working on a multi layer sky to make a dynamic sky. For the moment, my sky has 3 layers of sphere. The furthest has a cubemap of stars that is 100% opaque. Then I have another sphere that is inside the “stars” layer that I want to color using vertex coloring. This will allow me to animate the sky colors to make sunsets, etc. It will let the stars show through too, so the color is opaque during the day and transparent during the night. And the last sky layer is inside the color sphere and it shows a transparent cubemap texture of clouds that moves. Okay, so that’s the initial setup.
It works great as long as the camera doesn’t move. To create the “stars” and the “clouds”, I use the Sky.jm3d material definition. I guess it is within this material definition that the infinitely far away sky is handled. There is no vertex coloring option for the sky material, so I use Unshaded.jm3d for the “color” layer. I believe that because of this, when the camera moves, the “stars” and “clouds” follow, but the "color remains there.
I uploaded two images with the 3 sky layers and a red cube to use as a marker. In one image, the camera is just besides the red cube, that is the camera initial position. as you can see, the color cover the sky and all is well.
http://imgur.com/43cJ5
In the other image, I went far away from the cube (you can see it in the middle of the image). The stars and the clouds are still at the same position (except maybe for the camera angle that is a little lower), but the colored sphere is still surrounding the cube and didn’t follow the camera… Not pretty.
http://imgur.com/f3knJ
Can someone tell me if my assumption is right and it is because of the Sky.jm3d that the “clouds” and “stars” follow? I opened both jm3d files and associated .frag and .vert files and I see some code, but I don’t really get what this is all about. I suppose the logical approach would be to create a new material definition for SkyVertexColor.jm3d. Can someone give me some tips on how to create this jm3d file?
Or am I completely wrong and the material has nothing to do with the fact that the sky layers follow the camera or not?
Thanks a lot!
Sorry, I’m still very green…
Well, I’ve got it working. I made my own material definition file by sort of merging unshaded and sky. I made it through trial and error, so it’s most definitely not optimal, but it gets the work done.
For those interested here is the code:
VertexColorSky.j3md
[java]
//old code removed
[/java]
VertexColorSky.vert
[java]
//old code removed
[/java]
VertexColorSky.frag
[java]
//old code removed
[/java]
I hope this can help someone. And also, if anyone has cues on how to improve this, please let me know.
Thanks!
For those interested, I had another problem with the vertex colored sky. The sphere with a sky material will not rotate. The texture does, but the vertices do not. So I had to modify the .vert shader to manually rotate the sphere so the top of the sky is at the zenith and not at the horizon. Here is the new version:
VertexColorSky.jm3d
[java]
MaterialDef VertexColorSky {
MaterialParameters {
Color Color ( Color )
Boolean VertexColor
}
Technique {
VertexShader GLSL100: MatDefs/VertexColorSky.vert
FragmentShader GLSL100: MatDefs/VertexColorSky.frag
WorldParameters {
ViewMatrix
ProjectionMatrix
WorldMatrix
}
Defines {
HAS_VERTEXCOLOR : VertexColor
}
}
Technique FixedFunc {
}
}
[/java]
VertexColorSky.vert
[java]
uniform mat4 g_ViewMatrix;
uniform mat4 g_ProjectionMatrix;
uniform mat4 g_WorldMatrix;
uniform vec3 m_NormalScale;
attribute vec3 inPosition;
attribute vec3 inNormal;
varying vec3 direction;
#ifdef HAS_VERTEXCOLOR
attribute vec4 inColor;
varying vec4 vertColor;
#endif
void main(){
#ifdef HAS_VERTEXCOLOR
vertColor = inColor;
#endif
mat4 rotation = mat4(1.0, 0.0, 0.0, 0.0,
0.0, 0.0,-1.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 0.0, 1.0);
// set w coordinate to 0
vec4 pos = vec4(inPosition, 0.0) * rotation;
// compute rotation only for view matrix
pos = g_ViewMatrix * pos;
// now find projection
pos.w = 1.0;
gl_Position = g_ProjectionMatrix * pos;
vec4 normal = vec4(inNormal * m_NormalScale, 0.0);
direction = normalize( (g_WorldMatrix * normal).xyz );
}
[/java]
VertexColorSky.frag
[java]
uniform vec4 m_Color;
#if defined(HAS_GLOWMAP) || defined(HAS_COLORMAP) || (defined(HAS_LIGHTMAP) && !defined(SEPARATE_TEXCOORD))
#define NEED_TEXCOORD1
#endif
#ifdef HAS_COLORMAP
uniform sampler2D m_ColorMap;
#endif
#ifdef NEED_TEXCOORD1
varying vec2 texCoord1;
#endif
#ifdef HAS_VERTEXCOLOR
varying vec4 vertColor;
#endif
void main(){
vec4 color = vec4(1.0);
#ifdef HAS_VERTEXCOLOR
color *= vertColor;
#endif
gl_FragColor = color;
}
[/java]
I hope this is helpful for someone.
For those who are curious to see what the sky looks like now, here’s a couple of screenshots:
around midnight: http://imgur.com/JozDE
morning sunrise: http://imgur.com/vJFZP
around noon: http://imgur.com/6XcVt
late evening: http://imgur.com/NSNUv
Of course, this is only color, clouds and stars. I’m skratching my head to find a way to add a moon, a sun, etc.
i actually was curious
Haha! I hope I satisfied your curiosity.