UNPKG

awv3

Version:
55 lines (49 loc) 2.1 kB
class StoredProperties { constructor() { this.props = {}; // saved original properties of a material this.refcount = 0; // so that we won't forget original properties before all restore animation are complete } store(material, props) { let animationProps = {}; for (let key of Object.keys(props)) { if (material[key] === undefined) continue; // skip properties unappliable to this material animationProps[key] = props[key]; if (this.props[key] === undefined) this.props[key] = material[key].clone ? material[key].clone() : material[key]; } return material.animate(animationProps); } restore(material) { return material.animate(this.props); } } /** * @class that stores and restores the original color and opacity of materials */ export default class MaterialStore { constructor() { this.storedPropertiesMap = new WeakMap(); } /** * Stores properties of a material into a map and creates an animation * @param {THREE.Material} material * @params props - material properties to animate to * @return {Tween} animation - not started animation to the given properties */ store(material, props) { if (!this.storedPropertiesMap.has(material)) this.storedPropertiesMap.set(material, new StoredProperties()); ++this.storedPropertiesMap.get(material).refcount; return this.storedPropertiesMap.get(material).store(material, props); } /** * Restores the original properties of a material from the map * @param {THREE.Material} material - material that was stored earlier * @return {Tween} animation - not started restoration animation */ restore(material) { if (!this.storedPropertiesMap.has(material)) return material.animate({}); return this.storedPropertiesMap.get(material).restore(material).onComplete(() => { if (!--this.storedPropertiesMap.get(material).refcount) this.storedPropertiesMap.delete(material); }); } }