Advertisement
Guest User

Wild Witch Project - toon shading test GLSL

a guest
Jan 23rd, 2011
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.91 KB | None | 0 0
  1. ###########################################
  2. #
  3. #   Testes de Shader
  4. #   www.wildwitchproject.com
  5. #
  6. #   developer: Marcos Augusto Bitetti (marcosbitetti@gmail.com)
  7. #
  8. #   This code is under
  9. #   Creative Commons Licence - to Share, to Remix
  10. #
  11. ##########################################
  12.  
  13. import GameLogic
  14. import bpy
  15.  
  16.  
  17. vertex_shader = """
  18.  
  19. uniform vec3 point_light;
  20. uniform sampler2D nmap;
  21.  
  22. attribute vec4 gl_MultiTexCoord0;
  23.  
  24. //varying vec4 vcolor; // opcional usar cor do vertice
  25. varying vec4 diffuse,ambient;
  26. varying vec3 normal,lightDir,halfVector;
  27. varying mat4x4 viewNormal;
  28.    
  29. void main(void)
  30. {
  31.  
  32.   //variaveis
  33.   normal = gl_NormalMatrix * gl_Normal;
  34.   halfVector = normalize(gl_LightSource[3].halfVector.xyz);
  35.   diffuse = gl_FrontMaterial.diffuse * gl_LightSource[3].diffuse;
  36.   ambient = gl_FrontMaterial.ambient * gl_LightSource[3].ambient;
  37.   ambient += gl_LightModel.ambient * gl_FrontMaterial.ambient;
  38.        
  39.   gl_TexCoord[0] = gl_MultiTexCoord0;
  40.   gl_Position = ftransform();
  41.   lightDir = normalize(point_light);
  42.  
  43.   viewNormal = gl_NormalMatrix;
  44. }
  45. """
  46.  
  47. fragment_shader ="""
  48.  
  49. uniform sampler2D diffmap;
  50. uniform sampler2D nmap;
  51. uniform vec3 point_light;
  52.  
  53. varying vec4 vcolor;
  54. varying vec4 diffuse,ambient;
  55. varying vec3 normal,lightDir,halfVector;
  56. varying mat4x4 viewNormal;
  57.  
  58. vec4 color;
  59.  
  60. void main(void)
  61. {
  62.   vec3 n,halfV;
  63.   float NdotL,NdotHV;
  64.   float intensity;
  65.  
  66.   //computa a normal apartir do normalmap
  67.   vec3 vnormal = normal * vec3(texture2D(nmap, gl_TexCoord[0].st).xyz);
  68.  
  69.   n = normalize(vnormal); // a fragment shader can't write a varying variable, hence we need a new variable to store the normalized interpolated normal
  70.   NdotL = max(dot(n,lightDir),0.0); // compute the dot product between normal and ldir
  71.  
  72.   intensity = 0.0;//NdotL;
  73.  
  74.   if (NdotL > 0.0) {
  75.         halfV = normalize(halfVector);
  76.         NdotHV = max(dot(n,halfV),0.0);
  77.        intensity = pow(NdotHV, gl_FrontMaterial.shininess);
  78.   }
  79.  
  80.  
  81.   if (intensity > 0.95)
  82.         color = vec4(0.95); //vec4(1.0,0.5,0.5,1.0);
  83.     else if (intensity > 0.75)
  84.         color = vec4(0.75); //vec4(0.6,0.3,0.3,1.0);
  85.    else if (intensity > 0.5)
  86.        color = vec4(0.5);
  87.     else if (intensity > 0.25)
  88.         color = vec4(0.25); //vec4(0.4,0.2,0.2,1.0);
  89.     else
  90.        color = vec4(0.00); //vec4(0.2,0.1,0.1,1.0);
  91.  
  92.  
  93.   vec3 texture   = texture2D(diffmap, gl_TexCoord[0].st).xyz;
  94.   gl_FragColor = color + vec4(texture, 1.0);
  95.  
  96. }
  97.  
  98.  
  99. """
  100.  
  101. obj = GameLogic.getCurrentController().owner
  102.  
  103. mesh = obj.meshes[0]
  104. light = bpy.data.objects['Lamp']
  105.  
  106. shad1 = [0.95, 0.75, 0.5, 0.25 ]
  107.  
  108. for mat in mesh.materials:
  109.    shader = mat.getShader()
  110.    l = light.location
  111.    print(l)
  112.    if shader != None:
  113.        shader.setSource(vertex_shader, fragment_shader, True)
  114.        shader.setUniform3f("point_light", l[0], l[1], l[2] )
  115.        shader.setSampler("diffmap",0)
  116.        shader.setSampler("nmap",1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement