Class ShaderNode
One value in a portable shader: a constant, a uniform, a built-in input such as the world position, a texture sample, or an operation on other nodes. Nodes are immutable and can be shared; a node used twice is computed once in the generated program.
A graph is plain Java, so it is checked by the compiler, needs no parser
on the device and prints as GLSL, Metal or HLSL depending on the backend.
The types follow the shading languages' own rules: two operands of an
arithmetic operation have the same type, or one of them is a FLOAT that
is applied to every component of the other.
ShaderNode uv = ShaderNode.uv().mul(4f).add(ShaderNode.time().mul(0.05f));
ShaderNode wave = ShaderNode.sin(uv.x().mul(6.2831f)).mul(0.5f).add(0.5f);
ShaderNode color = ShaderNode.mix(ShaderNode.vec3(0.1f, 0.3f, 0.5f),
ShaderNode.vec3(0.6f, 0.8f, 0.9f), wave);
The built-in inputs that a stage can read are listed on each factory.
Reading a fragment input from the VERTEX_OFFSET hook, or the reverse,
fails when the material is compiled.
-
Method Summary
Modifier and TypeMethodDescriptionstatic ShaderNodeabs(ShaderNode a) Returns the absolute value, per component.add(float o) Returns this plus a constant.add(ShaderNode o) Returns this pluso.static ShaderNodeatan2(ShaderNode y, ShaderNode x) Returns the angle of the vector (x, y) in radians, -pi to pi.static ShaderNodeThe material colour times its texture, before theBASE_COLORhook replaces it.static ShaderNodeblendNormals(ShaderNode base, ShaderNode detail) Blends two tangent space normals so the detail rides on the base rather than averaging it flat ("whiteout" blend).static ShaderNodeThe camera position in world space.static ShaderNodeclamp(ShaderNode a, float lo, float hi) Returnsaclamped betweenloandhi, per component.static ShaderNodecolor(int rgb) An RGB colour constant from a packed0xRRGGBB(the alpha byte is ignored), in the 0 to 1 range.static ShaderNodecos(ShaderNode a) Returnscos(a), per component.static ShaderNodecross(ShaderNode a, ShaderNode b) Returns the cross product of twoVEC3nodes.static ShaderNodedistance(ShaderNode a, ShaderNode b) Returns the distance between two points.div(float o) Returns this divided by a constant.div(ShaderNode o) Returns this divided byo, component by component.static ShaderNodedot(ShaderNode a, ShaderNode b) Returns the dot product.static ShaderNodeexp(ShaderNode a) Returnseraised toa, per component.static ShaderNodef(float v) A float constant.static ShaderNodefloor(ShaderNode a) Returns the largest integer not greater thana, per component.static ShaderNodefract(ShaderNode a) Returnsa - floor(a), per component.getType()Returns the type of the value.static ShaderNodeThe colourGraphicsDevice.drawInstancedgives the copy being drawn, white when it gives none.static ShaderNodeThe index of the copyGraphicsDevice.drawInstancedis drawing, 0 for an ordinary draw.static ShaderNodeA random number in 0 to 1 that stays the same for each copy drawn byGraphicsDevice.drawInstanced(and 0 otherwise): vary the size, tint or sway phase of a field of grass or a crowd with it.static ShaderNodelength(ShaderNode a) Returns the length of a vector.static ShaderNodemax(ShaderNode a, ShaderNode b) Returns the larger of two values, per component.static ShaderNodemin(ShaderNode a, ShaderNode b) Returns the smaller of two values, per component.static ShaderNodemix(ShaderNode a, ShaderNode b, ShaderNode t) Linear interpolation fromatobbyt.static ShaderNodemod(ShaderNode a, ShaderNode b) Returnsamodulobwith the sign ofb, as GLSL'smoddoes (HLSL'sfmoddiffers for negative values, so this is printed asa - b * floor(a / b)everywhere).mul(float o) Returns this times a constant.mul(ShaderNode o) Returns this timeso, component by component.negate()Returns minus this.static ShaderNodenormal()The vertex normal in model space, or up when the mesh has none.static ShaderNodeReturns the unit vector in the direction ofa.oneMinus()Returns1 - this, the usual way to invert a mask.static ShaderNodeperturbNormal(ShaderNode tangentNormal) Bends the world space surface normal by a tangent space normal, for theNORMALhook.static ShaderNodeposition()The vertex position in model space.static ShaderNodepow(ShaderNode a, float b) Returnsaraised to a constant.static ShaderNodepow(ShaderNode a, ShaderNode b) Returnsaraised tob, per component.static ShaderNodereflect(ShaderNode i, ShaderNode n) Returns the reflection of incident directioniabout normaln.rgb()Returns the first three components, the colour of a texel.static ShaderNodesample(String textureName, ShaderNode uv) Samples a texture declared on the material withShaderMaterial.declareTexture.static ShaderNodeReturnsaclamped to 0 to 1, per component.static ShaderNodesceneColor(ShaderNode uv) Reads the frame in a custom full screen pass (com.codename1.gpu.PostProcess.addPass): the scene, or the output of the pass before.static ShaderNodescreenUv()The place of the pixel on screen in a custom full screen pass, from 0,0 top left to 1,1 bottom right.static ShaderNodeselect(ShaderNode condition, ShaderNode a, ShaderNode b) Returnsawhereconditionis above 0.5, otherwiseb.static ShaderNodesin(ShaderNode a) Returnssin(a), per component.static ShaderNodesmoothstep(float e0, float e1, ShaderNode x) Hermite step from 0 ate0to 1 ate1.static ShaderNodesmoothstep(ShaderNode e0, ShaderNode e1, ShaderNode x) Hermite step from 0 ate0to 1 ate1, with node edges.static ShaderNodesqrt(ShaderNode a) Returns the square root, per component.static ShaderNodestep(ShaderNode edge, ShaderNode x) Returns 0 wherexis belowedgeand 1 elsewhere.sub(float o) Returns this minus a constant.sub(ShaderNode o) Returns this minuso.static ShaderNodesunColor()The light colour.static ShaderNodesunDir()The unit direction toward the light (the sun).Picks components by name, as GLSL'sv.xzdoes: one to four ofx,y,z,w(orr,g,b,a).static ShaderNodetime()The clock in seconds thatGraphicsDevice.setTimesets; the scene renderer advances it every frame.static ShaderNodeuniform(String name, ShaderType type) A value the application sets per draw withShaderMaterial.setUniform.static ShaderNodeunpackNormal(ShaderNode texel, float strength) Decodes a texel of a tangent space normal map (the usual blue-ish image) into a direction, scaling its slope bystrength.static ShaderNodeuv()The first texture coordinate, or zero when the mesh has none.static ShaderNodevec(ShaderNode... parts) Builds a vector from nodes whose components add up to 2, 3 or 4, asvec3(v.xy, 1.0)does in GLSL.static ShaderNodevec2(float x, float y) A two component constant.static ShaderNodevec3(float x, float y, float z) A three component constant.static ShaderNodevec4(float x, float y, float z, float w) A four component constant.static ShaderNodeviewDir()The unit direction from the surface toward the camera.w()Returns the fourth component.static ShaderNodewiden(ShaderNode n, ShaderType t) Repeats aFLOATinto a vector oft, or returns the node itself when it already has that type.static ShaderNodeThe interpolated surface normal in world space, normalized, before theNORMALhook replaces it.static ShaderNodeworldPos()The position in world space: in the vertex stage before theVERTEX_OFFSEThook moves it, in the fragment stage after.x()Returns the first component.xy()Returns the first two components.xz()Returns the first and third components, the ground plane of a world position.y()Returns the second component.z()Returns the third component.
-
Method Details
-
f
A float constant.
Parameters
v: the value
Returns
a
FLOATnode -
vec2
A two component constant.
Parameters
-
x: the first component -
y: the second component
Returns
a
VEC2node -
-
vec3
A three component constant.
Parameters
-
x: the first component -
y: the second component -
z: the third component
Returns
a
VEC3node -
-
vec4
A four component constant.
Parameters
-
x: the first component -
y: the second component -
z: the third component -
w: the fourth component
Returns
a
VEC4node -
-
color
An RGB colour constant from a packed
0xRRGGBB(the alpha byte is ignored), in the 0 to 1 range.Parameters
rgb: the colour
Returns
a
VEC3node -
vec
Builds a vector from nodes whose components add up to 2, 3 or 4, as
vec3(v.xy, 1.0)does in GLSL.Parameters
parts: the nodes, in order
Returns
a vector node
-
uniform
A value the application sets per draw with
ShaderMaterial.setUniform. It must be declared on the material withShaderMaterial.declareUniform(the library shaders do that for you).Parameters
-
name: a name made of letters, digits and underscores -
type: its type
Returns
a node of that type
-
-
sample
Samples a texture declared on the material with
ShaderMaterial.declareTexture. Works in the fragment stage only.Parameters
-
textureName: the declared name -
uv: theVEC2coordinate
Returns
a
VEC4texel -
-
sceneColor
Reads the frame in a custom full screen pass (
com.codename1.gpu.PostProcess.addPass): the scene, or the output of the pass before. Means nothing on an ordinary material.Parameters
uv: theVEC2place on screen, 0,0 top left; usuallyscreenUv()or an offset from it
Returns
a
VEC4colour -
screenUv
The place of the pixel on screen in a custom full screen pass, from 0,0 top left to 1,1 bottom right. In a pass it is the same as
uv().Returns
a
VEC2node -
position
The vertex position in model space. Vertex stage.
Returns
a
VEC3node -
normal
The vertex normal in model space, or up when the mesh has none. Vertex stage.
Returns
a
VEC3node -
uv
The first texture coordinate, or zero when the mesh has none. Both stages.
Returns
a
VEC2node -
worldPos
The position in world space: in the vertex stage before the
VERTEX_OFFSEThook moves it, in the fragment stage after. Both stages.Returns
a
VEC3node -
worldNormal
The interpolated surface normal in world space, normalized, before the
NORMALhook replaces it. Fragment stage.Returns
a
VEC3node -
viewDir
The unit direction from the surface toward the camera. Fragment stage.
Returns
a
VEC3node -
cameraPos
The camera position in world space. Both stages.
Returns
a
VEC3node -
sunDir
The unit direction toward the light (the sun). Both stages.
Returns
a
VEC3node -
sunColor
The light colour. Both stages.
Returns
a
VEC3node -
instanceId
The index of the copy
GraphicsDevice.drawInstancedis drawing, 0 for an ordinary draw. Both stages. Reading it (orinstanceRandom,instanceColor) declares the uniform it needs on the material.Returns
a
FLOATnode -
instanceRandom
A random number in 0 to 1 that stays the same for each copy drawn by
GraphicsDevice.drawInstanced(and 0 otherwise): vary the size, tint or sway phase of a field of grass or a crowd with it. Both stages.Returns
a
FLOATnode -
instanceColor
The colour
GraphicsDevice.drawInstancedgives the copy being drawn, white when it gives none. Both stages.Returns
a
VEC4node -
time
The clock in seconds that
GraphicsDevice.setTimesets; the scene renderer advances it every frame. Both stages.Returns
a
FLOATnode -
baseColor
The material colour times its texture, before the
BASE_COLORhook replaces it. Fragment stage.Returns
a
VEC4node -
add
Returns this plus
o.Parameters
o: the other operand
Returns
the sum
-
add
Returns this plus a constant.
Parameters
o: the constant
Returns
the sum
-
sub
Returns this minus
o.Parameters
o: the other operand
Returns
the difference
-
sub
Returns this minus a constant.
Parameters
o: the constant
Returns
the difference
-
mul
Returns this times
o, component by component.Parameters
o: the other operand
Returns
the product
-
mul
Returns this times a constant.
Parameters
o: the constant
Returns
the product
-
div
Returns this divided by
o, component by component.Parameters
o: the divisor
Returns
the quotient
-
div
Returns this divided by a constant.
Parameters
o: the divisor
Returns
the quotient
-
negate
Returns minus this.
Returns
the negation
-
oneMinus
Returns
1 - this, the usual way to invert a mask.Returns
the complement
-
swizzle
Picks components by name, as GLSL's
v.xzdoes: one to four ofx,y,z,w(orr,g,b,a).Parameters
components: the component names
Returns
a node with as many components as named
-
x
Returns the first component.
Returns
a
FLOATnode -
y
Returns the second component.
Returns
a
FLOATnode -
z
Returns the third component.
Returns
a
FLOATnode -
w
Returns the fourth component.
Returns
a
FLOATnode -
rgb
Returns the first three components, the colour of a texel.
Returns
a
VEC3node -
xy
Returns the first two components.
Returns
a
VEC2node -
xz
Returns the first and third components, the ground plane of a world position.
Returns
a
VEC2node -
sin
Returns
sin(a), per component.Parameters
a: radians
Returns
the sine
-
cos
Returns
cos(a), per component.Parameters
a: radians
Returns
the cosine
-
abs
Returns the absolute value, per component.
Parameters
a: the value
Returns
the absolute value
-
floor
Returns the largest integer not greater than
a, per component.Parameters
a: the value
Returns
the floor
-
fract
Returns
a - floor(a), per component.Parameters
a: the value
Returns
the fractional part
-
sqrt
Returns the square root, per component.
Parameters
a: a non negative value
Returns
the square root
-
exp
Returns
eraised toa, per component.Parameters
a: the exponent
Returns
the exponential
-
normalize
Returns the unit vector in the direction of
a.Parameters
a: a non zero vector
Returns
the normalized vector
-
saturate
Returns
aclamped to 0 to 1, per component.Parameters
a: the value
Returns
the saturated value
-
length
Returns the length of a vector.
Parameters
a: the vector
Returns
a
FLOATnode -
distance
Returns the distance between two points.
Parameters
-
a: a point -
b: another point of the same type
Returns
a
FLOATnode -
-
dot
Returns the dot product.
Parameters
-
a: a vector -
b: another vector of the same type
Returns
a
FLOATnode -
-
cross
Returns the cross product of two
VEC3nodes.Parameters
-
a: a vector -
b: another vector
Returns
a
VEC3node -
-
reflect
Returns the reflection of incident direction
iabout normaln.Parameters
-
i: the incident direction -
n: the unit normal
Returns
a node of the type of
i -
-
min
Returns the smaller of two values, per component.
Parameters
-
a: a value -
b: another value
Returns
the minimum
-
-
max
Returns the larger of two values, per component.
Parameters
-
a: a value -
b: another value
Returns
the maximum
-
-
pow
Returns
araised tob, per component.amust not be negative.Parameters
-
a: the base -
b: the exponent, of the type ofaor aFLOAT
Returns
the power
-
-
pow
Returns
araised to a constant.Parameters
-
a: the base, not negative -
b: the exponent
Returns
the power
-
-
mod
Returns
amodulobwith the sign ofb, as GLSL'smoddoes (HLSL'sfmoddiffers for negative values, so this is printed asa - b * floor(a / b)everywhere).Parameters
-
a: the dividend -
b: the divisor
Returns
the remainder
-
-
atan2
Returns the angle of the vector (x, y) in radians, -pi to pi.
Parameters
-
y: the second coordinate -
x: the first coordinate
Returns
the angle
-
-
clamp
Returns
aclamped betweenloandhi, per component.Parameters
-
a: the value -
lo: the lower bound -
hi: the upper bound
Returns
the clamped value
-
-
mix
Linear interpolation from
atobbyt.Parameters
-
a: the value at 0 -
b: the value at 1, of the type ofa -
t: the weight, of the type ofaor aFLOAT
Returns
the blend
-
-
smoothstep
Hermite step from 0 at
e0to 1 ate1.Parameters
-
e0: the lower edge -
e1: the upper edge -
x: the value
Returns
a node of the type of
x -
-
smoothstep
Hermite step from 0 at
e0to 1 ate1, with node edges.Parameters
-
e0: the lower edge -
e1: the upper edge -
x: the value
Returns
a node of the type of
x -
-
step
Returns 0 where
xis belowedgeand 1 elsewhere.Parameters
-
edge: the edge -
x: the value
Returns
a node of the type of
x -
-
select
Returns
awhereconditionis above 0.5, otherwiseb. Both branches are computed; this is a select, not a branch.Parameters
-
condition: aFLOAT, usually astep -
a: the value when true -
b: the value when false, of the type ofa
Returns
the selected value
-
-
unpackNormal
Decodes a texel of a tangent space normal map (the usual blue-ish image) into a direction, scaling its slope by
strength.Parameters
-
texel: aVEC4read withsample -
strength: 1 for the map as authored, 0 for flat
Returns
a
VEC3tangent space normal, not normalized -
-
perturbNormal
Bends the world space surface normal by a tangent space normal, for the
NORMALhook. The tangent frame is derived from the surface normal and the world axes, so it needs no tangents in the mesh and is the same on every backend: on ground and water, tangentxruns along worldxandyalong worldz; on walls it runs around the vertical. That suits tiling detail, noise and terrain maps. A map painted for one model's UV layout reads correctly where that layout follows the world axes.Parameters
tangentNormal: aVEC3, such asunpackNormal's
Returns
the unit world space normal
-
blendNormals
Blends two tangent space normals so the detail rides on the base rather than averaging it flat ("whiteout" blend).
Parameters
-
base: aVEC3tangent space normal -
detail: another
Returns
the combined
VEC3, not normalized -
-
widen
Repeats a
FLOATinto a vector oft, or returns the node itself when it already has that type.Parameters
-
n: the node -
t: the target type
Returns
a node of type
t -
-
getType
Returns the type of the value.
Returns
the type
-