simplified toonshader script

(Mico27) #1

EDIT EDITI’m using a script for toonshading, but the way it works is that ALL the object I assign in the objlist MUST ALL be shown in the Current scene, so when only one of those object I want to be shaded is deleted from the scene or just not in the scene, all the other object stop being toon shaded. But with my NEW modified script I FINALY found a way!!! here’s the new toonshaders script:

 from math import *
import GameLogic as g
scene = g.getCurrentScene()
own = g.getCurrentController().getOwner()
objlist = scene.getObjectList()
g.setLogicTicRate(60.0)
# -------------------------------------
ShaderObjects = objlist
MaterialIndexList = [0]
GlobalAmbient = [0.39,0.35,0.32,1]
AmbF = 0.5
# -------------------------------------
VertexShader = """
varying vec2 texCoords;
varying vec3 vNormal;
varying vec3 vVertex;
void main() {
 gl_Position = ftransform();
 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex);
 vNormal = normalize(gl_NormalMatrix * gl_Normal);
 texCoords = gl_MultiTexCoord0.st;
 
}
"""
FragmentShader = """
varying vec2 texCoords;
varying vec3 vNormal;
varying vec3 vVertex;
uniform float silhouetteThreshold;
uniform sampler2D texture;
#define shininess 20.0
void main() {
 //vec4 materialColor = gl_FrontMaterial.diffuse;
 
 //vec4 materialColor = vec4(1.0,1.0,1.0,1.0);
 //vec4 materialColor = gl_Color;
 vec4 materialColor = texture2D(texture,texCoords);
 
 vec4 silhouetteColor = vec4(0.0,0.0,0.0,1.0);
 
 vec4 specularColor = gl_FrontMaterial.specular;
 
 vec3 eyePos = normalize(-vVertex);
 vec3 lightPos = gl_LightSource[0].position.xyz;
 
 vec3 Normal = vNormal;
 vec3 EyeVert = normalize(eyePos - vVertex);
 vec3 LightVert = normalize(lightPos - vVertex);
 vec3 EyeLight = normalize(LightVert+EyeVert);
 
 float sil = max(dot(Normal,EyeVert), 0.0);
 
 if(sil < silhouetteThreshold)
  gl_FragColor = silhouetteColor;
 else
 {
  gl_FragColor = materialColor;
 
  float spec = pow(max(dot(Normal,EyeLight),0.0), shininess);
 
  if(spec < 0.2)
   gl_FragColor *= 0.98;
 
  else
   gl_FragColor = materialColor+specularColor*.5;
 
  float diffuse = max(dot(Normal,LightVert),0.0);
  if( diffuse < 0.5)
   gl_FragColor *= 0.9;
 }
}
 
 
 
"""
 
def MainLoop ():
 # for each object
 for obj in ShaderObjects:
  mesh_index = 0
  mesh = own.getMesh(mesh_index)
  while mesh != None:
   for mat in mesh.materials:
 
    # regular TexFace materials do NOT have this function
    if not hasattr(mat, "getMaterialIndex"):
     return
 
    mat_index = mat.getMaterialIndex()
    # find an index    
    found = 0
    for i in range(len(MaterialIndexList)):
     if mat_index == MaterialIndexList[i]:
      found=1
      break
    if not found: continue
    shader = mat.getShader()
    if shader != None:
     if not shader.isValid():
      shader.setSource(VertexShader, FragmentShader,1)
     shader.setUniform1f('silhouetteThreshold',0.25)
     shader.setSampler('texture',0)
 
   mesh_index += 1
   mesh = own.getMesh(mesh_index)
# -------------------------------------
MainLoop()
# -------------------------------------
 

now with this, you dont have to type all the object you want to be shaded with all the side effects like ‘‘if one is not in all is not toonshaded anymore’’
you just assign the script in the logic bricks of the object you want to be shaded and … TADAA!!! it works :slight_smile: and work indepentently from other tonnshaded object

0 Likes

(Mmph!) #2

I fixed it
Make the mesh into a ghost,turn off dynamic button.
Make the armature into the dynamic object, increase the dynamic radius to 2.

Nice walk cycle BTW :smiley:

have fun!

0 Likes

(Mico27) #3

Mmph! Thk you VERY MUCH!!! I dunno why the setting must be like this but It works thanks a lot!!!

0 Likes

(Mico27) #4

See First Post

0 Likes

(Mmph!) #5

I really have no idea how to fix that, GLSL is still total greek to me.

0 Likes

(Mico27) #6

http://download.blender.org/documentation/GE/Blender.htm#GLSL
for those who doesnt know what exactly is GLSL

0 Likes

(ititrx) #7

I downloaded the file from the other post and tried it but never got back to you… I found that if you hit the key with the windows menu thingy on it? the one between ctrl and alt, then you can get the character to run all the time…

very cool thing actually…

0 Likes

(Mico27) #8

all I want to know is how I can make each object their shade independent. like if there’s an object in the groups of object that has to be toonshaded wont stop being shaded if one object toonshaded is not in the scene or have been deleted from the scene.

I tried the getOwner but it it send back a GameObject function and not an Actual OB (object)…

0 Likes

(Mico27) #9

Nevermind I rebuilted the script so now it works like I wanted!!! just set the script in the logic brick of the object you want shaded! (see first post)

0 Likes

(Dakka Dakka) #10

I reckon what’s most important is trying to get a toon shader to work in the GE without chewing up the framerate. The Blender peoples should incorporate a built in toon shader for 2.50. I think the problem might be that it’s rendering in specular, then again in toon shading. If there was a way to make it render in toon shading without materials, then you could do it with materials trned off, and therefore, render only a toon shader making it run quicker. (I hope.) Shame I don’t know a spec of GLSL. :frowning:

0 Likes

(Mico27) #11

of course just so everyone remember do NOT link this script to an always sensor triggered to Pulse mode , it eat almost all your frame rate. But if your object as to change mesh during the game use pulse mode but some thing like 10, so it wont lower the framrate but during the first 3 second when the game start there will be an odd effect of shading but after it looks perfect and do not low the framerate

0 Likes

(Dakka Dakka) #12

Ok, so if it isn’t attached to an always sensor, what is it attached to? This might be just what I’m looking for if it doesn’t destroy the framerate…

0 Likes

(Mico27) #13

its attached to an always sensor but it must NOT be in pulse mode, coz putting it in pulse mode would run the script indefinitly thus lowing the framerate. so when you attach it to an always sensor but Not pulsed, the script run once and the looping in the script do the job perfectly and doesnt low the framerate

0 Likes

(Larryboy) #14

Are there any other instructions for using this script.

I have tried it on the monkey mesh and it just goes black… if I add a texture to the material the monkey has some texture in her ears …apart from that black - I must be missing something…? Maybe a Blend for us idiots???

0 Likes

(Dakka Dakka) #15

Maybe a Blend for us idiots

Sorry for gravedigging, but I really need to get this swcript to work. Mico27, could you please post a demonstration .blend? Or anybody else who has managed to get this working.

0 Likes

(Mico27) #16

here ya go
http://www.savefile.com/files/1081058

0 Likes

(Larryboy) #17

Oooh its cool. …

0 Likes