Skip to content

Commit

Permalink
Add simple example and add README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
dragly committed Mar 21, 2017
1 parent 505f041 commit 2cf01c7
Show file tree
Hide file tree
Showing 15 changed files with 285 additions and 74 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# ShaderNodes #

ShaderNodes is a shader generator for Qt3D inspired by material designers found
in 3D software like Blender.
It produces GLSL shaders based on an object graph built in QML or C++.

Currently, only graphs built in QML are supported.

The simplest way to use ShaderNodes is to use the ShaderBuilderMaterial.
This can be used directly in place of any Qt3D material:

```qml
Entity {
components: [
ShaderBuilderMaterial {
id: material
fragmentColor: StandardMaterial {
color: "green"
normal: Bump {
texture: Noise {
vector: material.fragment.position
}
}
}
},
SphereMesh {
id: mesh
}
]
}
```

You can also build your own materials by using a ShaderBuilderEffect
or by generating the shader code with ShaderBuilder directly.
9 changes: 0 additions & 9 deletions app/Node.qml
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ Item {
updateHandleConnectionPoints()
}

function reloadShaderNode() {
// var tmp = loader.source
// loader.source = ""
// loader.source = tmp
}

function updateHandleConnectionPoints() {
for(var i in allHandles) {
var handle = allHandles[i]
Expand All @@ -70,7 +64,6 @@ Item {
console.log(handleComponent.errorString())
return
}
// var glslType = ShaderNodes.glslType(value)
var properties = {
name: name,
identifier: name,
Expand Down Expand Up @@ -142,8 +135,6 @@ Item {

allHandles = inputHandles.concat(outputHandles)

// object.destroy()

updateHandleConnectionPoints()
}

Expand Down
3 changes: 2 additions & 1 deletion examples/examples.pro
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ TEMPLATE = subdirs

SUBDIRS += \
sharedmaterials \
sharednodes
sharednodes \
simple
6 changes: 0 additions & 6 deletions examples/sharedmaterials/sharedmaterials.pro
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,4 @@ SOURCES += main.cpp

RESOURCES += qml.qrc

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

# Default rules for deployment.
include(deployment.pri)

include(../../package_vendor.pri)
111 changes: 82 additions & 29 deletions examples/sharednodes/main_sharednodes.qml
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,18 @@ ApplicationWindow {
camera: mainCamera
}

Entity {
components: [
mesh,
transform1,
material1
]

Transform {
id: transform1
translation: Qt.vector3d(-4, 0, 0)
}
}
// Entity {
// components: [
// mesh,
// transform1,
// material1
// ]

// Transform {
// id: transform1
// translation: Qt.vector3d(-4, 0, 0)
// }
// }
Entity {
components: [
mesh,
Expand All @@ -87,27 +87,34 @@ ApplicationWindow {
position: Qt.vector3d(0, 4, 4)
}

ShaderBuilderMaterial {
id: material1

fragmentColor: StandardMaterial {
color: "green"
lights: [
light1,
light2
]
}
}
// ShaderBuilderMaterial {
// id: material1

// fragmentColor: StandardMaterial {
// color: "green"
// lights: [
// light1,
// light2
// ]
// }
// }

ShaderBuilderMaterial {
id: material2

fragmentColor: StandardMaterial {
color: "green"
lights: [
light1,
light2
]
// fragmentColor: StandardMaterial {
// id: standardMaterial2
// color: "green"
// lights: [
// light1,
// light2
// ]
// }

fragmentColor: Mix {
id: standardMaterial2
value1: 1.0
value2: Qt.vector3d(0.1, 0.1, 0.1)
}
}

Expand All @@ -123,5 +130,51 @@ ApplicationWindow {
value: 0.5
width: 300
}

Timer {
property ShaderNode node: null
running: true
repeat: true
interval: 60
onTriggered: {
if(node) {
standardMaterial2.value1 = 2.3
standardMaterial2.value1 = Qt.vector3d(2.0, 1.0, 2.0)
node.destroy()
node = null
} else {
var object = Qt.createQmlObject("import ShaderNodes 1.0; Mix {}", material2)
standardMaterial2.value1 = object
node = object
}
}
}

// Timer {
// running: true
// repeat: true
// interval: 100
// onTriggered: {
// standardMaterial2.value1 = 1.0
// }
// }

// Timer {
// running: true
// repeat: true
// interval: 150
// onTriggered: {
// standardMaterial2.value1 = node
// }
// }

// Timer {
// running: true
// repeat: true
// interval: 180
// onTriggered: {
// standardMaterial2.value1 = Qt.vector3d(1.0, 0.5, 0.1)
// }
// }
}
}
6 changes: 0 additions & 6 deletions examples/sharednodes/sharednodes.pro
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,4 @@ SOURCES += main.cpp

RESOURCES += qml.qrc

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

# Default rules for deployment.
include(deployment.pri)

include(../../package_vendor.pri)
20 changes: 20 additions & 0 deletions examples/simple/main_simple.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QSurfaceFormat>
#include <vendor.h>

int main(int argc, char *argv[])
{
QSurfaceFormat format;
format.setMajorVersion(3);
format.setMinorVersion(3);
QSurfaceFormat::setDefaultFormat(format);

QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
qpm::init(app, engine);
engine.load(QUrl(QStringLiteral("qrc:/main_simple.qml")));

return app.exec();
}
75 changes: 75 additions & 0 deletions examples/simple/main_simple.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import QtQuick 2.7
import QtQuick.Controls 1.0 as QQC1
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0

import QtQuick.Scene3D 2.0

import Qt3D.Core 2.0
import Qt3D.Extras 2.0
import Qt3D.Input 2.0
import Qt3D.Logic 2.0
import Qt3D.Render 2.0

import ShaderNodes 1.0

ApplicationWindow {
id: window

visible: true
width: 1280
height: 1024
title: qsTr("Simple example")

Scene3D {
id: root

anchors.fill: parent

aspects: ["logic", "input"]
Entity {
components: [
RenderSettings{
activeFrameGraph: ForwardRenderer {
camera: Camera {
id: mainCamera
projectionType: CameraLens.PerspectiveProjection
fieldOfView: 50
aspectRatio: root.width / root.height
nearPlane: 1.0
farPlane: 100.0
position: Qt.vector3d(8, 4, 8)
viewCenter: Qt.vector3d(0, 0, 0)
upVector: Qt.vector3d(0.0, 1.0, 0.0)
}
clearColor: Qt.rgba(0.2, 0.2, 0.2)
}
},
InputSettings {}
]

OrbitCameraController {
camera: mainCamera
}

Entity {
components: [
ShaderBuilderMaterial {
id: material
fragmentColor: StandardMaterial {
color: "green"
normal: Bump {
texture: Noise {
vector: material.fragment.position
}
}
}
},
SphereMesh {
id: mesh
}
]
}
}
}
}
5 changes: 5 additions & 0 deletions examples/simple/qml.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
<file>main_simple.qml</file>
</qresource>
</RCC>
11 changes: 11 additions & 0 deletions examples/simple/simple.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
TEMPLATE = app

QT += qml quick
CONFIG += c++11

SOURCES += \
main_simple.cpp

RESOURCES += qml.qrc

include(../../package_vendor.pri)
12 changes: 7 additions & 5 deletions src/shaderbuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ void ShaderBuilder::clear()
m_renderPass->removeParameter(uniform->m_parameter);
}
}
m_uniforms.clear();

disconnectDependencies();

m_dependencies.clear();

m_uniforms.clear();
m_finalShader = "";
}

Expand Down Expand Up @@ -194,14 +194,16 @@ void ShaderBuilder::rebuildShader()
connect(node, &ShaderNode::identifierChanged, this, &ShaderBuilder::markDirty);
connect(node, &ShaderNode::propertyTypeChanged, this, &ShaderBuilder::markDirty);
connect(node, &ShaderNode::markDirty, this, &ShaderBuilder::markDirty);
}

if(m_renderPass) {
for(const ShaderUniformValue *uniform : m_uniforms) {
m_renderPass->addParameter(uniform->m_parameter);
if(m_renderPass) {
for(ShaderUniformValue *uniform : node->m_uniforms) {
m_uniforms.append(uniform);
m_renderPass->addParameter(uniform->m_parameter);
}
}
}


QString header = "";
header += "\n// ------ begin generated header ------\n\n";
for(ShaderOutput *output : m_outputs) {
Expand Down
Loading

0 comments on commit 2cf01c7

Please sign in to comment.