GLSL MatCap Shader (DONE!)

Hi!! I did a matcap shader for JME3. The shader is used seldom in gamedev, but it’s used in blender, zbrush… and it willbe very useful for scrollshooters to imitate different materials like glass, gold, metals, toon shader…



The shader does not use any lights… only one texture. So, it’s good for productivity.

The shader does not use UV coordinates, but it uses model’s normals/tangets/binormals and screen coordinates (as far as I understand).



I did a matcap shader! It is in the shaderBlow’s mercurial repository.

repository: http://code.google.com/p/jme-glsl-shaders/



MatCap textures library: http://www.pixologic.com/zbrush/downloadcenter/library/

MatCap Generator: http://bensimonds.com/2010/07/30/matcap-generator/



http://dl.dropbox.com/u/26887202/123/ShaderBlow_matcap.jpg

2 Likes

You have the normal, now you need to convert that to UV so you can read from the texture. One way is by using sphere mapping. jME3 has a GLSL function called “Optics_SphereCoord” that can be used for that in Optics.glsllib

Thanks for answering!!! I think sphere mapping is not what I need. But the solution could be very easy. I found a program “Sculptris”. It uses glsl for MatCap (material capture). Here is its shader.



vertex

[java]/



uniform vec3 light1_dir;



uniform vec3 light2_dir;



uniform vec3 observer_pos;



uniform float object_invscale;



/



uniform float texturesize;







varying vec3 normal;



varying vec3 cull_normal;



varying vec4 color;







varying vec3 tangent;



varying vec3 binormal;







attribute vec3 app_Tangent;







void main()



{



// normal=gl_Normal.xyz;



// position=gl_Vertex.xyz
object_invscale;



color=gl_Color;



// gl_TexCoord[0]=gl_MultiTexCoord0;



// gl_TexCoord[0]=gl_MultiTexCoord0-vec4(0.5/256, 0.5/256, 0, 0); // compensate for bumpmap emboss offset - TODO: uniform for texture size



// gl_TexCoord[0]=gl_MultiTexCoord0-vec4(0.5/512, 0.5/512, 0, 0); // compensate for bumpmap emboss offset - TODO: uniform for texture size



// gl_TexCoord[0]=gl_MultiTexCoord0-vec4(0.5/1024, 0.5/1024, 0, 0); // compensate for bumpmap emboss offset - TODO: uniform for texture size







gl_TexCoord[0]=gl_MultiTexCoord0-vec4(0.5/texturesize, 0.5/texturesize, 0, 0); // compensate for bumpmap emboss offset



gl_TexCoord[1]=gl_MultiTexCoord0; // no uv compensation for colortexture







gl_Position=ftransform();







normal=normalize(gl_NormalMatrix
gl_Normal.xyz);











vec3 tpos=(gl_ModelViewMatrixgl_Vertex).xyz;



vec3 nm_z=normalize(tpos);



vec3 nm_x=cross(nm_z, vec3(0,1,0));



vec3 nm_y=cross(nm_x, nm_z);



cull_normal=vec3(dot(normal, nm_x), dot(normal, nm_y), dot(normal, nm_z));











tangent=normalize(gl_NormalMatrix
app_Tangent);



binormal=cross(normal, tangent);



// binormal=normalize(cross(normal, tangent));



// tangent=cross(binormal, normal);



}





[/java]









fragment:

[java]uniform sampler2D materialtex;



uniform sampler2D normaltex;



uniform sampler2D colortex;







uniform float backfaces;







/



varying vec3 light1_vector;



varying vec3 light2_vector;



varying vec3 eye_vector;



/







varying vec3 normal;



varying vec3 cull_normal;



varying vec3 tangent;



varying vec3 binormal;



varying vec4 color;







//varying vec3 position;



//varying vec2 texcoords;







vec3 nnormal;







void main()



