Skip to content

Commit

Permalink
UPBGE: Implement parallax material node.
Browse files Browse the repository at this point in the history
This new node allow the user to use parallax in his material node tree.
This node tree takes as input the UV, image, steps count, bump scale and discard
flag, and gave as output the shifted UV.

Internally this node share some part with the texture node to manage the
image.

Fix issue: #476.
  • Loading branch information
panzergame committed Jul 30, 2017
1 parent 63013b4 commit 5d8cd9c
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 2 deletions.
2 changes: 2 additions & 0 deletions release/scripts/startup/nodeitems_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def object_shader_nodes_poll(context):
ShaderOldNodeCategory("SH_OP_VECTOR", "Vector", items=[
NodeItem("ShaderNodeNormal"),
NodeItem("ShaderNodeMapping"),
NodeItem("ShaderNodeParallax"),
NodeItem("ShaderNodeVectorCurve"),
NodeItem("ShaderNodeVectorTransform"),
NodeItem("ShaderNodeNormalMap"),
Expand Down Expand Up @@ -274,6 +275,7 @@ def object_shader_nodes_poll(context):
NodeItem("ShaderNodeBump"),
NodeItem("ShaderNodeNormalMap"),
NodeItem("ShaderNodeNormal"),
NodeItem("ShaderNodeParallax"),
NodeItem("ShaderNodeVectorCurve"),
NodeItem("ShaderNodeVectorTransform"),
]),
Expand Down
1 change: 1 addition & 0 deletions source/blender/blenkernel/BKE_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,7 @@ struct ShadeResult;
#define SH_NODE_BSDF_PRINCIPLED 193
#define SH_NODE_OBJECT 194
#define SH_NODE_TIME 195
#define SH_NODE_PARALLAX 196

/* custom defines options for Material node */
#define SH_NODE_MAT_DIFF 1
Expand Down
1 change: 1 addition & 0 deletions source/blender/blenkernel/intern/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -3551,6 +3551,7 @@ static void registerShaderNodes(void)
register_node_type_sh_normal();
register_node_type_sh_geom();
register_node_type_sh_mapping();
register_node_type_sh_parallax();
register_node_type_sh_curve_vec();
register_node_type_sh_curve_rgb();
register_node_type_sh_math();
Expand Down
1 change: 1 addition & 0 deletions source/blender/editors/space_node/drawnode.c
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,7 @@ static void node_shader_set_butfunc(bNodeType *ntype)
ntype->draw_buttons = node_shader_buts_material;
break;
case SH_NODE_TEXTURE:
case SH_NODE_PARALLAX:
ntype->draw_buttons = node_buts_texture;
break;
case SH_NODE_NORMAL:
Expand Down
5 changes: 5 additions & 0 deletions source/blender/gpu/shaders/gpu_shader_material.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4118,6 +4118,11 @@ void mtex_parallax(vec3 texco, vec3 vp, vec4 tangent, vec3 vn, sampler2D ima, fl
ptexcoord = vec3(finaltexuv, texco.z);
}

void parallax_uv_attribute(vec3 uv, out vec3 outuv)
{
outuv = vec3(uv.xy * 2.0 - vec2(1.0), uv.z);
}

/* ********************** matcap style render ******************** */

