|
@@ -10,7 +10,7 @@
|
|
|
THREE.ShaderLib[ 'water' ] = {
|
|
|
|
|
|
uniforms: THREE.UniformsUtils.merge( [
|
|
|
- THREE.UniformsLib[ "fog" ], {
|
|
|
+ THREE.UniformsLib[ "fog" ], {
|
|
|
"normalSampler": { type: "t", value: null },
|
|
|
"mirrorSampler": { type: "t", value: null },
|
|
|
"alpha": { type: "f", value: 1.0 },
|
|
@@ -18,10 +18,10 @@ THREE.ShaderLib[ 'water' ] = {
|
|
|
"distortionScale": { type: "f", value: 20.0 },
|
|
|
"noiseScale": { type: "f", value: 1.0 },
|
|
|
"textureMatrix" : { type: "m4", value: new THREE.Matrix4() },
|
|
|
- "sunColor": { type: "c", value: new THREE.Color(0x7F7F7F) },
|
|
|
- "sunDirection": { type: "v3", value: new THREE.Vector3(0.70707, 0.70707, 0) },
|
|
|
- "eye": { type: "v3", value: new THREE.Vector3(0, 0, 0) },
|
|
|
- "waterColor": { type: "c", value: new THREE.Color(0x555555) }
|
|
|
+ "sunColor": { type: "c", value: new THREE.Color( 0x7F7F7F ) },
|
|
|
+ "sunDirection": { type: "v3", value: new THREE.Vector3( 0.70707, 0.70707, 0 ) },
|
|
|
+ "eye": { type: "v3", value: new THREE.Vector3() },
|
|
|
+ "waterColor": { type: "c", value: new THREE.Color( 0x555555 ) }
|
|
|
}
|
|
|
] ),
|
|
|
|
|
@@ -31,7 +31,7 @@ THREE.ShaderLib[ 'water' ] = {
|
|
|
|
|
|
'varying vec4 mirrorCoord;',
|
|
|
'varying vec3 worldPosition;',
|
|
|
-
|
|
|
+
|
|
|
'void main()',
|
|
|
'{',
|
|
|
' mirrorCoord = modelMatrix * vec4( position, 1.0 );',
|
|
@@ -43,7 +43,7 @@ THREE.ShaderLib[ 'water' ] = {
|
|
|
|
|
|
fragmentShader: [
|
|
|
'precision highp float;',
|
|
|
-
|
|
|
+
|
|
|
'uniform sampler2D mirrorSampler;',
|
|
|
'uniform float alpha;',
|
|
|
'uniform float time;',
|
|
@@ -56,20 +56,20 @@ THREE.ShaderLib[ 'water' ] = {
|
|
|
|
|
|
'varying vec4 mirrorCoord;',
|
|
|
'varying vec3 worldPosition;',
|
|
|
-
|
|
|
+
|
|
|
'vec4 getNoise( vec2 uv )',
|
|
|
'{',
|
|
|
' vec2 uv0 = ( uv / 103.0 ) + vec2(time / 17.0, time / 29.0);',
|
|
|
' vec2 uv1 = uv / 107.0-vec2( time / -19.0, time / 31.0 );',
|
|
|
' vec2 uv2 = uv / vec2( 8907.0, 9803.0 ) + vec2( time / 101.0, time / 97.0 );',
|
|
|
' vec2 uv3 = uv / vec2( 1091.0, 1027.0 ) - vec2( time / 109.0, time / -113.0 );',
|
|
|
- ' vec4 noise = ( texture2D( normalSampler, uv0 ) ) +',
|
|
|
- ' ( texture2D( normalSampler, uv1 ) ) +',
|
|
|
- ' ( texture2D( normalSampler, uv2 ) ) +',
|
|
|
- ' ( texture2D( normalSampler, uv3 ) );',
|
|
|
+ ' vec4 noise = texture2D( normalSampler, uv0 ) +',
|
|
|
+ ' texture2D( normalSampler, uv1 ) +',
|
|
|
+ ' texture2D( normalSampler, uv2 ) +',
|
|
|
+ ' texture2D( normalSampler, uv3 );',
|
|
|
' return noise * 0.5 - 1.0;',
|
|
|
'}',
|
|
|
-
|
|
|
+
|
|
|
'void sunLight( const vec3 surfaceNormal, const vec3 eyeDirection, float shiny, float spec, float diffuse, inout vec3 diffuseColor, inout vec3 specularColor )',
|
|
|
'{',
|
|
|
' vec3 reflection = normalize( reflect( -sunDirection, surfaceNormal ) );',
|
|
@@ -79,7 +79,7 @@ THREE.ShaderLib[ 'water' ] = {
|
|
|
'}',
|
|
|
|
|
|
THREE.ShaderChunk[ "common" ],
|
|
|
- THREE.ShaderChunk[ "fog_pars_fragment" ],
|
|
|
+ THREE.ShaderChunk[ "fog_pars_fragment" ],
|
|
|
|
|
|
'void main()',
|
|
|
'{',
|
|
@@ -92,7 +92,7 @@ THREE.ShaderLib[ 'water' ] = {
|
|
|
' vec3 worldToEye = eye-worldPosition;',
|
|
|
' vec3 eyeDirection = normalize( worldToEye );',
|
|
|
' sunLight( surfaceNormal, eyeDirection, 100.0, 2.0, 0.5, diffuseLight, specularLight );',
|
|
|
-
|
|
|
+
|
|
|
' float distance = length(worldToEye);',
|
|
|
|
|
|
' vec2 distortion = surfaceNormal.xz * ( 0.001 + 1.0 / distance ) * distortionScale;',
|
|
@@ -105,7 +105,7 @@ THREE.ShaderLib[ 'water' ] = {
|
|
|
' vec3 albedo = mix( sunColor * diffuseLight * 0.3 + scatter, ( vec3( 0.1 ) + reflectionSample * 0.9 + reflectionSample * specularLight ), reflectance );',
|
|
|
' vec3 outgoingLight = albedo;',
|
|
|
THREE.ShaderChunk[ "fog_fragment" ],
|
|
|
- ' gl_FragColor = vec4( outgoingLight, alpha );',
|
|
|
+ ' gl_FragColor = vec4( outgoingLight, alpha );',
|
|
|
'}'
|
|
|
].join( '\n' )
|
|
|
|
|
@@ -123,9 +123,9 @@ THREE.Water = function ( renderer, camera, scene, options ) {
|
|
|
}
|
|
|
|
|
|
options = options || {};
|
|
|
-
|
|
|
+
|
|
|
this.matrixNeedsUpdate = true;
|
|
|
-
|
|
|
+
|
|
|
var width = optionalParameter( options.textureWidth, 512 );
|
|
|
var height = optionalParameter( options.textureHeight, 512 );
|
|
|
this.clipBias = optionalParameter( options.clipBias, 0.0 );
|
|
@@ -137,8 +137,8 @@ THREE.Water = function ( renderer, camera, scene, options ) {
|
|
|
this.waterColor = new THREE.Color( optionalParameter( options.waterColor, 0x7F7F7F ) );
|
|
|
this.eye = optionalParameter( options.eye, new THREE.Vector3( 0, 0, 0 ) );
|
|
|
this.distortionScale = optionalParameter( options.distortionScale, 20.0 );
|
|
|
- this.side = optionalParameter(options.side, THREE.FrontSide);
|
|
|
- this.fog = optionalParameter(options.fog, false);
|
|
|
+ this.side = optionalParameter( options.side, THREE.FrontSide );
|
|
|
+ this.fog = optionalParameter( options.fog, false );
|
|
|
|
|
|
this.renderer = renderer;
|
|
|
this.scene = scene;
|
|
@@ -149,33 +149,33 @@ THREE.Water = function ( renderer, camera, scene, options ) {
|
|
|
this.rotationMatrix = new THREE.Matrix4();
|
|
|
this.lookAtPosition = new THREE.Vector3( 0, 0, - 1 );
|
|
|
this.clipPlane = new THREE.Vector4();
|
|
|
-
|
|
|
+
|
|
|
if ( camera instanceof THREE.PerspectiveCamera )
|
|
|
this.camera = camera;
|
|
|
else {
|
|
|
|
|
|
this.camera = new THREE.PerspectiveCamera();
|
|
|
- console.log( this.name + ': camera is not a Perspective Camera!' )
|
|
|
+ console.log( this.name + ': camera is not a Perspective Camera!' );
|
|
|
|
|
|
}
|
|
|
|
|
|
this.textureMatrix = new THREE.Matrix4();
|
|
|
|
|
|
this.mirrorCamera = this.camera.clone();
|
|
|
-
|
|
|
+
|
|
|
this.texture = new THREE.WebGLRenderTarget( width, height );
|
|
|
this.tempTexture = new THREE.WebGLRenderTarget( width, height );
|
|
|
-
|
|
|
+
|
|
|
var mirrorShader = THREE.ShaderLib[ "water" ];
|
|
|
var mirrorUniforms = THREE.UniformsUtils.clone( mirrorShader.uniforms );
|
|
|
|
|
|
- this.material = new THREE.ShaderMaterial( {
|
|
|
- fragmentShader: mirrorShader.fragmentShader,
|
|
|
- vertexShader: mirrorShader.vertexShader,
|
|
|
+ this.material = new THREE.ShaderMaterial( {
|
|
|
+ fragmentShader: mirrorShader.fragmentShader,
|
|
|
+ vertexShader: mirrorShader.vertexShader,
|
|
|
uniforms: mirrorUniforms,
|
|
|
transparent: true,
|
|
|
side: this.side,
|
|
|
- fog: this.fog
|
|
|
+ fog: this.fog
|
|
|
} );
|
|
|
|
|
|
this.material.uniforms.mirrorSampler.value = this.texture;
|
|
@@ -187,9 +187,9 @@ THREE.Water = function ( renderer, camera, scene, options ) {
|
|
|
this.material.uniforms.waterColor.value = this.waterColor;
|
|
|
this.material.uniforms.sunDirection.value = this.sunDirection;
|
|
|
this.material.uniforms.distortionScale.value = this.distortionScale;
|
|
|
-
|
|
|
+
|
|
|
this.material.uniforms.eye.value = this.eye;
|
|
|
-
|
|
|
+
|
|
|
if ( ! THREE.Math.isPowerOfTwo( width ) || ! THREE.Math.isPowerOfTwo( height ) ) {
|
|
|
|
|
|
this.texture.generateMipmaps = false;
|
|
@@ -286,7 +286,7 @@ THREE.Water.prototype.updateTextureMatrix = function () {
|
|
|
projectionMatrix.elements[ 6 ] = c.y;
|
|
|
projectionMatrix.elements[ 10 ] = c.z + 1.0 - this.clipBias;
|
|
|
projectionMatrix.elements[ 14 ] = c.w;
|
|
|
-
|
|
|
+
|
|
|
var worldCoordinates = new THREE.Vector3();
|
|
|
worldCoordinates.setFromMatrixPosition( this.camera.matrixWorld );
|
|
|
this.eye = worldCoordinates;
|