{



vec3 tnormal=texture2D(normaltex, gl_TexCoord[0].st).xyz;



tnormal=(tnormal-vec3(0.5, 0.5, 0.5))2.0;



nnormal=normalize(tangent
tnormal.x + binormal
tnormal.y + normal
tnormal.z);







// overlay vertex color (selection, mask)



// outcolor+=color;







vec2 lightmap_coords=nnormal.xy0.495+vec2(0.5, 0.5);



lightmap_coords.y
=-1.0;







vec4 shade=texture2D(materialtex, lightmap_coords);







vec4 color=texture2D(colortex, gl_TexCoord[1].st);







vec4 multfactor=shade2;



vec4 screenfactor=multfactor-vec4(1,1,1,1);



multfactor=min(multfactor, vec4(1,1,1,1));



screenfactor=max(screenfactor, vec4(0,0,0,0));







color=vec4(1,1,1,1)-(vec4(1,1,1,1)-color)
(vec4(1,1,1,1)-screenfactor);







gl_FragColor=colormultfactor;







// gl_FragColor=color;







if(backfaces>0.5 && normalize(cull_normal).z>0.07) // backfacing



{



// gl_FragColor
=0.3;



// gl_FragColor.w=1.0;



float c=(gl_FragColor.x+gl_FragColor.y+gl_FragColor.z)*0.1;



gl_FragColor=vec4(c, c, c, 1);



}



// gl_FragColor=vec4(0.1, 0.1, 0.1, 1);



}





[/java]



I will try to translate it to JME next week. And thank you for spending time.

I did a matcap shader! It is in the shaderBlow’s mercurial repository.

repository: http://code.google.com/p/jme-glsl-shaders/





http://img585.imageshack.us/img585/5445/matcapzzz.jpg

1 Like

Another screenshots:



http://img155.imageshack.us/img155/7138/matcapzzz2.jpg



http://dl.dropbox.com/u/26887202/123/ShaderBlow_matcap.jpg

2 Likes

If some one is interested in MatCap textures…



MatCap textures library: http://www.pixologic.com/zbrush/downloadcenter/library/

MatCap Generator: http://bensimonds.com/2010/07/30/matcap-generator/

Awesome



http://toeas.com/emoticon/files/2011/08/awesome-emoticons-4.jpg



It’s really nice. Good job ;).

Hi @mifth



Is MatCap shader working fine on oyur pc?



This is what I see when I run TestMatCap on my:



Very weird!!!



Console log info:

INFO: Child (BitmapFont) attached to this node (null)

12/10/2012 12:37:13 com.jme3.scene.plugins.ogre.MeshLoader load

ADVERTENCIA: Cannot locate TestModels/LightBlow/jme_lightblow.material for model TestModels/LightBlow/jme_lightblow.mesh.xml

12/10/2012 12:37:13 com.jme3.scene.plugins.ogre.MeshLoader applyMaterial

ADVERTENCIA: Cannot locate Material for model TestModels/LightBlow/jme_lightblow.mesh.xml

12/10/2012 12:37:13 com.jme3.scene.Node attachChild

INFO: Child (jme_lightblow-geom-1) attached to this node (jme_lightblow-ogremesh)

12/10/2012 12:37:13 com.jme3.material.MaterialDef

INFO: Loaded material definition: FakeParticleBlow

12/10/2012 12:37:13 com.jme3.util.TangentBinormalGenerator processTriangleData

ADVERTENCIA: Angle between tangents exceeds tolerance for vertex 0.

12/10/2012 12:37:13 com.jme3.util.TangentBinormalGenerator processTriangleData

ADVERTENCIA: Angle between tangents exceeds tolerance for vertex 6.

12/10/2012 12:37:13 com.jme3.util.TangentBinormalGenerator processTriangleData

ADVERTENCIA: Angle between tangents exceeds tolerance for vertex 12.

12/10/2012 12:37:13 com.jme3.util.TangentBinormalGenerator processTriangleData

ADVERTENCIA: Angle between tangents exceeds tolerance for vertex 13.



INFO: Child (Statistics View) attached to this node (Gui Node)

12/10/2012 12:37:14 com.jme3.renderer.lwjgl.LwjglRenderer updateUniformLocation

INFO: Uniform g_WorldViewMatrix is not declared in shader [ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.vert, defines, type=Vertex, language=GLSL100], ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.frag, defines, type=Fragment, language=GLSL100]].

