Horde3D http://horde3d.org/forums/ |
|
Basic Toon shading http://horde3d.org/forums/viewtopic.php?f=2&t=1669 |
Page 1 of 1 |
Author: | quincy18 [ 23.05.2012, 12:48 ] |
Post subject: | Basic Toon shading |
I am trying to get some basic toon shading working, I am using the game engine + editor. I wrote my own pipeline : Code: <Pipeline> <CommandQueue> <Stage id="FirstStage" link="globalSettings.material.xml"> <ClearTarget depthBuf="true" colBuf0="true" /> <DrawGeometry context="AMBIENT" class="~TRANSLUCTENT" /> </Stage> <Stage id="Overlays"> <DrawOverlays context="OVERLAY" /> </Stage> </CommandQueue> </Pipeline> and this is my shader : Code: [[FX]] sampler2D albedoMap; // Contexts context AMBIENT { VertexShader = compile GLSL VS_OVERLAY; PixelShader = compile GLSL FS_OVERLAY; } [[VS_OVERLAY]] #include "shaders/utilityLib/vertCommon.glsl" #include "shaders/utilityLib/vertSkinning.glsl" uniform mat4 viewProjMat; uniform vec3 viewerPos; attribute vec3 vertPos; attribute vec2 texCoords0; attribute vec3 normal; varying vec4 pos, vsPos; varying vec2 texCoords; varying vec3 tsbNormal; void main( void ) { mat4 skinningMat = calcSkinningMat(); mat3 skinningMatVec = getSkinningMatVec( skinningMat ); pos = calcWorldPos( skinPos( vec4( vertPos, 1.0 ), skinningMat ) ); vsPos = calcViewPos( pos ); tsbNormal = normal; texCoords = texCoords0; gl_Position = viewProjMat * pos; } [[FS_OVERLAY]] uniform sampler2D albedoMap; varying vec2 texCoords; varying vec3 tsbNormal; void main( void ) { //Flip coords for glsl vec3 newCoords = vec3( texCoords, 0 ); newCoords.t *= -1.0; //Calculate shading vec3 textColour = texture2D( albedoMap, newCoords ).rgb; float light = min(dot(tsbNormal, vec3(1,0,0)), 1); //Thresholds for cell shading if(light < 0.3) light = 0.3; else if(light < 0.7) light = 0.7; else light = 1; gl_FragColor.rgb = textColour * light; } Currently I am trying to use a global lightning direction but that doesn't seem to work, the lightning always stays on the same side of the model. Lets say I have a model who I light from the back and I am looking at the front, then its all oke since the front is dark and the back is light, then if I turn the model 180 degrees and the back faces the camera its still light instead of going dark as it should be. Anyone got any clue why this is happening / how to fix it. |
Author: | eiguckamalda [ 23.05.2012, 14:06 ] |
Post subject: | Re: Basic Toon shading |
Hi! In this line vec3(1.0, 0.0, 0.0) should represent your light direction? Code: float light = min(dot(tsbNormal, vec3(1,0,0)), 1); The problem is, that dot can also result negative values. Normalizing your normal is also a good idea. Quote: vec3 normal = normalize(tsbNormal); vec3 lightDir = vec3(1.0, 0.0, 0.0); float light = max(0.0, dot(lightDir, normal)); Then I also suggest to modify your pipeline in the way, that you don't use the Overlay-Context. I don't know if this leads to some problems too. You can for example look at the forward pipeline how this could be done. Hope this fixes your problem |
Author: | MistaED [ 24.05.2012, 00:22 ] |
Post subject: | Re: Basic Toon shading |
Hi quincy18, Nice work with the toon shader, about the global lighting direction I'm guessing it's some uniform declaration in globalSettings.material.xml. You'll need to expose this uniform in your shader by the looks of it to whatever it is called, and also pass it as a varying to the fragment shader so when you do your light calculation in that dot it uses your uniform or varying vec3. Also instead of doing branching which can cause cache misses to get your toon shade curve I'd recommend perhaps use a lookup table (ie a sampler2d of a gradient ramp that matches what you're doing with the if statements) and multiply it with your 'float light' in the fragment stage. For more of a speed gain (and this is what Horde should be doing by default) is to move the flip coords from the fragment program to the vertex program so it doesn't do an unneeded dependent texture fetch, but that's up to you. Hope this helps! |
Page 1 of 1 | All times are UTC + 1 hour |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |