Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KHR materials #1646

Merged
merged 127 commits into from
Jul 7, 2022
Merged

KHR materials #1646

merged 127 commits into from
Jul 7, 2022

Conversation

julienduroure
Copy link
Collaborator

@julienduroure julienduroure commented May 8, 2022

This PR will implement some official extensions about materials:

  • Import
    • KHR_materials_ior
    • KHR_materials_sheen
    • KHR_materials_specular
    • KHR_materials_transmission
    • KHR_materials_variants
    • KHR_materials_emissive_strength
    • KHR_materials_volume
  • Export
    • KHR_materials_ior
    • KHR_materials_sheen
    • KHR_materials_specular
    • KHR_materials_variants
    • KHR_materials_emissive_strength
    • KHR_materials_volume

See following comments for status update

@julienduroure
Copy link
Collaborator Author

julienduroure commented May 8, 2022

  • Import
    • KHR_materials_ior
      • Basic Code
      • Code completed
      • Code cleanup
      • Test cases added
      • Documentation Updated
      • General status : Can be reviewed and tested by anyone
    • KHR_materials_sheen
      • Basic Code
      • Code completed
      • Code cleanup
      • Validation of approximation done
      • Test cases added
      • Documentation Updated
      • General status : Can be reviewed and tested by anyone.
    • KHR_materials_specular
      • Basic Code
      • Code completed
      • Code cleanup
      • Validation of approximation done
      • Test cases added
      • Documentation Updated
      • General status : Can be reviewed and tested by anyone
    • KHR_materials_transmission
      • Basic Code
      • Code completed
      • Code cleanup
      • Test cases added
      • Documentation Updated
      • General status : Can be reviewed and tested by anyone
    • KHR_materials_variants
      • Basic Code
      • Code completed
      • Code cleanup
      • Test cases added
      • Documentation Updated
      • General status : Can be reviewed and tested by anyone
    • KHR_materials_emissive_strength
      • Basic Code
      • Code completed
      • Code cleanup
      • Test cases added
      • Documentation Updated
      • General status : Can be reviewed and tested by anyone
    • KHR_materials_volume
      • Basic Code
      • Code completed
      • Code cleanup
      • Test cases added
      • Documentation Updated
      • General Status : Can be reviewed and tested by anyone
  • Export
    • KHR_materials_ior
      • Basic Code
      • Code completed
      • Code cleanup
      • Test cases added
      • Documentation Updated
      • General status : Can be reviewed and tested by anyone
    • KHR_materials_sheen
      • Basic Code
      • Code completed
      • Code cleanup
      • Validation of approximation done
      • Test cases added
      • Documentation Updated
      • General Status : Can be reviewed and tested by anyone.
    • KHR_materials_specular
      • Basic Code
      • Code completed
      • Code cleanup
      • Validation of approximation done
      • Test cases added
      • Documentation Updated
      • General Status : Can be reviewed and tested by anyone
    • KHR_materials_variants
      • Basic Code
      • Code completed
      • Code cleanup
      • Test cases added
      • Documentation Updated
      • General status : Can be reviewed and tested by anyone
    • KHR_materials_emissive_strength
      • Basic Code
      • Code completed
      • Code cleanup
      • Test cases added
      • Documentation Updated
      • General status : Can be reviewed and tested by anyone
    • KHR_materials_volume
      • Basic Code
      • Code completed
      • Code cleanup
      • Test cases added
      • Documentation Updated
      • General Status : Can be reviewed and tested by anyone
  • Technical Documentation import
  • Technical Documentation export

@scurest
Copy link
Contributor

scurest commented May 9, 2022

Principled Specular=0 doesn't roundtrip correctly.

@scurest
Copy link
Contributor

scurest commented May 9, 2022

Cleanup for emissive_strength import (not tested)

Patch
diff --git a/addons/io_scene_gltf2/blender/imp/gltf2_blender_KHR_materials_pbrSpecularGlossiness.py b/addons/io_scene_gltf2/blender/imp/gltf2_blender_KHR_materials_pbrSpecularGlossiness.py
index 0bcecdff..1ad6df22 100644
--- a/addons/io_scene_gltf2/blender/imp/gltf2_blender_KHR_materials_pbrSpecularGlossiness.py
+++ b/addons/io_scene_gltf2/blender/imp/gltf2_blender_KHR_materials_pbrSpecularGlossiness.py
@@ -43,11 +43,13 @@ def pbr_specular_glossiness(mh):
         make_volume_socket=None # No possible to have KHR_materials_volume with specular/glossiness
     )
 