void material_preview_matcap(vec4 color, sampler2D ima, vec4 N, vec4 mask, out vec4 result)
Expand Down
3 changes: 2 additions & 1 deletion source/blender/nodes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,12 @@ set(SRC
shader/nodes/node_shader_normal_map.c
shader/nodes/node_shader_object.c
shader/nodes/node_shader_object_info.c
shader/nodes/node_shader_time.c
shader/nodes/node_shader_hair_info.c
shader/nodes/node_shader_output_lamp.c
shader/nodes/node_shader_output_material.c
shader/nodes/node_shader_output_world.c
shader/nodes/node_shader_output_linestyle.c
shader/nodes/node_shader_parallax.c
shader/nodes/node_shader_particle_info.c
shader/nodes/node_shader_script.c
shader/nodes/node_shader_subsurface_scattering.c
Expand All @@ -207,6 +207,7 @@ set(SRC
shader/nodes/node_shader_tex_sky.c
shader/nodes/node_shader_tex_voronoi.c
shader/nodes/node_shader_tex_wave.c
shader/nodes/node_shader_time.c
shader/nodes/node_shader_volume_scatter.c
shader/nodes/node_shader_volume_absorption.c
shader/nodes/node_shader_uvAlongStroke.c
Expand Down
1 change: 1 addition & 0 deletions source/blender/nodes/NOD_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ void register_node_type_sh_gamma(void);
void register_node_type_sh_brightcontrast(void);
void register_node_type_sh_geom(void);
void register_node_type_sh_mapping(void);
void register_node_type_sh_parallax(void);
void register_node_type_sh_curve_vec(void);
void register_node_type_sh_curve_rgb(void);
void register_node_type_sh_math(void);
Expand Down
1 change: 1 addition & 0 deletions source/blender/nodes/NOD_static_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ DefNode( ShaderNode, SH_NODE_GAMMA, 0, "GAMMA
DefNode( ShaderNode, SH_NODE_BRIGHTCONTRAST, 0, "BRIGHTCONTRAST", BrightContrast, "Bright Contrast", "" )
DefNode( ShaderNode, SH_NODE_GEOMETRY, def_sh_geometry, "GEOMETRY", Geometry, "Geometry", "" )
DefNode( ShaderNode, SH_NODE_MAPPING, def_sh_mapping, "MAPPING", Mapping, "Mapping", "" )
DefNode( ShaderNode, SH_NODE_PARALLAX, def_texture, "PARALLAX", Parallax, "Parallax", "" )
DefNode( ShaderNode, SH_NODE_CURVE_VEC, def_vector_curve, "CURVE_VEC", VectorCurve, "Vector Curves", "" )
DefNode( ShaderNode, SH_NODE_CURVE_RGB, def_rgb_curve, "CURVE_RGB", RGBCurve, "RGB Curves", "" )
DefNode( ShaderNode, SH_NODE_CAMERA, 0, "CAMERA", CameraData, "Camera Data", "" )
Expand Down
84 changes: 84 additions & 0 deletions source/blender/nodes/shader/nodes/node_shader_parallax.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contributor(s): Tristan Porteries.
*
* ***** END GPL LICENSE BLOCK *****
*/

/** \file blender/nodes/shader/nodes/node_shader_parallax.c
* \ingroup shdnodes
*/


#include "node_shader_util.h"

/* **************** OBJECT INFO ******************** */
static bNodeSocketTemplate sh_node_mapping_in[] = {
{ SOCK_VECTOR, 1, N_("UV")},
{ SOCK_FLOAT, 1, N_("Steps"), 10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1000.0f, PROP_NONE, 0 },
{ SOCK_FLOAT, 1, N_("Bump Scale"), 0.01f, 0.0f, 0.0f, 0.0f, 0.0f, 1000.0f, PROP_NONE, 0 },
{ SOCK_FLOAT, 1, N_("Discard"), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_NONE, 0 },
{ -1, 0, "" }
};

static bNodeSocketTemplate sh_node_parallax_out[] = {
{ SOCK_VECTOR, 0, N_("UV")},
{ -1, 0, "" }
};

static int gpu_shader_parallax(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
{
Tex *tex = (Tex *)node->id;

if (tex && tex->ima && (tex->type == TEX_IMAGE)) {
GPUNodeLink *texlink = GPU_image(tex->ima, &tex->iuser, false);
GPUNodeLink *texco;
GPUNodeLink *norm;
GPUNodeLink *outuv;

for (unsigned short i = 0; i < 4; ++i) {
if (!in[i].link) {
in[i].link = GPU_uniform(in[i].vec);
}
}

GPU_link(mat, "texco_norm", GPU_builtin(GPU_VIEW_NORMAL), &norm);
GPU_link(mat, "mtex_2d_mapping", in[0].link, &texco);

GPU_link(mat, "mtex_parallax", texco, GPU_builtin(GPU_VIEW_POSITION), GPU_attribute(CD_TANGENT, ""), norm, texlink,
in[1].link, in[2].link, in[3].link, &outuv);

GPU_link(mat, "parallax_uv_attribute", outuv, &out[0].link);

return true;
}

return false;
}

void register_node_type_sh_parallax(void)
{
static bNodeType ntype;

sh_node_type_base(&ntype, SH_NODE_PARALLAX, "Parallax", NODE_CLASS_INPUT, 0);
node_type_compatibility(&ntype, NODE_OLD_SHADING | NODE_NEW_SHADING);
node_type_socket_templates(&ntype, sh_node_mapping_in, sh_node_parallax_out);
node_type_gpu(&ntype, gpu_shader_parallax);

nodeRegisterType(&ntype);
}
2 changes: 1 addition & 1 deletion source/blender/nodes/shader/nodes/node_shader_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* ***** END GPL LICENSE BLOCK *****
*/

/** \file blender/nodes/shader/nodes/node_shader_object.c
/** \file blender/nodes/shader/nodes/node_shader_time.c
* \ingroup shdnodes
*/

Expand Down

1 comment on commit 5d8cd9c

@youle31
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool! an image: http://pasteall.org/pic/show.php?id=117677 if you don't have one for the release notes

Please sign in to comment.