Jump to content

How to save depth with object rendered with extra camera?


Dreikblack
 Share

Go to solution Solved by Dreikblack,

Recommended Posts

How should be but with red box behind yellow box

image.png.dbe92f00ea2c1fb2e25338b5f457f22b.png

Result with SetClearMode(CLEAR_COLOR), white fog color and black clear color

image.png.f0dd1e70e2685ec37d016f57b5690da3.png

#include "UltraEngine.h"
#include "ComponentSystem.h"

using namespace UltraEngine;

int main(int argc, const char* argv[])
{
    auto displays = GetDisplays();
    auto window = CreateWindow("Ultra Engine", 0, 0, 1280, 720, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR);
    auto world = CreateWorld();
    auto framebuffer = CreateFramebuffer(window);
    auto camera = CreateCamera(world);
    camera->SetClearColor(0.125);
    camera->SetFov(70);
    camera->SetPosition(0, 0, -3);
    auto light = CreateBoxLight(world);
    light->SetRotation(35, 45, 0);
    light->SetRange(-10, 10);

    Vec4 color(0, 1, 0, 1);

    //Create a box
    auto box = CreateBox(world);

    //Render to texture
    box->SetRenderLayers(2);
    box->SetColor(1, 0, 0, 1);
    auto unlitShader = LoadShaderFamily("Shaders/Unlit.fam");
    auto boxMat = CreateMaterial();
    boxMat->SetShaderFamily(unlitShader);
    box->SetMaterial(boxMat);

    auto cam2 = CreateCamera(world);
    cam2->SetClearColor(0, 0, 0, 0);
    cam2->SetRenderLayers(2);
    cam2->SetFov(camera->GetFov());
    cam2->SetMatrix(camera->matrix);
    cam2->SetLighting(false);

    auto sz = framebuffer->GetSize();
    auto texbuffer = CreateTextureBuffer(sz.x, sz.y);
    cam2->SetRenderTarget(texbuffer);

    //Display overlay
    auto sprite = CreateSprite(world, sz.x, sz.y);
    sprite->SetRenderLayers(4);
    auto mtl = CreateMaterial();
    sprite->SetMaterial(mtl);
    mtl->SetTransparent(true);
    mtl->SetTexture(texbuffer->GetColorAttachment());
    mtl->SetColor(1, 1, 1, 0.5);
    auto cam3 = CreateCamera(world, PROJECTION_ORTHOGRAPHIC);
    cam3->SetClearMode(CLEAR_COLOR);
    cam3->SetRenderLayers(4);
    cam3->SetPosition(sz.x * 0.5f, sz.y * 0.5f, 0);
    cam3->SetLighting(false);

    cam3->SetFog(true);
    cam3->SetFogRange(0, 0);
    cam3->SetFogColor(1, 1, 1, 1);
    cam3->SetClearColor(0, 0, 0, 1);

    auto box2 = CreateBox(world, 5);
    box2->SetPosition(0, 0, 7);
    box2->SetColor(color);

    auto box3= CreateBox(world, 1);
    box3->SetPosition(-0.5, 0, -0.5);
    Vec4 color2(1, 1, 0, 1);
    box3->SetColor(color2);

    //Main loop
    while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false)
    {
        world->Update();
        world->Render(framebuffer);
    }
    return 0;
}

 

 

 

 

Link to comment
Share on other sites

I think you would have to create another camera in between "camera" and cam2 with the same characteristics as "camera" but rendering to the texture buffer. That would draw the depth information, then cam2 would render the transparent objects on that, and retain the depth info to discard fragments.

It would be better if "camera" was rendering to a texture buffer, you could get the depth attachment, and attach that to the texture buffer cam2 is using. Then when cam2 starts drawing, it already has the depth info from the main camera.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

12 hours ago, Josh said:

It would be better if "camera" was rendering to a texture buffer, you could get the depth attachment, and attach that to the texture buffer cam2 is using. Then when cam2 starts drawing, it already has the depth info from the main camera.

