From 120492ace9150913f106f329942684c583a6f697 Mon Sep 17 00:00:00 2001 From: Sergey Sharybin Date: Fri, 20 May 2016 14:16:54 +0200 Subject: Cycles: Support bump mapping in GLSL viewport This commit implements Bump node in GLSL, making it possible to see previews of bump mapping in viewport without need to render. Nothing really fancy going on here, just uses internal dFdx/dFdy functions to get derivatives of the surface and map itself. Quite basic but seems to behave correct-ish. This commit also makes Displacement material output to affect viewport shading by re-linking unconnected Normal input to a node which was used for displacement output (via Bump node). Intention of all this is to make it really easy to do bump map painting with Cycles as an active render engine. Reviewers: campbellbarton, mont29, brecht, psy-fi Reviewed By: brecht Subscribers: Blendify, eyecandy Differential Revision: https://developer.blender.org/D2014 --- .../blender/gpu/shaders/gpu_shader_material.glsl | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'source/blender/gpu') diff --git a/source/blender/gpu/shaders/gpu_shader_material.glsl b/source/blender/gpu/shaders/gpu_shader_material.glsl index dea5b994e74..28bf99b83dc 100644 --- a/source/blender/gpu/shaders/gpu_shader_material.glsl +++ b/source/blender/gpu/shaders/gpu_shader_material.glsl @@ -3160,9 +3160,27 @@ void node_normal_map(vec4 tangent, vec3 normal, vec3 texnormal, out vec3 outnorm outnormal = normalize(outnormal); } -void node_bump(float strength, float dist, float height, vec3 N, out vec3 result) +void node_bump(float strength, float dist, float height, vec3 N, vec3 surf_pos, out vec3 result) { - result = N; + vec3 dPdx = dFdx(surf_pos); + vec3 dPdy = dFdy(surf_pos); + + /* Get surface tangents from normal. */ + vec3 Rx = cross(dPdy, N); + vec3 Ry = cross(N, dPdx); + + /* Compute surface gradient and determinant. */ + float det = dot(dPdx, Rx); + float absdet = abs(det); + + float dHdx = dFdx(height); + float dHdy = dFdy(height); + vec3 surfgrad = dHdx*Rx + dHdy*Ry; + + strength = max(strength, 0.0); + + result = normalize(absdet*N - dist*sign(det)*surfgrad); + result = normalize(strength*result + (1.0 - strength)*N); } /* output */ -- cgit v1.2.3