0

Hi I try to create a decal shader with the forward rendering technique in OpenGl ES. So I need an object to render to (which is a box most of the time). Then I have the Decal texture and the Scenes Depth Texture. The agk_XXXX uniforms are the current camera matrices and hopefully self explanatory. I'm not to sure if I need to somehow unproject the position into model space again and how I would do this. I'm not to sure if my approach is right at all ?! Currently its working somehow but only as if the decal object is the camera... This is my Vertex shader:

attribute vec3 position;
attribute vec3 normal;
attribute vec2 uv;

varying vec2 uvVarying;
varying highp vec3 posVarying;
varying vec3 normalVarying;
varying highp vec4 clipSpace;

uniform vec4 uvBounds0;
uniform mat4 agk_World;
uniform mat4 agk_View;
uniform mat4 agk_Proj;
uniform mat4 agk_ViewProj;
uniform mat3 agk_WorldNormal;
uniform mat4 agk_WorldViewProj;

void main()
{
    uvVarying = uv * uvBounds0.xy + uvBounds0.zw;
    vec4 pos = agk_World * vec4(position,1);
    posVarying = pos.xyz;
    clipSpace = agk_ViewProj * pos;
    gl_Position = agk_WorldViewProj * vec4(position,1);
    vec3 norm = agk_WorldNormal * normal;
    normalVarying = norm;
}

This is my Fragment Shader:

uniform sampler2D texture0; //decal texture
uniform highp sampler2D texture1; //scene depth

varying mediump vec2 uvVarying;
varying highp vec3 posVarying;
varying mediump vec3 normalVarying;
varying highp vec4 clipSpace;

uniform mediump vec2 cameraRange;
uniform mediump vec2 agk_resolution;
uniform lowp float agk_invert;
uniform mediump vec3 agk_CameraPos;
uniform mediump mat4 agk_World;
uniform mediump mat4 agk_View;
uniform mediump mat4 agk_Proj;

mat4 inverse(mat4 m);

void main()
{
    highp vec2 ndc = (clipSpace.xy/clipSpace.w) * 0.5 + 0.5;
    ndc = vec2(ndc.x, mix(ndc.y,1.0-ndc.y, agk_invert * 0.5 + 0.5));
    highp float worldDepth = texture2D(texture1, ndc ).r;

    vec2 deproject = vec2(agk_Proj[1][1],agk_Proj[2][2]);
    vec4 position = vec4(clipSpace.xy * worldDepth / (deproject * clipSpace.w), -worldDepth,1.0);
    position *= inverse(agk_World * agk_View);

    vec2 textureCoordinate = position.xz + 0.5;
    gl_FragColor = texture2D(texture0,textureCoordinate);
}

mat4 inverse(mat4 m)
{
  float
      a00 = m[0][0], a01 = m[0][1], a02 = m[0][2], a03 = m[0][3],
      a10 = m[1][0], a11 = m[1][1], a12 = m[1][2], a13 = m[1][3],
      a20 = m[2][0], a21 = m[2][1], a22 = m[2][2], a23 = m[2][3],
      a30 = m[3][0], a31 = m[3][1], a32 = m[3][2], a33 = m[3][3],

      b00 = a00 * a11 - a01 * a10,
      b01 = a00 * a12 - a02 * a10,
      b02 = a00 * a13 - a03 * a10,
      b03 = a01 * a12 - a02 * a11,
      b04 = a01 * a13 - a03 * a11,
      b05 = a02 * a13 - a03 * a12,
      b06 = a20 * a31 - a21 * a30,
      b07 = a20 * a32 - a22 * a30,
      b08 = a20 * a33 - a23 * a30,
      b09 = a21 * a32 - a22 * a31,
      b10 = a21 * a33 - a23 * a31,
      b11 = a22 * a33 - a23 * a32,

      det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;

  return mat4(
      a11 * b11 - a12 * b10 + a13 * b09,
      a02 * b10 - a01 * b11 - a03 * b09,
      a31 * b05 - a32 * b04 + a33 * b03,
      a22 * b04 - a21 * b05 - a23 * b03,
      a12 * b08 - a10 * b11 - a13 * b07,
      a00 * b11 - a02 * b08 + a03 * b07,
      a32 * b02 - a30 * b05 - a33 * b01,
      a20 * b05 - a22 * b02 + a23 * b01,
      a10 * b10 - a11 * b08 + a13 * b06,
      a01 * b08 - a00 * b10 - a03 * b06,
      a30 * b04 - a31 * b02 + a33 * b00,
      a21 * b02 - a20 * b04 - a23 * b00,
      a11 * b07 - a10 * b09 - a12 * b06,
      a00 * b09 - a01 * b07 + a02 * b06,
      a31 * b01 - a30 * b03 - a32 * b00,
      a20 * b03 - a21 * b01 + a22 * b00) / det;
}

1 Answers1

0

You don't have to this by hand - the textureProj() sampler functions in ESSL will do the computation for you.

solidpixel
  • 10,688
  • 1
  • 20
  • 33
  • What Matrices do I need from what perspective ? for now they are the default camera matrices so the decal follows the camera. Maybe you can post pseudo-code ? – Jan Bögemann Dec 03 '17 at 19:09