Commit 4371577d authored by Jonas Karlsson's avatar Jonas Karlsson
Browse files

Custom material LOL

Change-Id: Ic0227265624ce8ca2314e1749766b68b0629864a
parent ab9ac416
......@@ -32,6 +32,7 @@ set(qml_resource_files
"WeirdShape.qml"
"main.qml"
"weirdShape.mesh"
"teapot.mesh"
)
qt6_add_resources(custommaterial "qml"
......
......@@ -61,22 +61,13 @@ Window {
visible: true
title: "Custom Materials Example"
MouseArea {
anchors.fill: parent
hoverEnabled: screenSphere.visible
onPositionChanged: {
screenSphere.x = (mouse.x / window.width * 2.0 - 1.0) * 200
screenSphere.y = (mouse.y / window.height * 2.0 - 1.0) * -200
}
}
View3D {
id: v3d
anchors.fill: parent
camera: camera
environment: probeCb.checked ? probeEnv : env
environment: env
SceneEnvironment {
id: env
......@@ -84,153 +75,66 @@ Window {
backgroundMode: SceneEnvironment.Color
}
SceneEnvironment {
id: probeEnv
clearColor: "#444845"
backgroundMode: SceneEnvironment.Color
probeExposure: 10
lightProbe: Texture {
source: "maps/OpenfootageNET_garage-1024.hdr"
}
}
PerspectiveCamera {
id: camera
position: Qt.vector3d(0, 0, 600)
position: Qt.vector3d(0, 100, 600)
}
DirectionalLight {
position: Qt.vector3d(-500, 500, -100)
color: Qt.rgba(0.2, 0.2, 0.2, 1.0)
color: Qt.rgba(1.0, 1.0, 1.0, 1.0)
ambientColor: Qt.rgba(0.1, 0.1, 0.1, 1.0)
scope: probeCb.checked ? dummy : null
}
PointLight {
position: Qt.vector3d(0, 500, 0)
color: Qt.rgba(0.1, 1.0, 0.1, 1.0)
color: Qt.rgba(1.0, 1.0, 1.0, 1.0)
ambientColor: Qt.rgba(0.2, 0.2, 0.2, 1.0)
brightness: 5
castsShadow: true
shadowMapQuality: Light.ShadowMapQualityHigh
scope: probeCb.checked ? dummy : null
}
// NumberAnimation on position.x { from: -100; to: 100; duration: 10000; loops: Animation.Infinite }
Node {
id: dummy
}
property url fragShaderSrc: probeCb.checked ? "material_lightprobe.frag" : (builtinSpecularCb.checked ? "material_builtinspecular.frag" : "material.frag")
Model {
source: "#Rectangle"
y: -200
scale: Qt.vector3d(5, 5, 5)
eulerRotation.x: -90
source: "teapot.mesh"
scale: Qt.vector3d(50, 50, 50)
eulerRotation.z : -20
//NumberAnimation on { from: 0; to: 100; duration: 1000; loops: Animation.Infinite }
PropertyAnimation on eulerRotation.y {
from: 0
to: 360
running: true
duration: 5000
loops: Animation.Infinite
}
materials: [
CustomMaterial {
shadingMode: CustomMaterial.Shaded
vertexShader: "material.vert"
fragmentShader: v3d.fragShaderSrc
fragmentShader: "material.frag"
property real uTime: 0.0
property real uAmplitude: 0.0
property color uDiffuse: "white"
property color uDiffuse: "green"
property real uShininess: 50
}
]
}
WeirdShape {
customMaterial: CustomMaterial {
shadingMode: CustomMaterial.Shaded
vertexShader: "material.vert"
fragmentShader: v3d.fragShaderSrc
property real uTime: 0.0
property real uAmplitude: 0.0
property color uDiffuse: "purple"
property real uShininess: 50
}
position: Qt.vector3d(150, 150, -100)
}
Model {
position: Qt.vector3d(-100, 0, -50)
eulerRotation.x: 30
NumberAnimation on eulerRotation.y {
from: 0; to: 360; duration: 5000; loops: -1
}
scale: Qt.vector3d(1.5, 1.5, 1.5)
source: "#Cylinder"
materials: [
//NumberAnimation on uAmplitude { from: 0; to: 100; duration: 1000; loops: Animation.Infinite }
},
CustomMaterial {
shadingMode: CustomMaterial.Shaded
vertexShader: "material.vert"
fragmentShader: v3d.fragShaderSrc
fragmentShader: "screen.frag"
property real uTime: 0.0
property real uAmplitude: 0.0
property color uDiffuse: "yellow"
property color uDiffuse: "green"
property real uShininess: 50
SequentialAnimation on uAmplitude {
loops: -1
NumberAnimation { from: 0.0; to: 200.0; duration: 5000 }
NumberAnimation { from: 200.0; to: 0.0; duration: 5000 }
}
}
]
}
Model {
id: screenSphere
source: "#Sphere"
visible: screenTexCb.checked
scale: Qt.vector3d(4, 4, 4)
z: 100
materials: [
CustomMaterial {
shadingMode: CustomMaterial.Shaded
sourceBlend: uKeepAlpha ? CustomMaterial.SrcAlpha : CustomMaterial.NoBlend
destinationBlend: uKeepAlpha ? CustomMaterial.OneMinusSrcAlpha : CustomMaterial.NoBlend
fragmentShader: "screen.frag"
property bool uKeepAlpha: screenTexAlphaCb.checked
//NumberAnimation on uAmplitude { from: 0; to: 100; duration: 1000; loops: Animation.Infinite }
}
]
}
}
Rectangle {
color: "lightGray"
width: controls.implicitWidth
height: controls.implicitHeight
ColumnLayout {
id: controls
RadioButton {
checked: true
text: "Lights, custom diffuse and specular"
}
RadioButton {
id: builtinSpecularCb
checked: false
text: "Light, custom diffuse, built-in specular"
}
RadioButton {
id: probeCb
checked: false
text: "Light probe, metalness (disables directional/point lights)"
onCheckedChanged: if (checked) screenTexCb.checked = false
}
RowLayout {
CheckBox {
id: screenTexCb
enabled: !probeCb.checked
checked: false
text: "Emboss Sphere"
}
CheckBox {
id: screenTexAlphaCb
checked: false
text: "Keep alpha"
}
}
}
}
}
void AMBIENT_LIGHT()
{
DIFFUSE += TOTAL_AMBIENT_COLOR;
}
void DIRECTIONAL_LIGHT()
void POINT_LIGHT()
{
DIFFUSE += uDiffuse.rgb * LIGHT_COLOR * SHADOW_CONTRIB * vec3(max(0.0, dot(normalize(NORMAL), TO_LIGHT_DIR)));
}
void POINT_LIGHT()
void SPECULAR_LIGHT()
{
DIFFUSE += uDiffuse.rgb * LIGHT_COLOR * LIGHT_ATTENUATION * SHADOW_CONTRIB * vec3(max(0.0, dot(normalize(NORMAL), TO_LIGHT_DIR)));
}
void SPECULAR_LIGHT()
float LinearizeDepth(float z)
{
float n = CAMERA_PROPERTIES.x;
float f = CAMERA_PROPERTIES.y;
return (2.0 * n) / (f + n - z * (f - n));
}
float calcSobel() {
vec2 coord = FRAGCOORD.xy / vec2(textureSize(DEPTH_TEXTURE, 0));
vec2 screen_size = vec2(textureSize(DEPTH_TEXTURE, 0));
float w = 1.0 / screen_size[0];
float h = 1.0 / screen_size[1];
float n[9];
n[0] = LinearizeDepth(texture(DEPTH_TEXTURE, coord + vec2( -w, -h)).r);
n[1] = LinearizeDepth(texture(DEPTH_TEXTURE, coord + vec2(0.0, -h)).r);
n[2] = LinearizeDepth(texture(DEPTH_TEXTURE, coord + vec2( w, -h)).r);
n[3] = LinearizeDepth(texture(DEPTH_TEXTURE, coord + vec2( -w, 0.0)).r);
n[4] = LinearizeDepth(texture(DEPTH_TEXTURE, coord).r);
n[5] = LinearizeDepth(texture(DEPTH_TEXTURE, coord + vec2( w, 0.0)).r);
n[6] = LinearizeDepth(texture(DEPTH_TEXTURE, coord + vec2( -w, h)).r);
n[7] = LinearizeDepth(texture(DEPTH_TEXTURE, coord + vec2(0.0, h)).r);
n[8] = LinearizeDepth(texture(DEPTH_TEXTURE, coord + vec2( w, h)).r);
float sobel_edge_h = n[2] + (2.0*n[5]) + n[8] - (n[0] + (2.0*n[3]) + n[6]);
float sobel_edge_v = n[0] + (2.0*n[1]) + n[2] - (n[6] + (2.0*n[7]) + n[8]);
float sobel = sqrt((sobel_edge_h * sobel_edge_h) + (sobel_edge_v * sobel_edge_v));
return sobel;
}
void DIRECTIONAL_LIGHT()
{
vec3 V = normalize(VAR_VIEW_VECTOR);
vec3 H = normalize(V + TO_LIGHT_DIR);
float cosAlpha = max(0.0, dot(H, normalize(NORMAL)));
float shine = pow(cosAlpha, uShininess);
const vec3 specularColor = vec3(1.0);
SPECULAR += shine * specularColor;
float shades = 4.0;
vec3 outlineColor = vec3(0.0);
vec3 dirLight = uDiffuse.rgb * LIGHT_COLOR * SHADOW_CONTRIB * vec3(max(0.0, dot(normalize(NORMAL), TO_LIGHT_DIR)));
if (calcSobel() > 0.01)
DIFFUSE = outlineColor;
else
DIFFUSE = ceil(dirLight * shades)/shades;
}
......@@ -3,5 +3,6 @@
<file>main.qml</file>
<file>weirdShape.mesh</file>
<file>WeirdShape.qml</file>
<file>teapot.mesh</file>
</qresource>
</RCC>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment