loadBuffer loads and returns the data from the specified GLTF Buffer index
(bufIdx int)
| 1066 | |
| 1067 | // loadBuffer loads and returns the data from the specified GLTF Buffer index |
| 1068 | func (g *GLTF) loadBuffer(bufIdx int) ([]byte, error) { |
| 1069 | |
| 1070 | // Check if provided buffer index is valid |
| 1071 | if bufIdx < 0 || bufIdx >= len(g.Buffers) { |
| 1072 | return nil, fmt.Errorf("invalid buffer index") |
| 1073 | } |
| 1074 | bufData := &g.Buffers[bufIdx] |
| 1075 | // Return cached if available |
| 1076 | if bufData.cache != nil { |
| 1077 | log.Debug("Fetching Buffer %d (cached)", bufIdx) |
| 1078 | return bufData.cache, nil |
| 1079 | } |
| 1080 | log.Debug("Loading Buffer %d", bufIdx) |
| 1081 | |
| 1082 | // If buffer URI use the chunk data field |
| 1083 | if bufData.Uri == "" { |
| 1084 | return g.data, nil |
| 1085 | } |
| 1086 | |
| 1087 | // Checks if buffer URI is a data URI |
| 1088 | var data []byte |
| 1089 | var err error |
| 1090 | if isDataURL(bufData.Uri) { |
| 1091 | data, err = loadDataURL(bufData.Uri) |
| 1092 | } else { |
| 1093 | // Try to load buffer from file |
| 1094 | data, err = g.loadFileBytes(bufData.Uri) |
| 1095 | } |
| 1096 | if err != nil { |
| 1097 | return nil, err |
| 1098 | } |
| 1099 | |
| 1100 | // Checks data length |
| 1101 | if len(data) != bufData.ByteLength { |
| 1102 | return nil, fmt.Errorf("buffer:%d read data length:%d expected:%d", bufIdx, len(data), bufData.ByteLength) |
| 1103 | } |
| 1104 | // Cache buffer data |
| 1105 | g.Buffers[bufIdx].cache = data |
| 1106 | log.Debug("cache data:%v", len(bufData.cache)) |
| 1107 | return data, nil |
| 1108 | } |
| 1109 | |
| 1110 | // loadFileBytes loads the file with specified path as a byte array. |
| 1111 | func (g *GLTF) loadFileBytes(uri string) ([]byte, error) { |
no test coverage detected