in packages/miew/src/gfx/shaders/Uber.frag [488:728]
void main() {
#ifdef CLIP_PLANE
if (vViewPosition.z < clipPlaneValue) discard;
#endif
#ifdef ZCLIP
if (vViewPosition.z < zClipValue) discard;
#endif
#if defined(USE_LIGHTS) && defined(SHADOWMAP)
#if NUM_DIR_LIGHTS > 0
// see THREE.WebGLProgram.unrollLoops
#pragma unroll_loop_start
for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {
vDirLightWorldCoord[ i ] = vDirectionalShadowCoord[ i ];
vDirLightWorldNormal[ i ] = vDirectionalShadowNormal[ i ];
}
#pragma unroll_loop_end
#endif
#endif
vec4 pixelPosWorld = vec4(vWorldPosition, 1.0);
vec4 pixelPosEye;
#ifdef SPHERE_SPRITE
vec3 viewNormalSprites;
float frontFaced = 1.0;
vec3 normal;
/* quick-and-dirty method
normal.xy = ' + INSTANCED_SPRITE_OVERSCALE + ' * (2.0 * vUv - 1.0);
float r2 = dot(normal.xy, normal.xy);
if (r2 > 1.0) discard;
float normalZ = sqrt(1.0 - r2);
normal.z = normalZ;
normal = normal * ( -1.0 + 2.0 * float( gl_FrontFacing ) );
pixelPosEye = vec4(spritePosEye.xyz, 1.0);
pixelPosEye.z += spritePosEye.w * normalZ;
*/
// ray-trace sphere surface
{
vec3 p;
if (!get_sphere_point(-vViewPosition, p, frontFaced)) discard;
vec4 v = vec4(instOffset.xyz + p * instOffset.w, 1.0);
pixelPosWorld = modelMatrix * v;
pixelPosEye = modelViewMatrix * v;
normal = normalize(normalMatrix * p);
#ifdef NORMALS_TO_G_BUFFER
viewNormalSprites = normalize(mat3(modelViewMatrix)*p);
#endif
#if defined(USE_LIGHTS) && defined(SHADOWMAP)
#if NUM_DIR_LIGHTS > 0
// see THREE.WebGLProgram.unrollLoops
#pragma unroll_loop_start
for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {
vDirLightWorldCoord[ i ] = directionalShadowMatrix[ i ] * pixelPosWorld;
vDirLightWorldNormal[ i ] = (directionalShadowMatrix[ i ] * (modelMatrix * vec4(p, 0.0))).xyz;
}
#pragma unroll_loop_end
#endif
#endif
}
#endif
#ifdef CYLINDER_SPRITE
vec3 normal;
vec3 viewNormalSprites;
float frontFaced = 1.0;
float cylinderY = 0.0;
// ray-trace cylinder surface
{
vec3 p;
if (!get_cylinder_point(-vViewPosition, p, frontFaced)) discard;
cylinderY = 0.5 * (p.y + 1.0);
vec4 v = vec4(p, 1.0);
v = vec4(dot(v, matVec1), dot(v, matVec2), dot(v, matVec3), 1.0);
pixelPosWorld = modelMatrix * v;
pixelPosEye = modelViewMatrix * v;
vec3 localNormal = normalize(vec3(p.x, 0.0, p.z));
normal = vec3(
dot(localNormal, matVec1.xyz),
dot(localNormal, matVec2.xyz),
dot(localNormal, matVec3.xyz));
#ifdef NORMALS_TO_G_BUFFER
viewNormalSprites = normalize(mat3(modelViewMatrix)*normal);
#endif
#if defined(USE_LIGHTS) && defined(SHADOWMAP)
#if NUM_DIR_LIGHTS > 0
// see THREE.WebGLProgram.unrollLoops
#pragma unroll_loop_start
for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {
vDirLightWorldCoord[ i ] = directionalShadowMatrix[ i ] * pixelPosWorld;
vDirLightWorldNormal[ i ] = (directionalShadowMatrix[ i ] * (modelMatrix * vec4(normal, 0.0))).xyz;
}
#pragma unroll_loop_end
#endif
#endif
normal = normalize(normalMatrix * normal);
}
#endif
#ifdef ATTR_COLOR
vec3 vertexColor = vColor;
#else
vec3 vertexColor = vec3(1.0, 1.0, 1.0);
#endif
#ifdef ATTR_COLOR2
#ifdef CYLINDER_SPRITE
float colorCoef = cylinderY; // cylinder parameter is calculated from ray-tracing
#else
float colorCoef = vUv.y; // cylinder parameter is interpolated as tex coord
#endif
// choose either color or color2
vertexColor = mix(vColor2, vColor, step(0.5, colorCoef));
#endif
// negative red component is a special condition
if (vertexColor.x < 0.0) discard;
#ifdef DASHED_LINE
if ( mod( vLineDistance, dashedLinePeriod ) > dashedLineSize ) discard;
#endif
// transparency prepass writes only z, so we don't need to calc the color
#ifdef PREPASS_TRANSP
fragColor = vec4(1.0, 1.0, 1.0, 1.0);
#if defined(SPHERE_SPRITE) || defined(CYLINDER_SPRITE)
gl_FragDepthEXT = calcDepthForSprites(pixelPosEye, zOffset, projectionMatrix);
#endif
return;
#endif
float totalOpacity = opacity;
#ifdef ATTR_ALPHA_COLOR
totalOpacity *= alphaCol;
#endif
// discard fully transparent pixels
if (totalOpacity == 0.0) discard;
#ifdef FAKE_OPACITY
// discard pixels in checker pattern
vec2 dm_coord = floor(gl_FragCoord.xy);
dm_coord = fract(dm_coord * 0.5);
if (totalOpacity < 1.0 && (dm_coord.x < 0.5 ^^ dm_coord.y < 0.5)) discard;
vec4 diffuseColor = vec4(diffuse, 1.0);
#else
vec4 diffuseColor = vec4(diffuse, totalOpacity);
#endif
float flipNormal;
#if !defined (SPHERE_SPRITE) && !defined (CYLINDER_SPRITE)
flipNormal = 1.0;
#ifdef DOUBLE_SIDED
flipNormal = float( gl_FrontFacing );
#endif
vec3 normal = normalize( vNormal ) * flipNormal;
#endif
diffuseColor.rgb *= vertexColor;
#if defined(SPHERE_SPRITE) || defined(CYLINDER_SPRITE)
gl_FragDepthEXT = calcDepthForSprites(pixelPosEye, zOffset, projectionMatrix);
#endif
#ifdef NORMALS_TO_G_BUFFER
#if defined (SPHERE_SPRITE) || defined (CYLINDER_SPRITE)
vec3 viewNormaInColor = viewNormalSprites;
#else
vec3 viewNormaInColor = viewNormal;
float frontFaced = float( gl_FrontFacing );
#endif
// [-1, 1] -> [0, 1]
viewNormaInColor = 0.5 * viewNormaInColor + 0.5;
gl_FragData[1] = vec4(viewNormaInColor, frontFaced);
#endif
#if defined(USE_LIGHTS) && NUM_DIR_LIGHTS > 0
vec3 viewDir;
#if defined(SPHERE_SPRITE) || defined(CYLINDER_SPRITE)
viewDir = -pixelPosEye.xyz;
#else
viewDir = vViewPosition;
#endif
GeometricContext geometry = GeometricContext(normal, normalize( viewDir ));
BlinnPhongMaterial material = BlinnPhongMaterial(diffuseColor.rgb, specular, shininess);
vec3 outgoingLight = calcLighting(geometry, material, viewDir);
#else
vec3 outgoingLight = diffuseColor.rgb;
#endif
#ifdef COLOR_FROM_DEPTH
float depth = 0.0;
#if defined(SPHERE_SPRITE) || defined(CYLINDER_SPRITE)
gl_FragDepthEXT = calcDepthForSprites(pixelPosEye, zOffset, projectionMatrix);
depth = gl_FragDepthEXT;
#else
depth = gl_FragCoord.z;
#endif
fragColor = packDepthToRGBA(depth);
return;
#endif
#ifdef COLOR_FROM_POS
fragColor = world2colorMatrix * pixelPosWorld;
#else
#ifdef OVERRIDE_COLOR
fragColor = vec4(fixedColor, diffuseColor.a);
#else
fragColor = vec4(outgoingLight, diffuseColor.a);//vec4(vNormal, 1.0);
#endif
#ifdef USE_FOG
float viewDistance;
#if defined(SPHERE_SPRITE) || defined(CYLINDER_SPRITE)
viewDistance = abs(pixelPosEye.z);
#else
viewDistance = vViewPosition.z;
#endif
float fogFactor = smoothstep( fogNear, fogFar, viewDistance) * fogAlpha;
#ifdef FOG_TRANSPARENT
fragColor.a = fragColor.a * (1.0 - fogFactor);
#else
fragColor.rgb = mix( fragColor.rgb, fogColor, fogFactor );
#endif
#endif
#endif
}