Same result but no lighting now for unknown reason:

image.png.884792fe70af3842a64852560f8f6f0e.png

#include "UltraEngine.h"
#include "ComponentSystem.h"

using namespace UltraEngine;

int main(int argc, const char* argv[])
{
    auto displays = GetDisplays();
    auto window = CreateWindow("Ultra Engine", 0, 0, 1280, 720, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR);
    auto world = CreateWorld();
    auto framebuffer = CreateFramebuffer(window);

    auto sz = framebuffer->GetSize();
    int MAIN_LAYER = 1, OUTPUT_LAYER = 2, EXTRA_LAYER = 4, EXTRA_OUTPUT_LAYER = 8;

    auto light = CreateBoxLight(world);
    light->SetRotation(35, 45, 0);
    light->SetRange(-10, 10);

    auto backBox = CreateBox(world, 5);
    backBox->SetPosition(0, 0, 7);
    backBox->SetColor(0, 1, 0, 1);

    auto betweenBox = CreateBox(world);
    betweenBox->SetRenderLayers(EXTRA_LAYER);
    betweenBox->SetColor(1, 0, 0, 1);
    auto unlitShader = LoadShaderFamily("Shaders/Unlit.fam");
    auto boxMat = CreateMaterial();
    boxMat->SetShaderFamily(unlitShader);
    betweenBox->SetMaterial(boxMat);

    auto frontBox = CreateBox(world, 1);
    frontBox->SetPosition(-0.5, 0, -0.5);
    frontBox->SetColor(1, 1, 0, 1);

    auto mainCameraInput = CreateCamera(world);
    mainCameraInput->SetPosition(0, 0, -3);
    mainCameraInput->SetRenderLayers(MAIN_LAYER);

    auto mainTextureBuffer = CreateTextureBuffer(sz.x, sz.y);
    mainCameraInput->SetRenderTarget(mainTextureBuffer);

    auto mainSprite = CreateSprite(world, sz.x, sz.y);
    mainSprite->SetRenderLayers(OUTPUT_LAYER);
    auto mainMaterial = CreateMaterial();
    mainSprite->SetMaterial(mainMaterial);
    mainMaterial->SetTexture(mainTextureBuffer->GetColorAttachment());

    auto mainCameraOutput = CreateCamera(world, PROJECTION_ORTHOGRAPHIC);
    mainCameraOutput->SetPosition(sz.x * 0.5f, sz.y * 0.5f, 0);
    mainCameraOutput->SetRenderLayers(OUTPUT_LAYER);
    mainCameraOutput->SetLighting(true);

    //red transporent box render part
    auto extraCameraInput = CreateCamera(world);
    extraCameraInput->SetPosition(0, 0, -3);
    extraCameraInput->SetRenderLayers(EXTRA_LAYER);
    extraCameraInput->SetMatrix(mainCameraInput->matrix);
    extraCameraInput->SetLighting(false);
    
    auto extraTextureBuffer = CreateTextureBuffer(sz.x, sz.y);
    extraCameraInput->SetRenderTarget(extraTextureBuffer);

    auto extraSprite = CreateSprite(world, sz.x, sz.y);
    extraSprite->SetRenderLayers(EXTRA_OUTPUT_LAYER);
    auto extraMaterial = CreateMaterial();
    extraSprite->SetMaterial(extraMaterial);
    extraMaterial->SetTransparent(true);
    extraMaterial->SetTexture(extraTextureBuffer->GetColorAttachment());
    extraMaterial->SetColor(1, 1, 1, 0.5);

    auto extraCameraOutput = CreateCamera(world, PROJECTION_ORTHOGRAPHIC);
    extraCameraOutput->SetPosition(sz.x * 0.5f, sz.y * 0.5f, 0);
    extraCameraOutput->SetRenderLayers(EXTRA_OUTPUT_LAYER);
    extraCameraOutput->SetClearMode(CLEAR_DEPTH);
    extraTextureBuffer->SetDepthAttachment(mainTextureBuffer->GetDepthAttachment());
    extraCameraOutput->SetOrder(2);
    extraCameraOutput->SetLighting(false);
    //extraCameraOutput->SetHidden(true);

    //Main loop
    while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false)
    {
        world->Update();
        world->Render(framebuffer);
    }
    return 0;
}
Link to comment
Share on other sites

