Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public final class ShaderNode
- Object
- 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.
Methods
Inherited methods
Method details
f
public static ShaderNode f(float v)Parameters
vfloat- the value
Returns
FLOAT nodevec2
public static ShaderNode vec2(float x, float y)Parameters
xfloat- the first component
yfloat- the second component
Returns
VEC2 nodevec3
public static ShaderNode vec3(float x, float y, float z)Parameters
xfloat- the first component
yfloat- the second component
zfloat- the third component
Returns
VEC3 nodevec4
public static ShaderNode vec4(float x, float y, float z, float w)Parameters
xfloat- the first component
yfloat- the second component
zfloat- the third component
wfloat- the fourth component
Returns
VEC4 nodecolor
public static ShaderNode color(int rgb)0xRRGGBB (the alpha byte is
ignored), in the 0 to 1 range.Parameters
rgbint- the colour
Returns
VEC3 nodevec
public static ShaderNode vec(ShaderNode... parts)vec3(v.xy, 1.0) does in GLSL.Parameters
partsShaderNode...- the nodes, in order
Returns
uniform
public static ShaderNode uniform(String name, ShaderType type)ShaderMaterial.setUniform. It must be declared on the material with
ShaderMaterial.declareUniform (the library shaders do that for
you).Parameters
nameString- a name made of letters, digits and underscores
typeShaderType- its type
Returns
sample
public static ShaderNode sample(String textureName, ShaderNode uv)ShaderMaterial.declareTexture. Works in the fragment stage only.Parameters
textureNameString- the declared name
uvShaderNode- the
VEC2coordinate
Returns
VEC4 texelsceneColor
public static ShaderNode sceneColor(ShaderNode uv)com.codename1.gpu.PostProcess.addPass): the scene, or the output
of the pass before. Means nothing on an ordinary material.Parameters
uvShaderNode- the
VEC2place on screen, 0,0 top left; usuallyscreenUv()or an offset from it
Returns
VEC4 colourscreenUv
public static ShaderNode screenUv()uv().Returns
VEC2 nodeposition
public static ShaderNode position()Returns
VEC3 nodenormal
public static ShaderNode normal()Returns
VEC3 nodeuv
public static ShaderNode uv()Returns
VEC2 nodeworldPos
public static ShaderNode worldPos()VERTEX_OFFSET hook moves it, in the fragment stage after. Both
stages.Returns
VEC3 nodeworldNormal
public static ShaderNode worldNormal()NORMAL hook replaces it. Fragment stage.Returns
VEC3 nodeviewDir
public static ShaderNode viewDir()Returns
VEC3 nodecameraPos
public static ShaderNode cameraPos()Returns
VEC3 nodesunDir
public static ShaderNode sunDir()Returns
VEC3 nodesunColor
public static ShaderNode sunColor()Returns
VEC3 nodeinstanceId
public static ShaderNode instanceId()GraphicsDevice.drawInstanced is drawing, 0
for an ordinary draw. Both stages. Reading it (or instanceRandom,
instanceColor) declares the uniform it needs on the material.Returns
FLOAT nodeinstanceRandom
public static ShaderNode instanceRandom()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
FLOAT nodeinstanceColor
public static ShaderNode instanceColor()GraphicsDevice.drawInstanced gives the copy being drawn,
white when it gives none. Both stages.Returns
VEC4 nodetime
public static ShaderNode time()GraphicsDevice.setTime sets; the scene
renderer advances it every frame. Both stages.Returns
FLOAT nodebaseColor
public static ShaderNode baseColor()BASE_COLOR hook
replaces it. Fragment stage.Returns
VEC4 nodeadd
public ShaderNode add(ShaderNode o)o.Parameters
oShaderNode- the other operand
Returns
add
public ShaderNode add(float o)Parameters
ofloat- the constant
Returns
sub
public ShaderNode sub(ShaderNode o)o.Parameters
oShaderNode- the other operand
Returns
sub
public ShaderNode sub(float o)Parameters
ofloat- the constant
Returns
mul
public ShaderNode mul(ShaderNode o)o, component by component.Parameters
oShaderNode- the other operand
Returns
mul
public ShaderNode mul(float o)Parameters
ofloat- the constant
Returns
div
public ShaderNode div(ShaderNode o)o, component by component.Parameters
oShaderNode- the divisor
Returns
div
public ShaderNode div(float o)Parameters
ofloat- the divisor
Returns
negate
public ShaderNode negate()Returns
oneMinus
public ShaderNode oneMinus()1 - this, the usual way to invert a mask.Returns
swizzle
public ShaderNode swizzle(String components)v.xz does: one to four of
x, y, z, w (or r, g, b, a).Parameters
componentsString- the component names
Returns
x
public ShaderNode x()Returns
FLOAT nodey
public ShaderNode y()Returns
FLOAT nodez
public ShaderNode z()Returns
FLOAT nodew
public ShaderNode w()Returns
FLOAT nodergb
public ShaderNode rgb()Returns
VEC3 nodexy
public ShaderNode xy()Returns
VEC2 nodexz
public ShaderNode xz()Returns
VEC2 nodesin
public static ShaderNode sin(ShaderNode a)sin(a), per component.Parameters
aShaderNode- radians
Returns
cos
public static ShaderNode cos(ShaderNode a)cos(a), per component.Parameters
aShaderNode- radians
Returns
abs
public static ShaderNode abs(ShaderNode a)Parameters
aShaderNode- the value
Returns
floor
public static ShaderNode floor(ShaderNode a)a, per component.Parameters
aShaderNode- the value
Returns
fract
public static ShaderNode fract(ShaderNode a)a - floor(a), per component.Parameters
aShaderNode- the value
Returns
sqrt
public static ShaderNode sqrt(ShaderNode a)Parameters
aShaderNode- a non negative value
Returns
exp
public static ShaderNode exp(ShaderNode a)e raised to a, per component.Parameters
aShaderNode- the exponent
Returns
normalize
public static ShaderNode normalize(ShaderNode a)a.Parameters
aShaderNode- a non zero vector
Returns
saturate
public static ShaderNode saturate(ShaderNode a)a clamped to 0 to 1, per component.Parameters
aShaderNode- the value
Returns
length
public static ShaderNode length(ShaderNode a)Parameters
aShaderNode- the vector
Returns
FLOAT nodedistance
public static ShaderNode distance(ShaderNode a, ShaderNode b)Parameters
aShaderNode- a point
bShaderNode- another point of the same type
Returns
FLOAT nodedot
public static ShaderNode dot(ShaderNode a, ShaderNode b)Parameters
aShaderNode- a vector
bShaderNode- another vector of the same type
Returns
FLOAT nodecross
public static ShaderNode cross(ShaderNode a, ShaderNode b)VEC3 nodes.Parameters
aShaderNode- a vector
bShaderNode- another vector
Returns
VEC3 nodereflect
public static ShaderNode reflect(ShaderNode i, ShaderNode n)i about normal n.Parameters
iShaderNode- the incident direction
nShaderNode- the unit normal
Returns
imin
public static ShaderNode min(ShaderNode a, ShaderNode b)Parameters
aShaderNode- a value
bShaderNode- another value
Returns
max
public static ShaderNode max(ShaderNode a, ShaderNode b)Parameters
aShaderNode- a value
bShaderNode- another value
Returns
pow
public static ShaderNode pow(ShaderNode a, ShaderNode b)a raised to b, per component. a must not be negative.Parameters
aShaderNode- the base
bShaderNode- the exponent, of the type of
aor aFLOAT
Returns
pow
public static ShaderNode pow(ShaderNode a, float b)a raised to a constant.Parameters
aShaderNode- the base, not negative
bfloat- the exponent
Returns
mod
public static ShaderNode mod(ShaderNode a, ShaderNode b)a modulo b with the sign of b, as GLSL’s mod does
(HLSL’s fmod differs for negative values, so this is printed as
a - b * floor(a / b) everywhere).Parameters
aShaderNode- the dividend
bShaderNode- the divisor
Returns
atan2
public static ShaderNode atan2(ShaderNode y, ShaderNode x)Parameters
yShaderNode- the second coordinate
xShaderNode- the first coordinate
Returns
clamp
public static ShaderNode clamp(ShaderNode a, float lo, float hi)a clamped between lo and hi, per component.Parameters
aShaderNode- the value
lofloat- the lower bound
hifloat- the upper bound
Returns
mix
public static ShaderNode mix(ShaderNode a, ShaderNode b, ShaderNode t)a to b by t.Parameters
aShaderNode- the value at 0
bShaderNode- the value at 1, of the type of
a tShaderNode- the weight, of the type of
aor aFLOAT
Returns
smoothstep
public static ShaderNode smoothstep(float e0, float e1, ShaderNode x)e0 to 1 at e1.Parameters
e0float- the lower edge
e1float- the upper edge
xShaderNode- the value
Returns
xsmoothstep
public static ShaderNode smoothstep(ShaderNode e0, ShaderNode e1, ShaderNode x)e0 to 1 at e1, with node edges.Parameters
e0ShaderNode- the lower edge
e1ShaderNode- the upper edge
xShaderNode- the value
Returns
xstep
public static ShaderNode step(ShaderNode edge, ShaderNode x)x is below edge and 1 elsewhere.Parameters
edgeShaderNode- the edge
xShaderNode- the value
Returns
xselect
public static ShaderNode select(ShaderNode condition, ShaderNode a, ShaderNode b)a where condition is above 0.5, otherwise b. Both
branches are computed; this is a select, not a branch.Parameters
conditionShaderNode- a
FLOAT, usually astep aShaderNode- the value when true
bShaderNode- the value when false, of the type of
a
Returns
unpackNormal
public static ShaderNode unpackNormal(ShaderNode texel, float strength)strength.Parameters
texelShaderNode- a
VEC4read withsample strengthfloat- 1 for the map as authored, 0 for flat
Returns
VEC3 tangent space normal, not normalizedperturbNormal
public static ShaderNode perturbNormal(ShaderNode tangentNormal)NORMAL hook. 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, tangent x runs
along world x and y along world z; 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
tangentNormalShaderNode- a
VEC3, such asunpackNormal’s
Returns
blendNormals
public static ShaderNode blendNormals(ShaderNode base, ShaderNode detail)Parameters
baseShaderNode- a
VEC3tangent space normal detailShaderNode- another
Returns
VEC3, not normalizedwiden
public static ShaderNode widen(ShaderNode n, ShaderType t)FLOAT into a vector of t, or returns the node itself
when it already has that type.Parameters
nShaderNode- the node
tShaderType- the target type
Returns
tgetType
public ShaderType getType()