]> git.tdb.fi Git - libs/gl.git/commitdiff
Add a missing file
authorMikko Rasa <tdb@tdb.fi>
Thu, 23 May 2019 21:06:05 +0000 (00:06 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 23 May 2019 21:06:09 +0000 (00:06 +0300)
It was supposed to be in bde3ec8.

blender/io_mspgl/material.py [new file with mode: 0644]

diff --git a/blender/io_mspgl/material.py b/blender/io_mspgl/material.py
new file mode 100644 (file)
index 0000000..54b2c5d
--- /dev/null
@@ -0,0 +1,49 @@
+class MaterialMap:
+       def __init__(self, materials):
+               self.technique = materials[0].technique
+               self.name = "material_map_"+self.technique
+               self.materials = materials
+               self.srgb_colors = materials[0].srgb_colors
+               for m in self.materials:
+                       if m.technique!=self.technique:
+                               raise Exception("Conflicting techniques in MaterialMap constructor")
+                       if m.srgb_colors!=self.srgb_colors:
+                               raise Exception("Conflicting colorspace settings in MaterialMap constructor")
+
+               count = len(self.materials)
+               size = 1
+               while size*size*2<count:
+                       size *= 2
+               if size*size>=count:
+                       self.size = (size, size)
+               else:
+                       self.size = (size*2, size)
+
+               from .util import get_colormap
+
+               cm = get_colormap(self.srgb_colors)
+               self.diffuse_data = ""
+               for m in self.materials:
+                       diff = [int(cm(c)*255) for c in m.diffuse_color*m.diffuse_intensity]
+                       self.diffuse_data += "\\x{:02X}\\x{:02X}\\x{:02X}\\x{:02X}".format(*diff, int(m.ambient*255))
+               self.diffuse_data += "\\x00\\x00\\x00\\x00"*(self.size[0]*self.size[1]-count)
+
+       def get_material_uv(self, material):
+               index = self.materials.index(material)
+               x = index%self.size[0]
+               y = index//self.size[0]
+               return ((x+0.5)/self.size[0], (y+0.5)/self.size[1])
+
+def create_material_map(context, material):
+       if not material.material_map:
+               raise Exception("Material is not part of a material map")
+
+       tech = material.technique
+       materials = []
+       for m in context.blend_data.materials:
+               if m.material_map and m.technique==tech:
+                       materials.append(m)
+
+       mat_map = MaterialMap(materials)
+
+       return mat_map