Is this what you are trying to do?

image.thumb.png.f0f7ef7d723fca0db884beca32e6df2d.png

#include "UltraEngine.h"
#include "ComponentSystem.h"

using namespace UltraEngine;

int main(int argc, const char* argv[])
{
    auto displays = GetDisplays();
    auto window = CreateWindow("Ultra Engine", 0, 0, 1280, 720, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR);
    auto world = CreateWorld();
    auto framebuffer = CreateFramebuffer(window);

    auto sz = framebuffer->GetSize();
    int MAIN_LAYER = 1, OUTPUT_LAYER = 2, EXTRA_LAYER = 4, EXTRA_OUTPUT_LAYER = 8;

    auto light = CreateBoxLight(world);
    light->SetRotation(35, 45, 0);
    light->SetRange(-10, 10);

    auto backBox = CreateBox(world, 5);
    backBox->SetPosition(0, 0, 7);
    backBox->SetColor(0, 1, 0, 1);

    auto betweenBox = CreateBox(world);
    betweenBox->SetRenderLayers(EXTRA_LAYER);
    betweenBox->SetColor(1, 0, 0, 1);
    auto unlitShader = LoadShaderFamily("Shaders/Unlit.fam");
    auto boxMat = CreateMaterial();
    boxMat->SetShaderFamily(unlitShader);
    betweenBox->SetMaterial(boxMat);

    auto frontBox = CreateBox(world, 1);
    frontBox->SetPosition(-0.5, 0, -0.5);
    frontBox->SetColor(1, 1, 0, 1);

    auto mainCameraInput = CreateCamera(world);
    mainCameraInput->SetPosition(0, 0, -3);
    mainCameraInput->SetRenderLayers(MAIN_LAYER);

    auto mainTextureBuffer = CreateTextureBuffer(sz.x, sz.y);
    mainCameraInput->SetRenderTarget(mainTextureBuffer);

    auto mainSprite = CreateSprite(world, sz.x, sz.y);
    mainSprite->SetRenderLayers(OUTPUT_LAYER);
    auto mainMaterial = CreateMaterial();
    mainSprite->SetMaterial(mainMaterial);
    mainMaterial->SetTexture(mainTextureBuffer->GetColorAttachment());

    //red transporent box render part
    auto extraCameraInput = CreateCamera(world);
    extraCameraInput->SetPosition(0, 0, -3);
    extraCameraInput->SetRenderLayers(EXTRA_LAYER);
    extraCameraInput->SetMatrix(mainCameraInput->matrix);
    extraCameraInput->SetLighting(false);
    extraCameraInput->SetClearMode(CLEAR_COLOR);

    auto extraTextureBuffer = CreateTextureBuffer(sz.x, sz.y);
    extraTextureBuffer->SetDepthAttachment(mainTextureBuffer->GetDepthAttachment());
    extraCameraInput->SetRenderTarget(extraTextureBuffer);

    auto extraSprite = CreateSprite(world, sz.x, sz.y);
    extraSprite->SetRenderLayers(EXTRA_OUTPUT_LAYER);
    auto extraMaterial = CreateMaterial();
    extraSprite->SetMaterial(extraMaterial);
    extraMaterial->SetTransparent(true);
    extraMaterial->SetTexture(extraTextureBuffer->GetColorAttachment());
    extraMaterial->SetColor(1, 1, 1, 0.5);

    auto mainCameraOutput = CreateCamera(world, PROJECTION_ORTHOGRAPHIC);
    mainCameraOutput->SetPosition(sz.x * 0.5f, sz.y * 0.5f, 0);
    mainCameraOutput->SetRenderLayers(OUTPUT_LAYER);
    mainCameraOutput->SetLighting(false);

    auto extraCameraOutput = CreateCamera(world, PROJECTION_ORTHOGRAPHIC);
    extraCameraOutput->SetPosition(sz.x * 0.5f, sz.y * 0.5f, 0);
    extraCameraOutput->SetRenderLayers(EXTRA_OUTPUT_LAYER);
    extraCameraOutput->SetClearMode(CLEAR_DEPTH);
    extraTextureBuffer->SetDepthAttachment(mainTextureBuffer->GetDepthAttachment());
    //extraCameraOutput->SetOrder(2);
    extraCameraOutput->SetLighting(false);
    //extraCameraOutput->SetHidden(true);

    //Main loop
    while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false)
    {
        world->Update();
        world->Render(framebuffer);
    }
    return 0;
}

 

  • Thanks 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

  • Solution

