STKMesh: Add support for Displacing
This commit is contained in:
@@ -1,24 +1,19 @@
|
||||
#version 130
|
||||
uniform sampler2D tex;
|
||||
uniform vec2 screen;
|
||||
uniform vec2 dir;
|
||||
uniform vec2 dir2;
|
||||
|
||||
in vec2 uv;
|
||||
in vec2 edger_uv;
|
||||
in vec2 uv_bis;
|
||||
in float camdist;
|
||||
|
||||
out vec4 FragColor;
|
||||
const float maxlen = 0.02;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec2 tc = uv;
|
||||
|
||||
vec4 col = vec4(0.0);
|
||||
const float maxlen = 0.02;
|
||||
|
||||
float horiz = texture(tex, tc + dir).x;
|
||||
float vert = texture(tex, (tc.yx + dir2) * vec2(0.9)).x;
|
||||
float horiz = texture(tex, uv + dir).x;
|
||||
float vert = texture(tex, (uv.yx + dir2) * vec2(0.9)).x;
|
||||
|
||||
vec2 offset = vec2(horiz, vert);
|
||||
offset *= 2.0;
|
||||
@@ -28,14 +23,14 @@ void main()
|
||||
float fade = 1.0 - smoothstep(1.0, 100.0, camdist);
|
||||
|
||||
// Fade according to distance from the edges
|
||||
vec2 edger = edger_uv;
|
||||
const float mindist = 0.1;
|
||||
fade *= smoothstep(0.0, mindist, edger.x) * smoothstep(0.0, mindist, edger.y) *
|
||||
(1.0 - smoothstep(1.0 - mindist, 1.0, edger.x)) *
|
||||
(1.0 - smoothstep(1.0 - mindist, 1.0, edger.y));
|
||||
fade *= smoothstep(0.0, mindist, uv_bis.x) * smoothstep(0.0, mindist, uv_bis.y) *
|
||||
(1.0 - smoothstep(1.0 - mindist, 1.0, uv_bis.x)) *
|
||||
(1.0 - smoothstep(1.0 - mindist, 1.0, uv_bis.y));
|
||||
|
||||
offset *= 50.0 * fade * maxlen;
|
||||
|
||||
vec4 col;
|
||||
col.r = step(offset.x, 0.0) * -offset.x;
|
||||
col.g = step(0.0, offset.x) * offset.x;
|
||||
col.b = step(offset.y, 0.0) * -offset.y;
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
#version 130
|
||||
uniform mat4 ModelViewProjectionMatrix;
|
||||
uniform mat4 ModelViewMatrix;
|
||||
uniform mat4 ProjectionMatrix;
|
||||
|
||||
in vec3 Position;
|
||||
in vec2 Texcoord;
|
||||
in vec2 SecondTexcoord;
|
||||
out vec2 uv;
|
||||
out vec2 edger_uv;
|
||||
out vec2 uv_bis;
|
||||
out float camdist;
|
||||
|
||||
void main() {
|
||||
vec4 position = ModelViewMatrix * gl_Vertex;
|
||||
gl_Position = ProjectionMatrix * position;
|
||||
uv = gl_MultiTexCoord0.xy;
|
||||
edger_uv = gl_MultiTexCoord1.xy;
|
||||
|
||||
camdist = length(position.xyz);
|
||||
gl_Position = ModelViewProjectionMatrix * vec4(Position, 1.);
|
||||
uv = Texcoord;
|
||||
uv_bis = SecondTexcoord;
|
||||
camdist = length(ModelViewMatrix * vec4(Position, 1.));
|
||||
}
|
||||
|
||||
@@ -601,26 +601,29 @@ void DisplaceProvider::OnSetConstants(IMaterialRendererServices *srv, int)
|
||||
srv->setVertexShaderConstant("ProjectionMatrix", ProjectionMatrix.pointer(), 16);
|
||||
srv->setVertexShaderConstant("ModelViewMatrix", ModelViewMatrix.pointer(), 16);
|
||||
|
||||
const float time = irr_driver->getDevice()->getTimer()->getTime() / 1000.0f;
|
||||
const float speed = World::getWorld()->getTrack()->getDisplacementSpeed();
|
||||
|
||||
float strength = time;
|
||||
strength = fabsf(noise2d(strength / 10.0f)) * 0.006f + 0.002f;
|
||||
|
||||
vector3df wind = irr_driver->getWind() * strength * speed;
|
||||
m_dir[0] += wind.X;
|
||||
m_dir[1] += wind.Z;
|
||||
|
||||
strength = time * 0.56f + sinf(time);
|
||||
strength = fabsf(noise2d(0.0, strength / 6.0f)) * 0.0095f + 0.0025f;
|
||||
|
||||
wind = irr_driver->getWind() * strength * speed;
|
||||
wind.rotateXZBy(cosf(time));
|
||||
m_dir2[0] += wind.X;
|
||||
m_dir2[1] += wind.Z;
|
||||
|
||||
srv->setVertexShaderConstant("dir", m_dir, 2);
|
||||
srv->setVertexShaderConstant("dir2", m_dir2, 2);
|
||||
|
||||
srv->setVertexShaderConstant("screen", m_screen, 2);
|
||||
}
|
||||
|
||||
void DisplaceProvider::update()
|
||||
{
|
||||
const float time = irr_driver->getDevice()->getTimer()->getTime() / 1000.0f;
|
||||
const float speed = World::getWorld()->getTrack()->getDisplacementSpeed();
|
||||
|
||||
float strength = time;
|
||||
strength = fabsf(noise2d(strength / 10.0f)) * 0.006f + 0.002f;
|
||||
|
||||
vector3df wind = irr_driver->getWind() * strength * speed;
|
||||
m_dir[0] += wind.X;
|
||||
m_dir[1] += wind.Z;
|
||||
|
||||
strength = time * 0.56f + sinf(time);
|
||||
strength = fabsf(noise2d(0.0, strength / 6.0f)) * 0.0095f + 0.0025f;
|
||||
|
||||
wind = irr_driver->getWind() * strength * speed;
|
||||
wind.rotateXZBy(cosf(time));
|
||||
m_dir2[0] += wind.X;
|
||||
m_dir2[1] += wind.Z;
|
||||
}
|
||||
@@ -555,6 +555,28 @@ public:
|
||||
m_dir[0] = m_dir[1] = m_dir2[0] = m_dir2[1] = 0;
|
||||
}
|
||||
|
||||
void update();
|
||||
|
||||
float getDirX() const
|
||||
{
|
||||
return m_dir[0];
|
||||
}
|
||||
|
||||
float getDirY() const
|
||||
{
|
||||
return m_dir[1];
|
||||
}
|
||||
|
||||
float getDir2X() const
|
||||
{
|
||||
return m_dir2[0];
|
||||
}
|
||||
|
||||
float getDir2Y() const
|
||||
{
|
||||
return m_dir2[1];
|
||||
}
|
||||
|
||||
private:
|
||||
float m_screen[2];
|
||||
float m_dir[2], m_dir2[2];
|
||||
|
||||
@@ -248,6 +248,7 @@ void Shaders::loadShaders()
|
||||
MeshShader::GrassPass2Shader::init();
|
||||
MeshShader::BubbleShader::init();
|
||||
MeshShader::TransparentShader::init();
|
||||
MeshShader::DisplaceShader::init();
|
||||
}
|
||||
|
||||
Shaders::~Shaders()
|
||||
@@ -641,6 +642,38 @@ namespace MeshShader
|
||||
glUniformMatrix4fv(uniform_MVP, 1, GL_FALSE, ModelViewProjectionMatrix.pointer());
|
||||
glUniform3f(uniform_col, r, g, b);
|
||||
}
|
||||
|
||||
GLuint DisplaceShader::Program;
|
||||
GLuint DisplaceShader::attrib_position;
|
||||
GLuint DisplaceShader::attrib_texcoord;
|
||||
GLuint DisplaceShader::attrib_second_texcoord;
|
||||
GLuint DisplaceShader::uniform_MVP;
|
||||
GLuint DisplaceShader::uniform_MV;
|
||||
GLuint DisplaceShader::uniform_tex;
|
||||
GLuint DisplaceShader::uniform_dir;
|
||||
GLuint DisplaceShader::uniform_dir2;
|
||||
|
||||
void DisplaceShader::init()
|
||||
{
|
||||
Program = LoadProgram(file_manager->getAsset("shaders/displace.vert").c_str(), file_manager->getAsset("shaders/displace.frag").c_str());
|
||||
attrib_position = glGetAttribLocation(Program, "Position");
|
||||
attrib_texcoord = glGetAttribLocation(Program, "Texcoord");
|
||||
attrib_second_texcoord = glGetAttribLocation(Program, "SecondTexcoord");
|
||||
uniform_MVP = glGetUniformLocation(Program, "ModelViewProjectionMatrix");
|
||||
uniform_MV = glGetUniformLocation(Program, "ModelViewMatrix");
|
||||
uniform_tex = glGetUniformLocation(Program, "tex");
|
||||
uniform_dir = glGetUniformLocation(Program, "dir");
|
||||
uniform_dir2 = glGetUniformLocation(Program, "dir2");
|
||||
}
|
||||
|
||||
void DisplaceShader::setUniforms(const core::matrix4 &ModelViewProjectionMatrix, const core::matrix4 &ModelViewMatrix, float dirX, float dirY, float dir2X, float dir2Y, unsigned TU_tex)
|
||||
{
|
||||
glUniformMatrix4fv(uniform_MVP, 1, GL_FALSE, ModelViewProjectionMatrix.pointer());
|
||||
glUniformMatrix4fv(uniform_MV, 1, GL_FALSE, ModelViewMatrix.pointer());
|
||||
glUniform2f(uniform_dir, dirX, dirY);
|
||||
glUniform2f(uniform_dir2, dir2X, dir2Y);
|
||||
glUniform1i(uniform_tex, TU_tex);
|
||||
}
|
||||
}
|
||||
|
||||
static GLuint createVAO(GLuint Program)
|
||||
|
||||
@@ -159,6 +159,17 @@ public:
|
||||
static void setUniforms(const core::matrix4 &ModelViewProjectionMatrix, float r, float g, float b);
|
||||
};
|
||||
|
||||
class DisplaceShader
|
||||
{
|
||||
public:
|
||||
static GLuint Program;
|
||||
static GLuint attrib_position, attrib_texcoord, attrib_second_texcoord;
|
||||
static GLuint uniform_MVP, uniform_MV, uniform_tex, uniform_dir, uniform_dir2;
|
||||
|
||||
static void init();
|
||||
static void setUniforms(const core::matrix4 &ModelViewProjectionMatrix, const core::matrix4 &ModelViewMatrix, float dirX, float dirY, float dir2X, float dir2Y, unsigned TU_tex);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
namespace FullScreenShader
|
||||
|
||||
Reference in New Issue
Block a user