Changeset View
Changeset View
Standalone View
Standalone View
source/blender/draw/modes/shaders/object_grid_frag.glsl
| /* Infinite grid | /* Infinite grid | ||||
| * Author: Clément Foucault */ | * Author: Clément Foucault */ | ||||
| /* We use the normalized local position to avoid precision | /* We use the normalized local position to avoid precision | ||||
| * loss during interpolation. */ | * loss during interpolation. */ | ||||
| in vec3 local_pos; | in vec3 local_pos; | ||||
| out vec4 FragColor; | out vec4 FragColor; | ||||
| uniform vec3 planeAxes; | uniform vec3 planeAxes; | ||||
| uniform vec4 gridSettings; | uniform vec3 screen_vecs[2]; | ||||
brecht: This should be camel case as well then. | |||||
| uniform float gridDistance; | |||||
Done Inline ActionsStyle: Uniform should be camelCase fclem: Style: Uniform should be camelCase | |||||
| uniform float meshSize; | uniform float meshSize; | ||||
| uniform float lineKernel = 0.0; | uniform float lineKernel = 0.0; | ||||
| uniform float gridOneOverLogSubdiv; | |||||
| uniform sampler2D depthBuffer; | uniform sampler2D depthBuffer; | ||||
| #define gridDistance gridSettings.x | |||||
| #define gridResolution gridSettings.y | |||||
| #define gridScale gridSettings.z | |||||
| #define gridSubdiv gridSettings.w | |||||
| #define cameraPos (ViewMatrixInverse[3].xyz) | #define cameraPos (ViewMatrixInverse[3].xyz) | ||||
| uniform int gridFlag; | uniform int gridFlag; | ||||
| #define STEPS_LEN 8 | |||||
| uniform float grid_steps[STEPS_LEN] = float[](0.001, 0.01, 0.1, 1.0, 10.0, 100.0, 1000.0, 10000.0); | |||||
brechtUnsubmitted Done Inline ActionsCamel case. brecht: Camel case. | |||||
| #define AXIS_X (1 << 0) | #define AXIS_X (1 << 0) | ||||
| #define AXIS_Y (1 << 1) | #define AXIS_Y (1 << 1) | ||||
| #define AXIS_Z (1 << 2) | #define AXIS_Z (1 << 2) | ||||
| #define GRID (1 << 3) | #define GRID (1 << 3) | ||||
| #define PLANE_XY (1 << 4) | #define PLANE_XY (1 << 4) | ||||
| #define PLANE_XZ (1 << 5) | #define PLANE_XZ (1 << 5) | ||||
| #define PLANE_YZ (1 << 6) | #define PLANE_YZ (1 << 6) | ||||
| #define GRID_BACK (1 << 9) /* grid is behind objects */ | #define GRID_BACK (1 << 9) /* grid is behind objects */ | ||||
| Show All 33 Lines | vec3 get_axes(vec3 co, vec3 fwidthCos, float line_size) | ||||
| * (make line have the same width under perspective) */ | * (make line have the same width under perspective) */ | ||||
| axes_domain /= fwidthCos; | axes_domain /= fwidthCos; | ||||
| return 1.0 - smoothstep(GRID_LINE_SMOOTH_START, | return 1.0 - smoothstep(GRID_LINE_SMOOTH_START, | ||||
| GRID_LINE_SMOOTH_END, | GRID_LINE_SMOOTH_END, | ||||
| axes_domain - (line_size + lineKernel)); | axes_domain - (line_size + lineKernel)); | ||||
| } | } | ||||
| #define linearstep(p0, p1, v) (clamp(((v) - (p0)) / abs((p1) - (p0)), 0.0, 1.0)) | |||||
| void main() | void main() | ||||
| { | { | ||||
| vec3 wPos = local_pos * meshSize; | vec3 wPos = local_pos * meshSize; | ||||
| vec3 fwidthPos = fwidth(wPos); | vec3 dFdxPos = dFdx(wPos); | ||||
| vec3 dFdyPos = dFdy(wPos); | |||||
| vec3 fwidthPos = abs(dFdxPos) + abs(dFdyPos); | |||||
| wPos += cameraPos * planeAxes; | wPos += cameraPos * planeAxes; | ||||
| float dist, fade; | float dist, fade; | ||||
| /* if persp */ | /* if persp */ | ||||
| if (ProjectionMatrix[3][3] == 0.0) { | if (ProjectionMatrix[3][3] == 0.0) { | ||||
| vec3 viewvec = cameraPos - wPos; | vec3 viewvec = cameraPos - wPos; | ||||
| dist = length(viewvec); | dist = length(viewvec); | ||||
| viewvec /= dist; | viewvec /= dist; | ||||
| Show All 23 Lines | if ((gridFlag & PLANE_XY) != 0) { | ||||
| float angle = 1.0 - abs(ViewMatrixInverse[2].z); | float angle = 1.0 - abs(ViewMatrixInverse[2].z); | ||||
| dist = 1.0 + angle * 2.0; | dist = 1.0 + angle * 2.0; | ||||
| angle *= angle; | angle *= angle; | ||||
| fade *= 1.0 - angle * angle; | fade *= 1.0 - angle * angle; | ||||
| } | } | ||||
| } | } | ||||
| if ((gridFlag & GRID) != 0) { | if ((gridFlag & GRID) != 0) { | ||||
| float grid_res = log(dist * gridResolution) * gridOneOverLogSubdiv; | //float grid_res = max(dot(dFdxPos, screen_vecs[0]), dot(dFdyPos, screen_vecs[1])); | ||||
brechtUnsubmitted Done Inline ActionsDon't leave commented out code without explaining why it is commented out. brecht: Don't leave commented out code without explaining why it is commented out. | |||||
| float grid_res = dot(dFdxPos, screen_vecs[0]); | |||||
| float blend = fract(-max(grid_res, 0.0)); | /* The gride begins to appear when it comprises 4 pixels */ | ||||
| float lvl = floor(grid_res); | grid_res *= 4; | ||||
| /* from biggest to smallest */ | /* from biggest to smallest */ | ||||
| float scaleA = gridScale * pow(gridSubdiv, max(lvl - 1.0, 0.0)); | vec4 scale; | ||||
| float scaleB = gridScale * pow(gridSubdiv, max(lvl + 0.0, 0.0)); | #if 0 | ||||
| float scaleC = gridScale * pow(gridSubdiv, max(lvl + 1.0, 1.0)); | int step_id = 0; | ||||
| scale[0] = 0.0; | |||||
| scale[1] = grid_steps[0]; | |||||
| while (scale[1] < grid_res && step_id != STEPS_LEN - 1) { | |||||
| scale[0] = scale[1]; | |||||
| scale[1] = grid_steps[++step_id]; | |||||
| } | |||||
| scale[2] = grid_steps[min(step_id + 1, STEPS_LEN - 1)]; | |||||
| scale[3] = grid_steps[min(step_id + 2, STEPS_LEN - 1)]; | |||||
| #else | |||||
| /* For more efficiency, unroll the loop above. */ | |||||
| if (grid_steps[0] > grid_res) { | |||||
| scale = vec4(0.0, grid_steps[0], grid_steps[1], grid_steps[2]); | |||||
| } | |||||
| else if (grid_steps[1] > grid_res) { | |||||
| scale = vec4(grid_steps[0], grid_steps[1], grid_steps[2], grid_steps[3]); | |||||
| } | |||||
| else if (grid_steps[2] > grid_res) { | |||||
| scale = vec4(grid_steps[1], grid_steps[2], grid_steps[3], grid_steps[4]); | |||||
| } | |||||
| else if (grid_steps[3] > grid_res) { | |||||
| scale = vec4(grid_steps[2], grid_steps[3], grid_steps[4], grid_steps[5]); | |||||
| } | |||||
| else if (grid_steps[4] > grid_res) { | |||||
| scale = vec4(grid_steps[3], grid_steps[4], grid_steps[5], grid_steps[6]); | |||||
| } | |||||
| else if (grid_steps[5] > grid_res) { | |||||
| scale = vec4(grid_steps[4], grid_steps[5], grid_steps[6], grid_steps[7]); | |||||
| } | |||||
| else if (grid_steps[6] > grid_res) { | |||||
| scale = vec4(grid_steps[5], grid_steps[6], grid_steps[7], grid_steps[7]); | |||||
| } | |||||
| else { | |||||
| scale = vec4(grid_steps[6], grid_steps[7], grid_steps[7], grid_steps[7]); | |||||
| } | |||||
| #endif | |||||
| float blend = 1.0 - linearstep(scale[0], scale[1], grid_res); | |||||
| blend = blend * blend * blend; | |||||
| vec2 grid_pos, grid_fwidth; | vec2 grid_pos, grid_fwidth; | ||||
| if ((gridFlag & PLANE_XZ) != 0) { | if ((gridFlag & PLANE_XZ) != 0) { | ||||
| grid_pos = wPos.xz; | grid_pos = wPos.xz; | ||||
| grid_fwidth = fwidthPos.xz; | grid_fwidth = fwidthPos.xz; | ||||
| } | } | ||||
| else if ((gridFlag & PLANE_YZ) != 0) { | else if ((gridFlag & PLANE_YZ) != 0) { | ||||
| grid_pos = wPos.yz; | grid_pos = wPos.yz; | ||||
| grid_fwidth = fwidthPos.yz; | grid_fwidth = fwidthPos.yz; | ||||
| } | } | ||||
| else { | else { | ||||
| grid_pos = wPos.xy; | grid_pos = wPos.xy; | ||||
| grid_fwidth = fwidthPos.xy; | grid_fwidth = fwidthPos.xy; | ||||
| } | } | ||||
| float gridA = get_grid(grid_pos, grid_fwidth, scaleA); | float gridA = get_grid(grid_pos, grid_fwidth, scale[1]); | ||||
| float gridB = get_grid(grid_pos, grid_fwidth, scaleB); | float gridB = get_grid(grid_pos, grid_fwidth, scale[2]); | ||||
| float gridC = get_grid(grid_pos, grid_fwidth, scaleC); | float gridC = get_grid(grid_pos, grid_fwidth, scale[3]); | ||||
| FragColor = colorGrid; | FragColor = colorGrid; | ||||
| FragColor.a *= gridA * blend; | FragColor.a *= gridA * blend; | ||||
| FragColor = mix(FragColor, mix(colorGrid, colorGridEmphasise, blend), gridB); | FragColor = mix(FragColor, mix(colorGrid, colorGridEmphasise, blend), gridB); | ||||
| FragColor = mix(FragColor, colorGridEmphasise, gridC); | FragColor = mix(FragColor, colorGridEmphasise, gridC); | ||||
| } | } | ||||
| else { | else { | ||||
| FragColor = vec4(colorGrid.rgb, 0.0); | FragColor = vec4(colorGrid.rgb, 0.0); | ||||
| ▲ Show 20 Lines • Show All 55 Lines • Show Last 20 Lines | |||||
This should be camel case as well then.