Some questions about the JSON material settings

Discussion area about developing with Ogre-Next (2.1, 2.2 and beyond)


knn217
Halfling
Posts: 78
Joined: Wed Jan 25, 2023 9:04 am
x 5

Some questions about the JSON material settings

Post by knn217 »

Hello, the module for exporting JSON material in "blender2ogre" seems quite limited for my use, so I'm trying to modify it to add and fix some features, so I've been messing with a .material.json file for "leaf.png" to learn about the correct setups for certain effects. I kinda got the basics, but still have 2 questions:

  1. As I understand, OgreV2 use "alpha_test" to determine which pixels to cut out from the texture file. Currently, it seems only the alpha channel from the "diffuse" image is supported, are there any plans to give the users the choice to use another image like "opacity.png" instead?
    _Since using "opacity.png" as replacement for alpha channel is supported in Blender, right now I'm planning to modify blender2ogre to turn "opacity.png" into the alpha channel for "basecolor.png" at the output.

  2. How to use "Transparency" properly? This setting is quite confusing and I still don't know if it is correlated to "alpha_test" given how they both use alpha channel.
    _I've been able to create transparent texture by modifying the blendblock to "Type: Transparent Colour" like instructed here:https://ogrecave.github.io/ogre-next/ap ... ckref.html
    _However, "Transparency" doesn't seem to be affected by "use_alpha_from_textures". And "value " only determines transparency on "Transparent" mode, but makes the texture dark in "Fade" mode.

Here's the json:

Code: Select all

{
  "pbs": {
    "alpha_material": {
      "alpha_test": ["greater_equal", 0.5, false],
      "blendblock" : ["blendblock_name", "blendblock_name_for_shadows"],

  "diffuse": {
    "texture": "leaf.png",
    "value": [1.0, 1.0, 1.0, 1.0]
  },
  "metalness": {
    "value": 0.0
  },
  "roughness": {
    "value": 0.5
  },
  "specular": {
    "value": [1.0, 1.0, 1.0]
  },
  "transparency": {
    "mode": "Transparent",
    "use_alpha_from_textures": true,
    "value": 1
  },
  "two_sided": false,
  "workflow": "metallic"
}
  },

  "blendblocks" :
    {
        "blendblock_name" :
        {
            "alpha_to_coverage" : false,
            "blendmask" : "rgba",
            "separate_blend" : false,
            "src_blend_factor" : "src_colour",
            "dst_blend_factor" : "one_minus_src_colour",
            "src_alpha_blend_factor" : "src_colour",
            "dst_alpha_blend_factor" : "one_minus_src_colour",
            "blend_operation" : "add",
            "blend_operation_alpha" : "add"
        }
    }

}

I want to make sure the how the alpha affect "Transparency" for Ogre-next to decide which alpha setup in Blender should export the right json file

User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5477
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1359

Re: Some questions about the JSON material settings

Post by dark_sylinc »

Hi!

As I understand, OgreV2 use "alpha_test" to determine which pixels to cut out from the texture file. Currently, it seems only the alpha channel from the "diffuse" image is supported

That's correct.

are there any plans to give the users the choice to use another image like "opacity.png" instead?

There are no plans.
Normally packing as much data as possible in a single texture fetch is preferred for performance reasons; and putting the alpha channel with the diffuse texture is what engines have traditionally done.

How to use "Transparency" properly? This setting is quite confusing and I still don't know if it is correlated to "alpha_test" given how they both use alpha channel.

