Przeglądaj źródła

Update ProjectionMatrix on change aspect

gam0022 7 lat temu
rodzic
commit
881b25b58d
1 zmienionych plików z 9 dodań i 3 usunięć
  1. 9 3
      examples/webgl_raymarching_reflect.html

+ 9 - 3
examples/webgl_raymarching_reflect.html

@@ -202,10 +202,13 @@
 			void main(void) {
 
 				// screen position
-				vec2 screenPos = ( gl_FragCoord.xy * 2.0 - resolution ) / min( resolution.x, resolution.y );
+				vec2 screenPos = ( gl_FragCoord.xy * 2.0 - resolution ) / resolution;
 
-				// convert ray direction from screen coordinate to world coordinate
-				vec3 ray = (cameraWorldMatrix * cameraProjectionMatrixInverse * vec4( screenPos.xy, 1.0, 1.0 )).xyz;
+				// ray direction in normalized device coordinate
+				vec4 ndcRay = vec4( screenPos.xy, 1.0, 1.0 );
+
+				// convert ray direction from normalized device coordinate to world coordinate
+				vec3 ray = ( cameraWorldMatrix * cameraProjectionMatrixInverse * ndcRay ).xyz;
 				ray = normalize( ray );
 
 				// camera position
@@ -371,6 +374,9 @@
 
 				}
 
+				camera.aspect = canvas.width / canvas.height;
+				camera.updateProjectionMatrix();
+
 			}
 
 		</script>