I’m currently working on a Bubble Shader ( this is my first JME3 Shader ) and got some Results I wanted to show to you.
Perhabs you can give me some feedback
[video]http://youtu.be/rkFbLZ1EOhg[/video]
Matdef:
[java]MaterialDef BubbleMatDef {
MaterialParameters {
Texture2D ColorMap
Float Shininess
Color SpecularColor
Boolean UseSpecularNormal
}
Technique {
VertexShader GLSL100: Shaders/bubbleVertexShader.vert
FragmentShader GLSL100: Shaders/bubbleFragmentShader.frag
WorldParameters {
WorldViewProjectionMatrix
Time
CameraPosition
WorldMatrix
NormalMatrix
}
RenderState {
Blend Alpha
}
Defines {
SPECULARCOLOR : SpecularColor
USESPECULARNORMAL : UseSpecularNormal
}
}
}
[/java]
Vertex Shader:
[java]attribute vec3 inPosition;
attribute vec3 inNormal;
attribute vec2 inTexCoord;
uniform mat3 g_NormalMatrix;
uniform mat4 g_WorldViewProjectionMatrix;
uniform mat4 g_WorldMatrix;
uniform float g_Time;
uniform vec3 g_CameraPosition;
varying vec3 vNdotV;
varying vec2 vTexCoord;
varying vec4 vPosition;
varying vec3 vTransformedNormal;
varying vec3 vNormal;
void main(){
vTexCoord = inTexCoord;
vNormal = inNormal;
vec3 rel = vec3(inPosition.x - 1.0, inPosition.y - 0, inPosition.z -0);
vec3 wave = vec3(1,0,0) * (abs(inPosition.x))+vec3(0,1,0) * (abs(inPosition.y))+vec3(0,0,1) * (abs(inPosition.z));
wave *= vec3(0.125*sin(g_Time / 3),0.3*sin(g_Time / 200),0.125*sin(g_Time /400))*(sin(g_Time)+0.5);
vec3 newPos = inNormal * wave; //* sin(length(rel)*g_Time);
newPos = inPosition + newPos;
gl_Position = g_WorldViewProjectionMatrix * vec4(newPos, 1.0);
vec3 viewVec = normalize(vec4(g_CameraPosition,1) - vec4(newPos, 1) * g_WorldMatrix).xyz;
vNdotV = dot(inNormal, viewVec);
vPosition = vec4(newPos, 1.0) * g_WorldMatrix;
vTransformedNormal = inNormal*g_NormalMatrix;
}[/java]
Fragment-Shader:
[java]varying vec3 vNdotV;
varying vec2 vTexCoord;
varying vec3 vNormal;
varying vec4 vPosition;
varying vec3 vTransformedNormal;
uniform sampler2D m_ColorMap;
#ifdef SPECULARCOLOR
uniform vec4 m_SpecularColor;
uniform float m_Shininess;
#endif
void main() {
vec4 specularLightWeighting = vec4(0,0,0,0);
#ifdef SPECULARCOLOR
vec3 eyeDirection = normalize(-vPosition.xyz);
vec3 reflectionDirection = reflect(-1*vec3(0,0,1), normalize(vTransformedNormal));
float specLightWeighting = pow(max(dot(reflectionDirection, eyeDirection), 0.0), m_Shininess);
vec4 specColor = m_SpecularColor;
#ifdef USESPECULARNORMAL
specColor = vec4(vNormal, m_SpecularColor.a);
#endif
specularLightWeighting = specColor * specLightWeighting;
#endif
vec4 modulatedColor;
vec4 soapRefColor = texture2D(m_ColorMap, vTexCoord);
vec4 groundColor = vec4(0.5,0.5,0.5,0);
modulatedColor.rgb = saturate(2 * soapRefColor * groundColor);
modulatedColor.a = (1- vNdotV)*0.5 - 0.01;
float opacity = saturate(4*(groundColor.a*groundColor.a - 0.75));
gl_FragColor.rgb = lerp(modulatedColor, groundColor, opacity)+specularLightWeighting.rgb;
gl_FragColor.a = modulatedColor.a + opacity ;
gl_FragColor.a /= 2;
gl_FragColor.a += specularLightWeighting.a;
}
[/java]
[java]@Override
public void simpleInitApp() {
Sphere b = new com.jme3.scene.shape.Sphere(200, 200, 1);
Geometry geom = new Geometry("Box", b);
Material mat = new Material(assetManager,"MatDefs/bubbleMat.j3md"); //"Common/MatDefs/Misc/Unshaded.j3md");//"MatDefs/bubbleMat.j3md");
mat.setTexture("ColorMap", assetManager.loadTexture("Textures/rainbow.png"));
mat.setFloat("Shininess", 20f);
mat.setColor("SpecularColor", ColorRGBA.Blue);
mat.setBoolean("UseSpecularNormal", true);
geom.setMaterial(mat);
geom.setQueueBucket(Bucket.Transparent);
geoms.add(geom);
rootNode.attachChild(geom);
rootNode.attachChild(SkyFactory.createSky(
assetManager, "Textures/Sky/Bright/BrightSky.dds", false));
flyCam.setEnabled(false);
ChaseCamera chaseCam = new ChaseCamera(cam, geom, inputManager);
}[/java]