material.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. emissive = api.material.emissive_color(self.node)
  29. self[constants.EMISSIVE] = utilities.rgb2int(emissive)
  30. vertex_color = api.material.use_vertex_colors(self.node)
  31. if vertex_color:
  32. self[constants.VERTEX_COLORS] = constants.VERTEX_COLORS_ON
  33. else:
  34. self[constants.VERTEX_COLORS] = constants.VERTEX_COLORS_OFF
  35. self[constants.BLENDING] = api.material.blending(self.node)
  36. if api.material.transparent(self.node):
  37. self[constants.TRANSPARENT] = True
  38. self[constants.OPACITY] = api.material.opacity(self.node)
  39. if api.material.double_sided(self.node):
  40. self[constants.SIDE] = constants.SIDE_DOUBLE
  41. self[constants.DEPTH_TEST] = api.material.depth_test(self.node)
  42. self[constants.DEPTH_WRITE] = api.material.depth_write(self.node)
  43. def _phong_attributes(self):
  44. """Parse phong specific attributes"""
  45. logger.debug("Material()._phong_attributes()")
  46. specular = api.material.specular_color(self.node)
  47. self[constants.SPECULAR] = utilities.rgb2int(specular)
  48. self[constants.SHININESS] = api.material.specular_coef(self.node)
  49. def _update_maps(self):
  50. """Parses maps/textures and updates the textures array
  51. with any new nodes found.
  52. """
  53. logger.debug("Material()._update_maps()")
  54. mapping = (
  55. (api.material.diffuse_map, constants.MAP),
  56. (api.material.specular_map, constants.SPECULAR_MAP),
  57. (api.material.light_map, constants.LIGHT_MAP)
  58. )
  59. for func, key in mapping:
  60. map_node = func(self.node)
  61. if map_node:
  62. logger.info('Found map node %s for %s', map_node, key)
  63. tex_inst = self.scene.texture(map_node.name)
  64. self[key] = tex_inst[constants.UUID]
  65. if self[constants.TYPE] == constants.THREE_PHONG:
  66. mapping = (
  67. (api.material.bump_map, constants.BUMP_MAP,
  68. constants.BUMP_SCALE, api.material.bump_scale),
  69. (api.material.normal_map, constants.NORMAL_MAP,
  70. constants.NORMAL_SCALE, api.material.normal_scale)
  71. )
  72. for func, map_key, scale_key, scale_func in mapping:
  73. map_node = func(self.node)
  74. if not map_node:
  75. continue
  76. logger.info("Found map node %s for %s", map_node, map_key)
  77. tex_inst = self.scene.texture(map_node.name)
  78. self[map_key] = tex_inst[constants.UUID]
  79. self[scale_key] = scale_func(self.node)