envmap_fragment.glsl 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 ENVMAP_TYPE_CUBE
  15. vec4 envColor = textureCube( envMap, vec3( flipEnvMap * reflectVec.x, reflectVec.yz ) );
  16. #elif defined( ENVMAP_TYPE_EQUIREC )
  17. vec2 sampleUV;
  18. reflectVec = normalize( reflectVec );
  19. sampleUV.y = asin( clamp( reflectVec.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;
  20. sampleUV.x = atan( reflectVec.z, reflectVec.x ) * RECIPROCAL_PI2 + 0.5;
  21. vec4 envColor = texture2D( envMap, sampleUV );
  22. #elif defined( ENVMAP_TYPE_SPHERE )
  23. reflectVec = normalize( reflectVec );
  24. vec3 reflectView = normalize( ( viewMatrix * vec4( reflectVec, 0.0 ) ).xyz + vec3( 0.0, 0.0, 1.0 ) );
  25. vec4 envColor = texture2D( envMap, reflectView.xy * 0.5 + 0.5 );
  26. #else
  27. vec4 envColor = vec4( 0.0 );
  28. #endif
  29. envColor = envMapTexelToLinear( envColor );
  30. #ifdef ENVMAP_BLENDING_MULTIPLY
  31. outgoingLight = mix( outgoingLight, outgoingLight * envColor.xyz, specularStrength * reflectivity );
  32. #elif defined( ENVMAP_BLENDING_MIX )
  33. outgoingLight = mix( outgoingLight, envColor.xyz, specularStrength * reflectivity );
  34. #elif defined( ENVMAP_BLENDING_ADD )
  35. outgoingLight += envColor.xyz * specularStrength * reflectivity;
  36. #endif
  37. #endif