12/10/2012 12:37:14 com.jme3.renderer.lwjgl.LwjglRenderer updateUniformLocation

INFO: Uniform m_NormalMapPower is not declared in shader [ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.vert, defines, type=Vertex, language=GLSL100], ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.frag, defines, type=Fragment, language=GLSL100]].

12/10/2012 12:37:14 com.jme3.renderer.lwjgl.LwjglRenderer updateUniformLocation

INFO: Uniform m_Nor_Inv_Y is not declared in shader [ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.vert, defines, type=Vertex, language=GLSL100], ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.frag, defines, type=Fragment, language=GLSL100]].

12/10/2012 12:37:14 com.jme3.renderer.lwjgl.LwjglRenderer updateUniformLocation

INFO: Uniform m_Nor_Inv_X is not declared in shader [ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.vert, defines, type=Vertex, language=GLSL100], ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.frag, defines, type=Fragment, language=GLSL100]].

12/10/2012 12:37:14 com.jme3.renderer.lwjgl.LwjglRenderer updateUniformLocation

INFO: Uniform m_Nor_Inv_Z is not declared in shader [ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.vert, defines, type=Vertex, language=GLSL100], ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.frag, defines, type=Fragment, language=GLSL100]].

12/10/2012 12:37:15 com.jme3.renderer.lwjgl.LwjglRenderer updateUniformLocation

INFO: Uniform g_WorldViewMatrix is not declared in shader [ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.vert, defines, type=Vertex, language=GLSL100], ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.frag, defines, type=Fragment, language=GLSL100]].

12/10/2012 12:37:15 com.jme3.renderer.lwjgl.LwjglRenderer updateUniformLocation

INFO: Uniform m_Nor_Inv_Y is not declared in shader [ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.vert, defines, type=Vertex, language=GLSL100], ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.frag, defines, type=Fragment, language=GLSL100]].

12/10/2012 12:37:15 com.jme3.renderer.lwjgl.LwjglRenderer updateUniformLocation

INFO: Uniform m_Nor_Inv_X is not declared in shader [ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.vert, defines, type=Vertex, language=GLSL100], ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.frag, defines, type=Fragment, language=GLSL100]].

12/10/2012 12:37:15 com.jme3.renderer.lwjgl.LwjglRenderer updateUniformLocation

INFO: Uniform m_Nor_Inv_Z is not declared in shader [ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.vert, defines, type=Vertex, language=GLSL100], ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.frag, defines, type=Fragment, language=GLSL100]].

12/10/2012 12:37:15 com.jme3.renderer.lwjgl.LwjglRenderer updateUniformLocation

INFO: Uniform g_WorldViewMatrix is not declared in shader [ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.vert, defines, type=Vertex, language=GLSL100], ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.frag, defines, type=Fragment, language=GLSL100]].

12/10/2012 12:37:15 com.jme3.renderer.lwjgl.LwjglRenderer updateUniformLocation

INFO: Uniform m_NormalMapPower is not declared in shader [ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.vert, defines, type=Vertex, language=GLSL100], ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.frag, defines, type=Fragment, language=GLSL100]].

12/10/2012 12:37:15 com.jme3.renderer.lwjgl.LwjglRenderer updateUniformLocation

INFO: Uniform m_Nor_Inv_Y is not declared in shader [ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.vert, defines, type=Vertex, language=GLSL100], ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.frag, defines, type=Fragment, language=GLSL100]].

12/10/2012 12:37:15 com.jme3.renderer.lwjgl.LwjglRenderer updateUniformLocation

INFO: Uniform m_Nor_Inv_X is not declared in shader [ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.vert, defines, type=Vertex, language=GLSL100], ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.frag, defines, type=Fragment, language=GLSL100]].

12/10/2012 12:37:15 com.jme3.renderer.lwjgl.LwjglRenderer updateUniformLocation

INFO: Uniform m_Nor_Inv_Z is not declared in shader [ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.vert, defines, type=Vertex, language=GLSL100], ShaderSource[name=ShaderBlow/Shaders/MatCap/MatCap.frag, defines, type=Fragment, language=GLSL100]].

12/10/2012 12:37:15 com.jme3.renderer.lwjgl.LwjglRenderer updateUniformLocation

