カスタムシェーダー(フレネル反射+バンプマッピング)

attribute vec3 position; attribute vec3 normal; attribute vec2 texcoord; attribute vec3 tangent; attribute vec3 binormal; uniform vec3 light0_Direction; uniform mat4 normalMatrix; uniform mat4 modelViewMatrix; uniform mat4 modelViewMatrixInverse; uniform mat4 modelViewProjectionMatrix; varying vec2 fragTexCoord; varying vec3 fragEye; varying vec3 fragNormal; varying vec3 fragNormal2; varying vec3 fragTangent; varying vec3 fragBinormal; varying vec3 fragLight; void main() { vec4 eye = vec4(modelViewMatrixInverse * vec4(0.0, 0.0, 0.0, 1.0)); fragEye = position - eye.xyz; fragNormal = normal; fragTangent = tangent; fragBinormal = binormal; fragLight = light0_Direction; fragNormal2 = (normalMatrix * vec4(normal, 0.0)).xyz; fragTexCoord = vec2(texcoord.x, 1.0 - texcoord.y); gl_Position = modelViewProjectionMatrix * vec4(position, 1.0); } #ifdef GL_ES precision highp float; #endif uniform float eta; uniform float bias; uniform float scale; uniform float power; uniform float specpower; uniform sampler2D tex; uniform samplerCube cube; uniform sampler2D bump; varying vec2 fragTexCoord; varying vec3 fragEye; varying vec3 fragNormal; varying vec3 fragTangent; varying vec3 fragBinormal; varying vec3 fragLight; varying vec3 fragNormal2; void main() { vec3 eye = normalize(fragEye); vec3 normal = normalize(fragNormal); vec3 tangent = normalize(fragTangent); vec3 binormal = normalize(fragBinormal); //バンプマッピング vec4 texCol = texture2D(tex, fragTexCoord); vec3 bumpCol = texture2D(bump, fragTexCoord).rgb; vec3 tsn = 2.0 * (normalize(bumpCol) - 0.5); tsn = tsn.z * normal + tsn.y * tangent + tsn.x * binormal; normal = -normalize(tsn); float p = max(0.1, dot(normal, eye)); texCol.rgb *= p; texCol.rgb += max(0.0, pow(p, 128.0)) * vec3(0.8); //フレネル反射 //入射,反射,屈折ベクトルの計算 vec3 N = -normalize(tsn); //法線ベクトル vec3 I = eye; //入射ベクトル vec3 R = reflect(I, N); //反射ベクトル vec3 T = refract(I, N, eta); //屈折ベクトル //反射因数の計算 float fresnel = bias + scale*pow(1.0 - abs(dot(I, N)), power); //float fresnel = bias + scale * pow(1.0 - max(0.0, dot(I, N)), power); //反射環境色の取得 vec4 reflecColor = textureCube(cube, R); reflecColor = reflecColor * vec4(1.333); reflecColor.a = 1.0; //屈折環境色の計算 vec4 refracColor; refracColor = textureCube(cube, T); refracColor.a = 1.0; //ディフューズ反射 //スペキュラー反射 vec3 IS = normalize((normalize(fragLight) + eye) / 2.0); //入射ベクトル vec3 ID = normalize(fragLight); //入射ベクトル vec3 NS = normalize(fragNormal2); //法線ベクトル float spec = pow(max(0.0, dot(-IS, NS)), specpower); float diff = max(0.0, dot(-ID, NS)); vec3 col = diff * vec3(0.6); col += spec * vec3(1.0); //vec3 col = spec * vec3(1.0); vec4 lgtCol = vec4(col, 1.0); //色を統合 vec4 cubeCol = refracColor+fresnel*(reflecColor - refracColor); cubeCol.a = fresnel * 0.5 + 0.5; cubeCol = clamp(cubeCol, 0.0, 1.0); //gl_FragColor = cubeCol; //gl_FragColor = reflecColor; //gl_FragColor = refracColor; //gl_FragColor = lgtCol; gl_FragColor = mix(cubeCol, lgtCol, 0.4); gl_FragColor = mix(gl_FragColor, texCol, 0.5); }





 






戻る