Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:ovilab/ShaderNodes into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
dragly committed Mar 21, 2017
2 parents 2cf01c7 + 5975426 commit 21eb977
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 58 deletions.
40 changes: 20 additions & 20 deletions src/imports/ShaderNodes/Add.qml
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import QtQuick 2.0 as QQ2

import ShaderNodes 1.0

ShaderNode {
id: root

property var values: [0.5]
property var value1: 0.0
property var value2: 0.0
property var mix: 0.5

arrayProperties: ["values"]
exportedTypeName: "Add"

name: "add"
type: ShaderNodes.glslType(values)

source: {
var result = ""
result += type + " $sumresult = 0.0;\n"
if(values && values.length) {
for(var i in values) {
result += "$sumresult += $(values[" + i + "], " + type + ");\n"
}
} else {
result += "$sumresult = $values;\n"
type: {
var type1 = glslType(value1)
var type2 = glslType(value2)
if(type1 === type2) {
return type1
}

return result;
if(type1 === "float") {
return type2
}
if(type2 === "float") {
return type1
}
return type1
}

result: "$sumresult"
result: {
var type1 = glslType(value1)
var type2 = glslType(value2)
return "$(value1, " + type + ") + $(value2, " + type + ")"
}
}
12 changes: 12 additions & 0 deletions src/imports/ShaderNodes/CombineRgbVectorAlpha.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import ShaderNodes 1.0

ShaderNode {
property var vector: 0.0
property var alpha: 0.0

exportedTypeName: "CombineRgba"

name: "combine_rgb_vector_alpha"
type: "vec4"
result: "vec4($(vector, vec3), $(alpha, float))"
}
14 changes: 14 additions & 0 deletions src/imports/ShaderNodes/CombineRgba.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import ShaderNodes 1.0

ShaderNode {
property var red: 0.0
property var green: 0.0
property var blue: 0.0
property var alpha: 0.0

exportedTypeName: "CombineRgba"

name: "combine_rgba"
type: "vec4"
result: "vec4($(red, float), $(green, float), $(blue, float), $(alpha, float))"
}
97 changes: 73 additions & 24 deletions src/imports/ShaderNodes/ShaderBuilderEffect.qml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ import QtQuick.Scene3D 2.0
Effect {
id: root

property int sourceRgbArg: BlendEquationArguments.SourceAlpha
property int destinationRgbArg: BlendEquationArguments.OneMinusSourceAlpha
property int sourceAlphaArg: BlendEquationArguments.SourceAlpha
property int destinationAlphaArg: BlendEquationArguments.OneMinusSourceAlpha

property int blendFunctionArg: BlendEquation.Add

property alias fragmentShaderBuilder: fragmentShaderBuilder
property alias vertexShaderBuilder: vertexShaderBuilder
property alias fragmentColor: _fragmentColor.value
property alias deferredColor: deferredColor.value
property alias vertexPosition: _position.value

techniques: [
Expand All @@ -32,7 +40,18 @@ Effect {
shaderProgram: ShaderProgram {
vertexShaderCode: vertexShaderBuilder.finalShader
fragmentShaderCode: fragmentShaderBuilder.finalShader
}
}
renderStates: [
BlendEquationArguments {
sourceRgb: sourceRgbArg
destinationRgb: destinationRgbArg
sourceAlpha: sourceAlphaArg
destinationAlpha: destinationAlphaArg
},
BlendEquation {
blendFunction: blendFunctionArg
}
]
}
},
Technique {
Expand All @@ -44,6 +63,7 @@ Effect {
}
filterKeys: FilterKey { name: "renderingStyle"; value: "deferred" }
renderPasses: RenderPass {
id: deferredGeometryRenderPass
filterKeys: FilterKey { name: "pass"; value: "geometry" }
shaderProgram : ShaderProgram {
vertexShaderCode:
Expand All @@ -52,7 +72,6 @@ in vec4 vertexPosition;
in vec3 vertexNormal;
out vec3 normal0;
out vec3 position0;
out vec4 color0;
uniform mat4 mvp;
uniform mat4 modelMatrix;
uniform mat4 modelView;
Expand All @@ -61,32 +80,11 @@ uniform mat3 modelViewNormal;
uniform vec4 meshColor;
void main()
{
color0 = vec4(1.0, 0.0, 0.0, 1.0);
position0 = (modelMatrix * vec4(vertexPosition.xyz, 1.0)).xyz ;
normal0 = modelNormalMatrix * vertexNormal;
gl_Position = mvp * vertexPosition;
}"
fragmentShaderCode:
"#version 330
in vec3 normal0;
in vec3 position0;
in vec4 color0;
out vec4 normal;
out vec4 position;
out vec4 color;
uniform vec3 eyePosition;
uniform vec3 viewVector;
void main()
{
color = color0;
float posMin = -100; // TODO should be set as uniforms
float posMax = 100;
float deltaMaxMin = posMax - posMin;
position = vec4((position0-eyePosition-posMin) / deltaMaxMin, 1.0);
normal = vec4((normalize(normal0) + 1.0) / 2.0, 1.0);
}"
fragmentShaderCode: deferredFragmentShaderBuilder.finalShader
}
}
}
Expand Down Expand Up @@ -186,4 +184,55 @@ void main()
}
]
}
ShaderBuilder {
id: deferredFragmentShaderBuilder

shaderType: ShaderBuilder.Fragment

source: "#version 330
in vec3 normal0;
in vec3 position0;
out vec4 normal;
out vec4 position;
out vec4 fragColor;
uniform vec3 eyePosition;
uniform vec3 viewVector;
#pragma shadernodes header
void main()
{
float posMin = -100; // TODO should be set as uniforms
float posMax = 100;
float deltaMaxMin = posMax - posMin;
position = vec4((position0-eyePosition-posMin) / deltaMaxMin, 1.0);
normal = vec4((normalize(normal0) + 1.0) / 2.0, 1.0);
#pragma shadernodes body
}"
renderPass: deferredGeometryRenderPass
inputs: [
ShaderNode {
type: "vec3"
name: "position"
result: "position"
},
ShaderNode {
type: "vec3"
name: "normal"
result: "normal"
}
]

outputs: [
ShaderOutput {
id: deferredColor
type: "vec4"
name: "fragColor"
value: "purple"
}
]
}
}
1 change: 1 addition & 0 deletions src/imports/ShaderNodes/ShaderBuilderMaterial.qml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import QtQuick.Scene3D 2.0
Material {
id: root
property alias fragmentColor: shaderBuilderEffect.fragmentColor
property alias deferredColor: shaderBuilderEffect.deferredColor
property alias vertexPosition: shaderBuilderEffect.vertexPosition

property alias vertex: shaderBuilderEffect.vertexShaderBuilder
Expand Down
7 changes: 4 additions & 3 deletions src/imports/ShaderNodes/StandardMaterial.qml
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ ShaderNode {
arrayProperties: ["lights"]

name: "diffuse"
type: "vec3"
type: "vec4"
source: {
var output = ""
output += "$this = vec3(0.0, 0.0, 0.0);\n"
output += "$this = vec4(0.0, 0.0, 0.0, 0.0);\n"
output += "$this.a = $(diffuseColor, vec4).a;\n"

for(var i in lights) {
output += "$this += standardMaterialLight($lights[" + i + "], $(normal, vec3), $(position, vec3), eyePosition,\n"
output += "$this.xyz += standardMaterialLight($lights[" + i + "], $(normal, vec3), $(position, vec3), eyePosition,\n"
output += " $(ambientColor, vec3), $(diffuseColor, vec3), $(specularColor, vec3),\n"
output += " 0.01 * $(ambientIntensity, float), $(diffuseIntensity, float), 0.01 * $(specularIntensity, float),\n"
output += " 150.0 * $(hardness, float), $(attenuationOffset, float));\n"
Expand Down
27 changes: 20 additions & 7 deletions src/imports/ShaderNodes/Sum.qml
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import QtQuick 2.0 as QQ2

import ShaderNodes 1.0

ShaderNode {
property list<ShaderNode> inputs
id: root

property var values: [0.5]

arrayProperties: ["values"]
exportedTypeName: "Sum"

type: "float"
name: "sum"
type: ShaderNodes.glslType(values)

source: {
var output = "";
output += "float $sumresult = 0.0;\n"
for(var i in inputs) {
output += "$sumresult += $(inputs[" + i + "], float);\n";
var result = ""
result += type + " $sumresult = 0.0;\n"
if(values && values.length) {
for(var i in values) {
result += "$sumresult += $(values[" + i + "], " + type + ");\n"
}
} else {
result += "$sumresult = $values;\n"
}
return output

return result;
}

result: "$sumresult"
}
2 changes: 2 additions & 0 deletions src/imports/ShaderNodes/qmldir
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ AmbientOcclusion 1.0 AmbientOcclusion.qml
Bump 1.0 Bump.qml
Clamp 1.0 Clamp.qml
CombineRgb 1.0 CombineRgb.qml
CombineRgba 1.0 CombineRgba.qml
CombineRgbVectorAlpha 1.0 CombineRgbVectorAlpha.qml
Decolorize 1.0 Decolorize.qml
Displacement 1.0 Displacement.qml
Function 1.0 Function.qml
Expand Down
2 changes: 2 additions & 0 deletions src/imports/shadernodes_imports.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,7 @@
<file>ShaderNodes/CombineRgb.qml</file>
<file>ShaderNodes/images/empty.png</file>
<file>ShaderNodes/ShaderBuilderEffect.qml</file>
<file>ShaderNodes/CombineRgba.qml</file>
<file>ShaderNodes/CombineRgbVectorAlpha.qml</file>
</qresource>
</RCC>
1 change: 0 additions & 1 deletion src/shaderbuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ ShaderBuilder::ShaderBuilder(QNode *parent)

ShaderBuilder::~ShaderBuilder()
{
disconnectDependencies();
}

void ShaderBuilder::disconnectDependencies()
Expand Down
11 changes: 8 additions & 3 deletions src/shadernode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,15 @@ ShaderNodeSetupResult ShaderNode::setup(ShaderBuilder* shaderBuilder, QString te
continue;
}
// replaces '$property' or '$(property, type)'
QRegularExpression namedTargetRegex("\\$(\\(\\s*)?" + propertyName + "\\b(\\s*,\\s*" + targetType + "\\s*\\))?");
sourceContent.replace(namedTargetRegex, ShaderUtils::convert(sourceType, targetType, targetIdentifier));
if(targetType.isEmpty()) { // TODO do similar splits for indexed and others
QRegularExpression namedTargetRegex("\\$(\\(\\s*)?" + propertyName + "\\b");
sourceContent.replace(namedTargetRegex, targetIdentifier);
} else {
QRegularExpression namedTargetRegex("\\$(\\(\\s*)?" + propertyName + "\\b\\s*,\\s*" + targetType + "\\s*\\)");
sourceContent.replace(namedTargetRegex, ShaderUtils::convert(sourceType, targetType, targetIdentifier));
}

alreadyReplaced.append(propertyName);
// alreadyReplaced.append(propertyName);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/shaderuniformvalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ void ShaderUniformValue::updateValue()
{
QByteArray name = m_propertyName.toUtf8();
QVariant value = m_node->property(name.constData());
if(!value.isValid()) {
// TODO consider throwing a warning
value = 0.0;
}
if(value.type() == QVariant::String) {
value = QColor(value.toString());
}
Expand Down

0 comments on commit 21eb977

Please sign in to comment.