Final working example:

image.png.9462bb78634ca62913ba3cbeb5e01c2e.png

#include "UltraEngine.h"
#include "ComponentSystem.h"

using namespace UltraEngine;

int main(int argc, const char* argv[])
{
    auto displays = GetDisplays();
    auto window = CreateWindow("Ultra Engine", 0, 0, 1280, 720, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR);
    auto world = CreateWorld();
    auto framebuffer = CreateFramebuffer(window);

    auto sz = framebuffer->GetSize();
    int MAIN_LAYER = 1, OUTPUT_LAYER = 2, EXTRA_LAYER = 4, EXTRA_OUTPUT_LAYER = 8;

    auto light = CreateBoxLight(world);
    light->SetRotation(35, 45, 0);
    light->SetRange(-10, 10);

    auto backBox = CreateBox(world, 5);
    backBox->SetPosition(0, 0, 7);
    backBox->SetColor(0, 1, 0, 1);

    auto betweenBox = CreateBox(world);
    betweenBox->SetRenderLayers(EXTRA_LAYER);
    betweenBox->SetColor(1, 0, 0, 1);
    auto unlitShader = LoadShaderFamily("Shaders/Unlit.fam");
    auto boxMat = CreateMaterial();
    boxMat->SetShaderFamily(unlitShader);
    betweenBox->SetMaterial(boxMat);
    auto betweenBox2 = CreateBox(world);
    betweenBox2->SetPosition(0.5, 0.5, 0);
    betweenBox2->SetRenderLayers(EXTRA_LAYER);
    betweenBox2->SetColor(1, 0, 0, 1);
    betweenBox2->SetMaterial(boxMat);

    auto frontBox = CreateBox(world, 1);
    frontBox->SetPosition(-0.5, 0, -0.5);
    frontBox->SetColor(1, 1, 0, 1);

    auto mainCameraInput = CreateCamera(world);
    mainCameraInput->SetPosition(0, 0, -3);
    mainCameraInput->SetRenderLayers(MAIN_LAYER);

    auto mainTextureBuffer = CreateTextureBuffer(sz.x, sz.y);
    mainCameraInput->SetRenderTarget(mainTextureBuffer);

    auto mainSprite = CreateSprite(world, sz.x, sz.y);
    mainSprite->SetRenderLayers(OUTPUT_LAYER);
    auto mainMaterial = CreateMaterial();
    mainMaterial->SetShaderFamily(unlitShader);
    mainSprite->SetMaterial(mainMaterial);
    mainMaterial->SetTexture(mainTextureBuffer->GetColorAttachment());

    auto mainCameraOutput = CreateCamera(world, PROJECTION_ORTHOGRAPHIC);
    mainCameraOutput->SetPosition(sz.x * 0.5f, sz.y * 0.5f, 0);
    mainCameraOutput->SetRenderLayers(OUTPUT_LAYER);
    mainCameraOutput->SetLighting(false);
    mainCameraOutput->SetClearMode(CLEAR_DEPTH);

    //red transporent boxes render part
    auto extraCameraInput = CreateCamera(world);
    extraCameraInput->SetPosition(0, 0, -3);
    extraCameraInput->SetRenderLayers(EXTRA_LAYER);
    extraCameraInput->SetMatrix(mainCameraInput->matrix);
    extraCameraInput->SetClearMode(CLEAR_COLOR);
    extraCameraInput->SetLighting(false);
    
    auto extraTextureBuffer = CreateTextureBuffer(sz.x, sz.y);
    extraTextureBuffer->SetDepthAttachment(mainTextureBuffer->GetDepthAttachment());
    extraCameraInput->SetRenderTarget(extraTextureBuffer);

    auto extraSprite = CreateSprite(world, sz.x, sz.y);
    extraSprite->SetPosition(0, 0, -0.00001);
    extraSprite->SetRenderLayers(OUTPUT_LAYER);
    auto extraMaterial = CreateMaterial();
    extraMaterial->SetShaderFamily(unlitShader);
    extraSprite->SetMaterial(extraMaterial);
    extraMaterial->SetTransparent(true);
    extraMaterial->SetTexture(extraTextureBuffer->GetColorAttachment());
    extraMaterial->SetColor(1, 1, 1, 0.5);

    //Main loop
    while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false)
    {
        world->Update();
        world->Render(framebuffer);
    }
    return 0;
}

 

  • Like 2
