Hello,
I am trying to figure out how to make a GLSL Shader use the objects UV map.
I am trying to modify this reflection shader, I want the shader to use the UV map of the diffuse and bump texture, instead of specifying a custom UV tiling. I am looking at the floor reflection shader. Defined by the text objects “reflect_ground” and “ground_reflection.py” in the “stained glass” scene.
This is the shader below. As you can see the UV for the diffuse and bump textures is defined by
gl_TexCoord[0] = gl_MultiTexCoord0*0.5+0.5;
and then later for example
...
float texscale = 40.0;
...
vec4 diffuse = texture2D(diffuse, gl_TexCoord[0].st*texscale).rgba;
How would I tell the shader I want to use the UV map defined in Blender? If I could understand this, then I could use this shader for my Vray lightmaps + UV tiled texture method, and with reflections of course
Complete shader “reflect_ground”:
from bge import logic as g
from bge import render as r
import bgl
cont = g.getCurrentController()
own = cont.owner
VertexShader = """
attribute vec4 Tangent;
varying vec3 T, B, N;
void main()
{
vec3 pos = vec3(gl_Vertex);
T = Tangent.xyz;
B = cross(gl_Normal, Tangent.xyz);
N = gl_Normal;
gl_TexCoord[1] = ftransform();
gl_TexCoord[2].xyz = pos - gl_ModelViewMatrixInverse[3].xyz;
gl_TexCoord[0] = gl_MultiTexCoord0*0.5+0.5;
gl_Position = ftransform();
}
"""
FragmentShader = """
varying vec3 T, B, N;
uniform sampler2D reflection,refraction, normals, diffuse;
float aberration = 0.005;
float bump = 0.2;
float texscale = 40.0;
vec3 TangentSpace(vec3 v)
{
vec3 vec;
vec.xy=v.xy;
vec.z=sqrt(1.0-dot(vec.xy,vec.xy));
vec.xyz= normalize(vec.x*T+vec.y*B+vec.z*N);
return vec;
}
void main() {
vec2 coord = gl_TexCoord[1].xy/gl_TexCoord[1].w*0.5+0.5;
coord = clamp(coord,0.005,0.995);
vec3 normal = texture2D(normals, gl_TexCoord[0].st*texscale).rgb*2.0-1.0;
normal.x = -normal.x;
normal.y = -normal.y;
vec3 nVec = TangentSpace(vec3(normal*bump));
vec3 vVec = normalize(gl_TexCoord[2].xyz);
float cosine = abs(dot(-vVec,nVec));
float fresnel = pow(0.8-cosine+0.2,1.0);
vec3 reflect = texture2D(reflection, coord+nVec.st).rgb;
vec3 luminosity = vec3(0.30, 0.59, 0.11);
float reflectivity = pow(dot(luminosity, reflect.rgb),5.0);
vec3 refract = vec3(0.0);
refract.r = texture2D(refraction, (coord+nVec.st)*1.0).r;
refract.g = texture2D(refraction, (coord+nVec.st)*1.0-(vVec.st*aberration)).g;
refract.b = texture2D(refraction, (coord+nVec.st)*1.0-(vVec.st*aberration*2.0)).b;
vec4 diffuse = texture2D(diffuse, gl_TexCoord[0].st*texscale).rgba;
vec3 color = mix(diffuse.rgb*0.8+0.2,reflect,clamp(fresnel+reflectivity,0.0,1.0));
gl_FragColor = vec4(color,1.0);
}
"""
mesh = own.meshes[0]
for mat in mesh.materials:
shader = mat.getShader()
if shader != None:
if not shader.isValid():
shader.setSource(VertexShader, FragmentShader, 1)
shader.setAttrib(g.SHD_TANGENT)
shader.setSampler('reflection',0)
#shader.setSampler('refraction',1)
shader.setSampler('normals',2)
shader.setSampler('diffuse',3)