Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions atlas/application/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1703,6 +1703,10 @@ bool Window::stepFrame() {
model->material.useNormalMap;
meshObject->material.normalMapStrength =
model->material.normalMapStrength;
meshObject->material.textureScale =
model->material.textureScale;
meshObject->material.textureOffset =
model->material.textureOffset;
meshObject->useDeferredRendering =
model->useDeferredRendering;
if (meshObject->canUseDeferredRendering()) {
Expand Down
4 changes: 4 additions & 0 deletions atlas/graphics/deferred.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,10 @@ void Window::deferredRendering(
model->material.useNormalMap;
meshRenderable->material.normalMapStrength =
model->material.normalMapStrength;
meshRenderable->material.textureScale =
model->material.textureScale;
meshRenderable->material.textureOffset =
model->material.textureOffset;
meshRenderable->useDeferredRendering =
model->useDeferredRendering;
if (!meshRenderable->canUseDeferredRendering()) {
Expand Down
4 changes: 4 additions & 0 deletions atlas/object/core_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,10 @@ void CoreObject::render(float dt,
material.normalMapStrength);
this->pipeline->setUniform1i("useNormalMap",
material.useNormalMap ? 1 : 0);
this->pipeline->setUniform2f("textureScale", material.textureScale[0],
material.textureScale[1]);
this->pipeline->setUniform2f("textureOffset", material.textureOffset[0],
material.textureOffset[1]);
if (Window::mainWindow != nullptr &&
Window::mainWindow->getCamera() != nullptr) {
this->pipeline->setUniform3f(
Expand Down
216 changes: 133 additions & 83 deletions editor/views/editor/materialEditor.cpp

Large diffs are not rendered by default.

136 changes: 38 additions & 98 deletions include/atlas/core/default_shaders.h

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions include/atlas/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ struct Material {
* @brief Whether normal map sampling is enabled for this material.
*/
bool useNormalMap = true;
TextureCoordinate textureScale = {1.0f, 1.0f};
TextureCoordinate textureOffset = {0.0f, 0.0f};
/**
* @brief Fraction of transmitted light for translucent materials.
*/
Expand Down Expand Up @@ -883,6 +885,8 @@ class Model : public GameObject {
}
obj->material.useNormalMap = material.useNormalMap;
obj->material.normalMapStrength = material.normalMapStrength;
obj->material.textureScale = material.textureScale;
obj->material.textureOffset = material.textureOffset;
obj->useDeferredRendering = useDeferredRendering;
obj->render(dt, commandBuffer, updatePipeline);
}
Expand All @@ -903,6 +907,8 @@ class Model : public GameObject {
}
obj->material.useNormalMap = material.useNormalMap;
obj->material.normalMapStrength = material.normalMapStrength;
obj->material.textureScale = material.textureScale;
obj->material.textureOffset = material.textureOffset;
obj->useDeferredRendering = useDeferredRendering;
obj->update(window);
}
Expand All @@ -926,6 +932,8 @@ class Model : public GameObject {
}
obj->material.useNormalMap = material.useNormalMap;
obj->material.normalMapStrength = material.normalMapStrength;
obj->material.textureScale = material.textureScale;
obj->material.textureOffset = material.textureOffset;
obj->useDeferredRendering = useDeferredRendering;
obj->initialize();
}
Expand Down
4 changes: 4 additions & 0 deletions include/editor/views/materialEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ class MaterialEditorPanel : public QWidget {
QDoubleSpinBox *reflectivityField = nullptr;
QDoubleSpinBox *emissiveIntensityField = nullptr;
QDoubleSpinBox *normalStrengthField = nullptr;
QDoubleSpinBox *textureScaleUField = nullptr;
QDoubleSpinBox *textureScaleVField = nullptr;
QDoubleSpinBox *textureOffsetUField = nullptr;
QDoubleSpinBox *textureOffsetVField = nullptr;
QDoubleSpinBox *transmittanceField = nullptr;
QDoubleSpinBox *iorField = nullptr;
QCheckBox *normalMapField = nullptr;
Expand Down
20 changes: 20 additions & 0 deletions runtime/lib/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,10 @@ TextureType parseTextureTypeString(const std::string &value) {
if (token == "normal" || token == "normalmap") {
return TextureType::Normal;
}
if (token == "parallax" || token == "displacement" ||
token == "height") {
return TextureType::Parallax;
}
if (token == "metallic" || token == "metalness") {
return TextureType::Metallic;
}
Expand Down Expand Up @@ -739,6 +743,19 @@ MaterialDefinition loadMaterialDefinition(const json &value,
loaded.material.normalMapStrength);
tryReadBoolAny(materialData, {"useNormalMap"},
loaded.material.useNormalMap);
if (const json *scale = findField(materialData, {"textureScale", "uvScale"});
scale != nullptr && scale->is_array() && scale->size() >= 2) {
loaded.material.textureScale = {
static_cast<float>((*scale)[0].get<double>()),
static_cast<float>((*scale)[1].get<double>())};
}
if (const json *offset =
findField(materialData, {"textureOffset", "uvOffset"});
offset != nullptr && offset->is_array() && offset->size() >= 2) {
loaded.material.textureOffset = {
static_cast<float>((*offset)[0].get<double>()),
static_cast<float>((*offset)[1].get<double>())};
}
tryReadFloatAny(materialData, {"transmittance"},
loaded.material.transmittance);
tryReadFloatAny(materialData, {"ior"}, loaded.material.ior);
Expand All @@ -758,6 +775,9 @@ MaterialDefinition loadMaterialDefinition(const json &value,
appendTexture({"specularTexture", "specularMap"}, TextureType::Specular,
false);
appendTexture({"normalTexture", "normalMap"}, TextureType::Normal, false);
appendTexture({"displacementTexture", "displacementMap", "heightTexture",
"heightMap"},
TextureType::Parallax, false);
appendTexture({"metallicTexture", "metalnessTexture"},
TextureType::Metallic, false);
appendTexture({"roughnessTexture"}, TextureType::Roughness, false);
Expand Down
46 changes: 8 additions & 38 deletions shaders/metal/deferred/deferred.frag.metal
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ struct UBO
float3 cameraPosition;
float normalMapStrength;
uint useNormalMap;
float2 textureScale;
float2 textureOffset;
};

struct MaterialPush
Expand Down Expand Up @@ -141,25 +143,25 @@ float2 parallaxMapping(thread const float2& texCoords, thread const float3& view
}
int param = textureIndex;
float2 param_1 = currentTexCoords;
float currentDepthMapValue = sampleTextureAt(param, param_1, texture1, texture1Smplr, texture2, texture2Smplr, texture3, texture3Smplr, texture4, texture4Smplr, texture5, texture5Smplr, texture6, texture6Smplr, texture7, texture7Smplr, texture8, texture8Smplr, texture9, texture9Smplr, texture10, texture10Smplr).x;
float currentDepthMapValue = 1.0 - sampleTextureAt(param, param_1, texture1, texture1Smplr, texture2, texture2Smplr, texture3, texture3Smplr, texture4, texture4Smplr, texture5, texture5Smplr, texture6, texture6Smplr, texture7, texture7Smplr, texture8, texture8Smplr, texture9, texture9Smplr, texture10, texture10Smplr).x;
int maxIterations = int(numLayers) + 1;
int iteration = 0;
while (currentLayerDepth < currentDepthMapValue && iteration < maxIterations)
{
currentTexCoords -= deltaTexCoords;
int param_2 = textureIndex;
float2 param_3 = currentTexCoords;
currentDepthMapValue = sampleTextureAt(param_2, param_3, texture1, texture1Smplr, texture2, texture2Smplr, texture3, texture3Smplr, texture4, texture4Smplr, texture5, texture5Smplr, texture6, texture6Smplr, texture7, texture7Smplr, texture8, texture8Smplr, texture9, texture9Smplr, texture10, texture10Smplr).x;
currentDepthMapValue = 1.0 - sampleTextureAt(param_2, param_3, texture1, texture1Smplr, texture2, texture2Smplr, texture3, texture3Smplr, texture4, texture4Smplr, texture5, texture5Smplr, texture6, texture6Smplr, texture7, texture7Smplr, texture8, texture8Smplr, texture9, texture9Smplr, texture10, texture10Smplr).x;
currentLayerDepth += layerDepth;
iteration++;
}
float2 prevTexCoords = currentTexCoords + deltaTexCoords;
float afterDepth = currentDepthMapValue - currentLayerDepth;
int param_4 = textureIndex;
float2 param_5 = prevTexCoords;
float beforeDepth = sampleTextureAt(param_4, param_5, texture1, texture1Smplr, texture2, texture2Smplr, texture3, texture3Smplr, texture4, texture4Smplr, texture5, texture5Smplr, texture6, texture6Smplr, texture7, texture7Smplr, texture8, texture8Smplr, texture9, texture9Smplr, texture10, texture10Smplr).x - (currentLayerDepth - layerDepth);
float denom = fast::max(afterDepth - beforeDepth, 9.9999997473787516355514526367188e-05);
float weight = fast::clamp(afterDepth / denom, 0.0, 1.0);
float beforeDepth = 1.0 - sampleTextureAt(param_4, param_5, texture1, texture1Smplr, texture2, texture2Smplr, texture3, texture3Smplr, texture4, texture4Smplr, texture5, texture5Smplr, texture6, texture6Smplr, texture7, texture7Smplr, texture8, texture8Smplr, texture9, texture9Smplr, texture10, texture10Smplr).x - (currentLayerDepth - layerDepth);
float denom = afterDepth - beforeDepth;
float weight = abs(denom) > 9.9999997473787516355514526367188e-05 ? fast::clamp(afterDepth / denom, 0.0, 1.0) : 0.0;
currentTexCoords = (prevTexCoords * weight) + (currentTexCoords * (1.0 - weight));
return currentTexCoords;
}
Expand Down Expand Up @@ -261,7 +263,7 @@ fragment main0_out main0(main0_in in [[stage_in]], constant UBO& _46 [[buffer(0)
TBN[0] = in.TBN_0;
TBN[1] = in.TBN_1;
TBN[2] = in.TBN_2;
float2 texCoord = in.TexCoord;
float2 texCoord = in.TexCoord * _46.textureScale + _46.textureOffset;
bool hasParallaxMap = false;
for (int i = 0; i < _46.textureCount; i++)
{
Expand All @@ -277,38 +279,6 @@ fragment main0_out main0(main0_in in [[stage_in]], constant UBO& _46 [[buffer(0)
float2 param = texCoord;
float3 param_1 = tangentViewDir;
texCoord = parallaxMapping(param, param_1, _46, texture1, texture1Smplr, texture2, texture2Smplr, texture3, texture3Smplr, texture4, texture4Smplr, texture5, texture5Smplr, texture6, texture6Smplr, texture7, texture7Smplr, texture8, texture8Smplr, texture9, texture9Smplr, texture10, texture10Smplr);
bool _476 = texCoord.x > 1.0;
bool _483;
if (!_476)
{
_483 = texCoord.y > 1.0;
}
else
{
_483 = _476;
}
bool _490;
if (!_483)
{
_490 = texCoord.x < 0.0;
}
else
{
_490 = _483;
}
bool _497;
if (!_490)
{
_497 = texCoord.y < 0.0;
}
else
{
_497 = _490;
}
if (_497)
{
discard_fragment();
}
}
int param_2 = 0;
float4 sampledColor = enableTextures(param_2, _46, texture1, texture1Smplr, texCoord, texture2, texture2Smplr, texture3, texture3Smplr, texture4, texture4Smplr, texture5, texture5Smplr, texture6, texture6Smplr, texture7, texture7Smplr, texture8, texture8Smplr, texture9, texture9Smplr, texture10, texture10Smplr);
Expand Down
46 changes: 8 additions & 38 deletions shaders/metal/main.frag.metal
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ struct Uniforms
float3 cameraPosition;
float normalMapStrength;
uint useNormalMap;
float2 textureScale;
float2 textureOffset;
};

struct Environment
Expand Down Expand Up @@ -357,23 +359,23 @@ float2 parallaxMapping(thread const float2& texCoords, thread const float3& view
}
int param = textureIndex;
float2 param_1 = currentTexCoords;
float currentDepthMapValue = sampleTextureAt(param, param_1, texture1, texture1Smplr, texture2, texture2Smplr, texture3, texture3Smplr, texture4, texture4Smplr, texture5, texture5Smplr, texture6, texture6Smplr, texture7, texture7Smplr, texture8, texture8Smplr, texture9, texture9Smplr, texture10, texture10Smplr).x;
float currentDepthMapValue = 1.0 - sampleTextureAt(param, param_1, texture1, texture1Smplr, texture2, texture2Smplr, texture3, texture3Smplr, texture4, texture4Smplr, texture5, texture5Smplr, texture6, texture6Smplr, texture7, texture7Smplr, texture8, texture8Smplr, texture9, texture9Smplr, texture10, texture10Smplr).x;
while (currentLayerDepth < currentDepthMapValue)
{
currentTexCoords = fast::clamp(currentTexCoords - deltaTexCoords, float2(0.0), float2(1.0));
currentTexCoords -= deltaTexCoords;
int param_2 = textureIndex;
float2 param_3 = currentTexCoords;
currentDepthMapValue = sampleTextureAt(param_2, param_3, texture1, texture1Smplr, texture2, texture2Smplr, texture3, texture3Smplr, texture4, texture4Smplr, texture5, texture5Smplr, texture6, texture6Smplr, texture7, texture7Smplr, texture8, texture8Smplr, texture9, texture9Smplr, texture10, texture10Smplr).x;
currentDepthMapValue = 1.0 - sampleTextureAt(param_2, param_3, texture1, texture1Smplr, texture2, texture2Smplr, texture3, texture3Smplr, texture4, texture4Smplr, texture5, texture5Smplr, texture6, texture6Smplr, texture7, texture7Smplr, texture8, texture8Smplr, texture9, texture9Smplr, texture10, texture10Smplr).x;
currentLayerDepth += layerDepth;
}
float2 prevTexCoords = currentTexCoords + deltaTexCoords;
float afterDepth = currentDepthMapValue - currentLayerDepth;
int param_4 = textureIndex;
float2 param_5 = prevTexCoords;
float beforeDepth = sampleTextureAt(param_4, param_5, texture1, texture1Smplr, texture2, texture2Smplr, texture3, texture3Smplr, texture4, texture4Smplr, texture5, texture5Smplr, texture6, texture6Smplr, texture7, texture7Smplr, texture8, texture8Smplr, texture9, texture9Smplr, texture10, texture10Smplr).x - (currentLayerDepth - layerDepth);
float beforeDepth = 1.0 - sampleTextureAt(param_4, param_5, texture1, texture1Smplr, texture2, texture2Smplr, texture3, texture3Smplr, texture4, texture4Smplr, texture5, texture5Smplr, texture6, texture6Smplr, texture7, texture7Smplr, texture8, texture8Smplr, texture9, texture9Smplr, texture10, texture10Smplr).x - (currentLayerDepth - layerDepth);
float weight = afterDepth / (afterDepth - beforeDepth);
currentTexCoords = (prevTexCoords * weight) + (currentTexCoords * (1.0 - weight));
return fast::clamp(currentTexCoords, float2(0.0), float2(1.0));
return currentTexCoords;
}

static inline __attribute__((always_inline))
Expand Down Expand Up @@ -923,7 +925,7 @@ fragment main0_out main0(main0_in in [[stage_in]], constant Uniforms& _163 [[buf
TBN[0] = in.TBN_0;
TBN[1] = in.TBN_1;
TBN[2] = in.TBN_2;
float2 texCoord = in.TexCoord;
float2 texCoord = in.TexCoord * _163.textureScale + _163.textureOffset;
bool hasParallaxMap = false;
for (int i = 0; i < _163.textureCount; i++)
{
Expand All @@ -939,38 +941,6 @@ fragment main0_out main0(main0_in in [[stage_in]], constant Uniforms& _163 [[buf
float2 param = texCoord;
float3 param_1 = tangentViewDir;
texCoord = parallaxMapping(param, param_1, _163, texture1, texture1Smplr, texture2, texture2Smplr, texture3, texture3Smplr, texture4, texture4Smplr, texture5, texture5Smplr, texture6, texture6Smplr, texture7, texture7Smplr, texture8, texture8Smplr, texture9, texture9Smplr, texture10, texture10Smplr);
bool _1744 = texCoord.x > 1.0;
bool _1751;
if (!_1744)
{
_1751 = texCoord.y > 1.0;
}
else
{
_1751 = _1744;
}
bool _1758;
if (!_1751)
{
_1758 = texCoord.x < 0.0;
}
else
{
_1758 = _1751;
}
bool _1765;
if (!_1758)
{
_1765 = texCoord.y < 0.0;
}
else
{
_1765 = _1758;
}
if (_1765)
{
discard_fragment();
}
}
int param_2 = 5;
float4 normTexture = enableTextures(param_2, _163, texture1, texture1Smplr, texCoord, texture2, texture2Smplr, texture3, texture3Smplr, texture4, texture4Smplr, texture5, texture5Smplr, texture6, texture6Smplr, texture7, texture7Smplr, texture8, texture8Smplr, texture9, texture9Smplr, texture10, texture10Smplr);
Expand Down
21 changes: 11 additions & 10 deletions shaders/opengl/deferred/deferred.frag
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ uniform int textureTypes[16];
uniform int textureCount;
uniform Material material;
uniform vec3 cameraPosition;
uniform vec2 textureScale;
uniform vec2 textureOffset;
uniform bool useTexture;
uniform bool useColor;

Expand Down Expand Up @@ -106,33 +108,32 @@ vec2 parallaxMapping(vec2 texCoords, vec3 viewDir) {
}
if (textureIndex == -1) return currentTexCoords;

float currentDepthMapValue = sampleTextureAt(textureIndex, currentTexCoords).r;
float currentDepthMapValue = 1.0 - sampleTextureAt(textureIndex, currentTexCoords).r;

while (currentLayerDepth < currentDepthMapValue) {
currentTexCoords -= deltaTexCoords;
currentDepthMapValue = sampleTextureAt(textureIndex, currentTexCoords).r;
currentDepthMapValue = 1.0 - sampleTextureAt(textureIndex, currentTexCoords).r;
currentLayerDepth += layerDepth;
}

vec2 prevTexCoords = currentTexCoords + deltaTexCoords;
float afterDepth = currentDepthMapValue - currentLayerDepth;
float beforeDepth = sampleTextureAt(textureIndex, prevTexCoords).r - (currentLayerDepth - layerDepth);
float denom = max(afterDepth - beforeDepth, 1e-4);
float weight = clamp(afterDepth / denom, 0.0, 1.0);
float beforeDepth = 1.0 - sampleTextureAt(textureIndex, prevTexCoords).r - (currentLayerDepth - layerDepth);
float denom = afterDepth - beforeDepth;
float weight = abs(denom) > 1e-4
? clamp(afterDepth / denom, 0.0, 1.0)
: 0.0;
currentTexCoords = prevTexCoords * weight + currentTexCoords * (1.0 - weight);

return currentTexCoords;
}

void main() {
texCoord = TexCoord;
texCoord = TexCoord * textureScale + textureOffset;

vec3 tangentViewDir = normalize(transpose(TBN) * (cameraPosition - FragPos));
texCoord = parallaxMapping(texCoord, tangentViewDir);

if (texCoord.x > 1.0 || texCoord.y > 1.0 || texCoord.x < 0.0 || texCoord.y < 0.0)
discard;

vec4 sampledColor = enableTextures(TEXTURE_COLOR);
bool hasColorTexture = sampledColor != vec4(-1.0);

Expand All @@ -146,7 +147,7 @@ void main() {
if (opacityTex.r < 0.1) {
discard;
}
} else if (material.albedo.a < 0.999 && baseColor.a < 0.1) {
} else if (baseColor.a < 0.1) {
discard;
}

Expand Down
Loading
Loading