material.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from .. import constants, logger
  2. from . import base_classes, utilities, api
  3. class Material(base_classes.BaseNode):
  4. """Class that wraps material nodes"""
  5. def __init__(self, node, parent):
  6. logger.debug("Material().__init__(%s)", node)
  7. base_classes.BaseNode.__init__(self, node, parent,
  8. constants.MATERIAL)
  9. self._common_attributes()
  10. if self[constants.TYPE] == constants.THREE_PHONG:
  11. self._phong_attributes()
  12. textures = self.parent.options.get(constants.MAPS)
  13. if textures:
  14. self._update_maps()
  15. def _common_attributes(self):
  16. """Parse the common material attributes"""
  17. logger.debug('Material()._common_attributes()')
  18. dispatch = {
  19. constants.PHONG: constants.THREE_PHONG,
  20. constants.LAMBERT: constants.THREE_LAMBERT,
  21. constants.BASIC: constants.THREE_BASIC
  22. }
  23. shader_type = api.material.type(self.node)
  24. self[constants.TYPE] = dispatch[shader_type]
  25. diffuse = api.material.diffuse_color(self.node)
  26. self[constants.COLOR] = utilities.rgb2int(diffuse)
  27. if self[constants.TYPE] != constants.THREE_BASIC:
  28. ambient = api.material.ambient_color(self.node)
  29. self[constants.AMBIENT] = utilities.rgb2int(ambient)
  30. emissive = api.material.emissive_color(self.node)
  31. self[constants.EMISSIVE] = utilities.rgb2int(emissive)
  32. vertex_color = api.material.use_vertex_colors(self.node)
  33. self[constants.VERTEX_COLORS] = vertex_color
  34. self[constants.BLENDING] = api.material.blending(self.node)
  35. self[constants.DEPTH_TEST] = api.material.depth_test(self.node)
  36. self[constants.DEPTH_WRITE] = api.material.depth_write(self.node)
  37. def _phong_attributes(self):
  38. """Parse phong specific attributes"""
  39. logger.debug("Material()._phong_attributes()")
  40. specular = api.material.specular_color(self.node)
  41. self[constants.SPECULAR] = utilities.rgb2int(specular)
  42. self[constants.SHININESS] = api.material.specular_coef(self.node)
  43. def _update_maps(self):
  44. """Parses maps/textures and updates the textures array
  45. with any new nodes found.
  46. """
  47. logger.debug("Material()._update_maps()")
  48. mapping = (
  49. (api.material.diffuse_map, constants.MAP),
  50. (api.material.specular_map, constants.SPECULAR_MAP),
  51. (api.material.light_map, constants.LIGHT_MAP)
  52. )
  53. for func, key in mapping:
  54. map_node = func(self.node)
  55. if map_node:
  56. logger.info('Found map node %s for %s', map_node, key)
  57. tex_inst = self.scene.texture(map_node.name)
  58. self[key] = tex_inst[constants.UUID]
  59. if self[constants.TYPE] == constants.THREE_PHONG:
  60. mapping = (
  61. (api.material.bump_map, constants.BUMP_MAP,
  62. constants.BUMP_SCALE, api.material.bump_scale),
  63. (api.material.normal_map, constants.NORMAL_MAP,
  64. constants.NORMAL_SCALE, api.material.normal_scale)
  65. )
  66. for func, map_key, scale_key, scale_func in mapping:
  67. map_node = func(self.node)
  68. if not map_node:
  69. continue
  70. logger.info("Found map node %s for %s", map_node, map_key)
  71. tex_inst = self.scene.texture(map_node.name)
  72. self[map_key] = tex_inst[constants.UUID]
  73. self[scale_key] = scale_func(self.node)