From 241cbce7e65ef4acad4a09bdd353d17dc5252ed9 Mon Sep 17 00:00:00 2001 From: "rem..om" Date: Tue, 26 Feb 2013 19:39:18 +0000 Subject: [PATCH] Shader Nodes implementation : Sample Shader nodes and test material definition git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10434 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../Common/MatDefs/Misc/UnshadedNodes.j3md | 95 +++++++++++++++++++ .../ShaderNodes/Basic/AttributeToVarying.j3sn | 29 ++++++ .../MatDefs/ShaderNodes/Basic/ColorMix.j3sn | 21 ++++ .../MatDefs/ShaderNodes/Basic/ColorMult.j3sn | 19 ++++ .../ShaderNodes/Basic/TextureFetch.j3sn | 19 ++++ .../ShaderNodes/Basic/TransformPosition.j3sn | 19 ++++ .../MatDefs/ShaderNodes/Basic/colorMix.frag | 3 + .../MatDefs/ShaderNodes/Basic/colorMult.frag | 3 + .../MatDefs/ShaderNodes/Basic/null.vert | 0 .../MatDefs/ShaderNodes/Basic/texture.frag | 3 + .../ShaderNodes/Basic/transformPosition.vert | 3 + .../ShaderNodes/Common/CommonVert.j3sn | 32 +++++++ .../MatDefs/ShaderNodes/Common/Unshaded.j3sn | 27 ++++++ .../ShaderNodes/Common/commonVert.vert | 3 + .../MatDefs/ShaderNodes/Common/unshaded.frag | 14 +++ .../Common/MatDefs/ShaderNodes/Fog/Fog.j3sn | 46 +++++++++ .../MatDefs/ShaderNodes/Fog/fogFactor.vert | 6 ++ .../MatDefs/ShaderNodes/Fog/fogOutput.frag | 3 + .../LightMapping/LightMapping.j3sn | 21 ++++ .../ShaderNodes/LightMapping/lightMap.frag | 3 + 20 files changed, 369 insertions(+) create mode 100644 engine/src/core-data/Common/MatDefs/Misc/UnshadedNodes.j3md create mode 100644 engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/AttributeToVarying.j3sn create mode 100644 engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/ColorMix.j3sn create mode 100644 engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/ColorMult.j3sn create mode 100644 engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/TextureFetch.j3sn create mode 100644 engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/TransformPosition.j3sn create mode 100644 engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/colorMix.frag create mode 100644 engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/colorMult.frag create mode 100644 engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/null.vert create mode 100644 engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/texture.frag create mode 100644 engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/transformPosition.vert create mode 100644 engine/src/core-data/Common/MatDefs/ShaderNodes/Common/CommonVert.j3sn create mode 100644 engine/src/core-data/Common/MatDefs/ShaderNodes/Common/Unshaded.j3sn create mode 100644 engine/src/core-data/Common/MatDefs/ShaderNodes/Common/commonVert.vert create mode 100644 engine/src/core-data/Common/MatDefs/ShaderNodes/Common/unshaded.frag create mode 100644 engine/src/core-data/Common/MatDefs/ShaderNodes/Fog/Fog.j3sn create mode 100644 engine/src/core-data/Common/MatDefs/ShaderNodes/Fog/fogFactor.vert create mode 100644 engine/src/core-data/Common/MatDefs/ShaderNodes/Fog/fogOutput.frag create mode 100644 engine/src/core-data/Common/MatDefs/ShaderNodes/LightMapping/LightMapping.j3sn create mode 100644 engine/src/core-data/Common/MatDefs/ShaderNodes/LightMapping/lightMap.frag diff --git a/engine/src/core-data/Common/MatDefs/Misc/UnshadedNodes.j3md b/engine/src/core-data/Common/MatDefs/Misc/UnshadedNodes.j3md new file mode 100644 index 000000000..8785ed968 --- /dev/null +++ b/engine/src/core-data/Common/MatDefs/Misc/UnshadedNodes.j3md @@ -0,0 +1,95 @@ +MaterialDef UnshadedNodes { + + MaterialParameters { + Texture2D ColorMap + Texture2D LightMap + Color Color (Color) + Boolean VertexColor (UseVertexColor) + Boolean SeparateTexCoord + + //FOG parameters + Boolean UseFog + Float FogDensity :1 + Color FogColor : 1 1 1 1 + } + + Technique { + + WorldParameters { + WorldViewProjectionMatrix + //used for fog + WorldViewMatrix + } + + VertexShaderNodes{ + ShaderNode FogFactor{ + Definition: FogFactor : Common/MatDefs/ShaderNodes/Fog/Fog.j3sn + Condition: UseFog + InputMapping{ + modelViewMatrix = WorldParam.WorldViewMatrix + modelPosition = Global.position + fogDensity = MatParam.FogDensity + } + } + ShaderNode UnshadedVert{ + Definition: CommonVert : Common/MatDefs/ShaderNodes/Common/CommonVert.j3sn + InputMapping{ + worldViewProjectionMatrix = WorldParam.WorldViewProjectionMatrix + modelPosition = Global.position.xyz + texCoord1 = Attr.inTexCoord: ColorMap || (LightMap && !SeparateTexCoord) + texCoord2 = Attr.inTexCoord2: SeparateTexCoord + vertColor = Attr.inColor: VertexColor + } + OutputMapping{ + Global.position = projPosition + } + } + + + } + FragmentShaderNodes{ + ShaderNode UnshadedFrag{ + Definition: Unshaded : Common/MatDefs/ShaderNodes/Common/Unshaded.j3sn + InputMapping{ + texCoord = UnshadedVert.texCoord1: ColorMap + vertColor = UnshadedVert.vertColor: VertexColor + matColor = MatParam.Color: Color + colorMap = MatParam.ColorMap: ColorMap + color = Global.outColor + } + OutputMapping{ + Global.outColor = color + } + } + ShaderNode LightMap{ + Definition: LightMapping : Common/MatDefs/ShaderNodes/LightMapping/LightMapping.j3sn + Condition: LightMap + InputMapping{ + texCoord = UnshadedVert.texCoord1: !SeparateTexCoord + texCoord = UnshadedVert.texCoord2: SeparateTexCoord + lightMap = MatParam.LightMap + color = Global.outColor + } + OutputMapping{ + Global.outColor = color + } + } + + ShaderNode FogOutput{ + Definition: FogOutput : Common/MatDefs/ShaderNodes/Fog/Fog.j3sn + Condition: UseFog + InputMapping{ + fogFactor = FogFactor.fogFactor + color = Global.outColor + fogColor = MatParam.FogColor + } + OutputMapping{ + Global.outColor = color + } + } + } + + } + + +} \ No newline at end of file diff --git a/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/AttributeToVarying.j3sn b/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/AttributeToVarying.j3sn new file mode 100644 index 000000000..ca2a7909a --- /dev/null +++ b/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/AttributeToVarying.j3sn @@ -0,0 +1,29 @@ +ShaderNodeDefinitions{ + ShaderNodeDefinition AttributeToVarying{ + Type : Vertex + Shader GLSL100: Common/MatDefs/ShaderNodes/Basic/null.vert + Documentation{ + This node can pass an attribute value to a varying value. + @input floatVariable a float attribute + @input vec2Variable a vec2 attribute + @input vec3Variable a vec3 attribute + @input vec4Variable a vec4 attribute + @output floatVariable a float varying + @output vec2Variable a vec2 varying + @output vec3Variable a vec3 varying + @output vec4Variable a vec4 varying + } + Input { + float floatVariable + vec2 vec2Variable + vec3 vec3Variable + vec4 vec4Variable + } + Output { + float floatVariable + vec2 vec2Variable + vec3 vec3Variable + vec4 vec4Variable + } + } +} \ No newline at end of file diff --git a/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/ColorMix.j3sn b/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/ColorMix.j3sn new file mode 100644 index 000000000..414615b6a --- /dev/null +++ b/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/ColorMix.j3sn @@ -0,0 +1,21 @@ +ShaderNodeDefinitions{ + ShaderNodeDefinition ColorMix { + Type: Fragment + Shader GLSL100: Common/MatDefs/ShaderNodes/Basic/colorMix.frag + Documentation{ + mixes two colors according to a mix factor + @input color1 the first color to mix + @input color2 the second color to mix + @input factor the mix factor (from 0.0 to 1.0) fpr more information see the gsls mix function + @output outColor the mixed color + } + Input { + vec4 color1 + vec4 color2 + float factor + } + Output { + vec4 outColor + } + } +} \ No newline at end of file diff --git a/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/ColorMult.j3sn b/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/ColorMult.j3sn new file mode 100644 index 000000000..4d1f44328 --- /dev/null +++ b/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/ColorMult.j3sn @@ -0,0 +1,19 @@ +ShaderNodeDefinitions{ + ShaderNodeDefinition ColorMult { + Type: Fragment + Shader GLSL100: Common/MatDefs/ShaderNodes/Basic/colorMult.frag + Documentation{ + Multiplies two colors + @input color1 the first color + @input color2 the second color + @output outColor the resulting color + } + Input { + vec4 color1 + vec4 color2 + } + Output { + vec4 outColor + } + } +} \ No newline at end of file diff --git a/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/TextureFetch.j3sn b/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/TextureFetch.j3sn new file mode 100644 index 000000000..b4ccd3640 --- /dev/null +++ b/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/TextureFetch.j3sn @@ -0,0 +1,19 @@ +ShaderNodeDefinitions{ + ShaderNodeDefinition TextureFetch { + Type: Fragment + Shader GLSL100: Common/MatDefs/ShaderNodes/Basic/texture.frag + Documentation{ + Fetches a color value in the given texture acording to given texture coordinates + @input texture the texture to read + @input texCoord the texture coordinates + @output outColor the fetched color + } + Input { + sampler2D texture + vec2 texCoord + } + Output { + vec4 outColor + } + } +} \ No newline at end of file diff --git a/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/TransformPosition.j3sn b/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/TransformPosition.j3sn new file mode 100644 index 000000000..e50ca8457 --- /dev/null +++ b/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/TransformPosition.j3sn @@ -0,0 +1,19 @@ +ShaderNodeDefinitions{ + ShaderNodeDefinition TransformPosition{ + Type: Vertex + Shader GLSL100: Common/MatDefs/ShaderNodes/Basic/transformPosition.vert + Documentation { + This node transforms a position according to the given matrix + @input inputPosition the position to transform + @input transformsMatrix the matrix to use for this transformation + @output outPosition the transformed position + } + Input { + vec3 inputPosition + mat4 transformsMatrix + } + Output { + vec4 outPosition + } + } +} \ No newline at end of file diff --git a/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/colorMix.frag b/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/colorMix.frag new file mode 100644 index 000000000..26e0b8873 --- /dev/null +++ b/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/colorMix.frag @@ -0,0 +1,3 @@ +void main(){ + outColor = mix(color1,color2,factor); +} \ No newline at end of file diff --git a/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/colorMult.frag b/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/colorMult.frag new file mode 100644 index 000000000..b27581289 --- /dev/null +++ b/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/colorMult.frag @@ -0,0 +1,3 @@ +void main(){ + outColor = color1 * color2; +} \ No newline at end of file diff --git a/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/null.vert b/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/null.vert new file mode 100644 index 000000000..e69de29bb diff --git a/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/texture.frag b/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/texture.frag new file mode 100644 index 000000000..eb83a7b1f --- /dev/null +++ b/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/texture.frag @@ -0,0 +1,3 @@ +void main(){ + outColor = texture2D(texture,texCoord); +} \ No newline at end of file diff --git a/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/transformPosition.vert b/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/transformPosition.vert new file mode 100644 index 000000000..f83240d56 --- /dev/null +++ b/engine/src/core-data/Common/MatDefs/ShaderNodes/Basic/transformPosition.vert @@ -0,0 +1,3 @@ +void main(){ + outPosition = transformsMatrix * vec4(inputPosition, 1.0); +} \ No newline at end of file diff --git a/engine/src/core-data/Common/MatDefs/ShaderNodes/Common/CommonVert.j3sn b/engine/src/core-data/Common/MatDefs/ShaderNodes/Common/CommonVert.j3sn new file mode 100644 index 000000000..9afc7c763 --- /dev/null +++ b/engine/src/core-data/Common/MatDefs/ShaderNodes/Common/CommonVert.j3sn @@ -0,0 +1,32 @@ +ShaderNodesDefinitions { + ShaderNodeDefinition CommonVert { + Type: Vertex + Shader GLSL100: Common/MatDefs/ShaderNodes/Common/commonVert.vert + Documentation { + This Node is responsible for computing vertex position in projection space. + It also can pass texture coordinates 1 & 2, and vertexColor to the frgment shader as varying (or inputs for glsl >=1.3) + @input modelPosition the vertex position in model space (usually assigned with Attr.inPosition or Global.position) + @input worldViewProjectionMatrix the World View Projection Matrix transforms model space to projection space. + @input texCoord1 The first texture coordinates of the vertex (usually assigned with Attr.inTexCoord) + @input texCoord2 The second texture coordinates of the vertex (usually assigned with Attr.inTexCoord2) + @input vertColor The color of the vertex (usually assigned with Attr.inColor) + @output projPosition Position of the vertex in projection space.(usually assigned to Global.position) + @output vec2 texCoord1 The first texture coordinates of the vertex (output as a varying) + @output vec2 texCoord2 The second texture coordinates of the vertex (output as a varying) + @output vec4 vertColor The color of the vertex (output as a varying) + } + Input{ + vec3 modelPosition + mat4 worldViewProjectionMatrix + vec2 texCoord1 + vec2 texCoord2 + vec4 vertColor + } + Output{ + vec4 projPosition + vec2 texCoord1 + vec2 texCoord2 + vec4 vertColor + } + } +} \ No newline at end of file diff --git a/engine/src/core-data/Common/MatDefs/ShaderNodes/Common/Unshaded.j3sn b/engine/src/core-data/Common/MatDefs/ShaderNodes/Common/Unshaded.j3sn new file mode 100644 index 000000000..f3cb30127 --- /dev/null +++ b/engine/src/core-data/Common/MatDefs/ShaderNodes/Common/Unshaded.j3sn @@ -0,0 +1,27 @@ +ShaderNodeDefinitions{ + ShaderNodeDefinition Unshaded{ + Type: Fragment + Shader GLSL100: Common/MatDefs/ShaderNodes/Common/unshaded.frag + Documentation { + This Node is responsible for outputing the unshaded color of a fragment. + It can support texture mapping, an arbitrary input color and a vertex color + (all resulting colors will be multiplied) + @input texCoord the texture coordinates to use for texture mapping + @input vertColor the vertex color (often comming from a varrying) + @input matColor the material color (often comming from a material parameter) + @input colorMap the texture to use for texture mapping + @input color the color this node contribution will be multiplied to + @output outColor the color of the pixel (usually assigned to Global.color) + } + Input{ + vec2 texCoord + vec4 vertColor + vec4 matColor + sampler2D colorMap + vec4 color + } + Output{ + vec4 color + } + } +} \ No newline at end of file diff --git a/engine/src/core-data/Common/MatDefs/ShaderNodes/Common/commonVert.vert b/engine/src/core-data/Common/MatDefs/ShaderNodes/Common/commonVert.vert new file mode 100644 index 000000000..7cd1a7622 --- /dev/null +++ b/engine/src/core-data/Common/MatDefs/ShaderNodes/Common/commonVert.vert @@ -0,0 +1,3 @@ +void main(){ + projPosition = worldViewProjectionMatrix * vec4(modelPosition, 1.0); +} \ No newline at end of file diff --git a/engine/src/core-data/Common/MatDefs/ShaderNodes/Common/unshaded.frag b/engine/src/core-data/Common/MatDefs/ShaderNodes/Common/unshaded.frag new file mode 100644 index 000000000..c14835fd4 --- /dev/null +++ b/engine/src/core-data/Common/MatDefs/ShaderNodes/Common/unshaded.frag @@ -0,0 +1,14 @@ +void main(){ + #ifdef colorMap + color *= texture2D(colorMap, texCoord); + #endif + + #ifdef vertColor + color *= vertColor; + #endif + + #ifdef matColor + color *= matColor; + #endif + +} \ No newline at end of file diff --git a/engine/src/core-data/Common/MatDefs/ShaderNodes/Fog/Fog.j3sn b/engine/src/core-data/Common/MatDefs/ShaderNodes/Fog/Fog.j3sn new file mode 100644 index 000000000..f6c549ff5 --- /dev/null +++ b/engine/src/core-data/Common/MatDefs/ShaderNodes/Fog/Fog.j3sn @@ -0,0 +1,46 @@ +ShaderNodesDefinitions { + ShaderNodeDefinition FogFactor{ + Type: Vertex + Shader GLSL100: Common/MatDefs/ShaderNodes/Fog/fogFactor.vert + Documentation { + This Node is responsible for computing the fog factor of a vertex in the vertex shader. + It computes the fogFactor according to view space z (distance from cam to vertex) and a fogDensity parameter. + This Node should be used with a FogOutput for the fragment shader to effectively output the fog color. + @input modelPostion the vertex position in model space + @input modelViewMatrix the model view matrix responsible to transform a vertex position from model space to view space. + @input fogDensity the fog density (usually assigned with a material parameter) + @output fogFactor the fog factor of the vertex output as a varying + } + Input{ + vec4 modelPosition + // Note here that the fog vertex shader will compute position of the vertex in view space + // This is a pretty common operation that could be used elsewhere. + // IMO I would split this in 2 ShaderNodes, so that the view space pos could be reused. + mat4 modelViewMatrix + float fogDensity + } + Output{ + float fogFactor + } + } + ShaderNodeDefinition FogOutput{ + Type: Fragment + Shader GLSL100: Common/MatDefs/ShaderNodes/Fog/fogOutput.frag + Documentation { + This Node is responsible for multiplying a fog contribution to a color according to a fogColor and a fogFactor. + This node should be used with a FogFactor node that will be responsible to compute the fogFactor in the vertex shader. + @input fogFactor the previously computed fog factor + @input fogColor the fog color + @input color the color the fog contribution will be multiplied to. + @output color the color with fog contribution (usually assigned to Global.color) + } + Input{ + float fogFactor + vec4 fogColor + vec4 color + } + Output{ + vec4 color + } + } +} \ No newline at end of file diff --git a/engine/src/core-data/Common/MatDefs/ShaderNodes/Fog/fogFactor.vert b/engine/src/core-data/Common/MatDefs/ShaderNodes/Fog/fogFactor.vert new file mode 100644 index 000000000..acfe8aced --- /dev/null +++ b/engine/src/core-data/Common/MatDefs/ShaderNodes/Fog/fogFactor.vert @@ -0,0 +1,6 @@ +const float LOG2 = 1.442695; +void main(){ + vec4 viewSpacePos = modelViewMatrix * modelPosition; + fogFactor = exp2(-fogDensity * fogDensity * viewSpacePos.z * viewSpacePos.z * LOG2 ); + fogFactor = clamp(fogFactor, 0.0, 1.0); +} \ No newline at end of file diff --git a/engine/src/core-data/Common/MatDefs/ShaderNodes/Fog/fogOutput.frag b/engine/src/core-data/Common/MatDefs/ShaderNodes/Fog/fogOutput.frag new file mode 100644 index 000000000..d9f245a3b --- /dev/null +++ b/engine/src/core-data/Common/MatDefs/ShaderNodes/Fog/fogOutput.frag @@ -0,0 +1,3 @@ +void main(){ + color = mix(fogColor, color, fogFactor); +} \ No newline at end of file diff --git a/engine/src/core-data/Common/MatDefs/ShaderNodes/LightMapping/LightMapping.j3sn b/engine/src/core-data/Common/MatDefs/ShaderNodes/LightMapping/LightMapping.j3sn new file mode 100644 index 000000000..548519ec9 --- /dev/null +++ b/engine/src/core-data/Common/MatDefs/ShaderNodes/LightMapping/LightMapping.j3sn @@ -0,0 +1,21 @@ +ShaderNodeDefinitions{ + ShaderNodeDefinition LightMapping{ + Type: Fragment + Shader GLSL100: Common/MatDefs/ShaderNodes/LightMapping/lightMap.frag + Documentation { + This Node is responsible for multiplying a light mapping contribution to a given color. + @input texCoord the texture coordinates to use for light mapping + @input lightMap the texture to use for light mapping + @input color the color the lightmap color will be multiplied to + @output color the resulting color + } + Input{ + vec2 texCoord + sampler2D lightMap + vec4 color + } + Output{ + vec4 color + } + } +} \ No newline at end of file diff --git a/engine/src/core-data/Common/MatDefs/ShaderNodes/LightMapping/lightMap.frag b/engine/src/core-data/Common/MatDefs/ShaderNodes/LightMapping/lightMap.frag new file mode 100644 index 000000000..cb0af8345 --- /dev/null +++ b/engine/src/core-data/Common/MatDefs/ShaderNodes/LightMapping/lightMap.frag @@ -0,0 +1,3 @@ +void main(){ + color *= texture2D(lightMap, texCoord); +} \ No newline at end of file