material.py 3.2 KB

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