Spaces:
Running
Running
File size: 5,421 Bytes
2b7aae2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | import { Sphere } from '../math/Sphere.js';
import { Vector3 } from '../math/Vector3.js';
import { BufferAttribute } from '../core/BufferAttribute.js';
import { BufferGeometry } from '../core/BufferGeometry.js';
import { FileLoader } from './FileLoader.js';
import { Loader } from './Loader.js';
import { InstancedBufferGeometry } from '../core/InstancedBufferGeometry.js';
import { InstancedBufferAttribute } from '../core/InstancedBufferAttribute.js';
import { InterleavedBufferAttribute } from '../core/InterleavedBufferAttribute.js';
import { InterleavedBuffer } from '../core/InterleavedBuffer.js';
import { getTypedArray } from '../utils.js';
class BufferGeometryLoader extends Loader {
constructor(manager) {
super(manager);
}
load(url, onLoad, onProgress, onError) {
const scope = this;
const loader = new FileLoader(scope.manager);
loader.setPath(scope.path);
loader.setRequestHeader(scope.requestHeader);
loader.setWithCredentials(scope.withCredentials);
loader.load(
url,
function (text) {
try {
onLoad(scope.parse(JSON.parse(text)));
} catch (e) {
if (onError) {
onError(e);
} else {
console.error(e);
}
scope.manager.itemError(url);
}
},
onProgress,
onError
);
}
parse(json) {
const interleavedBufferMap = {};
const arrayBufferMap = {};
function getInterleavedBuffer(json, uuid) {
if (interleavedBufferMap[uuid] !== undefined) return interleavedBufferMap[uuid];
const interleavedBuffers = json.interleavedBuffers;
const interleavedBuffer = interleavedBuffers[uuid];
const buffer = getArrayBuffer(json, interleavedBuffer.buffer);
const array = getTypedArray(interleavedBuffer.type, buffer);
const ib = new InterleavedBuffer(array, interleavedBuffer.stride);
ib.uuid = interleavedBuffer.uuid;
interleavedBufferMap[uuid] = ib;
return ib;
}
function getArrayBuffer(json, uuid) {
if (arrayBufferMap[uuid] !== undefined) return arrayBufferMap[uuid];
const arrayBuffers = json.arrayBuffers;
const arrayBuffer = arrayBuffers[uuid];
const ab = new Uint32Array(arrayBuffer).buffer;
arrayBufferMap[uuid] = ab;
return ab;
}
const geometry = json.isInstancedBufferGeometry ? new InstancedBufferGeometry() : new BufferGeometry();
const index = json.data.index;
if (index !== undefined) {
const typedArray = getTypedArray(index.type, index.array);
geometry.setIndex(new BufferAttribute(typedArray, 1));
}
const attributes = json.data.attributes;
for (const key in attributes) {
const attribute = attributes[key];
let bufferAttribute;
if (attribute.isInterleavedBufferAttribute) {
const interleavedBuffer = getInterleavedBuffer(json.data, attribute.data);
bufferAttribute = new InterleavedBufferAttribute(interleavedBuffer, attribute.itemSize, attribute.offset, attribute.normalized);
} else {
const typedArray = getTypedArray(attribute.type, attribute.array);
const bufferAttributeConstr = attribute.isInstancedBufferAttribute ? InstancedBufferAttribute : BufferAttribute;
bufferAttribute = new bufferAttributeConstr(typedArray, attribute.itemSize, attribute.normalized);
}
if (attribute.name !== undefined) bufferAttribute.name = attribute.name;
if (attribute.usage !== undefined) bufferAttribute.setUsage(attribute.usage);
if (attribute.updateRange !== undefined) {
bufferAttribute.updateRange.offset = attribute.updateRange.offset;
bufferAttribute.updateRange.count = attribute.updateRange.count;
}
geometry.setAttribute(key, bufferAttribute);
}
const morphAttributes = json.data.morphAttributes;
if (morphAttributes) {
for (const key in morphAttributes) {
const attributeArray = morphAttributes[key];
const array = [];
for (let i = 0, il = attributeArray.length; i < il; i++) {
const attribute = attributeArray[i];
let bufferAttribute;
if (attribute.isInterleavedBufferAttribute) {
const interleavedBuffer = getInterleavedBuffer(json.data, attribute.data);
bufferAttribute = new InterleavedBufferAttribute(interleavedBuffer, attribute.itemSize, attribute.offset, attribute.normalized);
} else {
const typedArray = getTypedArray(attribute.type, attribute.array);
bufferAttribute = new BufferAttribute(typedArray, attribute.itemSize, attribute.normalized);
}
if (attribute.name !== undefined) bufferAttribute.name = attribute.name;
array.push(bufferAttribute);
}
geometry.morphAttributes[key] = array;
}
}
const morphTargetsRelative = json.data.morphTargetsRelative;
if (morphTargetsRelative) {
geometry.morphTargetsRelative = true;
}
const groups = json.data.groups || json.data.drawcalls || json.data.offsets;
if (groups !== undefined) {
for (let i = 0, n = groups.length; i !== n; ++i) {
const group = groups[i];
geometry.addGroup(group.start, group.count, group.materialIndex);
}
}
const boundingSphere = json.data.boundingSphere;
if (boundingSphere !== undefined) {
const center = new Vector3();
if (boundingSphere.center !== undefined) {
center.fromArray(boundingSphere.center);
}
geometry.boundingSphere = new Sphere(center, boundingSphere.radius);
}
if (json.name) geometry.name = json.name;
if (json.userData) geometry.userData = json.userData;
return geometry;
}
}
export { BufferGeometryLoader };
|