Jump to content

How to handle "cast shadow" property from Blender meshes


Recommended Posts

Hi all,

My context: I have to load different meshes (not a complete scene) from different babylon files, without lights, and then I assemble them in one scene.
Some of the babylon files come from Blender and others from .obj conversion.

All my meshes from .obj should cast shadows. But I'd like to get "cast shadows" information from Blender files, since Blender meshes could, or could not, cast shadows. The shadow generator is defined independently in the code.

I also have to say that I'm both new to Babylon and Blender. Yes, I'm looking for trouble :)
I've made three hypothesis...

H1/ complete Blender meshes "cast shadow" export

I've noticed a "cast shadow" check box in Blender mesh Babylon properties but nothing seems to be exported.
To fix this I've added this line in io_export_babylon.py:

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -      
    def to_scene_file(self, file_handler, meshesAndNodes):
        write_string(file_handler, 'name', self.name, True)        
        write_string(file_handler, 'id', self.name)
        if hasattr(self, 'parentId'): write_string(file_handler, 'parentId', self.parentId)

        if hasattr(self, 'materialId'): write_string(file_handler, 'materialId', self.materialId)        
        write_int(file_handler, 'billboardMode', self.billboardMode)
        write_vector(file_handler, 'position', self.position)
        write_vector(file_handler, 'rotation', self.rotation)
        write_vector(file_handler, 'scaling', self.scaling)
        write_bool(file_handler, 'isVisible', self.isVisible)
        write_bool(file_handler, 'isEnabled', self.isEnabled)
        write_bool(file_handler, 'useFlatShading', self.useFlatShading)
        write_bool(file_handler, 'checkCollisions', self.checkCollisions)
        write_bool(file_handler, 'castShadows', self.castShadows)    # added line
        write_bool(file_handler, 'receiveShadows', self.receiveShadows)

OK, now I have a complete .babylon file, but the problem is the property is not read by babylon at import... If I'm not mistaken I saw nothing in babylonFileLoader class to read this.
This is easy to fix but would add a new shadow mesh property independent of shadowGenerator. This is not nice...

H2/ work with materials

... and then, I've saw there is already a standard "cast" checkbox in shadow section of Blender Materials. This seems logical, and so I'd like to get back this information in babylon.
I see some advantages:
- I use standard Blender properties
- I do not have to duplicate information between Blender material and meshes, with risk of errors

Once again it's easy to export the information: first, add this line

class Material:
    def __init__(self, material, scene, filepath):       
        self.name = BabylonExporter.nameSpace + '.' + material.name        
        BabylonExporter.log('processing begun of material:  ' + self.name)
        self.ambient = material.ambient * material.diffuse_color
        self.diffuse = material.diffuse_intensity * material.diffuse_color
        self.specular = material.specular_intensity * material.specular_color
        self.emissive = material.emit * material.diffuse_color       
        self.specularPower = material.specular_hardness
        self.alpha = material.alpha
        self.backFaceCulling = material.game_settings.use_backface_culling
        self.castShadows = material.use_cast_shadows # added line

and then add this one:

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -                
    def to_scene_file(self, file_handler):       
        write_string(file_handler, 'name', self.name, True)      
        write_string(file_handler, 'id', self.name)
        write_color(file_handler, 'ambient', self.ambient)
        write_color(file_handler, 'diffuse', self.diffuse)
        write_color(file_handler, 'specular', self.specular)
        write_color(file_handler, 'emissive', self.emissive)        
        write_float(file_handler, 'specularPower', self.specularPower)
        write_float(file_handler, 'alpha', self.alpha)
        write_bool(file_handler, 'backFaceCulling', self.backFaceCulling)
        write_bool(file_handler, 'castShadows', self.castShadows) # added line
        for texSlot in self.textures:
But once again I'll have to update babylonFileLoader to read this.
Not hard, but at this moment I am not sure at all it is the "philosophy" of Babylon materials to hold this information.

H3/ Forget all this and use naming convention to know what to do with casting shadows !

Conclusion: so, before doing anything, I need your opinion :) Maybe I completely missed the point, but it seems interesting to catch this Blender cast shadow materiel property...
Thanks for reading.

Link to comment
Share on other sites



shadows are loaded from ShadowsGenerators

This is already handle by Blender exporter:



But in you case, because you are importing a mesh and not the complete scene you do not get this info :(




Doc extract from here:http://doc.babylonjs.com/page.php?p=22661:


A shadowGenerator is defined by the following JSON:

"useVarianceShadowMap": boolean,
"usePoissonSampling": boolean,
"mapSize": int (between 64 and 2048, must be a power of 2),
"lightId": string,
"renderList": array of string (which are IDs of meshes)

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.

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.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...