summaryrefslogtreecommitdiffstats
path: root/src/extras
diff options
context:
space:
mode:
authorSean Harmer <sean.harmer@kdab.com>2017-05-20 14:57:26 +0100
committerSean Harmer <sean.harmer@kdab.com>2017-05-20 14:57:53 +0100
commitcd9059a22604307f483764db134d51b15c540758 (patch)
tree9ceea8edf2b41e55f17a49552d9cf37fcdedc9b2 /src/extras
parent1d024678ba16dd979a8c7023b35cdc76db357d37 (diff)
parent9bf4daddeb62ec6a40e2d90587a7327acbb3780f (diff)
Merge branch '5.9.0' into 5.9
Diffstat (limited to 'src/extras')
-rw-r--r--src/extras/defaults/qskyboxentity.cpp2
-rw-r--r--src/extras/geometries/qplanegeometry.cpp47
-rw-r--r--src/extras/geometries/qplanegeometry.h4
-rw-r--r--src/extras/geometries/qplanegeometry_p.h1
-rw-r--r--src/extras/geometries/qplanemesh.cpp24
-rw-r--r--src/extras/geometries/qplanemesh.h4
-rw-r--r--src/extras/shaders/es2/light.inc.frag310
-rw-r--r--src/extras/shaders/gl3/metalrough.frag112
-rw-r--r--src/extras/shaders/gl3/metalroughuniform.frag123
9 files changed, 377 insertions, 250 deletions
diff --git a/src/extras/defaults/qskyboxentity.cpp b/src/extras/defaults/qskyboxentity.cpp
index ae3fa1087..3c7b0dd4e 100644
--- a/src/extras/defaults/qskyboxentity.cpp
+++ b/src/extras/defaults/qskyboxentity.cpp
@@ -138,10 +138,8 @@ void QSkyboxEntityPrivate::init()
m_gl3RenderPass->addRenderState(seamlessCubemap);
m_gl2RenderPass->addRenderState(cullFront);
m_gl2RenderPass->addRenderState(depthTest);
- m_gl2RenderPass->addRenderState(seamlessCubemap);
m_es2RenderPass->addRenderState(cullFront);
m_es2RenderPass->addRenderState(depthTest);
- m_es2RenderPass->addRenderState(seamlessCubemap);
m_gl3Technique->addRenderPass(m_gl3RenderPass);
m_gl2Technique->addRenderPass(m_gl2RenderPass);
diff --git a/src/extras/geometries/qplanegeometry.cpp b/src/extras/geometries/qplanegeometry.cpp
index f6eba9e61..14ddb25e6 100644
--- a/src/extras/geometries/qplanegeometry.cpp
+++ b/src/extras/geometries/qplanegeometry.cpp
@@ -54,7 +54,7 @@ namespace Qt3DExtras {
namespace {
-QByteArray createPlaneVertexData(float w, float h, const QSize &resolution)
+QByteArray createPlaneVertexData(float w, float h, const QSize &resolution, bool mirrored)
{
Q_ASSERT(w > 0.0f);
Q_ASSERT(h > 0.0f);
@@ -95,7 +95,7 @@ QByteArray createPlaneVertexData(float w, float h, const QSize &resolution)
// texture coordinates
*fptr++ = u;
- *fptr++ = v;
+ *fptr++ = mirrored ? 1.0f - v : v;
// normal
*fptr++ = 0.0f;
@@ -149,17 +149,18 @@ QByteArray createPlaneIndexData(const QSize &resolution)
class PlaneVertexBufferFunctor : public QBufferDataGenerator
{
public:
- explicit PlaneVertexBufferFunctor(float w, float h, const QSize &resolution)
+ explicit PlaneVertexBufferFunctor(float w, float h, const QSize &resolution, bool mirrored)
: m_width(w)
, m_height(h)
, m_resolution(resolution)
+ , m_mirrored(mirrored)
{}
~PlaneVertexBufferFunctor() {}
QByteArray operator()() Q_DECL_FINAL
{
- return createPlaneVertexData(m_width, m_height, m_resolution);
+ return createPlaneVertexData(m_width, m_height, m_resolution, m_mirrored);
}
bool operator ==(const QBufferDataGenerator &other) const Q_DECL_FINAL
@@ -168,7 +169,8 @@ public:
if (otherFunctor != nullptr)
return (otherFunctor->m_width == m_width &&
otherFunctor->m_height == m_height &&
- otherFunctor->m_resolution == m_resolution);
+ otherFunctor->m_resolution == m_resolution &&
+ otherFunctor->m_mirrored == m_mirrored);
return false;
}
@@ -178,6 +180,7 @@ public:
float m_width;
float m_height;
QSize m_resolution;
+ bool m_mirrored;
};
class PlaneIndexBufferFunctor : public QBufferDataGenerator
@@ -237,6 +240,13 @@ public:
*/
/*!
+ * \qmlproperty bool PlaneGeometry::mirrored
+ * \since 5.9
+ *
+ * Controls if the UV coordinates of the plane should be flipped vertically.
+ */
+
+/*!
* \qmlproperty Attribute PlaneGeometry::positionAttribute
*
* Holds the geometry position attribute.
@@ -318,7 +328,7 @@ void QPlaneGeometry::updateVertices()
d->m_normalAttribute->setCount(nVerts);
d->m_texCoordAttribute->setCount(nVerts);
d->m_tangentAttribute->setCount(nVerts);
- d->m_vertexBuffer->setDataGenerator(QSharedPointer<PlaneVertexBufferFunctor>::create(d->m_width, d->m_height, d->m_meshResolution));
+ d->m_vertexBuffer->setDataGenerator(QSharedPointer<PlaneVertexBufferFunctor>::create(d->m_width, d->m_height, d->m_meshResolution, d->m_mirrored));
}
/*!
@@ -365,6 +375,16 @@ void QPlaneGeometry::setHeight(float height)
emit heightChanged(height);
}
+void QPlaneGeometry::setMirrored(bool mirrored)
+{
+ Q_D(QPlaneGeometry);
+ if (mirrored == d->m_mirrored)
+ return;
+ d->m_mirrored = mirrored;
+ updateVertices();
+ emit mirroredChanged(mirrored);
+}
+
/*!
* \property QPlaneGeometry::resolution
*
@@ -399,6 +419,18 @@ float QPlaneGeometry::height() const
}
/*!
+ * \property QPlaneGeometry::mirrored
+ * \since 5.9
+ *
+ * Controls if the UV coordinates of the plane should be flipped vertically.
+ */
+bool QPlaneGeometry::mirrored() const
+{
+ Q_D(const QPlaneGeometry);
+ return d->m_mirrored;
+}
+
+/*!
* \property QPlaneGeometry::positionAttribute
*
* Holds the geometry position attribute.
@@ -458,6 +490,7 @@ QPlaneGeometryPrivate::QPlaneGeometryPrivate()
, m_width(1.0f)
, m_height(1.0f)
, m_meshResolution(QSize(2, 2))
+ , m_mirrored(false)
, m_positionAttribute(nullptr)
, m_normalAttribute(nullptr)
, m_texCoordAttribute(nullptr)
@@ -525,7 +558,7 @@ void QPlaneGeometryPrivate::init()
// Each primitive has 3 vertives
m_indexAttribute->setCount(faces * 3);
- m_vertexBuffer->setDataGenerator(QSharedPointer<PlaneVertexBufferFunctor>::create(m_width, m_height, m_meshResolution));
+ m_vertexBuffer->setDataGenerator(QSharedPointer<PlaneVertexBufferFunctor>::create(m_width, m_height, m_meshResolution, m_mirrored));
m_indexBuffer->setDataGenerator(QSharedPointer<PlaneIndexBufferFunctor>::create(m_meshResolution));
q->addAttribute(m_positionAttribute);
diff --git a/src/extras/geometries/qplanegeometry.h b/src/extras/geometries/qplanegeometry.h
index 694e04e82..4a4efe6eb 100644
--- a/src/extras/geometries/qplanegeometry.h
+++ b/src/extras/geometries/qplanegeometry.h
@@ -62,6 +62,7 @@ class QT3DEXTRASSHARED_EXPORT QPlaneGeometry : public Qt3DRender::QGeometry
Q_PROPERTY(float width READ width WRITE setWidth NOTIFY widthChanged)
Q_PROPERTY(float height READ height WRITE setHeight NOTIFY heightChanged)
Q_PROPERTY(QSize resolution READ resolution WRITE setResolution NOTIFY resolutionChanged)
+ Q_PROPERTY(bool mirrored READ mirrored WRITE setMirrored NOTIFY mirroredChanged REVISION 9)
Q_PROPERTY(Qt3DRender::QAttribute *positionAttribute READ positionAttribute CONSTANT)
Q_PROPERTY(Qt3DRender::QAttribute *normalAttribute READ normalAttribute CONSTANT)
Q_PROPERTY(Qt3DRender::QAttribute *texCoordAttribute READ texCoordAttribute CONSTANT)
@@ -78,6 +79,7 @@ public:
QSize resolution() const;
float width() const;
float height() const;
+ bool mirrored() const;
Qt3DRender::QAttribute *positionAttribute() const;
Qt3DRender::QAttribute *normalAttribute() const;
@@ -89,11 +91,13 @@ public Q_SLOTS:
void setResolution(const QSize &resolution);
void setWidth(float width);
void setHeight(float height);
+ void setMirrored(bool mirrored);
Q_SIGNALS:
void resolutionChanged(const QSize &resolution);
void widthChanged(float width);
void heightChanged(float height);
+ void mirroredChanged(bool mirrored);
protected:
QPlaneGeometry(QPlaneGeometryPrivate &dd, QNode *parent = nullptr);
diff --git a/src/extras/geometries/qplanegeometry_p.h b/src/extras/geometries/qplanegeometry_p.h
index cfde6da1c..68d979895 100644
--- a/src/extras/geometries/qplanegeometry_p.h
+++ b/src/extras/geometries/qplanegeometry_p.h
@@ -75,6 +75,7 @@ public:
float m_width;
float m_height;
QSize m_meshResolution;
+ bool m_mirrored;
Qt3DRender::QAttribute *m_positionAttribute;
Qt3DRender::QAttribute *m_normalAttribute;
Qt3DRender::QAttribute *m_texCoordAttribute;
diff --git a/src/extras/geometries/qplanemesh.cpp b/src/extras/geometries/qplanemesh.cpp
index 5991c5397..4804df024 100644
--- a/src/extras/geometries/qplanemesh.cpp
+++ b/src/extras/geometries/qplanemesh.cpp
@@ -73,6 +73,13 @@ namespace Qt3DExtras {
*/
/*!
+ * \qmlproperty bool PlaneMesh::mirrored
+ * \since 5.9
+ *
+ * Controls if the UV coordinates of the plane should be flipped vertically.
+ */
+
+/*!
* \class Qt3DExtras::QPlaneMesh
* \inheaderfile Qt3DExtras/QPlaneMesh
* \inmodule Qt3DExtras
@@ -92,6 +99,7 @@ QPlaneMesh::QPlaneMesh(QNode *parent)
QObject::connect(geometry, &QPlaneGeometry::widthChanged, this, &QPlaneMesh::widthChanged);
QObject::connect(geometry, &QPlaneGeometry::heightChanged, this, &QPlaneMesh::heightChanged);
QObject::connect(geometry, &QPlaneGeometry::resolutionChanged, this, &QPlaneMesh::meshResolutionChanged);
+ QObject::connect(geometry, &QPlaneGeometry::mirroredChanged, this, &QPlaneMesh::mirroredChanged);
QGeometryRenderer::setGeometry(geometry);
}
@@ -147,6 +155,22 @@ QSize QPlaneMesh::meshResolution() const
return static_cast<QPlaneGeometry *>(geometry())->resolution();
}
+void QPlaneMesh::setMirrored(bool mirrored)
+{
+ static_cast<QPlaneGeometry *>(geometry())->setMirrored(mirrored);
+}
+
+/*!
+ * \property QPlaneMesh::mirrored
+ * \since 5.9
+ *
+ * Controls if the UV coordinates of the plane should be flipped vertically.
+ */
+bool QPlaneMesh::mirrored() const
+{
+ return static_cast<QPlaneGeometry *>(geometry())->mirrored();
+}
+
} // namespace Qt3DExtras
QT_END_NAMESPACE
diff --git a/src/extras/geometries/qplanemesh.h b/src/extras/geometries/qplanemesh.h
index d68774ca0..1cf2ae79e 100644
--- a/src/extras/geometries/qplanemesh.h
+++ b/src/extras/geometries/qplanemesh.h
@@ -54,6 +54,7 @@ class QT3DEXTRASSHARED_EXPORT QPlaneMesh : public Qt3DRender::QGeometryRenderer
Q_PROPERTY(float width READ width WRITE setWidth NOTIFY widthChanged)
Q_PROPERTY(float height READ height WRITE setHeight NOTIFY heightChanged)
Q_PROPERTY(QSize meshResolution READ meshResolution WRITE setMeshResolution NOTIFY meshResolutionChanged)
+ Q_PROPERTY(bool mirrored READ mirrored WRITE setMirrored NOTIFY mirroredChanged REVISION 9)
public:
explicit QPlaneMesh(Qt3DCore::QNode *parent = nullptr);
@@ -62,16 +63,19 @@ public:
float width() const;
float height() const;
QSize meshResolution() const;
+ bool mirrored() const;
public Q_SLOTS:
void setWidth(float width);
void setHeight(float height);
void setMeshResolution(const QSize &resolution);
+ void setMirrored(bool mirrored);
Q_SIGNALS:
void meshResolutionChanged(const QSize &meshResolution);
void widthChanged(float width);
void heightChanged(float height);
+ void mirroredChanged(bool mirrored);
private:
// As this is a default provided geometry renderer, no one should be able
diff --git a/src/extras/shaders/es2/light.inc.frag b/src/extras/shaders/es2/light.inc.frag
index 726340d7e..074af5799 100644
--- a/src/extras/shaders/es2/light.inc.frag
+++ b/src/extras/shaders/es2/light.inc.frag
@@ -14,215 +14,179 @@ struct Light {
uniform Light lights[MAX_LIGHTS];
uniform int lightCount;
-void addLightAdsModelNormalMapped(const in FP vec3 vpos,
- const in FP vec3 n,
- const in FP vec3 eye,
- const in FP float shininess,
- const in FP mat3 tangentMatrix,
- const in Light light,
- inout FP vec3 diffuseColor,
- inout FP vec3 specularColor)
-{
- FP vec3 snormal = normalize( vec3( tangentMatrix[0][2], tangentMatrix[1][2], tangentMatrix[2][2] ) );
-
- FP vec3 s, ts;
- FP float att = 1.0;
- if ( light.type != TYPE_DIRECTIONAL ) {
- s = light.position - vpos;
- if ( dot(snormal, s) < 0.0 )
- att = 0.0;
- else {
- ts = normalize( tangentMatrix * s );
- if (length( light.attenuation ) != 0.0) {
- FP float dist = length(s);
- att = 1.0 / (light.attenuation.x + light.attenuation.y * dist + light.attenuation.z * dist * dist);
- }
- s = normalize( s );
- if ( light.type == TYPE_SPOT ) {
- if ( degrees(acos(dot(-s, normalize(light.direction))) ) > light.cutOffAngle)
- att = 0.0;
- }
- }
- } else {
- if ( dot(snormal, -light.direction) > 0.0 )
- s = normalize( tangentMatrix * -light.direction );
- else
- att = 0.0;
- }
-
- FP float diffuse = max( dot( ts, n ), 0.0 );
-
- FP float specular = 0.0;
- if (diffuse > 0.0 && shininess > 0.0 && att > 0.0) {
- FP vec3 r = reflect( -ts, n );
- FP vec3 v = normalize( tangentMatrix * ( eye - vpos ) );
- FP float normFactor = ( shininess + 2.0 ) / 2.0;
- specular = normFactor * pow( max( dot( r, v ), 0.0 ), shininess );
- }
-
- diffuseColor += att * light.intensity * diffuse * light.color;
- specularColor += att * light.intensity * specular * light.color;
-}
-
void adsModelNormalMapped(const in FP vec3 vpos, const in FP vec3 vnormal, const in FP vec3 eye, const in FP float shininess,
const in FP mat3 tangentMatrix,
out FP vec3 diffuseColor, out FP vec3 specularColor)
{
diffuseColor = vec3(0.0);
specularColor = vec3(0.0);
- if (lightCount < 1) return;
- FP vec3 n = normalize( vnormal );
- addLightAdsModelNormalMapped(vpos, n, eye, shininess, tangentMatrix, lights[0], diffuseColor, specularColor);
- if (lightCount < 2) return;
-
- addLightAdsModelNormalMapped(vpos, n, eye, shininess, tangentMatrix, lights[1], diffuseColor, specularColor);
- if (lightCount < 3) return;
-
- addLightAdsModelNormalMapped(vpos, n, eye, shininess, tangentMatrix, lights[2], diffuseColor, specularColor);
- if (lightCount < 4) return;
-
- addLightAdsModelNormalMapped(vpos, n, eye, shininess, tangentMatrix, lights[3], diffuseColor, specularColor);
- if (lightCount < 5) return;
-
- addLightAdsModelNormalMapped(vpos, n, eye, shininess, tangentMatrix, lights[4], diffuseColor, specularColor);
- if (lightCount < 6) return;
-
- addLightAdsModelNormalMapped(vpos, n, eye, shininess, tangentMatrix, lights[5], diffuseColor, specularColor);
- if (lightCount < 7) return;
-
- addLightAdsModelNormalMapped(vpos, n, eye, shininess, tangentMatrix, lights[6], diffuseColor, specularColor);
- if (lightCount < 8) return;
-
- addLightAdsModelNormalMapped(vpos, n, eye, shininess, tangentMatrix, lights[7], diffuseColor, specularColor);
-}
+ FP vec3 snormal = normalize( vec3( tangentMatrix[0][2], tangentMatrix[1][2], tangentMatrix[2][2] ) );
-void addLightAdsModel(const in FP vec3 vpos,
- const in FP vec3 n,
- const in FP vec3 eye,
- const in FP float shininess,
- const in Light light,
- inout FP vec3 diffuseColor,
- inout FP vec3 specularColor)
-{
- FP vec3 s;
+ FP vec3 n = normalize( vnormal );
- FP float att = 1.0;
- if ( light.type != TYPE_DIRECTIONAL ) {
- s = light.position - vpos;
- if (length( light.attenuation ) != 0.0) {
- FP float dist = length(s);
- att = 1.0 / (light.attenuation.x + light.attenuation.y * dist + light.attenuation.z * dist * dist);
- }
- s = normalize( s );
- if ( light.type == TYPE_SPOT ) {
- if ( degrees(acos(dot(-s, normalize(light.direction))) ) > light.cutOffAngle)
+ FP vec3 s, ts;
+ Light light;
+ for (int i = 0; i < lightCount; ++i) {
+ if (i == 0)
+ light = lights[0];
+ else if (i == 1)
+ light = lights[1];
+ else if (i == 2)
+ light = lights[2];
+ else if (i == 3)
+ light = lights[3];
+ else if (i == 4)
+ light = lights[4];
+ else if (i == 5)
+ light = lights[5];
+ else if (i == 6)
+ light = lights[6];
+ else if (i == 7)
+ light = lights[7];
+
+ FP float att = 1.0;
+ if ( light.type != TYPE_DIRECTIONAL ) {
+ s = light.position - vpos;
+ if ( dot(snormal, s) < 0.0 )
+ att = 0.0;
+ else {
+ ts = normalize( tangentMatrix * s );
+ if (length( light.attenuation ) != 0.0) {
+ FP float dist = length(s);
+ att = 1.0 / (light.attenuation.x + light.attenuation.y * dist + light.attenuation.z * dist * dist);
+ }
+ s = normalize( s );
+ if ( light.type == TYPE_SPOT ) {
+ if ( degrees(acos(dot(-s, normalize(light.direction))) ) > light.cutOffAngle)
+ att = 0.0;
+ }
+ }
+ } else {
+ if ( dot(snormal, -light.direction) > 0.0 )
+ s = normalize( tangentMatrix * -light.direction );
+ else
att = 0.0;
}
- } else {
- s = normalize( -light.direction );
- }
- FP float diffuse = max( dot( s, n ), 0.0 );
- FP float specular = 0.0;
+ FP float diffuse = max( dot( ts, n ), 0.0 );
- if (diffuse > 0.0 && shininess > 0.0 && att > 0.0) {
- FP vec3 r = reflect( -s, n );
- FP vec3 v = normalize( eye - vpos );
- FP float normFactor = ( shininess + 2.0 ) / 2.0;
- specular = normFactor * pow( max( dot( r, v ), 0.0 ), shininess );
- }
+ FP float specular = 0.0;
+ if (diffuse > 0.0 && shininess > 0.0 && att > 0.0) {
+ FP vec3 r = reflect( -ts, n );
+ FP vec3 v = normalize( tangentMatrix * ( eye - vpos ) );
+ FP float normFactor = ( shininess + 2.0 ) / 2.0;
+ specular = normFactor * pow( max( dot( r, v ), 0.0 ), shininess );
+ }
- diffuseColor += att * light.intensity * diffuse * light.color;
- specularColor += att * light.intensity * specular * light.color;
+ diffuseColor += att * light.intensity * diffuse * light.color;
+ specularColor += att * light.intensity * specular * light.color;
+ }
}
-
void adsModel(const in FP vec3 vpos, const in FP vec3 vnormal, const in FP vec3 eye, const in FP float shininess,
out FP vec3 diffuseColor, out FP vec3 specularColor)
{
diffuseColor = vec3(0.0);
specularColor = vec3(0.0);
- if (lightCount < 1) return;
FP vec3 n = normalize( vnormal );
- addLightAdsModel(vpos, n, eye, shininess, lights[0], diffuseColor, specularColor);
- if (lightCount < 2) return;
-
- addLightAdsModel(vpos, n, eye, shininess, lights[1], diffuseColor, specularColor);
- if (lightCount < 3) return;
-
- addLightAdsModel(vpos, n, eye, shininess, lights[2], diffuseColor, specularColor);
- if (lightCount < 4) return;
-
- addLightAdsModel(vpos, n, eye, shininess, lights[3], diffuseColor, specularColor);
- if (lightCount < 5) return;
-
- addLightAdsModel(vpos, n, eye, shininess, lights[4], diffuseColor, specularColor);
- if (lightCount < 6) return;
-
- addLightAdsModel(vpos, n, eye, shininess, lights[5], diffuseColor, specularColor);
- if (lightCount < 7) return;
- addLightAdsModel(vpos, n, eye, shininess, lights[6], diffuseColor, specularColor);
- if (lightCount < 8) return;
-
- addLightAdsModel(vpos, n, eye, shininess, lights[7], diffuseColor, specularColor);
-}
-
-void addLightAdModel(const in FP vec3 vpos,
- const in FP vec3 n,
- const in Light light,
- inout FP vec3 diffuseColor)
-{
FP vec3 s;
- FP float att = 1.0;
- if ( light.type != TYPE_DIRECTIONAL ) {
- s = light.position - vpos;
- if (length( light.attenuation ) != 0.0) {
- FP float dist = length(s);
- att = 1.0 / (light.attenuation.x + light.attenuation.y * dist + light.attenuation.z * dist * dist);
- }
- s = normalize( s );
- if ( light.type == TYPE_SPOT ) {
- if ( degrees(acos(dot(-s, normalize(light.direction))) ) > light.cutOffAngle)
- att = 0.0;
+ Light light;
+ for (int i = 0; i < lightCount; ++i) {
+ if (i == 0)
+ light = lights[0];
+ else if (i == 1)
+ light = lights[1];
+ else if (i == 2)
+ light = lights[2];
+ else if (i == 3)
+ light = lights[3];
+ else if (i == 4)
+ light = lights[4];
+ else if (i == 5)
+ light = lights[5];
+ else if (i == 6)
+ light = lights[6];
+ else if (i == 7)
+ light = lights[7];
+
+ FP float att = 1.0;
+ if ( light.type != TYPE_DIRECTIONAL ) {
+ s = light.position - vpos;
+ if (length( light.attenuation ) != 0.0) {
+ FP float dist = length(s);
+ att = 1.0 / (light.attenuation.x + light.attenuation.y * dist + light.attenuation.z * dist * dist);
+ }
+ s = normalize( s );
+ if ( light.type == TYPE_SPOT ) {
+ if ( degrees(acos(dot(-s, normalize(light.direction))) ) > light.cutOffAngle)
+ att = 0.0;
+ }
+ } else {
+ s = normalize( -light.direction );
}
- } else {
- s = normalize( -light.direction );
- }
- FP float diffuse = max( dot( s, n ), 0.0 );
+ FP float diffuse = max( dot( s, n ), 0.0 );
+
+ FP float specular = 0.0;
+ if (diffuse > 0.0 && shininess > 0.0 && att > 0.0) {
+ FP vec3 r = reflect( -s, n );
+ FP vec3 v = normalize( eye - vpos );
+ FP float normFactor = ( shininess + 2.0 ) / 2.0;
+ specular = normFactor * pow( max( dot( r, v ), 0.0 ), shininess );
+ }
- diffuseColor += att * light.intensity * diffuse * light.color;
+ diffuseColor += att * light.intensity * diffuse * light.color;
+ specularColor += att * light.intensity * specular * light.color;
+ }
}
void adModel(const in FP vec3 vpos, const in FP vec3 vnormal, out FP vec3 diffuseColor)
{
diffuseColor = vec3(0.0);
- if (lightCount < 1) return;
FP vec3 n = normalize( vnormal );
- addLightAdModel(vpos, n, lights[0], diffuseColor);
- if (lightCount < 2) return;
-
- addLightAdModel(vpos, n, lights[1], diffuseColor);
- if (lightCount < 3) return;
- addLightAdModel(vpos, n, lights[2], diffuseColor);
- if (lightCount < 4) return;
-
- addLightAdModel(vpos, n, lights[3], diffuseColor);
- if (lightCount < 5) return;
-
- addLightAdModel(vpos, n, lights[4], diffuseColor);
- if (lightCount < 6) return;
-
- addLightAdModel(vpos, n, lights[5], diffuseColor);
- if (lightCount < 7) return;
+ FP vec3 s;
+ Light light;
+ for (int i = 0; i < lightCount; ++i) {
+ if (i == 0)
+ light = lights[0];
+ else if (i == 1)
+ light = lights[1];
+ else if (i == 2)
+ light = lights[2];
+ else if (i == 3)
+ light = lights[3];
+ else if (i == 4)
+ light = lights[4];
+ else if (i == 5)
+ light = lights[5];
+ else if (i == 6)
+ light = lights[6];
+ else if (i == 7)
+ light = lights[7];
+
+ FP float att = 1.0;
+ if ( light.type != TYPE_DIRECTIONAL ) {
+ s = light.position - vpos;
+ if (length( light.attenuation ) != 0.0) {
+ FP float dist = length(s);
+ att = 1.0 / (light.attenuation.x + light.attenuation.y * dist + light.attenuation.z * dist * dist);
+ }
+ s = normalize( s );
+ if ( light.type == TYPE_SPOT ) {
+ if ( degrees(acos(dot(-s, normalize(light.direction))) ) > light.cutOffAngle)
+ att = 0.0;
+ }
+ } else {
+ s = normalize( -light.direction );
+ }
- addLightAdModel(vpos, n, lights[6], diffuseColor);
- if (lightCount < 8) return;
+ FP float diffuse = max( dot( s, n ), 0.0 );
- addLightAdModel(vpos, n, lights[7], diffuseColor);
+ diffuseColor += att * light.intensity * diffuse * light.color;
+ }
}
diff --git a/src/extras/shaders/gl3/metalrough.frag b/src/extras/shaders/gl3/metalrough.frag
index e1eb6bab2..7f2f3d20e 100644
--- a/src/extras/shaders/gl3/metalrough.frag
+++ b/src/extras/shaders/gl3/metalrough.frag
@@ -71,11 +71,6 @@ uniform sampler2D ambientOcclusionMap;
// User control parameters
uniform float metalFactor = 1.0;
-// Roughness -> mip level mapping
-uniform float maxT = 0.939824;
-uniform float mipLevels = 11.0;
-uniform float mipOffset = 5.0;
-
// Exposure correction
uniform float exposure = 0.0;
// Gamma correction
@@ -83,6 +78,25 @@ uniform float gamma = 2.2;
#pragma include light.inc.frag
+int mipLevelCount(const in samplerCube cube)
+{
+ int baseSize = textureSize(cube, 0).x;
+ int nMips = int(log2(float(baseSize>0 ? baseSize : 1))) + 1;
+ return nMips;
+}
+
+float remapRoughness(const in float roughness)
+{
+ // As per page 14 of
+ // http://www.frostbite.com/wp-content/uploads/2014/11/course_notes_moving_frostbite_to_pbr.pdf
+ // we remap the roughness to give a more perceptually linear response
+ // of "bluriness" as a function of the roughness specified by the user.
+ // r = roughness^2
+ const float maxSpecPower = 999999.0;
+ const float minRoughness = sqrt(2.0 / (maxSpecPower + 2));
+ return max(roughness * roughness, minRoughness);
+}
+
mat3 calcWorldSpaceToTangentSpaceMatrix(const in vec3 wNormal, const in vec4 wTangent)
{
// Make the tangent truly orthogonal to the normal by using Gram-Schmidt.
@@ -103,32 +117,42 @@ mat3 calcWorldSpaceToTangentSpaceMatrix(const in vec3 wNormal, const in vec4 wTa
return worldToTangentMatrix;
}
-float roughnessToMipLevel(float roughness)
+float alphaToMipLevel(float alpha)
{
- // HACK: Improve the roughness -> mip level mapping for roughness map from substace painter
- // TODO: Use mathematica or similar to improve this mapping more generally
- roughness = 0.75 + (1.7 * (roughness - 0.5));
- return (mipLevels - 1.0 - mipOffset) * (1.0 - (1.0 - roughness) / maxT);
+ float specPower = 2.0 / (alpha * alpha) - 2.0;
+
+ // We use the mip level calculation from Lys' default power drop, which in
+ // turn is a slight modification of that used in Marmoset Toolbag. See
+ // https://docs.knaldtech.com/doku.php?id=specular_lys for details.
+ // For now we assume a max specular power of 999999 which gives
+ // maxGlossiness = 1.
+ const float k0 = 0.00098;
+ const float k1 = 0.9921;
+ float glossiness = (pow(2.0, -10.0 / sqrt(specPower)) - k0) / k1;
+
+ // TODO: Optimize by doing this on CPU and set as
+ // uniform int envLight.specularMipLevels say (if present in shader).
+ // Lookup the number of mips in the specular envmap
+ int mipLevels = mipLevelCount(envLight.specular);
+
+ // Offset of smallest miplevel we should use (corresponds to specular
+ // power of 1). I.e. in the 32x32 sized mip.
+ const float mipOffset = 5.0;
+
+ // The final factor is really 1 - g / g_max but as mentioned above g_max
+ // is 1 by definition here so we can avoid the division. If we make the
+ // max specular power for the spec map configurable, this will need to
+ // be handled properly.
+ float mipLevel = (mipLevels - 1.0 - mipOffset) * (1.0 - glossiness);
+ return mipLevel;
}
-// Helper function to map from linear roughness value to non-linear alpha (shininess)
-float roughnessToAlpha(const in float roughness)
+float normalDistribution(const in vec3 n, const in vec3 h, const in float alpha)
{
- // Constants to control how to convert from roughness [0,1] to
- // shininess (alpha) [minAlpha, maxAlpha] using a power law with
- // a power of 1 / rho.
- const float minAlpha = 1.0;
- const float maxAlpha = 1024.0;
- const float rho = 3.0;
-
- return minAlpha + (maxAlpha - minAlpha) * (1.0 - pow(roughness, 1.0 / rho));
-}
-
-float normalDistribution(const in vec3 n, const in vec3 h, const in float roughness)
-{
- // Blinn-Phong approximation
- float alpha = roughnessToAlpha(roughness);
- return (alpha + 2.0) / (2.0 * 3.14159) * pow(max(dot(n, h), 0.0), alpha);
+ // Blinn-Phong approximation - see
+ // http://graphicrants.blogspot.co.uk/2013/08/specular-brdf-reference.html
+ float specPower = 2.0 / (alpha * alpha) - 2.0;
+ return (specPower + 2.0) / (2.0 * 3.14159) * pow(max(dot(n, h), 0.0), specPower);
}
vec3 fresnelFactor(const in vec3 color, const in float cosineFactor)
@@ -176,7 +200,7 @@ vec3 pbrModel(const in int lightIndex,
const in vec3 wView,
const in vec3 baseColor,
const in float metalness,
- const in float roughness,
+ const in float alpha,
const in float ambientOcclusion)
{
// Calculate some useful quantities
@@ -234,7 +258,7 @@ vec3 pbrModel(const in int lightIndex,
vec3 specularFactor = vec3(0.0);
if (sDotN > 0.0) {
specularFactor = specularModel(F0, sDotH, sDotN, vDotN, n, h);
- specularFactor *= normalDistribution(n, h, roughness);
+ specularFactor *= normalDistribution(n, h, alpha);
}
vec3 specularColor = lights[lightIndex].color;
vec3 specular = specularColor * specularFactor;
@@ -252,7 +276,7 @@ vec3 pbrIblModel(const in vec3 wNormal,
const in vec3 wView,
const in vec3 baseColor,
const in float metalness,
- const in float roughness,
+ const in float alpha,
const in float ambientOcclusion)
{
// Calculate reflection direction of view vector about surface normal
@@ -277,7 +301,26 @@ vec3 pbrIblModel(const in vec3 wNormal,
vec3 F0 = mix(dielectricColor, baseColor, metalness);
vec3 specularFactor = specularModel(F0, lDotH, lDotN, vDotN, n, h);
- float lod = roughnessToMipLevel(roughness);
+ float lod = alphaToMipLevel(alpha);
+//#define DEBUG_SPECULAR_LODS
+#ifdef DEBUG_SPECULAR_LODS
+ if (lod > 7.0)
+ return vec3(1.0, 0.0, 0.0);
+ else if (lod > 6.0)
+ return vec3(1.0, 0.333, 0.0);
+ else if (lod > 5.0)
+ return vec3(1.0, 1.0, 0.0);
+ else if (lod > 4.0)
+ return vec3(0.666, 1.0, 0.0);
+ else if (lod > 3.0)
+ return vec3(0.0, 1.0, 0.666);
+ else if (lod > 2.0)
+ return vec3(0.0, 0.666, 1.0);
+ else if (lod > 1.0)
+ return vec3(0.0, 0.0, 1.0);
+ else if (lod > 0.0)
+ return vec3(1.0, 0.0, 1.0);
+#endif
vec3 specularSkyColor = textureLod(envLight.specular, l, lod).rgb;
vec3 specular = specularSkyColor * specularFactor;
@@ -317,12 +360,15 @@ void main()
vec3 tNormal = 2.0 * texture(normalMap, texCoord).rgb - vec3(1.0);
vec3 wNormal = normalize(transpose(worldToTangentMatrix) * tNormal);
+ // Remap roughness for a perceptually more linear correspondence
+ float alpha = remapRoughness(roughness);
+
for (int i = 0; i < envLightCount; ++i) {
cLinear += pbrIblModel(wNormal,
wView,
baseColor,
metalness,
- roughness,
+ alpha,
ambientOcclusion);
}
@@ -333,7 +379,7 @@ void main()
wView,
baseColor.rgb,
metalness,
- roughness,
+ alpha,
ambientOcclusion);
}
diff --git a/src/extras/shaders/gl3/metalroughuniform.frag b/src/extras/shaders/gl3/metalroughuniform.frag
index cccd31b52..f4bad0a00 100644
--- a/src/extras/shaders/gl3/metalroughuniform.frag
+++ b/src/extras/shaders/gl3/metalroughuniform.frag
@@ -66,11 +66,6 @@ uniform vec4 baseColor;
uniform float metalness;
uniform float roughness;
-// Roughness -> mip level mapping
-uniform float maxT = 0.939824;
-uniform float mipLevels = 11.0;
-uniform float mipOffset = 5.0;
-
// Exposure correction
uniform float exposure = 0.0;
// Gamma correction
@@ -78,6 +73,25 @@ uniform float gamma = 2.2;
#pragma include light.inc.frag
+int mipLevelCount(const in samplerCube cube)
+{
+ int baseSize = textureSize(cube, 0).x;
+ int nMips = int(log2(float(baseSize>0 ? baseSize : 1))) + 1;
+ return nMips;
+}
+
+float remapRoughness(const in float roughness)
+{
+ // As per page 14 of
+ // http://www.frostbite.com/wp-content/uploads/2014/11/course_notes_moving_frostbite_to_pbr.pdf
+ // we remap the roughness to give a more perceptually linear response
+ // of "bluriness" as a function of the roughness specified by the user.
+ // r = roughness^2
+ const float maxSpecPower = 999999.0;
+ const float minRoughness = sqrt(2.0 / (maxSpecPower + 2));
+ return max(roughness * roughness, minRoughness);
+}
+
mat3 calcWorldSpaceToTangentSpaceMatrix(const in vec3 wNormal, const in vec4 wTangent)
{
// Make the tangent truly orthogonal to the normal by using Gram-Schmidt.
@@ -98,32 +112,42 @@ mat3 calcWorldSpaceToTangentSpaceMatrix(const in vec3 wNormal, const in vec4 wTa
return worldToTangentMatrix;
}
-float roughnessToMipLevel(float roughness)
-{
- // HACK: Improve the roughness -> mip level mapping for roughness map from substace painter
- // TODO: Use mathematica or similar to improve this mapping more generally
- roughness = 0.75 + (1.7 * (roughness - 0.5));
- return (mipLevels - 1.0 - mipOffset) * (1.0 - (1.0 - roughness) / maxT);
-}
-
-// Helper function to map from linear roughness value to non-linear alpha (shininess)
-float roughnessToAlpha(const in float roughness)
+float alphaToMipLevel(float alpha)
{
- // Constants to control how to convert from roughness [0,1] to
- // shininess (alpha) [minAlpha, maxAlpha] using a power law with
- // a power of 1 / rho.
- const float minAlpha = 1.0;
- const float maxAlpha = 1024.0;
- const float rho = 3.0;
-
- return minAlpha + (maxAlpha - minAlpha) * (1.0 - pow(roughness, 1.0 / rho));
+ float specPower = 2.0 / (alpha * alpha) - 2.0;
+
+ // We use the mip level calculation from Lys' default power drop, which in
+ // turn is a slight modification of that used in Marmoset Toolbag. See
+ // https://docs.knaldtech.com/doku.php?id=specular_lys for details.
+ // For now we assume a max specular power of 999999 which gives
+ // maxGlossiness = 1.
+ const float k0 = 0.00098;
+ const float k1 = 0.9921;
+ float glossiness = (pow(2.0, -10.0 / sqrt(specPower)) - k0) / k1;
+
+ // TODO: Optimize by doing this on CPU and set as
+ // uniform int envLight.specularMipLevels say (if present in shader).
+ // Lookup the number of mips in the specular envmap
+ int mipLevels = mipLevelCount(envLight.specular);
+
+ // Offset of smallest miplevel we should use (corresponds to specular
+ // power of 1). I.e. in the 32x32 sized mip.
+ const float mipOffset = 5.0;
+
+ // The final factor is really 1 - g / g_max but as mentioned above g_max
+ // is 1 by definition here so we can avoid the division. If we make the
+ // max specular power for the spec map configurable, this will need to
+ // be handled properly.
+ float mipLevel = (mipLevels - 1.0 - mipOffset) * (1.0 - glossiness);
+ return mipLevel;
}
-float normalDistribution(const in vec3 n, const in vec3 h, const in float roughness)
+float normalDistribution(const in vec3 n, const in vec3 h, const in float alpha)
{
- // Blinn-Phong approximation
- float alpha = roughnessToAlpha(roughness);
- return (alpha + 2.0) / (2.0 * 3.14159) * pow(max(dot(n, h), 0.0), alpha);
+ // Blinn-Phong approximation - see
+ // http://graphicrants.blogspot.co.uk/2013/08/specular-brdf-reference.html
+ float specPower = 2.0 / (alpha * alpha) - 2.0;
+ return (specPower + 2.0) / (2.0 * 3.14159) * pow(max(dot(n, h), 0.0), specPower);
}
vec3 fresnelFactor(const in vec3 color, const in float cosineFactor)
@@ -171,7 +195,7 @@ vec3 pbrModel(const in int lightIndex,
const in vec3 wView,
const in vec3 baseColor,
const in float metalness,
- const in float roughness)
+ const in float alpha)
{
// Calculate some useful quantities
vec3 n = wNormal;
@@ -228,7 +252,7 @@ vec3 pbrModel(const in int lightIndex,
vec3 specularFactor = vec3(0.0);
if (sDotN > 0.0) {
specularFactor = specularModel(F0, sDotH, sDotN, vDotN, n, h);
- specularFactor *= normalDistribution(n, h, roughness);
+ specularFactor *= normalDistribution(n, h, alpha);
}
vec3 specularColor = lights[lightIndex].color;
vec3 specular = specularColor * specularFactor;
@@ -241,7 +265,7 @@ vec3 pbrIblModel(const in vec3 wNormal,
const in vec3 wView,
const in vec3 baseColor,
const in float metalness,
- const in float roughness)
+ const in float alpha)
{
// Calculate reflection direction of view vector about surface normal
// vector in world space. This is used in the fragment shader to sample
@@ -265,7 +289,31 @@ vec3 pbrIblModel(const in vec3 wNormal,
vec3 F0 = mix(dielectricColor, baseColor, metalness);
vec3 specularFactor = specularModel(F0, lDotH, lDotN, vDotN, n, h);
- float lod = roughnessToMipLevel(roughness);
+ // As per page 14 of
+ // http://www.frostbite.com/wp-content/uploads/2014/11/course_notes_moving_frostbite_to_pbr.pdf
+ // we remap the roughness to give a more perceptually linear response
+ // of "bluriness" as a function of the roughness specified by the user.
+ // r = roughness^2
+ float lod = alphaToMipLevel(alpha);
+//#define DEBUG_SPECULAR_LODS
+#ifdef DEBUG_SPECULAR_LODS
+ if (lod > 7.0)
+ return vec3(1.0, 0.0, 0.0);
+ else if (lod > 6.0)
+ return vec3(1.0, 0.333, 0.0);
+ else if (lod > 5.0)
+ return vec3(1.0, 1.0, 0.0);
+ else if (lod > 4.0)
+ return vec3(0.666, 1.0, 0.0);
+ else if (lod > 3.0)
+ return vec3(0.0, 1.0, 0.666);
+ else if (lod > 2.0)
+ return vec3(0.0, 0.666, 1.0);
+ else if (lod > 1.0)
+ return vec3(0.0, 0.0, 1.0);
+ else if (lod > 0.0)
+ return vec3(1.0, 0.0, 1.0);
+#endif
vec3 specularSkyColor = textureLod(envLight.specular, l, lod).rgb;
vec3 specular = specularSkyColor * specularFactor;
@@ -287,23 +335,28 @@ void main()
{
vec3 cLinear = vec3(0.0);
+ // Remap roughness for a perceptually more linear correspondence
+ float alpha = remapRoughness(roughness);
+
+
+ vec3 wNormal = normalize(worldNormal);
vec3 worldView = normalize(eyePosition - worldPosition);
for (int i = 0; i < envLightCount; ++i) {
- cLinear += pbrIblModel(worldNormal,
+ cLinear += pbrIblModel(wNormal,
worldView,
baseColor.rgb,
metalness,
- roughness);
+ alpha);
}
for (int i = 0; i < lightCount; ++i) {
cLinear += pbrModel(i,
worldPosition,
- worldNormal,
+ wNormal,
worldView,
baseColor.rgb,
metalness,
- roughness);
+ alpha);
}
// Apply exposure correction