Skip to content

Commit

Permalink
Material.textureTransformation
Browse files Browse the repository at this point in the history
  • Loading branch information
gecko0307 committed Jan 30, 2025
1 parent d52c3ed commit d2df71a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
5 changes: 2 additions & 3 deletions data/__internal/shaders/SimpleForward/SimpleForward.vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ layout (location = 2) in vec2 va_Texcoord;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
uniform mat4 normalMatrix;

uniform vec2 textureScale;
uniform mat3 textureMatrix;

out vec3 eyeNormal;
out vec3 eyePosition;
Expand All @@ -22,7 +21,7 @@ void main()
vec4 modelNormal = vec4(va_Normal, 0.0);
eyeNormal = (normalMatrix * modelNormal).xyz;

texCoord = va_Texcoord * textureScale;
texCoord = (textureMatrix * vec3(va_Texcoord, 1.0)).xy;

vec4 currPosition = projectionMatrix * pos;

Expand Down
33 changes: 31 additions & 2 deletions src/dagon/graphics/material.d
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2017-2022 Timur Gafarov
Copyright (c) 2017-2025 Timur Gafarov
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
Expand Down Expand Up @@ -34,6 +34,7 @@ import std.algorithm;
import dlib.core.memory;
import dlib.core.ownership;
import dlib.math.vector;
import dlib.math.matrix;
import dlib.image.color;
import dlib.image.image;
import dlib.image.unmanaged;
Expand Down Expand Up @@ -85,7 +86,7 @@ class Material: Owner
Color4f baseColorFactor = Color4f(1.0f, 1.0f, 1.0f, 1.0f);
Color4f emissionFactor = Color4f(0.0f, 0.0f, 0.0f, 1.0f);
Vector3f normalFactor = Vector3f(0.0f, 0.0f, 1.0f);
Vector2f textureScale = Vector2f(1.0f, 1.0f); // TODO: textureTransformation matrix instead
Matrix3x3f textureTransformation;
float heightFactor = 0.0f;
float emissionEnergy = 1.0f;
float opacity = 1.0f;
Expand Down Expand Up @@ -113,9 +114,13 @@ class Material: Owner
bool outputPBR = true;
bool outputEmission = true;

protected Vector2f textureOffsetInternal = Vector2f(0.0f, 0.0f);
protected Vector2f textureScalingInternal = Vector2f(1.0f, 1.0f);

this(Owner o)
{
super(o);
textureTransformation = Matrix3x3f.identity;
}

~this()
Expand All @@ -127,6 +132,30 @@ class Material: Owner
return (blendMode != Opaque);
}

Vector2f textureOffset()
{
return textureOffsetInternal;
}

void textureOffset(Vector2f v)
{
textureOffsetInternal = v;
textureTransformation.a13 = v.x;
textureTransformation.a23 = v.y;
}

Vector2f textureScale()
{
return textureScalingInternal;
}

void textureScale(Vector2f s)
{
textureScalingInternal = s;
textureTransformation.a11 = s.x;
textureTransformation.a22 = s.y;
}

void bind(GraphicsState* state)
{
if (blendMode == Transparent)
Expand Down
9 changes: 7 additions & 2 deletions src/dagon/graphics/shader.d
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2018-2022 Timur Gafarov
Copyright (c) 2018-2025 Timur Gafarov
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
Expand Down Expand Up @@ -193,7 +193,8 @@ if (is(T == bool) ||
is(T == Vector3f) ||
is(T == Vector4f) ||
is(T == Color4f) ||
is(T == Matrix4x4f))
is(T == Matrix4x4f) ||
is(T == Matrix3x3f))
{
T* source;
T value;
Expand Down Expand Up @@ -264,6 +265,10 @@ if (is(T == bool) ||
{
glUniformMatrix4fv(location, 1, GL_FALSE, value.arrayof.ptr);
}
else static if (is(T == Matrix3x3f))
{
glUniformMatrix3fv(location, 1, GL_FALSE, value.arrayof.ptr);
}
}

override void unbind()
Expand Down
2 changes: 1 addition & 1 deletion src/dagon/render/simple/shaders/simpleforward.d
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class SimpleForwardShader: Shader
setParameter("normalMatrix", state.normalMatrix);

setParameter("opacity", mat.opacity * state.opacity);
setParameter("textureScale", mat.textureScale);
setParameter("textureMatrix", mat.textureTransformation);

setParameter("alphaTestThreshold", mat.alphaTestThreshold);

Expand Down

0 comments on commit d2df71a

Please sign in to comment.