def test_write_read_f(self):
        expected = RGBA(r=244, g=123, b=33, a=99)

        io_stream = io.BytesIO()
        expected.write_f(io_stream)
        io_stream = io.BytesIO(io_stream.getvalue())

        self.assertEqual(expected, RGBA.read_f(io_stream))
 def read(io_stream):
     return VertexMaterialInfo(attributes=read_long(io_stream),
                               ambient=RGBA.read(io_stream),
                               diffuse=RGBA.read(io_stream),
                               specular=RGBA.read(io_stream),
                               emissive=RGBA.read(io_stream),
                               shininess=read_float(io_stream),
                               opacity=read_float(io_stream),
                               translucency=read_float(io_stream))
Exemplo n.º 3
0
class Box(Struct):
    version = Version()
    box_type = 0
    collision_types = 0
    name = ""
    color = RGBA()
    center = Vector((0.0, 0.0, 0.0))
    extend = Vector((0.0, 0.0, 0.0))

    @staticmethod
    def read(io_stream):
        ver = Version.read(io_stream)
        flags = read_ulong(io_stream)
        return Box(version=ver,
                   box_type=(flags & 0b11),
                   collision_types=(flags & 0xFF0),
                   name=read_long_fixed_string(io_stream),
                   color=RGBA.read(io_stream),
                   center=read_vector(io_stream),
                   extend=read_vector(io_stream))

    @staticmethod
    def size_in_bytes():
        return 68

    def write(self, io_stream):
        write_chunk_head(io_stream, W3D_CHUNK_BOX, self.size_in_bytes())

        self.version.write(io_stream)
        write_ulong(io_stream,
                    (self.collision_types & 0xFF) | (self.box_type & 0b11))
        write_long_fixed_string(io_stream, self.name)
        self.color.write(io_stream)
        write_vector(io_stream, self.center)
        write_vector(io_stream, self.extend)
    def test_write_read(self):
        expected = Box()
        expected.version = Version(major=5, minor=22)
        expected.box_type = 3
        expected.collision_types = 64
        expected.name = "TestBoxxx"
        expected.color = RGBA(r=125, g=110, b=55, a=244)
        expected.center = Vector((1.0, 2.0, 3.0))
        expected.extend = Vector((4.0, 5.0, 6.0))

        self.assertEqual(68, expected.size_in_bytes())

        io_stream = io.BytesIO()
        expected.write(io_stream)
        io_stream = io.BytesIO(io_stream.getvalue())

        (chunkType, chunkSize, _) = read_chunk_head(io_stream)
        self.assertEqual(W3D_CHUNK_BOX, chunkType)
        self.assertEqual(expected.size_in_bytes(), chunkSize)

        actual = Box.read(io_stream)
        self.assertEqual(expected.version.major, actual.version.major)
        self.assertEqual(expected.version.minor, actual.version.minor)
        self.assertEqual(expected.box_type, actual.box_type)
        self.assertEqual(expected.collision_types, actual.collision_types)
        self.assertEqual(expected.name, actual.name)
        self.assertEqual(expected.color, actual.color)

        self.assertEqual(expected.center.x, actual.center.x)
        self.assertEqual(expected.center.y, actual.center.y)
        self.assertEqual(expected.center.z, actual.center.z)

        self.assertEqual(expected.extend.x, actual.extend.x)
        self.assertEqual(expected.extend.y, actual.extend.y)
        self.assertEqual(expected.extend.z, actual.extend.z)
Exemplo n.º 5
0
 def read(io_stream):
     ver = Version.read(io_stream)
     flags = read_ulong(io_stream)
     return Box(version=ver,
                box_type=(flags & 0b11),
                collision_types=(flags & 0xFF0),
                name=read_long_fixed_string(io_stream),
                color=RGBA.read(io_stream),
                center=read_vector(io_stream),
                extend=read_vector(io_stream))