Alpha testing and transparency will always be at odds because they have similar functionality and often try to solve the same problem with a slightly different approach. Usually you want to enable one or the other, but not both at the same time (unless you're trying to achieve something very specific, I guess).

I've been able to create transparent texture by modifying the blendblock to "Type: Transparent Colour" like

OK I see what the problem is. Here's the thing:

  1. HlmsPbsDatablock::setTransparency lets you automate transparency settings by using two modes: "Transparent" & "Fade". "Transparent" tries to mimic physically real windows and glasses. There are limitations, but that's what it wants to achieve. It also means that even if alpha = 0, a glass reflecting specular lighting may still be visible. "Fade" is regular old-school alpha blending where alpha = 0 means the object is invisible.

    • HlmsPbsDatablock::setTransparency will automatically set a blendblock for you with the right settings.

    • I'm skipping "Refractive" transparency mode because it requires Compositor setup (see Samples/2.0/ApiUsage/Refractions sample)

  2. The JSON material is very flexible, and lets you specify a blendblock. If you do that (which you are doing) then HlmsPbsDatablock::setTransparency will not automatically set a blendblock.

    • One simple solution is to simply remove "blendblock" : ["blendblock_name", "blendblock_name_for_shadows"], from your JSON material, and thus setting transparency will automatically set one for you

    • Another is that you use HlmsManager::saveMaterials and/or analyze HlmsPbsDatablock::setTransparency (or keep scrolling down for the answer) to see how the autogenerated blendblock looks like, and you set one yourself.

I've been able to create transparent texture by modifying the blendblock to "Type: Transparent Colour"

While I personally thinks "Transparent Colour" is pretty, it is not what you're looking for. What you're looking for is "Type: Transparent Alpha" for the "Fade" mode.

For "Transparency" mode you need:

Code: Select all

"src_blend_factor" : "one",
"dst_blend_factor" : "one_minus_src_alpha",
"src_alpha_blend_factor" : "one",
"dst_alpha_blend_factor" : "one_minus_src_alpha"

However, "Transparency" doesn't seem to be affected by "use_alpha_from_textures"

Mmm, that sounds strange. Originally that setting was introduced for performance optimization though.

The rationale was that if a diffuse texture alpha does NOT ever have alpha < 1.0; then the user only wants to control transparency at material level via HlmsPbsDatablock::setDiffuse.

From what I'm reading from you, you want that setting to be always true.

knn217
Halfling
Posts: 78
Joined: Wed Jan 25, 2023 9:04 am
x 5

Re: Some questions about the JSON material settings

Post by knn217 »

Thanks for the answers! I did test some of the setups in the docs but ended up using custom setups in the code.

Using one image as another's alpha channel probably made the alpha channel has poor quality which affected the BlendBlock's effects.
The 1st Custom Blend Type is a bit more clear than Type: Transparent Colour

Code: Select all

"src_blend_factor" : "one"
"dst_blend_factor" : "one_minus_src_colour"
"src_alpha_blend_factor" : "one"
"dst_alpha_blend_factor" : "one_minus_src_colour"

The 2nd Custom Blend Type is even more clear than the 1st, but still quite transparent

Code: Select all

"src_blend_factor" : "one"
"dst_blend_factor" : "dst_colour"
"src_alpha_blend_factor" : "one"
"dst_alpha_blend_factor" : "dst_colour"
knn217
Halfling
Posts: 78
Joined: Wed Jan 25, 2023 9:04 am
x 5

Re: Some questions about the JSON material settings

Post by knn217 »

Hello again, I'm done with the support for the "alpha channel combining" feature for blender2ogre, while waiting for the pull request to be accepted, I want to learn more about setting up other things like: metalness, roughness, specular,... in case there are more optimization to be made. So I want to ask a few questions regarding these settings.

First off, since JSON material already supported metalness/roughness, this is where I'm focusing on right now. Here's the .material.json output file for the setup in the image:
image: Image
.material.json:

Code: Select all

{
  "blendblocks": {},
  "pbs": {
    "Material_MR": {
      "diffuse": {
        "texture": "Default_albedo.jpg",
        "value": [
          1.0,
          1.0,
          1.0,
          1.0
        ]
      },
      "metalness": {
        "texture": "Default_metalRoughness.jpg",
        "value": 0.0
      },
      "roughness": {
        "texture": "Default_metalRoughness.jpg",
        "value": 0.5
      },
      "specular": {
        "value": [
          1.0,
          1.0,
          1.0
        ]
      },
      "transparency": {
        "mode": "Transparent",
        "use_alpha_from_textures": true,
        "value": 1
      },
      "two_sided": true,
      "workflow": "metallic"
    }
  }
}

As I understand from the docs:https://ogrecave.github.io/ogre-next/ap ... ckref.html
Metallic and Roughness will use the red channel from their texture image, but since they are using the same image in the current JSON settings, they basically are using the same map for their effects. Is this good practice or should they have used different images? Or even different channels from the same image?
The docs for blender2ogre follow glTF setup, which uses the blue and green channels from the same image, but OgreV2 seems to auto-pick the red channel which might give unintended results.

The fix I have in mind right now is to generate a "metallic.png" and a "roughness.png" from the blue and green channel of the original image and then assign these new images to the JSON file. Before fixing, I need to confirm if this is bad practice, since when I tried loading this material into Ogre, there was some small reflections on the mesh but I'm not sure if it's because of bad lighting setups.

Edit: turns out it wasn't the original module but my new edits changing the JSON file setup which cause this bug, the original code already output the blue and green channels. I made a quick fix and also added support for standalone metallic and roughness textures (no need to strictly use glTF format), the changes are already pushed to github

knn217
Halfling
Posts: 78
Joined: Wed Jan 25, 2023 9:04 am
x 5

Re: Some questions about the JSON material settings

Post by knn217 »

The next thing that I couldn't get right is the normal map. For some reason, adding normal map into the JSON config will make the text becomes white, without any texture or color. I tested on the damaged helmet model here: https://github.com/KhronosGroup/glTF-Sa ... agedHelmet

And here's the JSON:

Code: Select all

{
  "blendblocks": {},
  "pbs": {
    "Material_MR": {
      "diffuse": {
        "texture": "Default_albedo.jpeg",
        "value": [
          1.0,
          1.0,
          1.0,
          1.0
        ]
      },
      "metalness": {
        "texture": "Default_metalRoughness_c2.jpeg",
        "value": 0.818
      },
      "normal": {
        "texture": "Default_normal.jpeg",
        "value": 1.0
      },
      "roughness": {
        "texture": "Default_metalRoughness_c1.jpeg",
        "value": 1.0
      },
      "specular": {
        "value": [
          1.0,
          1.0,
          1.0
        ]
      },
      "transparency": {
        "mode": "Transparent",
        "use_alpha_from_textures": true,
        "value": 1
      },
      "two_sided": true,
      "workflow": "metallic"
    }
  }
}

Image

Deleting the "normal" section makes all the textures visible again.

User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5477
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1359

Re: Some questions about the JSON material settings

Post by dark_sylinc »

knn217 wrote: Wed Nov 08, 2023 7:55 am

Metallic and Roughness will use the red channel from their texture image, but since they are using the same image in the current JSON settings, they basically are using the same map for their effects. Is this good practice or should they have used different images? Or even different channels from the same image?

OgreNext right now expects two different textures to sample from the red channel.
There was a plan to allow arbitrary "mix and match" of textures to support this type of setup, but it was never implemented (it wasn't even designed actually).

So I'm afraid you will have to use different images.

The next thing that I couldn't get right is the normal map. For some reason, adding normal map into the JSON config will make the text becomes white, without any texture or color. I tested on the damaged helmet model here: https://github.com/KhronosGroup/glTF-Sa ... agedHelmet

Look at the Ogre.log. I suspect the exported mesh has no tangents (no tangents == can't use normal mapping) and thus a fallback material was used instead.

knn217
Halfling
Posts: 78
Joined: Wed Jan 25, 2023 9:04 am
x 5

Re: Some questions about the JSON material settings

Post by knn217 »

dark_sylinc wrote: Wed Nov 08, 2023 7:49 pm

Look at the Ogre.log. I suspect the exported mesh has no tangents (no tangents == can't use normal mapping) and thus a fallback material was used instead.

Thanks for the answer, I just checked the exporter and tangent generation was indeed set to none. Sorry about all the basic mistakes, I'm still learning as I go.