diff options
| author | Loic Guegan <manzerbredes@mailbox.org> | 2020-07-09 13:02:31 +0200 |
|---|---|---|
| committer | Loic Guegan <manzerbredes@mailbox.org> | 2020-07-09 13:02:31 +0200 |
| commit | 12829892b262a0c7fcccba9198e5b6b31b2a8015 (patch) | |
| tree | 908152ccc4fd1f4ee2c16b4bb8e0bee215ebdae7 /resources/shaders/algorithms/normal.frag | |
| parent | aac94e911b0bab8db5cdb5efb8d4d8f2d4072610 (diff) | |
Diffstat (limited to 'resources/shaders/algorithms/normal.frag')
| -rw-r--r-- | resources/shaders/algorithms/normal.frag | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/resources/shaders/algorithms/normal.frag b/resources/shaders/algorithms/normal.frag new file mode 100644 index 0000000..0f61544 --- /dev/null +++ b/resources/shaders/algorithms/normal.frag @@ -0,0 +1,24 @@ +// Requirements: +// #define NORMAL_ACCURACY 0.01 +// GetDist: (vec3 position) => float +// GetDist should return the closest object point distance from position (in the whole scene) + +/** + * Compute the normal of an object at a given surface point + * this can be usefull for lighting etc.. + * This function exploit the fact that the gradient on a given surface point in 3D space + * given the normal direction (source: https://fr.wikipedia.org/wiki/Gradient#Dimension_3_:_gradient_normal_%C3%A0_une_surface_en_un_point,_plan_tangent) + * + * + */ +vec3 GetNormal(vec3 point_position) { + float point_distance = GetDist(point_position); + vec2 epsilon = vec2(NORMAL_ACCURACY, 0); // Just a convenient way to use NORMAL_ACCURACY and 0 + // Compute the gradient + vec3 gradient = point_distance - vec3( + GetDist(point_position-epsilon.xyy), + GetDist(point_position-epsilon.yxy), + GetDist(point_position-epsilon.yyx)); + + return normalize(gradient); +}
\ No newline at end of file |
