6#include <unordered_map>
8#include "nlohmann/json.hpp"
64 if (from == to)
return true;
168 float defVec[4] = { 0.f, 0.f, 0.f, 1.f };
363 std::map<int, kString> nodeVar;
364 std::vector<kString> samplers;
365 std::vector<kString> uniforms;
366 std::set<kString> emittedSamplerNames;
367 std::vector<kString> body;
374 kString newVar() {
return "v_" + std::to_string(counter++); }
385 static kString emitPin(
const kShaderGraph& g,
int nodeId,
int pinId, Ctx& ctx);
394 static kString emitNode(
const kShaderGraph& g,
int nodeId, Ctx& ctx);
410 static kString pinDefault(
const kShaderPin& pin);
423 static kString vertexTemplate();
432 static kString fragmentPreamble(
const kShaderGraph& g,
const kShaderNode& outNode,
Compiles a kShaderGraph into combined GLSL source.
Definition kshadernode.h:345
static kShaderCompileResult compile(const kShaderGraph &graph)
Compiles a shader graph into GLSL.
Core type aliases, enumerations, structs, and utility functions used throughout the engine.
Top-level Kemena3D engine namespace.
Definition kanimation.h:23
@ Divide
a / b.
Definition kscriptgraph.h:93
@ Subtract
a - b.
Definition kscriptgraph.h:91
@ Multiply
a * b.
Definition kscriptgraph.h:92
@ Add
a + b.
Definition kscriptgraph.h:90
kPinType
Data type carried by a shader-node pin (link/connection type).
Definition kshadernode.h:24
@ Vec3
3-component vector.
Definition kshadernode.h:27
@ Float
Scalar float.
Definition kshadernode.h:25
@ SamplerCube
Cube-map texture sampler.
Definition kshadernode.h:30
@ Sampler2D
2D texture sampler.
Definition kshadernode.h:29
@ Vec4
4-component vector.
Definition kshadernode.h:28
@ Vec2
2-component vector.
Definition kshadernode.h:26
bool kPinCompatible(kPinType from, kPinType to)
Tests whether an output pin type may feed into an input pin type.
Definition kshadernode.h:62
@ Vec3
3-component vector value.
Definition kscriptgraph.h:42
@ Float
Floating-point value.
Definition kscriptgraph.h:39
std::string kString
Standard string alias.
Definition kdatatype.h:42
const char * kPinTypeName(kPinType t)
Returns the GLSL type keyword for a pin type.
Definition kshadernode.h:38
kShaderNodeType
Identifies the kind of operation a shader graph node performs.
Definition kshadernode.h:89
@ WorldPosition
Fragment world-space position.
Definition kshadernode.h:98
@ Swizzle
Reorder/select components via a mask.
Definition kshadernode.h:132
@ Clamp
Clamp value between min and max.
Definition kshadernode.h:115
@ TextureCube
Sample a cube-map texture.
Definition kshadernode.h:104
@ ConstVec3
User-supplied constant vec3.
Definition kshadernode.h:93
@ OutputPhong
Phong lit output (requires lights).
Definition kshadernode.h:145
@ Abs
Absolute value.
Definition kshadernode.h:118
@ Smoothstep
Smooth Hermite interpolation between edges.
Definition kshadernode.h:126
@ Mix
Linear interpolation (lerp).
Definition kshadernode.h:116
@ MaterialDiffuse
Material diffuse color.
Definition kshadernode.h:137
@ ViewDirection
Direction from fragment toward the camera.
Definition kshadernode.h:99
@ Fract
Fractional part.
Definition kshadernode.h:121
@ Step
Step function (0 or 1 about an edge).
Definition kshadernode.h:125
@ OutputFlat
Unlit/flat shading output.
Definition kshadernode.h:144
@ UVCoord
Interpolated texture coordinates.
Definition kshadernode.h:95
@ Max
Component-wise maximum.
Definition kshadernode.h:124
@ OneMinus
1 - value.
Definition kshadernode.h:127
@ Min
Component-wise minimum.
Definition kshadernode.h:123
@ Split
Decompose a vector into its components.
Definition kshadernode.h:130
@ Pow
Raise base to an exponent.
Definition kshadernode.h:117
@ ConstVec4
User-supplied constant vec4.
Definition kshadernode.h:94
@ MaterialTiling
Material UV tiling factor.
Definition kshadernode.h:135
@ MaterialSpecular
Material specular color.
Definition kshadernode.h:138
@ MaterialAmbient
Material ambient color.
Definition kshadernode.h:136
@ ConstVec2
User-supplied constant vec2.
Definition kshadernode.h:92
@ Texture2D
Sample a 2D texture.
Definition kshadernode.h:103
@ Time
Elapsed time uniform.
Definition kshadernode.h:96
@ Sqrt
Square root.
Definition kshadernode.h:122
@ Dot
Dot product.
Definition kshadernode.h:111
@ MaterialMetallic
Material metallic factor (PBR).
Definition kshadernode.h:140
@ Ceil
Round up to integer.
Definition kshadernode.h:120
@ Length
Vector length.
Definition kshadernode.h:114
@ ConstFloat
User-supplied constant float.
Definition kshadernode.h:91
@ MaterialShininess
Material shininess exponent.
Definition kshadernode.h:139
@ VertexColor
Per-vertex color attribute.
Definition kshadernode.h:97
@ OutputPBR
Physically based output (requires lights).
Definition kshadernode.h:146
@ Normalize
Normalize a vector.
Definition kshadernode.h:113
@ MaterialRoughness
Material roughness factor (PBR).
Definition kshadernode.h:141
@ VertexNormal
Interpolated surface normal.
Definition kshadernode.h:100
@ Cross
Cross product.
Definition kshadernode.h:112
@ Combine
Assemble scalars into a vector.
Definition kshadernode.h:131
@ Floor
Round down to integer.
Definition kshadernode.h:119
Result of compiling a shader graph into GLSL.
Definition kshadernode.h:322
bool needsMaterial
Graph references material.* uniforms.
Definition kshadernode.h:334
bool usesAoMap
Graph samples an ambient-occlusion map.
Definition kshadernode.h:331
bool usesMetallicRoughnessMap
Graph samples a metallic-roughness map.
Definition kshadernode.h:330
kString error
Error message when success is false.
Definition kshadernode.h:324
bool usesAlbedoMap
Graph samples an albedo/base-color map.
Definition kshadernode.h:328
kString glsl
Combined vertex + fragment GLSL source.
Definition kshadernode.h:325
bool usesEmissiveMap
Graph samples an emissive map.
Definition kshadernode.h:332
bool success
True if compilation succeeded.
Definition kshadernode.h:323
bool usesNormalMap
Graph samples a normal map.
Definition kshadernode.h:329
std::vector< std::pair< kString, kString > > customSamplers
Extra samplers: { uniformName, "2D"|"Cube" }.
Definition kshadernode.h:333
bool needsLights
Output requires lighting (Phong/PBR).
Definition kshadernode.h:335
A complete node-based shader graph: nodes, links, and identity.
Definition kshadernode.h:232
int newId()
Allocates and returns a fresh unique id.
Definition kshadernode.h:244
void fromJson(const nlohmann::json &j)
Rebuilds the graph from a JSON representation.
bool isPinConnected(int nodeId, int pinId) const
Tests whether a pin has any link attached.
nlohmann::json toJson() const
Serializes the graph to JSON.
void removeLinksByPin(int nodeId, int pinId)
Removes every link touching the given pin.
const kShaderNode * findNode(int id) const
Finds a node by id (const overload).
bool dirty
True when the graph has unsaved changes.
Definition kshadernode.h:238
kString name
Display name of the graph.
Definition kshadernode.h:234
const kShaderLink * incomingLink(int nodeId, int pinId) const
Finds the link feeding a given input pin.
std::vector< kShaderNode > nodes
All nodes in the graph.
Definition kshadernode.h:235
void removeLinksByNode(int nodeId)
Removes every link touching the given node.
int nextId
Next id to hand out via newId().
Definition kshadernode.h:237
kShaderNode makeNode(kShaderNodeType type, float x, float y)
Creates a node of a given type with its default pins wired up.
std::vector< kShaderLink > links
All links between node pins.
Definition kshadernode.h:236
kString uuid
Stable unique identifier for the graph asset.
Definition kshadernode.h:233
kShaderNode * findNode(int id)
Finds a node by id.
A directed edge connecting an output pin to an input pin.
Definition kshadernode.h:215
int fromPin
Source node id and output pin id.
Definition kshadernode.h:217
int toNode
Definition kshadernode.h:218
int fromNode
Definition kshadernode.h:217
int toPin
Destination node id and input pin id.
Definition kshadernode.h:218
A node in the shader graph: a typed operation with input/output pins.
Definition kshadernode.h:185
float valueFloat[4]
Constant value for Const* nodes.
Definition kshadernode.h:195
kString name
Display name of the node.
Definition kshadernode.h:188
kShaderNodeType type
Operation this node performs.
Definition kshadernode.h:187
std::vector< kShaderPin > outputs
Output pins.
Definition kshadernode.h:192
float posY
Node position in the editor canvas.
Definition kshadernode.h:189
kString valueStr
Texture uniform name or swizzle mask.
Definition kshadernode.h:196
std::vector< kShaderPin > inputs
Input pins.
Definition kshadernode.h:191
static const char * typeName(kShaderNodeType t)
Returns a human-readable name for a shader node type.
float posX
Definition kshadernode.h:189
bool valueBool
Generic boolean payload.
Definition kshadernode.h:197
A single input or output connection point on a shader node.
Definition kshadernode.h:160
kPinType type
Data type carried by the pin.
Definition kshadernode.h:163
float defVec[4]
Default vector value when unconnected.
Definition kshadernode.h:168
float defFloat
Default scalar value when unconnected.
Definition kshadernode.h:167
float uiY
Cached screen-space position (set during draw).
Definition kshadernode.h:171
float uiX
Definition kshadernode.h:171
kString name
Display name of the pin.
Definition kshadernode.h:162
bool isOutput
True if this is an output pin.
Definition kshadernode.h:164