Advanced Graphics
with Shader Graphs

Alec Shedelbower, Wolfram Research

(RowDefault[, (Spacer1), , (Spacer1), ])  #WolframTechConf

Overview

What are Shaders?

Shaders in the Wolfram Language

Shader Graphs

Demonstrations

Graphics Pipeline

Graphics Pipeline – Stages

How to Use Shaders in the Wolfram Language

Standard Shader with Directives

Alternative Shaders (MaterialShading, HatchShading, etc.)

SurfaceAppearance (OpenGL Shading Language)

Standard Shader

Graphics3D[Torus[],]

Graphics3D[{Red,Specularity[White,100],Torus[]},]

Alternative Shaders

Graphics3D[{#,Torus[]},]&/@{ToonShading[Red],HatchShading[],MaterialShading["Copper"]}

Graphics[{#,Annulus[]},]&/@{RadialGradientFilling[],HatchFilling[],PatternFilling["Checkerboard"]}

SurfaceAppearance

Provides full customization of the vertex and fragment shader stages through writing GLSL programs.

Powerful, but not user friendly.

Detailed Knowledge of Graphics Pipeline

Lower-Level Programming with Unique Restrictions

Inconsistent Support Across Hardware

Difficult to Debug

Shader Graphs

Compromise between flexibility and complexity.

Use by numerous applications:

({{({{, , , , }})}, {({{, , , }})}})

What is a Shader Graph?

Shader graphs consist of nodes that act like functions.

Nodes & Ports

Node ports are connected by edges to create directed acyclic graphs (DAGs).

Shader Graph Advantages

Discoverability

Guard Rails

Debugging

Explicit Data Flow

Portability

ShaderGraph – Standard Shading

graph=ShaderGraph[{"StandardShading","SetPixelColor"},{1->2}]

graph["NodeList"]

Graphics3D[{graph["SurfaceAppearance"],Sphere[]}]

Graphics3D[{Red,Opacity[0.2],graph["SurfaceAppearance"],Sphere[]}]

DisplayShaderCode[graph]

ShaderGraph – Custom Properties

node=ShaderNode["GetCustomProperty","TypeSignature"->{"ColorRGBA"},"Controls"-><|"Name"->"MainColor"|>]

graph=ShaderGraph[{node,"StandardShading","SetPixelColor"},{1->{2,"DiffuseColor"},2->3}]

graph["NodeList"][[2]]

%["PortNames"]

Manipulate[
Graphics3D[{graph["SurfaceAppearance","MainColor"->color],Sphere[]}],
{color,Red}
]

ShaderGraph – Subgraphs

checker=

graph=ShaderGraph[{checker,"StandardShading","SetPixelColor"},{1->{2,"DiffuseColor"},2->3}]

Manipulate[Graphics3D[{graph["SurfaceAppearance","ColorA"->,"ColorB"->,"Size"->scale],EdgeForm[],model},Lighting->"Neutral"],
{{scale,1},1/5,3},{model,{Cube[5]->"Cube",ResourceData["Sample 3D Model: Beethoven"]->"Beethoven"}},
ControlPlacement->Left
]

ShaderGraph – Contours

contours=

cgraph=

models=();

Manipulate[
Graphics3D[{EdgeForm[],cgraph["SurfaceAppearance","Frequency"->freq,"Thickness"->thickness,"Offset"->offset,"BaseColor"->base,"LineColor"->line],model},ImageSize->Large,Boxed->False,Lighting->lighting],
{{freq,1.5},0.01,3.0},
{{thickness,0.5},0.0,1.0},
{{offset,0.0},-4.0,4.0},
{base,},
{line,},
{model,models},
{lighting,{"ThreePoint","Standard","Accent"}},
ControlPlacement->Left
]

ShaderGraph – Vertex Displacement

The previous examples were Fragment shaders. We can also make Vertex shaders to alter the geometry during rendering.

distortGraph=

vertexGraph=ShaderGraph[
<|"pos"->,"normal"->,"distort"->distortGraph,"setPos"->|>,{"pos"->{"distort","Position"},"normal"->{"distort","Normal"},"distort"->"setPos"}
]

tsphere:=
Manipulate[Graphics3D[{vertexGraph["SurfaceAppearance","Time"->t,"Amplitude"->amp,"Frequency"->freq],If[showEdges,Null,EdgeForm[]],tsphere},],
{{amp,0},0,10.0},
{t,0,20},
{{freq,0.2},0.1,1},
{showEdges,{False,True}},
ControlPlacement->Left
]

Plans for the Future

Simplify Design

Custom Nodes

ShaderGraphs, GLSL Code, WL Code

Integration with MaterialX

Import/Export

GUI

Customize Mathematica Graphics Rendering with GLSL 
Yuzhu Lu, Thursday 2:30 PM CDT

Wolfram R&D Youtube Channel
@WolframRD

(RowDefault[, (Spacer1), , (Spacer1), ])  #WolframTechConf