envmap_fragment.glsl 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #ifdef USE_ENVMAP
  2. #if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG ) || defined( TOON )
  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, flipNormal * vec3( flipEnvMap * reflectVec.x, reflectVec.yz ) );
  16. #elif defined( ENVMAP_TYPE_EQUIREC )
  17. vec2 sampleUV;
  18. sampleUV.y = saturate( flipNormal * reflectVec.y * 0.5 + 0.5 );
  19. sampleUV.x = atan( flipNormal * reflectVec.z, flipNormal * reflectVec.x ) * RECIPROCAL_PI2 + 0.5;
  20. vec4 envColor = texture2D( envMap, sampleUV );
  21. #elif defined( ENVMAP_TYPE_SPHERE )
  22. vec3 reflectView = flipNormal * normalize( ( viewMatrix * vec4( reflectVec, 0.0 ) ).xyz + vec3( 0.0, 0.0, 1.0 ) );
  23. vec4 envColor = texture2D( envMap, reflectView.xy * 0.5 + 0.5 );
  24. #else
  25. vec4 envColor = vec4( 0.0 );
  26. #endif
  27. envColor = envMapTexelToLinear( envColor );
  28. #ifdef ENVMAP_BLENDING_MULTIPLY
  29. outgoingLight = mix( outgoingLight, outgoingLight * envColor.xyz, specularStrength * reflectivity );
  30. #elif defined( ENVMAP_BLENDING_MIX )
  31. outgoingLight = mix( outgoingLight, envColor.xyz, specularStrength * reflectivity );
  32. #elif defined( ENVMAP_BLENDING_ADD )
  33. outgoingLight += envColor.xyz * specularStrength * reflectivity;
  34. #endif
  35. #endif