Thanks! Updated loops as it was in Outline.frag and now it works. I did not that before because it looked wrong but with unlit sprite now it's looks as should be.
Updated shader code:
#version 450
#extension GL_ARB_separate_shader_objects : enable
layout(binding = 0) uniform sampler2DMS DepthBuffer;
layout(binding = 1) uniform sampler2D ColorBuffer;
uniform int Thickness = 5;
#include "../Base/CameraInfo.glsl"
#include "../Utilities/Dither.glsl"
#include "../Utilities/DepthFunctions.glsl"
layout(location = 0) out vec4 outColor;
void main()
{
vec2 BufferSize = vec2(DrawViewport.z, DrawViewport.w);
ivec2 coord = ivec2(gl_FragCoord.x, gl_FragCoord.y);
outColor = vec4(0.0f);
int count = textureSamples(DepthBuffer);
const int m = max(0, (Thickness - 1) / 2);
float depth;
//Handle selected objects
for (int n = 0; n < count; ++n)
{
bool done = false;
depth = texelFetch(DepthBuffer, coord, n).r;
if (depth < 1.0f)
{
vec2 pixelsize = vec2(1.0f) / BufferSize;
for (int x = -m; x <= m; ++x)
{
for (int y = -m; y <= m; ++y)
{
if (x == 0 && y == 0) continue;
float neighbour = texelFetch(DepthBuffer, coord + ivec2(x, y), n).r;
if (neighbour == 1.0f)
{
outColor += texelFetch(ColorBuffer, coord, gl_SampleID);
done = true;
break;
}
}
if (done) break;
}
}
}
outColor /= float(count);
}
Updated example:
#include "UltraEngine.h"
#include "ComponentSystem.h"
using namespace UltraEngine;
int main(int argc, const char* argv[])
{
//Get the displays
auto displays = GetDisplays();
//Create a window
auto window = CreateWindow("Ultra Engine", 0, 0, 1200, 800, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR);
//Create a world
auto world = CreateWorld();
world->RecordStats();
//Create a framebuffer
auto framebuffer = CreateFramebuffer(window);
//Create a camera
auto camera = CreateCamera(world);
camera->SetClearColor(0.125);
camera->SetFov(50);
camera->SetPosition(0, 0, -3);
//Create a light
auto light = CreateDirectionalLight(world);
light->SetColor(1);
//Create a box
auto box = CreateBox(world);
box->SetColor(1, 0, 0);
auto outlineBox = CreateBox(world);
outlineBox->SetColor(0, 1, 0);
box->SetPosition(-1, 0, 0);
auto material = CreateMaterial();
auto unlitShader = LoadShaderFamily("Shaders/Unlit.fam");
material->SetShaderFamily(unlitShader);
outlineBox->SetMaterial(material);
outlineBox->SetPosition(box->GetPosition());
outlineBox->SetParent(box);
//Create a box2
auto box2 = CreateBox(world);
box2->SetColor(1, 0, 0);
auto outlineBox2 = CreateBox(world);
outlineBox2->SetColor(0, 0, 1);
auto material2 = CreateMaterial();
material2->SetShaderFamily(unlitShader);
outlineBox2->SetMaterial(material);
box2->SetPosition(1, 0, 0);
outlineBox2->SetPosition(1, 0, 0);
outlineBox2->SetParent(box2);
//Entity component system
auto component = box->AddComponent<Mover>();
component->rotationspeed.y = 45;
auto component2 = box2->AddComponent<Mover>();
component2->rotationspeed.y = 45;
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//Render to texture
outlineBox->SetRenderLayers(2);
outlineBox2->SetRenderLayers(2);
auto cam2 = CreateCamera(world);
cam2->SetClearColor(0, 0, 0, 0);
cam2->SetRenderLayers(2);
cam2->SetFov(camera->GetFov());
cam2->SetMatrix(camera->matrix);
cam2->AddPostEffect(LoadPostEffect("Shaders/MultipleOutlines.fx"));
cam2->SetLighting(false);
cam2->SetUniform(0, "Thickness", 10);
auto sz = framebuffer->GetSize();
auto texbuffer = CreateTextureBuffer(sz.x, sz.y);
auto pixels = CreatePixmap(sz.x, sz.y, TEXTURE_RGBA);
auto tex = CreateTexture(TEXTURE_2D, sz.x, sz.y, pixels->format, { pixels }, 1, TEXTURE_DEFAULT, TEXTUREFILTER_NEAREST);
texbuffer->SetColorAttachment(tex);
cam2->SetRenderTarget(texbuffer);
////Display overlay
auto sprite = CreateSprite(world, sz.x, sz.y);
sprite->SetRenderLayers(4);
auto mtl = CreateMaterial();
mtl->SetShaderFamily(unlitShader);
mtl->SetTransparent(true);
mtl->SetTexture(texbuffer->GetColorAttachment());
sprite->SetMaterial(mtl);
auto cam3 = CreateCamera(world, PROJECTION_ORTHOGRAPHIC);
cam3->SetClearMode(CLEAR_DEPTH);
cam3->SetRenderLayers(4);
cam3->SetPosition(sz.x * 0.5f, sz.y * 0.5f, 0);
cam3->SetLighting(false);
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//Main loop
while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false)
{
world->Update();
world->Render(framebuffer, false);
window->SetText(world->renderstats.framerate);
}
return 0;
}