OGRECave / blender2ogre

Blender exporter for the OGRE 3D engine
GNU Lesser General Public License v2.1
206 stars 69 forks source link

Metal-roughness maps not exported #138

Open paroj opened 2 years ago

paroj commented 2 years ago

we should follow glTF and handle this case:

as we support this in Ogre: https://ogrecave.github.io/ogre/api/latest/rtss.html#autotoc_md248

sercero commented 1 year ago

Hello @paroj,

I'm struggling to make RTSS work in order to test this (OGRE 13.6.2, Rendersystem: RenderSystem_GL3Plus).

I'm just testing Normal Maps for now. Here is my Material:

sampler RTSS/NormalMapSampler
{
    filtering bilinear
    max_anisotropy 1
    mipmap_bias -1.0
}

material Material {
    technique {
        pass {
            specular 1.0 1.0 1.0 32

            texture_unit {
                texture toy_box_diffuse.png
            }

            rtshader_system {
                lighting_stage toy_box_normal.png tangent_space 0 RTSS/NormalMapSampler
            }
        }
    }
}

The textures: toy_box_diffuse toy_box_normal

And the result: screenshot20230228_225256767

Resulting Vertex Shader:

#version 460
//-----------------------------------------------------------------------------
//                         PROGRAM DEPENDENCIES
//-----------------------------------------------------------------------------
#define USE_OGRE_FROM_FUTURE
#include <OgreUnifiedShader.h>
#include "FFPLib_Transform.glsl"
#include "SGXLib_PerPixelLighting.glsl"
#include "FFPLib_Texturing.glsl"

//-----------------------------------------------------------------------------
//                         GLOBAL PARAMETERS
//-----------------------------------------------------------------------------

layout(location = 0) uniform    mat4    worldviewproj_matrix;
layout(location = 4) uniform    mat3    normal_matrix;
layout(location = 7) uniform    mat4    worldview_matrix;

//-----------------------------------------------------------------------------
//                         MAIN
//-----------------------------------------------------------------------------
IN(vec4 vertex, POSITION)
IN(vec3 normal, NORMAL)
IN(vec4 uv0, TEXCOORD0)
OUT(vec3    iTexcoord_0, 0)
OUT(vec3    iTexcoord_1, 1)
OUT(vec2    iTexcoord_2, 2)
out gl_PerVertex
{
vec4 gl_Position;
float gl_PointSize;
float gl_ClipDistance[];
};

void main(void) {
    vec4    lColor_0;
    vec4    lColor_1;

    FFP_Transform(worldviewproj_matrix, vertex, gl_Position);
    lColor_0    =   vec4(1.00000,1.00000,1.00000,1.00000);
    lColor_1    =   vec4(0.00000,0.00000,0.00000,0.00000);
    FFP_Transform(normal_matrix, normal, iTexcoord_0);
    FFP_Transform(worldview_matrix, vertex, iTexcoord_1);
    iTexcoord_2 =   uv0.xy;
}

Resulting Fragment Shader:

#version 460

//-----------------------------------------------------------------------------
//                         PROGRAM DEPENDENCIES
//-----------------------------------------------------------------------------
#define USE_OGRE_FROM_FUTURE
#include <OgreUnifiedShader.h>
#include "SGXLib_PerPixelLighting.glsl"
#include "FFPLib_Texturing.glsl"

//-----------------------------------------------------------------------------
//                         GLOBAL PARAMETERS
//-----------------------------------------------------------------------------
SAMPLER2D(gTextureSampler0, 0);
layout(location = 0) uniform    vec4    derived_ambient_light_colour;
layout(location = 1) uniform    vec4    surface_emissive_colour;
layout(location = 2) uniform    vec4    derived_scene_colour;
layout(location = 3) uniform    float   surface_shininess;
layout(location = 4) uniform    vec4    light_position_view_space;
layout(location = 5) uniform    vec4    light_attenuation;
layout(location = 6) uniform    vec4    derived_light_diffuse_colour;
layout(location = 7) uniform    vec4    derived_light_specular_colour;

