Skip to content

Commit

Permalink
Merge branch 'gltf2'
Browse files Browse the repository at this point in the history
  • Loading branch information
Moguri committed Nov 13, 2018
2 parents 285468b + 21027fc commit aac0c4f
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 747 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "pman"]
path = pman
url = https://github.com/Moguri/pman.git
[submodule "panda3dgltf"]
path = panda3dgltf
url = https://github.com/Moguri/panda3d-gltf.git
736 changes: 0 additions & 736 deletions converter.py

This file was deleted.

81 changes: 81 additions & 0 deletions ext_materials_legacy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from .blendergltf.blendergltf import Reference


class ExtMaterialsLegacy:
ext_meta = {
'name': 'BP_materials_legacy',
'isDraft': True,
}

def export_material(self, state, material):
all_textures = [
slot for slot in material.texture_slots
if slot and slot.texture.type == 'IMAGE'
]
diffuse_textures = [
Reference('textures', t.texture.name, t.texture, None)
for t in all_textures if t.use_map_color_diffuse
]
emission_textures = [
Reference('textures', t.texture.name, t.texture, None)
for t in all_textures
if (
(material.use_shadeless and t.use_map_color_diffuse)
or (not material.use_shadeless and t.use_map_emit)
)
]
specular_textures = [
Reference('textures', t.texture.name, t.texture, None)
for t in all_textures if t.use_map_color_spec
]

diffuse_color = list((material.diffuse_color * material.diffuse_intensity)[:])
diffuse_color += [material.alpha]
emission_color = list((material.diffuse_color * material.emit)[:])
emission_color += [material.alpha]
specular_color = list((material.specular_color * material.specular_intensity)[:])
specular_color += [material.specular_alpha]

gltf = {
'bpLegacy': {
'diffuseFactor': diffuse_color,
'emissionFactor': emission_color,
'specularFactor': specular_color,
'ambientFactor': ([material.ambient]*3) + [1.0],
'shininessFactor': material.specular_hardness,
}
}

if diffuse_textures:
texture = diffuse_textures[-1]
gltf['bpLegacy']['diffuseTexture'] = texture
gltf['bpLegacy']['diffuseTextureSrgb'] = texture.source.image.colorspace_settings.name == 'sRGB'
if emission_textures:
texture = emission_textures[-1]
gltf['bpLegacy']['emissionTexture'] = texture
gltf['bpLegacy']['emissionTextureSrgb'] = texture.source.image.colorspace_settings.name == 'sRGB'
if specular_textures:
texture = specular_textures[-1]
gltf['bpLegacy']['specularTexture'] = texture
gltf['bpLegacy']['specularTextureSrgb'] = texture.source.image.colorspace_settings.name == 'sRGB'

for prop in gltf['bpLegacy']:
if hasattr(gltf['bpLegacy'][prop], 'blender_type'):
ref = gltf['bpLegacy'][prop]
ref.source = gltf['bpLegacy']
ref.prop = prop
state['references'].append(ref)

return gltf

def export(self, state):
state['extensions_used'].append('BP_materials_legacy')

# Export materials
material_pairs = [
(material, state['output']['materials'][state['refmap'][('materials', material.name)]])
for material in state['input']['materials']
]
for bl_mat, gl_mat in material_pairs:
gl_mat['extensions'] = gl_mat.get('extensions', {})
gl_mat['extensions']['BP_materials_legacy'] = self.export_material(state, bl_mat)
7 changes: 7 additions & 0 deletions ext_zup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ExtZup:
ext_meta = {
'name': 'BP_zup',
}

def export(self, state):
state['extensions_used'].append('BP_zup')
29 changes: 21 additions & 8 deletions operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@

from . import pman

from .ext_materials_legacy import ExtMaterialsLegacy
from .ext_zup import ExtZup


_AVAILABLE_EXTENSIONS = blendergltf.extension_exporters
GLTF_SETTINGS = {
'nodes_export_hidden': True,
'images_allow_srgb': True,
'asset_profile': 'DESKTOP',
'asset_version': '1.0',
'nodes_global_matrix_apply': False,
'extension_exporters': [
_AVAILABLE_EXTENSIONS.khr_materials_common.KhrMaterialsCommon(),
_AVAILABLE_EXTENSIONS.khr_lights.KhrLights(),
_AVAILABLE_EXTENSIONS.blender_physics.BlenderPhysics(),
ExtZup(),
],
}