Link to comment
Share on other sites

New problem with skybox:

image.thumb.png.5f0890c069c1dc1fcb4519dc1cb3553a.png

Added just:

    const WString remotepath = "https://raw.githubusercontent.com/UltraEngine/Documentation/master/Assets";
    auto specmap = LoadTexture(remotepath + "/Materials/Environment/Storm/specular.dds");
    auto diffmap = LoadTexture(remotepath + "/Materials/Environment/Storm/diffuse.dds");
    world->SetEnvironmentMap(specmap, ENVIRONMENTMAP_BACKGROUND);
    world->SetEnvironmentMap(specmap, ENVIRONMENTMAP_SPECULAR);
    world->SetEnvironmentMap(diffmap, ENVIRONMENTMAP_DIFFUSE);

How to disable this effect for display sprite or camera?

Link to comment
Share on other sites

Now it's too dark :o

image.thumb.png.d6d6daaa7110c1169126e4b67ffb4a72.png

For comparison without sprite and fog:

image.thumb.png.f9e27d608ae8d3ea607811155f991822.png

#include "UltraEngine.h"
#include "ComponentSystem.h"

using namespace UltraEngine;

int main(int argc, const char* argv[])
{
    auto displays = GetDisplays();
    auto window = CreateWindow("Ultra Engine", 0, 0, 1280, 720, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR);
    auto world = CreateWorld();
    auto framebuffer = CreateFramebuffer(window);

    //Set environment maps
    const WString remotepath = "https://raw.githubusercontent.com/UltraEngine/Documentation/master/Assets";
    auto specmap = LoadTexture(remotepath + "/Materials/Environment/Storm/specular.dds");
    auto diffmap = LoadTexture(remotepath + "/Materials/Environment/Storm/diffuse.dds");
    world->SetEnvironmentMap(specmap, ENVIRONMENTMAP_BACKGROUND);
    world->SetEnvironmentMap(specmap, ENVIRONMENTMAP_SPECULAR);
    world->SetEnvironmentMap(diffmap, ENVIRONMENTMAP_DIFFUSE);

    auto sz = framebuffer->GetSize();
    int MAIN_LAYER = 1, OUTPUT_LAYER = 2, EXTRA_LAYER = 4;

    auto light = CreateBoxLight(world);
    light->SetRotation(35, 45, 0);
    light->SetRange(-10, 10);

    auto backBox = CreateBox(world, 5);
    backBox->SetPosition(0, 0, 7);
    backBox->SetColor(0, 1, 0, 1);

    auto betweenBox = CreateBox(world);
    betweenBox->SetRenderLayers(EXTRA_LAYER);
    betweenBox->SetColor(1, 0, 0, 1);
    auto unlitShader = LoadShaderFamily("Shaders/Unlit.fam");
    auto boxMat = CreateMaterial();
    boxMat->SetShaderFamily(unlitShader);
    betweenBox->SetMaterial(boxMat);
    auto betweenBox2 = CreateBox(world);
    betweenBox2->SetPosition(0.5, 0.5, 0);
    betweenBox2->SetRenderLayers(EXTRA_LAYER);
    betweenBox2->SetColor(1, 0, 0, 1);
    betweenBox2->SetMaterial(boxMat);

    auto frontBox = CreateBox(world, 1);
    frontBox->SetPosition(-0.5, 0, -0.5);
    frontBox->SetColor(1, 1, 0, 1);

    auto mainCameraInput = CreateCamera(world);
    mainCameraInput->SetPosition(0, 0, -3);
    mainCameraInput->SetRenderLayers(MAIN_LAYER);
    
    auto mainTextureBuffer = CreateTextureBuffer(sz.x, sz.y);
    mainCameraInput->SetRenderTarget(mainTextureBuffer);

    auto mainSprite = CreateSprite(world, sz.x, sz.y);
    mainSprite->SetRenderLayers(OUTPUT_LAYER);
    auto mainMaterial = CreateMaterial();
    mainMaterial->SetShaderFamily(unlitShader);
    mainMaterial->SetTexture(mainTextureBuffer->GetColorAttachment());
    mainSprite->SetMaterial(mainMaterial);

    auto mainCameraOutput = CreateCamera(world, PROJECTION_ORTHOGRAPHIC);
    mainCameraOutput->SetPosition(sz.x * 0.5f, sz.y * 0.5f, 0);
    mainCameraOutput->SetRenderLayers(OUTPUT_LAYER);
    mainCameraOutput->SetLighting(false);
    mainCameraOutput->SetClearMode(CLEAR_DEPTH);

    //red transporent boxes render part
    auto extraCameraInput = CreateCamera(world);
    extraCameraInput->SetPosition(0, 0, -3);
    extraCameraInput->SetRenderLayers(EXTRA_LAYER);
    extraCameraInput->SetMatrix(mainCameraInput->matrix);
    extraCameraInput->SetClearMode(CLEAR_COLOR);
    extraCameraInput->SetLighting(false);
    extraCameraInput->SetFogColor(0, 0, 0, 1);
    extraCameraInput->SetFogAngle(90, 91);
    extraCameraInput->SetFog(true);
    auto range = extraCameraInput->GetRange().y;
    extraCameraInput->SetFogRange(range * 0.98, range * 0.99);
    
    auto extraTextureBuffer = CreateTextureBuffer(sz.x, sz.y);
    extraTextureBuffer->SetDepthAttachment(mainTextureBuffer->GetDepthAttachment());
    extraCameraInput->SetRenderTarget(extraTextureBuffer);

    auto extraSprite = CreateSprite(world, sz.x, sz.y);
    extraSprite->SetPosition(0, 0, -0.00001);
    extraSprite->SetRenderLayers(OUTPUT_LAYER);
    extraSprite->SetShadows(false);
    auto extraMaterial = CreateMaterial();
    extraMaterial->SetShaderFamily(unlitShader);
    extraMaterial->SetTransparent(true);
    extraMaterial->SetTexture(extraTextureBuffer->GetColorAttachment());
    extraMaterial->SetColor(1, 1, 1, 0.5);
    extraSprite->SetMaterial(extraMaterial);
    extraSprite->SetHidden(false);

    //Main loop
    while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false)
    {
        world->Update();
        world->Render(framebuffer);
    }
    return 0;
}

 

Link to comment
Share on other sites

Oh, I see. The fog is turning the sprite color black, and then that gets overlaid on top of the screen, right?

I did not have any of these problems in my outline shader because I just used the depth buffer to decide whether the output should be white or black. But you are passing through the color buffer so that is more complicated.

Maybe the only way is a per-camera setting to disable the skybox if it has been set.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

You could also put a very large black plane in front of the camera to block to sky out, and make it only appear in the second camera.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...