-    emission(
-        mh,
-        location=(-200, 860),
-        color_socket=emission_socket,
-    )
+    if emission_socket:
+        emission(
+            mh,
+            location=(-200, 860),
+            color_socket=emission_socket,
+            strength_socket=emission_socket.node.inputs['Strength'],
+        )
 
     base_color(
         mh,
diff --git a/addons/io_scene_gltf2/blender/imp/gltf2_blender_pbrMetallicRoughness.py b/addons/io_scene_gltf2/blender/imp/gltf2_blender_pbrMetallicRoughness.py
index a96b4e66..f6b9a61a 100644
--- a/addons/io_scene_gltf2/blender/imp/gltf2_blender_pbrMetallicRoughness.py
+++ b/addons/io_scene_gltf2/blender/imp/gltf2_blender_pbrMetallicRoughness.py
@@ -68,7 +68,7 @@ def pbr_metallic_roughness(mh: MaterialHelper):
             mh.settings_node = make_settings_node(mh)
             mh.settings_node.location = additional_location
             mh.settings_node.width = 180
-            additional_location = additional_location[0], additional_location[1] - 150    
+            additional_location = additional_location[0], additional_location[1] - 150
 
     _, _, volume_socket = make_output_nodes(
         mh,
@@ -157,7 +157,7 @@ def pbr_metallic_roughness(mh: MaterialHelper):
     )
 
     specular(
-        mh, 
+        mh,
         location_specular=locs['specularTexture'],
         location_specular_tint=locs['specularColorTexture'],
         specular_socket=pbr_node.inputs['Specular'],
@@ -252,31 +252,28 @@ def calc_locations(mh):
 
 
 # [Texture] => [Emissive Factor] =>
-def emission(mh: MaterialHelper, location, color_socket, strength_socket=None):
+def emission(mh: MaterialHelper, location, color_socket, strength_socket):
     x, y = location
     emissive_factor = mh.pymat.emissive_factor or [0, 0, 0]
 
+    strength = 1
+    try:
+        strength = mh.pymat.extensions['KHR_materials_emissive_strength']['emissiveStrength']
+    except Exception:
+        pass
+
     if color_socket is None:
         return
 
     if mh.pymat.emissive_texture is None:
         color_socket.default_value = emissive_factor + [1]
-
-        # KHR_materials_emissive_strength if needed
-        if mh.pymat.extensions and mh.pymat.extensions.get('KHR_materials_emissive_strength'):
-            emission_strength = mh.pymat.extensions['KHR_materials_emissive_strength']['emissiveStrength']
-            strength_socket.default_value = emission_strength
+        strength_socket.default_value = strength
         return
 
     # Put grayscale emissive factors into the Emission Strength
     e0, e1, e2 = emissive_factor
-    if strength_socket and e0 == e1 == e2:
-        # Set emission strength if extension KHR_materials_emissive_strength
-        if mh.pymat.extensions and mh.pymat.extensions['KHR_materials_emissive_strength']:
-            emission_strength = mh.pymat.extensions['KHR_materials_emissive_strength']['emissiveStrength']
-            strength_socket.default_value = e0 * emission_strength
-        else:
-            strength_socket.default_value = e0
+    if e0 == e1 == e2:
+        strength_socket.default_value = e0 * strength
 
     # Otherwise, use a multiply node for it
     else:
@@ -294,10 +291,7 @@ def emission(mh: MaterialHelper, location, color_socket, strength_socket=None):
 
             x -= 200
 
-        # Set emission strength if extension KHR_materials_emissive_strength
-        if strength_socket and mh.pymat.extensions and mh.pymat.extensions['KHR_materials_emissive_strength']:
-            emission_strength = mh.pymat.extensions['KHR_materials_emissive_strength']['emissiveStrength']
-            strength_socket.default_value = emission_strength
+        strength_socket.default_value = strength
 
     texture(
         mh,

Copy link
Contributor

@donmccurdy donmccurdy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit worried that KHR_materials_variants is different enough from the rest of these extensions (and more complex...) that it might be best in a separate PR, would that be a lot of trouble?

Very excited to see these extensions added!

addons/io_scene_gltf2/__init__.py Outdated Show resolved Hide resolved
@donmccurdy
Copy link
Contributor

/cc #1344 (comment)

Slightly off topic but I wonder if it's worth raising this with the Principled BSDF developers? If they intend for the Principled BSDF sheen to be energy-conserving in time, we could consider going ahead and supporting that even if it hasn't been implemented that way yet.

@emackey
Copy link
Member

emackey commented Jun 28, 2022

I wonder if it's worth raising this with the Principled BSDF developers?

If I'm remembering correctly, I believe it was @PascalSchoen's suggestion to use the Velvet node, and he and others had some comments on the call about Blender's own sheen as well. The bottom line is that the Principled BSDF node's current sheen is sufficiently different from glTF's sheen as to require a completely different shading model, not just because of energy conservation, but parameterization and overall appearance as well.

[Very off-topic] Lately I've been wondering if Blender should be given a full "glTF Material" node with a proper mapping to the glTF PBR model in its modern state. The idea seems strangely similar to what UX3D had originally developed for the previous exporter so many years ago, but of course the new one would take modern PBR parameters and practices into account. Both MaterialX and Autodesk's 3DS Max have first-class "glTF Material" nodes now, perhaps it's time for Blender to acquire such a node once again. I'm sure it doesn't fit in the near-term 3.3 timeframe, but perhaps something we could think about down the road?

@julienduroure
Copy link
Collaborator Author

Seems this PR is now feature complete.
Please free to test and review. My plan is to get it into master (and blender repo) next Friday, July 8th

Note: There are still some unit tests and documentation to be updated

@Waterpicker
Copy link

I'm attempting to use the variant extension support added here.

I see that the Material Properties tab has an entry called 'glTF Material Variants' which I assume lets you assign materials of a mesh to actual variants.

I'm trying to figure out how to find where you define variants in the first place. I read in the addon code there is support to be a section for it but I can't seem to locate it.

@julienduroure
Copy link
Collaborator Author

@Waterpicker
Thanks for your interest on Variants!
I didn't write the documentation yet.

  • First, you need to activate the UI in addon preferences

image

  • Then, the simplest way to manage Variants is to use the tab in 3D Editor. use can create variants here.

image

Then you can create your materials, and then Assign to Variants.

image

More details documentation will come in next days/weeks

@takahirox
Copy link

takahirox commented Jul 4, 2022

Hi, nice work. That's great that the add-on supports glTF materials extensions.

I have a question. When we discussed the materials variants extension support last time in #231, we thought that Blender doesn't really have a matching concept of material variants and the materials variants add-on needs an additional UI but we were not sure if additional glTF extension specific data
and UI can follow the glTF blender add-on concept. Then we decided to make a separate materials variants add-on https:/takahirox/glTF-Blender-IO-materials-variants

So, question. Does the glTF blender add-on allow such additional UI now (does it have UI extensibility system)? Or does Blender have material variants concept in the core now?

@julienduroure
Copy link
Collaborator Author

Hello,

About glTF importer/exporter:
There is no specific UI that is displayed by default in Blender. The rule is : No change in UI for people that are not using glTF importer/exporter.
The solution is to add some addon preferences, that enables new UI. There is currently, in master, only 1 preference, to add new glTF shader nodes. This PR adds a second preference, that adds Variant UI + specific data types.

About "User extension addons" : Because users decided to install a new Blender addon (that is a glTF User extension addon), these user extensions are free to add UI in Blender, using directly Blender API.

@julienduroure
Copy link
Collaborator Author

For information :
https://developer.blender.org/T99447

We have a chance to discuss with Blender devs to have better compatibility

@julienduroure julienduroure merged commit c72c090 into master Jul 7, 2022
@emackey emackey mentioned this pull request Jul 19, 2022
@Phrogz
Copy link
Contributor

Phrogz commented Aug 17, 2022

More details documentation will come in next days/weeks

Thanks for the details so far, @julienduroure. Where will more documentation be available? Here, in this PR? Or is there another location where I might look to learn how to assign materials to variants and see them working?

@julienduroure
Copy link
Collaborator Author

@julienduroure julienduroure deleted the KHR_materials branch August 17, 2022 22:34
@elmajime
Copy link

Hi,

Is there a thread somewhere in which we are investigating the lossless conversion at import of the KHR_materials_specular of gltf to Blender's shaders?

I see that currently it is only imported as extra data and is not being used in the shaders.
My company needs this, I will implement it as an addon but will try my best to make it into the gltf importer. If there are some informations out there as to how to import it correctly that would be very helpfull !

Thanks !

@julienduroure
Copy link
Collaborator Author

Hello,

Importer already convert specular data and plug it into Principled Shader (with losses) (and also plug it into glTF Material node to be able to re-export without losses). You can see it, for example, importing this file https:/KhronosGroup/glTF-Sample-Models/blob/master/2.0/SpecularTest/glTF-Binary/SpecularTest.glb

For more technical background, see the following discussions:

@donmccurdy
Copy link
Contributor

donmccurdy commented Aug 25, 2022

Some longer-term context on Principled BSDF v2 + Specular here:

@elmajime
Copy link

Thanks both of you, I will investigate these links.

As for the SpecularTest.gdb yes I tested it and saw the loss of the specular color in the render. This is the missing property my company needs to be rendered.

I will keep you in the loop regarding my development !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants