Ejemplo n.º 1
0
 def test_get_embedded_uri5(self):
     # Test data taken from https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0/Triangle
     test_data = [{
         "data": [0, 1, 2],
         "comptype_id": 5123,
         "ele_type": "SCALAR",
         "vertex_attr": False
     }, {
         "data": [[0, 0, 0], [1, 0, 0], [0, 1, 0]],
         "comptype_id": 5126,
         "ele_type": "VEC3",
         "vertex_attr": True
     }]
     uri, length, buffer_view_data = BufferUtility.get_embedded_uri(
         test_data)
     self.assertEqual(
         uri,
         "data:application/octet-stream;base64,AAABAAIAAAAAAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAAAAAAC"
         "APwAAAAA=")
     self.assertEqual(length, 44)
     self.assertEqual(buffer_view_data, [{
         "byteOffset": 0,
         "byteLength": 6,
         "byteStride": None
     }, {
         "byteOffset": 8,
         "byteLength": 36,
         "byteStride": None
     }])
Ejemplo n.º 2
0
    def embed_data(self, buffer_name, accessor_data):
        """Create pairs of accessors and one buffer from raw data. Add them all to the glTF object.

        :param buffer_name: name of the buffer
        :param accessor_data: [{
                                 name: name of the bufferView and accessor
                                 data: raw data
                                 ele_type: type of each element in data
                                 count: number of elements in data
                                 comptype_id: type of each component of each element in data
                                 max_vals: maximum values of data
                                 min_vals: minimum values of data
                                 target: target that GPU buffer should be bound to
                                 normalized: specifies whether integer data should be normalized
                                 vertex_attr: specifies whether the data is a vertex attribute
                              }, ...]

        :return: (buffer index, accessor indices, bufferView indices)
        """
        accessor_indices = []
        buffer_view_indices = []

        buffer_data, buffer_byte_length, buffer_view_data = BufferUtility.get_embedded_uri(
            accessor_data)

        buffer_index = self._create_buffer(name=buffer_name,
                                           uri=buffer_data,
                                           byte_length=buffer_byte_length)

        bv_required_keys = ["name", "target"]
        ac_required_keys = [
            "name", "bufferview", "byte_offset", "ele_type", "comptype_id",
            "count", "max_vals", "min_vals", "normalized"
        ]
        for bv, ac in zip(buffer_view_data, accessor_data):
            bv_required_args = {
                key: ac[key]
                for key in bv_required_keys if key in ac
            }
            bv_index = self._create_bufferview(buffer=buffer_index,
                                               byte_offset=bv["byteOffset"],
                                               byte_length=bv["byteLength"],
                                               byte_stride=bv["byteStride"],
                                               **bv_required_args)
            buffer_view_indices.append(bv_index)

            ac_required_args = {
                key: ac[key]
                for key in ac_required_keys if key in ac
            }
            ac_index = self._create_accessor(bufferview=bv_index,
                                             **ac_required_args)
            accessor_indices.append(ac_index)

        return buffer_index, accessor_indices, buffer_view_indices
Ejemplo n.º 3
0
    def import_data(self, accessor_data):
        """Add data to the binary chunk. Each set of data generates a pair of accessor and bufferView, which will be
        added to the glb object.

        :param accessor_data: [{
                                 name: name of the bufferView and accessor
                                 data: raw data
                                 ele_type: type of each element in data
                                 count: number of elements in data
                                 comptype_id: type of each component of each element in data
                                 max_vals: maximum values of data
                                 min_vals: minimum values of data
                                 target: target that GPU buffer should be bound to
                                 normalized: specifies whether integer data should be normalized
                                 vertex_attr: specifies whether the data is a vertex attribute
                              }, ...]

        :return: accessor indices, bufferView indices
        """
        accessor_indices = []
        buffer_view_indices = []

        cur_byte_offset = len(self.glb_buffer)

        buffer_bin, buffer_bin_length, buffer_view_data = BufferUtility.get_binary_buffer(accessor_data,
                                                                                          cur_byte_offset)

        bv_required_keys = ["name", "target"]
        ac_required_keys = ["name", "bufferview", "byte_offset", "ele_type", "comptype_id", "count", "max_vals",
                            "min_vals", "normalized"]
        for bv, ac in zip(buffer_view_data, accessor_data):
            bv_required_args = {key: ac[key] for key in bv_required_keys if key in ac}
            bv_index = self._create_bufferview(buffer=0,
                                               byte_offset=bv["byteOffset"],
                                               byte_length=bv["byteLength"],
                                               byte_stride=bv["byteStride"],
                                               **bv_required_args)
            buffer_view_indices.append(bv_index)

            ac_required_args = {key: ac[key] for key in ac_required_keys if key in ac}
            ac_index = self._create_accessor(bufferview=bv_index,
                                             **ac_required_args)
            accessor_indices.append(ac_index)

        self.glb_buffer.extend(buffer_bin)
        self.buffers[0]["byteLength"] = len(self.glb_buffer)

        return accessor_indices, buffer_view_indices
Ejemplo n.º 4
0
    def test_get_embedded_uri2(self):
        test_data = [{
            "data": [1, 2, 3, 4],
            "comptype_id": 5120,
            "ele_type": "SCALAR",
            "vertex_attr": False
        }]

        uri, length, buffer_view_data = BufferUtility.get_embedded_uri(
            test_data)
        self.assertEqual(uri, "data:application/octet-stream;base64,AQIDBA==")
        self.assertEqual(length, 4)
        self.assertEqual(buffer_view_data, [{
            "byteLength": 4,
            "byteOffset": 0,
            "byteStride": None
        }])
Ejemplo n.º 5
0
    def test_get_embedded_uri3(self):
        test_data = [{
            "data": [[1, 2, 3, 4]],
            "comptype_id": 5122,
            "ele_type": "MAT2",
            "vertex_attr": False
        }]

        uri, length, buffer_view_data = BufferUtility.get_embedded_uri(
            test_data)
        self.assertEqual(uri,
                         "data:application/octet-stream;base64,AQACAAMABAA=")
        self.assertEqual(length, 8)
        self.assertEqual(buffer_view_data, [{
            "byteLength": 8,
            "byteOffset": 0,
            "byteStride": None
        }])
Ejemplo n.º 6
0
    def test_get_embedded_uri4(self):
        test_data = [{
            "data": [[1, 2, 3, 2, 3, 4, 3, 4, 5], [5, 5, 5, 6, 6, 6, 7, 7, 7]],
            "comptype_id":
            5122,
            "ele_type":
            "MAT3",
            "vertex_attr":
            False
        }]

        uri, length, buffer_view_data = BufferUtility.get_embedded_uri(
            test_data)
        self.assertEqual(
            uri,
            "data:application/octet-stream;base64,AQACAAMAAgADAAQAAwAEAAUABQAFAAUABgAGAAYABwAHAAcA"
        )
        self.assertEqual(length, 36)
        self.assertEqual(buffer_view_data, [{
            "byteLength": 36,
            "byteOffset": 0,
            "byteStride": None
        }])