INFO: Uniform m_VertexColor is not declared in shader [ShaderSource[name=Common/MatDefs/Misc/Unshaded.vert, defines, type=Vertex, language=GLSL100], ShaderSource[name=Common/MatDefs/Misc/Unshaded.frag, defines, type=Fragment, language=GLSL100]].

@H

Using a Mac?

@madjack said:
@H
Using a Mac?

Nop

Windows 7 64bits
SAPPHIRE HD 6970 2GB GDDR5 PCIE

Could’ve been a Mac problem with the shader, but I’ve just noticed that thread was over a year old so the recent changes must have broken the whole thing.

@H said:

Is MatCap shader working fine on oyur pc?



Hi man!!!

OMG!!! Something goes wrong...

I have this image:
http://i.imgur.com/C8jJ0.png


Can you update MatCap's matdef i committed and try again?
http://code.google.com/p/jmonkeyplatform-contributions/source/detail?r=513
@madjack said:
Could've been a Mac problem with the shader, but I've just noticed that thread was over a year old so the recent changes must have broken the whole thing.

Shaderblow was moved to jme contribution repo. I made a lot of refactoring after that. I rolled back to version before changes in order to check, but it always works that way on my pc

I just need confirmation if it is a problem with my pc or a general issue.

@H , also try to do this in java class:



[java]

public static void main(final String[] args) {

final TestMatCap app = new TestMatCap();

AppSettings aps = new AppSettings(true);

aps.setRenderer(AppSettings.LWJGL_OPENGL2);

app.setSettings(aps);

app.start();

}

[/java]





If i set “LWJGL_OPENGL3” i got nothing in the scene.

@mifth said:
@H , also try to do this in java class:

[java]
public static void main(final String[] args) {
final TestMatCap app = new TestMatCap();
AppSettings aps = new AppSettings(true);
aps.setRenderer(AppSettings.LWJGL_OPENGL2);
app.setSettings(aps);
app.start();
}
[/java]


If i set "LWJGL_OPENGL3" i got nothing in the scene.

I tryed that and I updated the project but issue remains. It is my pc then :cry: I will have to check
Thanks very much
@H said:
I tryed that and I updated the project but issue remains. It is my pc then :cry: I will have to check
Thanks very much


Try to update video driver.
@mifth said:
Try to update video driver.


I found the issue... yeah!!!. Issue was on MatCap.frag: varying vec3 diffuseColor
diffuseColor is not a varying. I mean it was not declare on MatCap.vert (I don't know how my GPU (ATI graphic card) create it or what value assign to it :?) and it was making a psychedelic mess on my GPU. I will post a video... the effect was very funny!
So, I removed varying keyword and that solve the issue.

Check http://code.google.com/p/jmonkeyplatform-contributions/source/detail?r=514

EDIT: video added
http://youtu.be/FyIznNFNcdE
1 Like
@H said:
I found the issue... yeah!!!. Issue was on MatCap.frag: varying vec3 diffuseColor
diffuseColor is not a varying. I mean it was not declare on MatCap.vert (I don't know how my GPU (ATI graphic card) create it or what value assign to it :?) and it was making a psychedelic mess on my GPU. I will post a video... the effect was very funny!
So, I removed varying keyword and that solve the issue.

Check http://code.google.com/p/jmonkeyplatform-contributions/source/detail?r=514

EDIT: video added
http://youtu.be/FyIznNFNcdE




OMG!!! Man!!! Thank you a lot!!! I was my fault. You did it really cool! Thank you!

Can you also check "Glass" shader? Class:
http://code.google.com/p/jmonkeyplatform-contributions/source/browse/trunk/ShaderBlow/?r=514#ShaderBlow%2Ftest-src%2Fcom%2Fshaderblow%2Ftest%2Fglass
@mifth said:
Can you also check "Glass" shader?


I commited a fix and code cleanup yesterday. You already saw that I think.
1 Like
@H said:
I commited a fix and code cleanup yesterday. You already saw that I think.


Yeah! You fixed a vital thing. I did a mistake with varying.

Good Job!!! You are doing really cool things!

I'll add Sky2 shader soon. This is a shader for stars making.