Spaces:
Sleeping
Sleeping
File size: 8,988 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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | import { PropertyBinding } from './PropertyBinding.js';
import * as MathUtils from '../math/MathUtils.js';
/**
*
* A group of objects that receives a shared animation state.
*
* Usage:
*
* - Add objects you would otherwise pass as 'root' to the
* constructor or the .clipAction method of AnimationMixer.
*
* - Instead pass this object as 'root'.
*
* - You can also add and remove objects later when the mixer
* is running.
*
* Note:
*
* Objects of this class appear as one object to the mixer,
* so cache control of the individual objects must be done
* on the group.
*
* Limitation:
*
* - The animated properties must be compatible among the
* all objects in the group.
*
* - A single property can either be controlled through a
* target group or directly, but not both.
*/
class AnimationObjectGroup {
constructor() {
this.uuid = MathUtils.generateUUID();
// cached objects followed by the active ones
this._objects = Array.prototype.slice.call(arguments);
this.nCachedObjects_ = 0; // threshold
// note: read by PropertyBinding.Composite
const indices = {};
this._indicesByUUID = indices; // for bookkeeping
for (let i = 0, n = arguments.length; i !== n; ++i) {
indices[arguments[i].uuid] = i;
}
this._paths = []; // inside: string
this._parsedPaths = []; // inside: { we don't care, here }
this._bindings = []; // inside: Array< PropertyBinding >
this._bindingsIndicesByPath = {}; // inside: indices in these arrays
const scope = this;
this.stats = {
objects: {
get total() {
return scope._objects.length;
},
get inUse() {
return this.total - scope.nCachedObjects_;
},
},
get bindingsPerObject() {
return scope._bindings.length;
},
};
}
add() {
const objects = this._objects,
indicesByUUID = this._indicesByUUID,
paths = this._paths,
parsedPaths = this._parsedPaths,
bindings = this._bindings,
nBindings = bindings.length;
let knownObject = undefined,
nObjects = objects.length,
nCachedObjects = this.nCachedObjects_;
for (let i = 0, n = arguments.length; i !== n; ++i) {
const object = arguments[i],
uuid = object.uuid;
let index = indicesByUUID[uuid];
if (index === undefined) {
// unknown object -> add it to the ACTIVE region
index = nObjects++;
indicesByUUID[uuid] = index;
objects.push(object);
// accounting is done, now do the same for all bindings
for (let j = 0, m = nBindings; j !== m; ++j) {
bindings[j].push(new PropertyBinding(object, paths[j], parsedPaths[j]));
}
} else if (index < nCachedObjects) {
knownObject = objects[index];
// move existing object to the ACTIVE region
const firstActiveIndex = --nCachedObjects,
lastCachedObject = objects[firstActiveIndex];
indicesByUUID[lastCachedObject.uuid] = index;
objects[index] = lastCachedObject;
indicesByUUID[uuid] = firstActiveIndex;
objects[firstActiveIndex] = object;
// accounting is done, now do the same for all bindings
for (let j = 0, m = nBindings; j !== m; ++j) {
const bindingsForPath = bindings[j],
lastCached = bindingsForPath[firstActiveIndex];
let binding = bindingsForPath[index];
bindingsForPath[index] = lastCached;
if (binding === undefined) {
// since we do not bother to create new bindings
// for objects that are cached, the binding may
// or may not exist
binding = new PropertyBinding(object, paths[j], parsedPaths[j]);
}
bindingsForPath[firstActiveIndex] = binding;
}
} else if (objects[index] !== knownObject) {
console.error(
'THREE.AnimationObjectGroup: Different objects with the same UUID ' +
'detected. Clean the caches or recreate your infrastructure when reloading scenes.'
);
} // else the object is already where we want it to be
} // for arguments
this.nCachedObjects_ = nCachedObjects;
}
remove() {
const objects = this._objects,
indicesByUUID = this._indicesByUUID,
bindings = this._bindings,
nBindings = bindings.length;
let nCachedObjects = this.nCachedObjects_;
for (let i = 0, n = arguments.length; i !== n; ++i) {
const object = arguments[i],
uuid = object.uuid,
index = indicesByUUID[uuid];
if (index !== undefined && index >= nCachedObjects) {
// move existing object into the CACHED region
const lastCachedIndex = nCachedObjects++,
firstActiveObject = objects[lastCachedIndex];
indicesByUUID[firstActiveObject.uuid] = index;
objects[index] = firstActiveObject;
indicesByUUID[uuid] = lastCachedIndex;
objects[lastCachedIndex] = object;
// accounting is done, now do the same for all bindings
for (let j = 0, m = nBindings; j !== m; ++j) {
const bindingsForPath = bindings[j],
firstActive = bindingsForPath[lastCachedIndex],
binding = bindingsForPath[index];
bindingsForPath[index] = firstActive;
bindingsForPath[lastCachedIndex] = binding;
}
}
} // for arguments
this.nCachedObjects_ = nCachedObjects;
}
// remove & forget
uncache() {
const objects = this._objects,
indicesByUUID = this._indicesByUUID,
bindings = this._bindings,
nBindings = bindings.length;
let nCachedObjects = this.nCachedObjects_,
nObjects = objects.length;
for (let i = 0, n = arguments.length; i !== n; ++i) {
const object = arguments[i],
uuid = object.uuid,
index = indicesByUUID[uuid];
if (index !== undefined) {
delete indicesByUUID[uuid];
if (index < nCachedObjects) {
// object is cached, shrink the CACHED region
const firstActiveIndex = --nCachedObjects,
lastCachedObject = objects[firstActiveIndex],
lastIndex = --nObjects,
lastObject = objects[lastIndex];
// last cached object takes this object's place
indicesByUUID[lastCachedObject.uuid] = index;
objects[index] = lastCachedObject;
// last object goes to the activated slot and pop
indicesByUUID[lastObject.uuid] = firstActiveIndex;
objects[firstActiveIndex] = lastObject;
objects.pop();
// accounting is done, now do the same for all bindings
for (let j = 0, m = nBindings; j !== m; ++j) {
const bindingsForPath = bindings[j],
lastCached = bindingsForPath[firstActiveIndex],
last = bindingsForPath[lastIndex];
bindingsForPath[index] = lastCached;
bindingsForPath[firstActiveIndex] = last;
bindingsForPath.pop();
}
} else {
// object is active, just swap with the last and pop
const lastIndex = --nObjects,
lastObject = objects[lastIndex];
if (lastIndex > 0) {
indicesByUUID[lastObject.uuid] = index;
}
objects[index] = lastObject;
objects.pop();
// accounting is done, now do the same for all bindings
for (let j = 0, m = nBindings; j !== m; ++j) {
const bindingsForPath = bindings[j];
bindingsForPath[index] = bindingsForPath[lastIndex];
bindingsForPath.pop();
}
} // cached or active
} // if object is known
} // for arguments
this.nCachedObjects_ = nCachedObjects;
}
// Internal interface used by befriended PropertyBinding.Composite:
subscribe_(path, parsedPath) {
// returns an array of bindings for the given path that is changed
// according to the contained objects in the group
const indicesByPath = this._bindingsIndicesByPath;
let index = indicesByPath[path];
const bindings = this._bindings;
if (index !== undefined) return bindings[index];
const paths = this._paths,
parsedPaths = this._parsedPaths,
objects = this._objects,
nObjects = objects.length,
nCachedObjects = this.nCachedObjects_,
bindingsForPath = new Array(nObjects);
index = bindings.length;
indicesByPath[path] = index;
paths.push(path);
parsedPaths.push(parsedPath);
bindings.push(bindingsForPath);
for (let i = nCachedObjects, n = objects.length; i !== n; ++i) {
const object = objects[i];
bindingsForPath[i] = new PropertyBinding(object, path, parsedPath);
}
return bindingsForPath;
}
unsubscribe_(path) {
// tells the group to forget about a property path and no longer
// update the array previously obtained with 'subscribe_'
const indicesByPath = this._bindingsIndicesByPath,
index = indicesByPath[path];
if (index !== undefined) {
const paths = this._paths,
parsedPaths = this._parsedPaths,
bindings = this._bindings,
lastBindingsIndex = bindings.length - 1,
lastBindings = bindings[lastBindingsIndex],
lastBindingsPath = path[lastBindingsIndex];
indicesByPath[lastBindingsPath] = index;
bindings[index] = lastBindings;
bindings.pop();
parsedPaths[index] = parsedPaths[lastBindingsIndex];
parsedPaths.pop();
paths[index] = paths[lastBindingsIndex];
paths.pop();
}
}
}
AnimationObjectGroup.prototype.isAnimationObjectGroup = true;
export { AnimationObjectGroup };
|