//-----------------------------------------------------------------------------
//                         MAIN
//-----------------------------------------------------------------------------
IN(vec3 iTexcoord_0, 0)
IN(vec3 iTexcoord_1, 1)
IN(vec2 iTexcoord_2, 2)
void main(void) {
    vec4    lColor_0;
    vec4    lColor_1;
    vec4    texel_0;

    lColor_0    =   vec4(1.00000,1.00000,1.00000,1.00000);
    lColor_1    =   vec4(0.00000,0.00000,0.00000,0.00000);
    gl_FragColor    =   lColor_0;
    gl_FragColor    =   derived_scene_colour;
    SGX_Light_Point_DiffuseSpecular(iTexcoord_0, iTexcoord_1, light_position_view_space.xyz, light_attenuation, derived_light_diffuse_colour.xyz, derived_light_specular_colour.xyz, surface_shininess, gl_FragColor.xyz, lColor_1.xyz);
    lColor_0    =   gl_FragColor;
    texel_0 =   texture2D(gTextureSampler0, iTexcoord_2);
    gl_FragColor    =   texel_0*gl_FragColor;
    gl_FragColor.xyz    =   gl_FragColor.xyz+lColor_1.xyz;
}
paroj commented 1 year ago

it should be
lighting_stage normal_map toy_box_normal.png

But blender2ogre should be able to export that correctly already.

BTW, this is a material setup that I managed to export to glTF2:

image

when exporting this via blender2ogre, really only the Default_metalRoughness.jpg is missing

sercero commented 1 year ago

it should be lighting_stage normal_map toy_box_normal.png

Wow, my mistake was that stupid? 🙄

Thanks, now it works.

I'm trying to understand a bit about RTSS, because I wonder if the way things are currently being exported is the proper way.

Can I ask more questions here or do you prefer the forums?

paroj commented 1 year ago

feel free to ask here

sercero commented 1 year ago

@paroj I'm trying to use the "DamagedHelmet" model from the PBR sample to check the output.

I see that the PBR example works with RTSS and looks quite nice.

But this is the result in my program: OHH-screenshot20230302_230513435

Here is the material I'm using:

material DamagedHelmet {    // DamagedHelmet_RTSS
    technique
    {
        pass
        {
            // ignored - just in case you want to try the FFP look
            ambient 0.8 0.8 0.8 1.0
            diffuse 0.64 0.64 0.64 1.0
            specular 0.64 0.64 0.64 256

            // albedo - simple
            texture_unit
            {
                texture Default_albedo.jpg
            }

            // emissive - also see rtshader_system section below
            texture_unit
            {
                texture Default_emissive.jpg
                colour_op add
            }

            // additional maps - requires RTSS
            rtshader_system
            {
                lighting_stage metal_roughness texture Default_metalRoughness.jpg
                lighting_stage normal_map Default_normal.jpg
                texturing_stage late_add_blend // needed for emissive to work
                // texture not generated by cmgen, so scale it up as a hack
                //image_based_lighting texture studio_garden.jpg luminance 4.0
            }
        }
    }
}

And the OGRE logs:

