2
0

envmap_fragment.glsl 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #ifdef USE_ENVMAP
  2. #if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG )
  3. vec3 cameraToVertex = normalize( vWorldPosition - cameraPosition );
  4. // Transforming Normal Vectors with the Inverse Transformation
  5. vec3 worldNormal = inverseTransformDirection( normal, viewMatrix );
  6. #ifdef ENVMAP_MODE_REFLECTION
  7. vec3 reflectVec = reflect( cameraToVertex, worldNormal );
  8. #else
  9. vec3 reflectVec = refract( cameraToVertex, worldNormal, refractionRatio );
  10. #endif
  11. #else
  12. vec3 reflectVec = vReflect;
  13. #endif
  14. #ifdef DOUBLE_SIDED
  15. float flipNormal = ( -1.0 + 2.0 * float( gl_FrontFacing ) );
  16. #else
  17. float flipNormal = 1.0;
  18. #endif
  19. #ifdef ENVMAP_TYPE_CUBE
  20. vec4 envColor = textureCube( envMap, flipNormal * vec3( flipEnvMap * reflectVec.x, reflectVec.yz ) );
  21. #elif defined( ENVMAP_TYPE_EQUIREC )
  22. vec2 sampleUV;
  23. sampleUV.y = saturate( flipNormal * reflectVec.y * 0.5 + 0.5 );
  24. sampleUV.x = atan( flipNormal * reflectVec.z, flipNormal * reflectVec.x ) * RECIPROCAL_2PI + 0.5;
  25. vec4 envColor = texture2D( envMap, sampleUV );
  26. #elif defined( ENVMAP_TYPE_SPHERE )
  27. vec3 reflectView = flipNormal * ( transformDirection( reflectVec, viewMatrix ) + vec3(0.0,0.0,1.0) );
  28. vec4 envColor = texture2D( envMap, reflectView.xy * 0.5 + 0.5 );
  29. #endif
  30. #ifdef GAMMA_INPUT
  31. envColor.xyz *= envColor.xyz;
  32. #endif
  33. #ifdef ENVMAP_BLENDING_MULTIPLY
  34. gl_FragColor.xyz = mix( gl_FragColor.xyz, gl_FragColor.xyz * envColor.xyz, specularStrength * reflectivity );
  35. #elif defined( ENVMAP_BLENDING_MIX )
  36. gl_FragColor.xyz = mix( gl_FragColor.xyz, envColor.xyz, specularStrength * reflectivity );
  37. #elif defined( ENVMAP_BLENDING_ADD )
  38. gl_FragColor.xyz += envColor.xyz * specularStrength * reflectivity;
  39. #endif
  40. #endif