|
| 1 | +// Copyright (c) 2016 The UUV Simulator Authors. |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +#version 330 |
| 17 | + |
| 18 | +////////// Input parameters ////////// |
| 19 | +// Textures |
| 20 | +uniform sampler2D bumpMap; |
| 21 | +uniform samplerCube cubeMap; |
| 22 | + |
| 23 | +// Colors |
| 24 | +uniform vec4 deepColor; |
| 25 | +uniform vec4 shallowColor; |
| 26 | +uniform float fresnelPower; |
| 27 | +uniform float hdrMultiplier; |
| 28 | + |
| 29 | +////////// Input computed in vertex shader ////////// |
| 30 | +in block |
| 31 | +{ |
| 32 | + mat3 rotMatrix; |
| 33 | + vec3 eyeVec; |
| 34 | + vec2 bumpCoord; |
| 35 | +} inPs; |
| 36 | + |
| 37 | +out vec4 fragColor; |
| 38 | + |
| 39 | +void main() |
| 40 | +{ |
| 41 | + // Apply bump mapping to normal vector to make waves look more detailed: |
| 42 | + vec4 bump = texture(bumpMap, inPs.bumpCoord)*2.0 - 1.0; |
| 43 | + vec3 N = normalize(inPs.rotMatrix * bump.xyz); |
| 44 | + |
| 45 | + // Reflected ray: |
| 46 | + vec3 E = normalize(inPs.eyeVec); |
| 47 | + vec3 R = reflect(E, N); |
| 48 | + |
| 49 | + // negate z for use with the skybox texture that comes with gz-rendering |
| 50 | + R = vec3(R.x, R.y, -R.z); |
| 51 | + |
| 52 | + // uncomment this line if using other textures that are Y up |
| 53 | + // Gazebo requires rotated cube map lookup. |
| 54 | + // R = vec3(R.x, R.z, R.y); |
| 55 | + |
| 56 | + // Get environment color of reflected ray: |
| 57 | + vec4 envColor = texture(cubeMap, R, 0.0); |
| 58 | + |
| 59 | + // Cheap hdr effect: |
| 60 | + envColor.rgb *= (envColor.r+envColor.g+envColor.b)*hdrMultiplier; |
| 61 | + |
| 62 | + // Compute refraction ratio (Fresnel): |
| 63 | + float facing = 1.0 - dot(-E, N); |
| 64 | + float waterEnvRatio = clamp(pow(facing, fresnelPower), 0.05, 1.0); |
| 65 | + |
| 66 | + // Refracted ray only considers deep and shallow water colors: |
| 67 | + vec4 waterColor = mix(shallowColor, deepColor, facing); |
| 68 | + |
| 69 | + // Perform linear interpolation between reflection and refraction. |
| 70 | + vec4 color = mix(waterColor, envColor, waterEnvRatio); |
| 71 | + |
| 72 | + fragColor = vec4(color.xyz, 0.9); |
| 73 | +} |
0 commit comments