Creating resource group General
Creating resource group OgreInternal
Creating resource group OgreAutodetect
SceneManagerFactory for type 'DefaultSceneManager' registered.
Registering ResourceManager for type Material
Registering ResourceManager for type Mesh
Registering ResourceManager for type Skeleton
MovableObjectFactory for type 'ParticleSystem' registered.
ArchiveFactory for type 'FileSystem' registered
ArchiveFactory for type 'Zip' registered
ArchiveFactory for type 'EmbeddedZip' registered
DDS codec registering
ETC codec registering
ASTC codec registering
Registering ResourceManager for type GpuProgram
Registering ResourceManager for type Compositor
MovableObjectFactory for type 'Entity' registered.
MovableObjectFactory for type 'Light' registered.
MovableObjectFactory for type 'BillboardSet' registered.
MovableObjectFactory for type 'ManualObject' registered.
MovableObjectFactory for type 'BillboardChain' registered.
MovableObjectFactory for type 'RibbonTrail' registered.
MovableObjectFactory for type 'StaticGeometry' registered.
MovableObjectFactory for type 'Rectangle2D' registered.
Loading library D:\OGRE2\Built\ogre-13.6.2\bin\RenderSystem_GL3Plus.dll
Installing plugin: GL 3+ RenderSystem
OpenGL 3+ Rendering Subsystem created.
Plugin successfully installed
Loading library D:\OGRE2\Built\ogre-13.6.2\bin\Plugin_DotScene.dll
Installing plugin: DotScene Loader
Plugin successfully installed
Loading library D:\OGRE2\Built\ogre-13.6.2\bin\Codec_STBI.dll
stb_image - v2.27 - public domain image loader
Supported formats: jpeg,jpg,png,bmp,psd,tga,gif,pic,ppm,pgm,hdr
*-*-* OGRE Initialising
*-*-* Version 13.6.2 (Tsathoggua)
 * Creando Mesh para Luz => Light
49681d7d43e125ba19d8f01844bd8726_VS
f5c4cc61924b369f0335d9fdeb9d260a_FS
GLSL program pipeline validation result:
WARNING: warning(#272) Implicit version number 110 not supported by GL3 forward compatible context
paroj commented 1 year ago

are you using mesh and material from the samples, just in your code or are you exporting the mesh with blender2ogre again?

sercero commented 1 year ago

I think that I'm using everything from the samples, there is a "DamagedHelmet" asset which contains the .mesh and image maps.

What's even worse is that somehow trying to enable IBL from my code now the helmet looks completely white, even putting everything as it was... I'm very confused.

So I'll leave the RTSS for now and try to implement this using the DamagedHelmet_RTSS material as a template and then worry about my code not working properly with RTSS.

I wanted to take a deeper look at RTSS, but not for now...

Thanks!

paroj commented 1 year ago

did you maybe forget to update the Ogre shaders when updating to 13.6?

Also the sample only uses one directional light. You should also start with that to ensure you dont run into any bugs that might be there with other light types.

sercero commented 1 year ago

Also the sample only uses one directional light. You should also start with that to ensure you dont run into any bugs that might be there with other light types.

Ouch, I was using a point light.

did you maybe forget to update the Ogre shaders when updating to 13.6?

No, the shaders are directly from the built OGRE 13.6.2 directory.

What really suprised me is that it suddenly broke (now the helmet looks all white).

But now I'm focusing on understanding material.py so I can make the relevant changes.

Thanks.

sercero commented 1 year ago

@paroj thanks to your guidance I was able to get the RTSS working.

Also the changes to material.py are ready but I want to make some more tests.

One thing that I find disturbing is that the lighting calculations seem to be wrong.

In this image the shading makes it look like the light is on the other side: screenshot20230318_230450521

In this image the same thing but you can see it in the highlights: screenshot20230318_230504031

The light is directional, I should use an arrow to represent it but it is pointing towards the helmet.

Unfortunately the official MeshViewer has no material support it seems and the Example Browser does not show the lights positions in the Damaged Helmet example.

paroj commented 1 year ago

you can easily visualise the light position with:

        auto bbs = mSceneMgr->createBillboardSet();
        bbs->setDefaultDimensions(1, 1);
        bbs->setMaterialName("Examples/Flare");
        mSceneMgr->getRootSceneNode()->attachObject(bbs);
        bbs->createBillboard(light->getParentSceneNode()->getPosition());

I quickly did this for the sample and the test that come with ogre:

pbr1

pbr2

which seems about right.

I mean it can still be bugged, but it is not as easy as a sign error..

Unfortunately the official MeshViewer has no material support

it does, but it only looks for resources in the .mesh directory by default. Alternatively, you can give it a resources.cfg via CLI.

sercero commented 1 year ago

@paroj what do you think about creating a new release?

paroj commented 1 year ago

@sercero I drafted a release for master. You should be able to just publish it.