Expand Down Expand Up @@ -70,6 +70,13 @@ def execute(self, _context):
gltf_settings = GLTF_SETTINGS.copy()
gltf_settings['gltf_output_dir'] = os.path.dirname(self.filepath)
gltf_settings['images_data_storage'] = 'COPY' if self.copy_images else 'REFERENCE'
gltf_settings['nodes_export_hidden'] = True
use_legacy_mats = (
config is None or
config['general']['material_mode'] == 'legacy'
)
if use_legacy_mats:
gltf_settings['extension_exporters'].append(ExtMaterialsLegacy())

collections_list = engine.DEFAULT_WATCHLIST + ['actions']
scene_delta = {
Expand All @@ -91,11 +98,17 @@ def execute(self, _context):
# Now convert the data to bam
gltf_fname = self.filepath + '.gltf'
with open(gltf_fname, 'w') as f:
json.dump(data, f)

json.dump(data, f, indent=4)

converter_path = os.path.join(
os.path.dirname(__file__),
'panda3dgltf',
'gltf',
'converter.py'
)
args = [
pycmd,
os.path.join(os.path.dirname(__file__), 'converter.py'),
converter_path,
gltf_fname,
self.filepath,
]
Expand Down
1 change: 1 addition & 0 deletions panda3dgltf
Submodule panda3dgltf added at 44630f
7 changes: 7 additions & 0 deletions panda_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .brte.brte.converters import BTFConverter
from . import pman
from . import operators
from .ext_materials_legacy import ExtMaterialsLegacy


class PandaEngine(bpy.types.RenderEngine, engine.RealTimeEngine):
Expand All @@ -26,6 +27,12 @@ def __init__(self):
gltf_settings['images_data_storage'] = 'REFERENCE'
gltf_settings['meshes_apply_modifiers'] = False # Cannot be done in a thread
gltf_settings['hacks_streaming'] = True
use_legacy_mats = (
config is None or
config['general']['material_mode'] == 'legacy'
)
if use_legacy_mats:
gltf_settings['extension_exporters'].append(ExtMaterialsLegacy())

super().__init__(
converter=BTFConverter(gltf_settings),
Expand Down
2 changes: 1 addition & 1 deletion pman
Submodule pman updated 1 files
+1 −0 pman/__init__.py
16 changes: 14 additions & 2 deletions processor_app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import json
import socket
import struct
Expand All @@ -16,7 +17,8 @@

import pman

from converter import Converter
sys.path.append(os.path.join(os.path.dirname(__file__), 'panda3dgltf'))
from gltf.converter import Converter # pylint: disable=wrong-import-position


p3d.load_prc_file_data(
Expand Down Expand Up @@ -159,7 +161,6 @@ def conversion(task):
self.converter.update(data)
bg_color = self.converter.background_color
self.bg_color = p3d.LVector4(bg_color[0], bg_color[1], bg_color[2], 1)
self.view_region.set_clear_color(self.bg_color)
self.converter.active_scene.reparent_to(self.render)
#self.render.ls()

Expand All @@ -176,6 +177,17 @@ def conversion(task):

self.taskMgr.add(conversion, 'Conversion')

def set_bg_clear_color(task):
# Keep bg color working even if DisplayRegions get switched around
# (e.g., from FilterManager)
for win in self.graphicsEngine.windows:
for dispregion in win.display_regions:
if dispregion.get_camera() == self.cam:
dispregion.set_clear_color_active(True)
dispregion.set_clear_color(self.bg_color)
return task.cont
self.taskMgr.add(set_bg_clear_color, 'Set BG Clear Color')

# Setup communication with Blender
self.server = Server(self.handle_data, self.get_img)
if USE_THREAD:
Expand Down
7 changes: 7 additions & 0 deletions ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import bpy

from .blendergltf.pbr_utils import PbrSettings
from . import pman
from . import operators

Expand Down Expand Up @@ -251,8 +252,14 @@ def register():
for panel in get_panels():
panel.COMPAT_ENGINES.add('PANDA')

if not hasattr(bpy.types.Material, 'pbr_export_settings'):
bpy.types.Material.pbr_export_settings = bpy.props.PointerProperty(type=PbrSettings)


def unregister():
for panel in get_panels():
if 'PANDA' in panel.COMPAT_ENGINES:
panel.COMPAT_ENGINES.remove('PANDA')

if hasattr(bpy.types.Material, 'pbr_export_settings'):
del bpy.types.Material.pbr_export_settings

0 comments on commit aac0c4f

Please sign in to comment.