Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###########################################
- #
- # Testes de Shader
- # www.wildwitchproject.com
- #
- # developer: Marcos Augusto Bitetti (marcosbitetti@gmail.com)
- #
- # This code is under
- # Creative Commons Licence - to Share, to Remix
- #
- ##########################################
- import GameLogic
- import bpy
- vertex_shader = """
- uniform vec3 point_light;
- uniform sampler2D nmap;
- attribute vec4 gl_MultiTexCoord0;
- //varying vec4 vcolor; // opcional usar cor do vertice
- varying vec4 diffuse,ambient;
- varying vec3 normal,lightDir,halfVector;
- varying mat4x4 viewNormal;
- void main(void)
- {
- //variaveis
- normal = gl_NormalMatrix * gl_Normal;
- halfVector = normalize(gl_LightSource[3].halfVector.xyz);
- diffuse = gl_FrontMaterial.diffuse * gl_LightSource[3].diffuse;
- ambient = gl_FrontMaterial.ambient * gl_LightSource[3].ambient;
- ambient += gl_LightModel.ambient * gl_FrontMaterial.ambient;
- gl_TexCoord[0] = gl_MultiTexCoord0;
- gl_Position = ftransform();
- lightDir = normalize(point_light);
- viewNormal = gl_NormalMatrix;
- }
- """
- fragment_shader ="""
- uniform sampler2D diffmap;
- uniform sampler2D nmap;
- uniform vec3 point_light;
- varying vec4 vcolor;
- varying vec4 diffuse,ambient;
- varying vec3 normal,lightDir,halfVector;
- varying mat4x4 viewNormal;
- vec4 color;
- void main(void)
- {
- vec3 n,halfV;
- float NdotL,NdotHV;
- float intensity;
- //computa a normal apartir do normalmap
- vec3 vnormal = normal * vec3(texture2D(nmap, gl_TexCoord[0].st).xyz);
- n = normalize(vnormal); // a fragment shader can't write a varying variable, hence we need a new variable to store the normalized interpolated normal
- NdotL = max(dot(n,lightDir),0.0); // compute the dot product between normal and ldir
- intensity = 0.0;//NdotL;
- if (NdotL > 0.0) {
- halfV = normalize(halfVector);
- NdotHV = max(dot(n,halfV),0.0);
- intensity = pow(NdotHV, gl_FrontMaterial.shininess);
- }
- if (intensity > 0.95)
- color = vec4(0.95); //vec4(1.0,0.5,0.5,1.0);
- else if (intensity > 0.75)
- color = vec4(0.75); //vec4(0.6,0.3,0.3,1.0);
- else if (intensity > 0.5)
- color = vec4(0.5);
- else if (intensity > 0.25)
- color = vec4(0.25); //vec4(0.4,0.2,0.2,1.0);
- else
- color = vec4(0.00); //vec4(0.2,0.1,0.1,1.0);
- vec3 texture = texture2D(diffmap, gl_TexCoord[0].st).xyz;
- gl_FragColor = color + vec4(texture, 1.0);
- }
- """
- obj = GameLogic.getCurrentController().owner
- mesh = obj.meshes[0]
- light = bpy.data.objects['Lamp']
- shad1 = [0.95, 0.75, 0.5, 0.25 ]
- for mat in mesh.materials:
- shader = mat.getShader()
- l = light.location
- print(l)
- if shader != None:
- shader.setSource(vertex_shader, fragment_shader, True)
- shader.setUniform3f("point_light", l[0], l[1], l[2] )
- shader.setSampler("diffmap",0)
- shader.setSampler("nmap",1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement