Преглед изворни кода

Changed water reset feedback to sample 1 point

yomboprime пре 6 година
родитељ
комит
8d3cae2f28
1 измењених фајлова са 26 додато и 25 уклоњено
  1. 26 25
      examples/webgl_gpgpu_water.html

+ 26 - 25
examples/webgl_gpgpu_water.html

@@ -127,7 +127,7 @@
 
 		</script>
 		
-		<!-- This is a 'compute shader' to calculate the current volume of water -->
+		<!-- This is a 'compute shader' to calculate the current volume of water at the center -->
 		<!-- It is used with a variable of size 1x1 -->
 		<script id="sumFragmentShader" type="x-shader/x-fragment">
 		
@@ -136,19 +136,31 @@
 			// Integer to float conversion from https://stackoverflow.com/questions/17981163/webgl-read-pixels-from-floating-point-render-target
 			
 			float shift_right( float v, float amt ) {
+
 				v = floor( v ) + 0.5;
 				return floor( v / exp2( amt ) );
+
 			}
+
 			float shift_left( float v, float amt ) { 
+
 				return floor( v * exp2( amt ) + 0.5 );
+
 			}
+
 			float mask_last( float v, float bits ) {
+
 				return mod( v, shift_left( 1.0, bits ) );
+
 			}
+
 			float extract_bits( float num, float from, float to ) {
+
 				from = floor( from + 0.5 ); to = floor( to + 0.5 );
 				return mask_last( shift_right( num, from ), to - from );
+
 			}
+
 			vec4 encode_float( float val ) {
 				if ( val == 0.0 ) return vec4( 0, 0, 0, 0 );
 				float sign = val > 0.0 ? 0.0 : 1.0;
@@ -168,26 +180,9 @@
 
 			void main()	{
 
-				vec2 cellSize = 1.0 / resolution.xy;
-				
-				float volume = 0.0;
-				
-				const int rx = int( resolution.x );
-				const int ry = int( resolution.y );
-
-				// Sum of water height over all water cells
-				for ( int j = 0; j < ry; j++ ) {
-					for ( int i = 0; i < rx; i++ ) {
-					
-						vec2 uv = vec2( i, j ) * cellSize;
-						vec4 textureValue = texture2D( texture, uv );
-						
-						volume += textureValue.x;
-
-					}
-				}
+				float waterLevel = texture2D( texture, vec2( 0.5, 0.5 ) ).x;
 
-				gl_FragColor = encode_float( volume );
+				gl_FragColor = encode_float( waterLevel );
 
 			}
 
@@ -559,7 +554,7 @@
 
 			function readWaterLevel() {
 				
-				// Returns current average water level
+				// Returns current water level at the center
 
 				var currentRenderTarget = gpuCompute.getCurrentRenderTarget( heightmapVariable );
 
@@ -572,7 +567,7 @@
 
 				var pixels = new Float32Array( readVolumeImage.buffer );
 
-				return pixels[ 0 ] / NUM_TEXELS;
+				return pixels[ 0 ];
 
 			}
 
@@ -667,18 +662,24 @@
 				
 					numFrames = 0;
 				
-					heightCompensation = readWaterLevel();
+					heightCompensation = - readWaterLevel();
 
 				}
 
 				// Apply gradually height compensation to reset water level
 				if ( heightCompensation !== 0 ) {
 				
-					var decremHeight = Math.min( 1, Math.abs( heightCompensation ) ) * Math.sign( heightCompensation );
+					var decremHeight = Math.min( 0.2, Math.abs( heightCompensation ) ) * Math.sign( heightCompensation );
 					heightCompensation -= decremHeight;
-					heightmapVariable.material.uniforms.heightCompensation.value = - decremHeight * Math.sign( heightCompensation );
+					heightmapVariable.material.uniforms.heightCompensation.value = decremHeight;
 					
 				}
+				else {
+				
+					heightmapVariable.material.uniforms.heightCompensation.value = 0;
+
+				}
+				
 
 				// Do the gpu computation
 				gpuCompute.compute();