class VertexMaterialInfo(Struct):
    attributes = 0
    ambient = RGBA()  # alpha is only padding in this and below
    diffuse = RGBA()
    specular = RGBA()
    emissive = RGBA()
    shininess = 0.0
    opacity = 0.0
    translucency = 0.0

    @staticmethod
    def read(io_stream):
        return VertexMaterialInfo(
            attributes=read_long(io_stream),
            ambient=RGBA.read(io_stream),
            diffuse=RGBA.read(io_stream),
            specular=RGBA.read(io_stream),
            emissive=RGBA.read(io_stream),
            shininess=read_float(io_stream),
            opacity=read_float(io_stream),
            translucency=read_float(io_stream))

    @staticmethod
    def size_in_bytes():
        return 32

    def write(self, io_stream):
        write_chunk_head(io_stream, W3D_CHUNK_VERTEX_MATERIAL_INFO,
                         self.size_in_bytes())
        write_long(io_stream, self.attributes)
        self.ambient.write(io_stream)
        self.diffuse.write(io_stream)
        self.specular.write(io_stream)
        self.emissive.write(io_stream)
        write_float(io_stream, self.shininess)
        write_float(io_stream, self.opacity)
        write_float(io_stream, self.translucency)
    def read(io_stream):
        result = ShaderMaterialProperty(type=read_long(io_stream),
                                        num_chars=read_long(io_stream),
                                        name=read_string(io_stream))

        if result.type == 1:
            read_long(io_stream)  # num available chars
            result.value = read_string(io_stream)
        elif result.type == 2:
            result.value = read_float(io_stream)
        elif result.type == 4:
            result.value = read_vector(io_stream)
        elif result.type == 5:
            result.value = RGBA.read_f(io_stream)
        elif result.type == 6:
            result.value = read_long(io_stream)
        elif result.type == 7:
            result.value = read_ubyte(io_stream)
        return result
    def test_write_read(self):
        expected = ShaderMaterial()
        expected.header = ShaderMaterialHeader(number=55,
                                               type_name="headerType",
                                               reserver=99)

        self.assertEqual(37, expected.header.size_in_bytes())

        prop1 = ShaderMaterialProperty(type=1,
                                       name="prop1",
                                       num_chars=44,
                                       value="test")

        expected.properties.append(prop1)

        prop2 = ShaderMaterialProperty(type=2,
                                       name="prop2",
                                       num_chars=44,
                                       value=3.14)

        expected.properties.append(prop2)

        prop3 = ShaderMaterialProperty(type=4,
                                       name="prop3",
                                       num_chars=44,
                                       value=Vector((1.0, 2.0, 3.0)))

        expected.properties.append(prop3)

        prop4 = ShaderMaterialProperty(type=5,
                                       name="prop4",
                                       num_chars=44,
                                       value=RGBA(r=3, g=1, b=22, a=133))

        expected.properties.append(prop4)

        prop5 = ShaderMaterialProperty(type=6,
                                       name="prop5",
                                       num_chars=44,
                                       value=1234)

        expected.properties.append(prop5)

        prop6 = ShaderMaterialProperty(type=7,
                                       name="prop6",
                                       num_chars=44,
                                       value=255)

        expected.properties.append(prop6)

        self.assertEqual(223, expected.size_in_bytes())

        io_stream = io.BytesIO()
        expected.write(io_stream)
        io_stream = io.BytesIO(io_stream.getvalue())

        (chunkType, chunkSize, chunkEnd) = read_chunk_head(io_stream)
        self.assertEqual(W3D_CHUNK_SHADER_MATERIAL, chunkType)
        self.assertEqual(expected.size_in_bytes(), chunkSize)

        actual = ShaderMaterial.read(self, io_stream, chunkEnd)
        self.assertEqual(expected.header.number, actual.header.number)
        self.assertEqual(expected.header.type_name, actual.header.type_name)
        self.assertEqual(expected.header.reserved, actual.header.reserved)

        self.assertEqual(len(expected.properties), len(actual.properties))

        for i, prop in enumerate(expected.properties):
            self.assertEqual(prop.type, actual.properties[i].type)
            self.assertEqual(prop.name, actual.properties[i].name)
            self.assertEqual(prop.num_chars, actual.properties[i].num_chars)
            self.assertAlmostEqual(prop.value, actual.properties[i].value, 5)
 def test_eq_false(self):
     rgba = RGBA(r=2, g=3, b=0, a=0)
     self.assertNotEqual(rgba, "test")
     self.assertNotEqual(rgba, 1)
 def test_eq_true(self):
     rgba = RGBA(r=244, g=222, b=1, a=0)
     self.assertEqual(rgba, rgba)