UNPKG

@esotericsoftware/spine-core

Version:
885 lines (884 loc) 212 kB
/****************************************************************************** * Spine Runtimes License Agreement * Last updated April 5, 2025. Replaces all prior versions. * * Copyright (c) 2013-2025, Esoteric Software LLC * * Integration of the Spine Runtimes into software or otherwise creating * derivative works of the Spine Runtimes is permitted under the terms and * conditions of Section 2 of the Spine Editor License Agreement: * http://esotericsoftware.com/spine-editor-license * * Otherwise, it is permitted to integrate the Spine Runtimes into software * or otherwise create derivative works of the Spine Runtimes (collectively, * "Products"), provided that each user of the Products must obtain their own * Spine Editor license and redistribution of the Products in any form must * include this license and copyright notice. * * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *****************************************************************************/ import { Animation, InheritTimeline, AttachmentTimeline, RGBATimeline, RGBTimeline, AlphaTimeline, RGBA2Timeline, RGB2Timeline, RotateTimeline, TranslateTimeline, TranslateXTimeline, TranslateYTimeline, ScaleTimeline, ScaleXTimeline, ScaleYTimeline, ShearTimeline, ShearXTimeline, ShearYTimeline, IkConstraintTimeline, TransformConstraintTimeline, PathConstraintPositionTimeline, PathConstraintSpacingTimeline, PathConstraintMixTimeline, DeformTimeline, DrawOrderTimeline, EventTimeline, PhysicsConstraintResetTimeline, PhysicsConstraintInertiaTimeline, PhysicsConstraintStrengthTimeline, PhysicsConstraintDampingTimeline, PhysicsConstraintMassTimeline, PhysicsConstraintWindTimeline, PhysicsConstraintGravityTimeline, PhysicsConstraintMixTimeline } from "./Animation.js"; import { BoneData, Inherit } from "./BoneData.js"; import { EventData } from "./EventData.js"; import { Event } from "./Event.js"; import { IkConstraintData } from "./IkConstraintData.js"; import { PathConstraintData, PositionMode, SpacingMode, RotateMode } from "./PathConstraintData.js"; import { SkeletonData } from "./SkeletonData.js"; import { Skin } from "./Skin.js"; import { SlotData, BlendMode } from "./SlotData.js"; import { TransformConstraintData } from "./TransformConstraintData.js"; import { Utils, Color } from "./Utils.js"; import { Sequence, SequenceMode } from "./attachments/Sequence.js"; import { SequenceTimeline } from "./Animation.js"; import { PhysicsConstraintData } from "./PhysicsConstraintData.js"; /** Loads skeleton data in the Spine JSON format. * * See [Spine JSON format](http://esotericsoftware.com/spine-json-format) and * [JSON and binary data](http://esotericsoftware.com/spine-loading-skeleton-data#JSON-and-binary-data) in the Spine * Runtimes Guide. */ export class SkeletonJson { attachmentLoader; /** Scales bone positions, image sizes, and translations as they are loaded. This allows different size images to be used at * runtime than were used in Spine. * * See [Scaling](http://esotericsoftware.com/spine-loading-skeleton-data#Scaling) in the Spine Runtimes Guide. */ scale = 1; linkedMeshes = new Array(); constructor(attachmentLoader) { this.attachmentLoader = attachmentLoader; } readSkeletonData(json) { let scale = this.scale; let skeletonData = new SkeletonData(); let root = typeof (json) === "string" ? JSON.parse(json) : json; // Skeleton let skeletonMap = root.skeleton; if (skeletonMap) { skeletonData.hash = skeletonMap.hash; skeletonData.version = skeletonMap.spine; skeletonData.x = skeletonMap.x; skeletonData.y = skeletonMap.y; skeletonData.width = skeletonMap.width; skeletonData.height = skeletonMap.height; skeletonData.referenceScale = getValue(skeletonMap, "referenceScale", 100) * scale; skeletonData.fps = skeletonMap.fps; skeletonData.imagesPath = skeletonMap.images ?? null; skeletonData.audioPath = skeletonMap.audio ?? null; } // Bones if (root.bones) { for (let i = 0; i < root.bones.length; i++) { let boneMap = root.bones[i]; let parent = null; let parentName = getValue(boneMap, "parent", null); if (parentName) parent = skeletonData.findBone(parentName); let data = new BoneData(skeletonData.bones.length, boneMap.name, parent); data.length = getValue(boneMap, "length", 0) * scale; data.x = getValue(boneMap, "x", 0) * scale; data.y = getValue(boneMap, "y", 0) * scale; data.rotation = getValue(boneMap, "rotation", 0); data.scaleX = getValue(boneMap, "scaleX", 1); data.scaleY = getValue(boneMap, "scaleY", 1); data.shearX = getValue(boneMap, "shearX", 0); data.shearY = getValue(boneMap, "shearY", 0); data.inherit = Utils.enumValue(Inherit, getValue(boneMap, "inherit", "Normal")); data.skinRequired = getValue(boneMap, "skin", false); let color = getValue(boneMap, "color", null); if (color) data.color.setFromString(color); skeletonData.bones.push(data); } } // Slots. if (root.slots) { for (let i = 0; i < root.slots.length; i++) { let slotMap = root.slots[i]; let slotName = slotMap.name; let boneData = skeletonData.findBone(slotMap.bone); if (!boneData) throw new Error(`Couldn't find bone ${slotMap.bone} for slot ${slotName}`); let data = new SlotData(skeletonData.slots.length, slotName, boneData); let color = getValue(slotMap, "color", null); if (color) data.color.setFromString(color); let dark = getValue(slotMap, "dark", null); if (dark) data.darkColor = Color.fromString(dark); data.attachmentName = getValue(slotMap, "attachment", null); data.blendMode = Utils.enumValue(BlendMode, getValue(slotMap, "blend", "normal")); data.visible = getValue(slotMap, "visible", true); skeletonData.slots.push(data); } } // IK constraints if (root.ik) { for (let i = 0; i < root.ik.length; i++) { let constraintMap = root.ik[i]; let data = new IkConstraintData(constraintMap.name); data.order = getValue(constraintMap, "order", 0); data.skinRequired = getValue(constraintMap, "skin", false); for (let ii = 0; ii < constraintMap.bones.length; ii++) { let bone = skeletonData.findBone(constraintMap.bones[ii]); if (!bone) throw new Error(`Couldn't find bone ${constraintMap.bones[ii]} for IK constraint ${constraintMap.name}.`); data.bones.push(bone); } let target = skeletonData.findBone(constraintMap.target); ; if (!target) throw new Error(`Couldn't find target bone ${constraintMap.target} for IK constraint ${constraintMap.name}.`); data.target = target; data.mix = getValue(constraintMap, "mix", 1); data.softness = getValue(constraintMap, "softness", 0) * scale; data.bendDirection = getValue(constraintMap, "bendPositive", true) ? 1 : -1; data.compress = getValue(constraintMap, "compress", false); data.stretch = getValue(constraintMap, "stretch", false); data.uniform = getValue(constraintMap, "uniform", false); skeletonData.ikConstraints.push(data); } } // Transform constraints. if (root.transform) { for (let i = 0; i < root.transform.length; i++) { let constraintMap = root.transform[i]; let data = new TransformConstraintData(constraintMap.name); data.order = getValue(constraintMap, "order", 0); data.skinRequired = getValue(constraintMap, "skin", false); for (let ii = 0; ii < constraintMap.bones.length; ii++) { let boneName = constraintMap.bones[ii]; let bone = skeletonData.findBone(boneName); if (!bone) throw new Error(`Couldn't find bone ${boneName} for transform constraint ${constraintMap.name}.`); data.bones.push(bone); } let targetName = constraintMap.target; let target = skeletonData.findBone(targetName); if (!target) throw new Error(`Couldn't find target bone ${targetName} for transform constraint ${constraintMap.name}.`); data.target = target; data.local = getValue(constraintMap, "local", false); data.relative = getValue(constraintMap, "relative", false); data.offsetRotation = getValue(constraintMap, "rotation", 0); data.offsetX = getValue(constraintMap, "x", 0) * scale; data.offsetY = getValue(constraintMap, "y", 0) * scale; data.offsetScaleX = getValue(constraintMap, "scaleX", 0); data.offsetScaleY = getValue(constraintMap, "scaleY", 0); data.offsetShearY = getValue(constraintMap, "shearY", 0); data.mixRotate = getValue(constraintMap, "mixRotate", 1); data.mixX = getValue(constraintMap, "mixX", 1); data.mixY = getValue(constraintMap, "mixY", data.mixX); data.mixScaleX = getValue(constraintMap, "mixScaleX", 1); data.mixScaleY = getValue(constraintMap, "mixScaleY", data.mixScaleX); data.mixShearY = getValue(constraintMap, "mixShearY", 1); skeletonData.transformConstraints.push(data); } } // Path constraints. if (root.path) { for (let i = 0; i < root.path.length; i++) { let constraintMap = root.path[i]; let data = new PathConstraintData(constraintMap.name); data.order = getValue(constraintMap, "order", 0); data.skinRequired = getValue(constraintMap, "skin", false); for (let ii = 0; ii < constraintMap.bones.length; ii++) { let boneName = constraintMap.bones[ii]; let bone = skeletonData.findBone(boneName); if (!bone) throw new Error(`Couldn't find bone ${boneName} for path constraint ${constraintMap.name}.`); data.bones.push(bone); } let targetName = constraintMap.target; let target = skeletonData.findSlot(targetName); if (!target) throw new Error(`Couldn't find target slot ${targetName} for path constraint ${constraintMap.name}.`); data.target = target; data.positionMode = Utils.enumValue(PositionMode, getValue(constraintMap, "positionMode", "Percent")); data.spacingMode = Utils.enumValue(SpacingMode, getValue(constraintMap, "spacingMode", "Length")); data.rotateMode = Utils.enumValue(RotateMode, getValue(constraintMap, "rotateMode", "Tangent")); data.offsetRotation = getValue(constraintMap, "rotation", 0); data.position = getValue(constraintMap, "position", 0); if (data.positionMode == PositionMode.Fixed) data.position *= scale; data.spacing = getValue(constraintMap, "spacing", 0); if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed) data.spacing *= scale; data.mixRotate = getValue(constraintMap, "mixRotate", 1); data.mixX = getValue(constraintMap, "mixX", 1); data.mixY = getValue(constraintMap, "mixY", data.mixX); skeletonData.pathConstraints.push(data); } } // Physics constraints. if (root.physics) { for (let i = 0; i < root.physics.length; i++) { const constraintMap = root.physics[i]; const data = new PhysicsConstraintData(constraintMap.name); data.order = getValue(constraintMap, "order", 0); data.skinRequired = getValue(constraintMap, "skin", false); const boneName = constraintMap.bone; const bone = skeletonData.findBone(boneName); if (bone == null) throw new Error("Physics bone not found: " + boneName); data.bone = bone; data.x = getValue(constraintMap, "x", 0); data.y = getValue(constraintMap, "y", 0); data.rotate = getValue(constraintMap, "rotate", 0); data.scaleX = getValue(constraintMap, "scaleX", 0); data.shearX = getValue(constraintMap, "shearX", 0); data.limit = getValue(constraintMap, "limit", 5000) * scale; data.step = 1 / getValue(constraintMap, "fps", 60); data.inertia = getValue(constraintMap, "inertia", 1); data.strength = getValue(constraintMap, "strength", 100); data.damping = getValue(constraintMap, "damping", 1); data.massInverse = 1 / getValue(constraintMap, "mass", 1); data.wind = getValue(constraintMap, "wind", 0); data.gravity = getValue(constraintMap, "gravity", 0); data.mix = getValue(constraintMap, "mix", 1); data.inertiaGlobal = getValue(constraintMap, "inertiaGlobal", false); data.strengthGlobal = getValue(constraintMap, "strengthGlobal", false); data.dampingGlobal = getValue(constraintMap, "dampingGlobal", false); data.massGlobal = getValue(constraintMap, "massGlobal", false); data.windGlobal = getValue(constraintMap, "windGlobal", false); data.gravityGlobal = getValue(constraintMap, "gravityGlobal", false); data.mixGlobal = getValue(constraintMap, "mixGlobal", false); skeletonData.physicsConstraints.push(data); } } // Skins. if (root.skins) { for (let i = 0; i < root.skins.length; i++) { let skinMap = root.skins[i]; let skin = new Skin(skinMap.name); if (skinMap.bones) { for (let ii = 0; ii < skinMap.bones.length; ii++) { let boneName = skinMap.bones[ii]; let bone = skeletonData.findBone(boneName); if (!bone) throw new Error(`Couldn't find bone ${boneName} for skin ${skinMap.name}.`); skin.bones.push(bone); } } if (skinMap.ik) { for (let ii = 0; ii < skinMap.ik.length; ii++) { let constraintName = skinMap.ik[ii]; let constraint = skeletonData.findIkConstraint(constraintName); if (!constraint) throw new Error(`Couldn't find IK constraint ${constraintName} for skin ${skinMap.name}.`); skin.constraints.push(constraint); } } if (skinMap.transform) { for (let ii = 0; ii < skinMap.transform.length; ii++) { let constraintName = skinMap.transform[ii]; let constraint = skeletonData.findTransformConstraint(constraintName); if (!constraint) throw new Error(`Couldn't find transform constraint ${constraintName} for skin ${skinMap.name}.`); skin.constraints.push(constraint); } } if (skinMap.path) { for (let ii = 0; ii < skinMap.path.length; ii++) { let constraintName = skinMap.path[ii]; let constraint = skeletonData.findPathConstraint(constraintName); if (!constraint) throw new Error(`Couldn't find path constraint ${constraintName} for skin ${skinMap.name}.`); skin.constraints.push(constraint); } } if (skinMap.physics) { for (let ii = 0; ii < skinMap.physics.length; ii++) { let constraintName = skinMap.physics[ii]; let constraint = skeletonData.findPhysicsConstraint(constraintName); if (!constraint) throw new Error(`Couldn't find physics constraint ${constraintName} for skin ${skinMap.name}.`); skin.constraints.push(constraint); } } for (let slotName in skinMap.attachments) { let slot = skeletonData.findSlot(slotName); if (!slot) throw new Error(`Couldn't find slot ${slotName} for skin ${skinMap.name}.`); let slotMap = skinMap.attachments[slotName]; for (let entryName in slotMap) { let attachment = this.readAttachment(slotMap[entryName], skin, slot.index, entryName, skeletonData); if (attachment) skin.setAttachment(slot.index, entryName, attachment); } } skeletonData.skins.push(skin); if (skin.name == "default") skeletonData.defaultSkin = skin; } } // Linked meshes. for (let i = 0, n = this.linkedMeshes.length; i < n; i++) { let linkedMesh = this.linkedMeshes[i]; let skin = !linkedMesh.skin ? skeletonData.defaultSkin : skeletonData.findSkin(linkedMesh.skin); if (!skin) throw new Error(`Skin not found: ${linkedMesh.skin}`); let parent = skin.getAttachment(linkedMesh.slotIndex, linkedMesh.parent); if (!parent) throw new Error(`Parent mesh not found: ${linkedMesh.parent}`); linkedMesh.mesh.timelineAttachment = linkedMesh.inheritTimeline ? parent : linkedMesh.mesh; linkedMesh.mesh.setParentMesh(parent); if (linkedMesh.mesh.region != null) linkedMesh.mesh.updateRegion(); } this.linkedMeshes.length = 0; // Events. if (root.events) { for (let eventName in root.events) { let eventMap = root.events[eventName]; let data = new EventData(eventName); data.intValue = getValue(eventMap, "int", 0); data.floatValue = getValue(eventMap, "float", 0); data.stringValue = getValue(eventMap, "string", ""); data.audioPath = getValue(eventMap, "audio", null); if (data.audioPath) { data.volume = getValue(eventMap, "volume", 1); data.balance = getValue(eventMap, "balance", 0); } skeletonData.events.push(data); } } // Animations. if (root.animations) { for (let animationName in root.animations) { let animationMap = root.animations[animationName]; this.readAnimation(animationMap, animationName, skeletonData); } } return skeletonData; } readAttachment(map, skin, slotIndex, name, skeletonData) { let scale = this.scale; name = getValue(map, "name", name); switch (getValue(map, "type", "region")) { case "region": { let path = getValue(map, "path", name); let sequence = this.readSequence(getValue(map, "sequence", null)); let region = this.attachmentLoader.newRegionAttachment(skin, name, path, sequence); if (!region) return null; region.path = path; region.x = getValue(map, "x", 0) * scale; region.y = getValue(map, "y", 0) * scale; region.scaleX = getValue(map, "scaleX", 1); region.scaleY = getValue(map, "scaleY", 1); region.rotation = getValue(map, "rotation", 0); region.width = map.width * scale; region.height = map.height * scale; region.sequence = sequence; let color = getValue(map, "color", null); if (color) region.color.setFromString(color); if (region.region != null) region.updateRegion(); return region; } case "boundingbox": { let box = this.attachmentLoader.newBoundingBoxAttachment(skin, name); if (!box) return null; this.readVertices(map, box, map.vertexCount << 1); let color = getValue(map, "color", null); if (color) box.color.setFromString(color); return box; } case "mesh": case "linkedmesh": { let path = getValue(map, "path", name); let sequence = this.readSequence(getValue(map, "sequence", null)); let mesh = this.attachmentLoader.newMeshAttachment(skin, name, path, sequence); if (!mesh) return null; mesh.path = path; let color = getValue(map, "color", null); if (color) mesh.color.setFromString(color); mesh.width = getValue(map, "width", 0) * scale; mesh.height = getValue(map, "height", 0) * scale; mesh.sequence = sequence; let parent = getValue(map, "parent", null); if (parent) { this.linkedMeshes.push(new LinkedMesh(mesh, getValue(map, "skin", null), slotIndex, parent, getValue(map, "timelines", true))); return mesh; } let uvs = map.uvs; this.readVertices(map, mesh, uvs.length); mesh.triangles = map.triangles; mesh.regionUVs = uvs; if (mesh.region != null) mesh.updateRegion(); mesh.edges = getValue(map, "edges", null); mesh.hullLength = getValue(map, "hull", 0) * 2; return mesh; } case "path": { let path = this.attachmentLoader.newPathAttachment(skin, name); if (!path) return null; path.closed = getValue(map, "closed", false); path.constantSpeed = getValue(map, "constantSpeed", true); let vertexCount = map.vertexCount; this.readVertices(map, path, vertexCount << 1); let lengths = Utils.newArray(vertexCount / 3, 0); for (let i = 0; i < map.lengths.length; i++) lengths[i] = map.lengths[i] * scale; path.lengths = lengths; let color = getValue(map, "color", null); if (color) path.color.setFromString(color); return path; } case "point": { let point = this.attachmentLoader.newPointAttachment(skin, name); if (!point) return null; point.x = getValue(map, "x", 0) * scale; point.y = getValue(map, "y", 0) * scale; point.rotation = getValue(map, "rotation", 0); let color = getValue(map, "color", null); if (color) point.color.setFromString(color); return point; } case "clipping": { let clip = this.attachmentLoader.newClippingAttachment(skin, name); if (!clip) return null; let end = getValue(map, "end", null); if (end) clip.endSlot = skeletonData.findSlot(end); let vertexCount = map.vertexCount; this.readVertices(map, clip, vertexCount << 1); let color = getValue(map, "color", null); if (color) clip.color.setFromString(color); return clip; } } return null; } readSequence(map) { if (map == null) return null; let sequence = new Sequence(getValue(map, "count", 0)); sequence.start = getValue(map, "start", 1); sequence.digits = getValue(map, "digits", 0); sequence.setupIndex = getValue(map, "setup", 0); return sequence; } readVertices(map, attachment, verticesLength) { let scale = this.scale; attachment.worldVerticesLength = verticesLength; let vertices = map.vertices; if (verticesLength == vertices.length) { let scaledVertices = Utils.toFloatArray(vertices); if (scale != 1) { for (let i = 0, n = vertices.length; i < n; i++) scaledVertices[i] *= scale; } attachment.vertices = scaledVertices; return; } let weights = new Array(); let bones = new Array(); for (let i = 0, n = vertices.length; i < n;) { let boneCount = vertices[i++]; bones.push(boneCount); for (let nn = i + boneCount * 4; i < nn; i += 4) { bones.push(vertices[i]); weights.push(vertices[i + 1] * scale); weights.push(vertices[i + 2] * scale); weights.push(vertices[i + 3]); } } attachment.bones = bones; attachment.vertices = Utils.toFloatArray(weights); } readAnimation(map, name, skeletonData) { let scale = this.scale; let timelines = new Array(); // Slot timelines. if (map.slots) { for (let slotName in map.slots) { let slotMap = map.slots[slotName]; let slot = skeletonData.findSlot(slotName); if (!slot) throw new Error("Slot not found: " + slotName); let slotIndex = slot.index; for (let timelineName in slotMap) { let timelineMap = slotMap[timelineName]; if (!timelineMap) continue; let frames = timelineMap.length; if (timelineName == "attachment") { let timeline = new AttachmentTimeline(frames, slotIndex); for (let frame = 0; frame < frames; frame++) { let keyMap = timelineMap[frame]; timeline.setFrame(frame, getValue(keyMap, "time", 0), getValue(keyMap, "name", null)); } timelines.push(timeline); } else if (timelineName == "rgba") { let timeline = new RGBATimeline(frames, frames << 2, slotIndex); let keyMap = timelineMap[0]; let time = getValue(keyMap, "time", 0); let color = Color.fromString(keyMap.color); for (let frame = 0, bezier = 0;; frame++) { timeline.setFrame(frame, time, color.r, color.g, color.b, color.a); let nextMap = timelineMap[frame + 1]; if (!nextMap) { timeline.shrink(bezier); break; } let time2 = getValue(nextMap, "time", 0); let newColor = Color.fromString(nextMap.color); let curve = keyMap.curve; if (curve) { bezier = readCurve(curve, timeline, bezier, frame, 0, time, time2, color.r, newColor.r, 1); bezier = readCurve(curve, timeline, bezier, frame, 1, time, time2, color.g, newColor.g, 1); bezier = readCurve(curve, timeline, bezier, frame, 2, time, time2, color.b, newColor.b, 1); bezier = readCurve(curve, timeline, bezier, frame, 3, time, time2, color.a, newColor.a, 1); } time = time2; color = newColor; keyMap = nextMap; } timelines.push(timeline); } else if (timelineName == "rgb") { let timeline = new RGBTimeline(frames, frames * 3, slotIndex); let keyMap = timelineMap[0]; let time = getValue(keyMap, "time", 0); let color = Color.fromString(keyMap.color); for (let frame = 0, bezier = 0;; frame++) { timeline.setFrame(frame, time, color.r, color.g, color.b); let nextMap = timelineMap[frame + 1]; if (!nextMap) { timeline.shrink(bezier); break; } let time2 = getValue(nextMap, "time", 0); let newColor = Color.fromString(nextMap.color); let curve = keyMap.curve; if (curve) { bezier = readCurve(curve, timeline, bezier, frame, 0, time, time2, color.r, newColor.r, 1); bezier = readCurve(curve, timeline, bezier, frame, 1, time, time2, color.g, newColor.g, 1); bezier = readCurve(curve, timeline, bezier, frame, 2, time, time2, color.b, newColor.b, 1); } time = time2; color = newColor; keyMap = nextMap; } timelines.push(timeline); } else if (timelineName == "alpha") { timelines.push(readTimeline1(timelineMap, new AlphaTimeline(frames, frames, slotIndex), 0, 1)); } else if (timelineName == "rgba2") { let timeline = new RGBA2Timeline(frames, frames * 7, slotIndex); let keyMap = timelineMap[0]; let time = getValue(keyMap, "time", 0); let color = Color.fromString(keyMap.light); let color2 = Color.fromString(keyMap.dark); for (let frame = 0, bezier = 0;; frame++) { timeline.setFrame(frame, time, color.r, color.g, color.b, color.a, color2.r, color2.g, color2.b); let nextMap = timelineMap[frame + 1]; if (!nextMap) { timeline.shrink(bezier); break; } let time2 = getValue(nextMap, "time", 0); let newColor = Color.fromString(nextMap.light); let newColor2 = Color.fromString(nextMap.dark); let curve = keyMap.curve; if (curve) { bezier = readCurve(curve, timeline, bezier, frame, 0, time, time2, color.r, newColor.r, 1); bezier = readCurve(curve, timeline, bezier, frame, 1, time, time2, color.g, newColor.g, 1); bezier = readCurve(curve, timeline, bezier, frame, 2, time, time2, color.b, newColor.b, 1); bezier = readCurve(curve, timeline, bezier, frame, 3, time, time2, color.a, newColor.a, 1); bezier = readCurve(curve, timeline, bezier, frame, 4, time, time2, color2.r, newColor2.r, 1); bezier = readCurve(curve, timeline, bezier, frame, 5, time, time2, color2.g, newColor2.g, 1); bezier = readCurve(curve, timeline, bezier, frame, 6, time, time2, color2.b, newColor2.b, 1); } time = time2; color = newColor; color2 = newColor2; keyMap = nextMap; } timelines.push(timeline); } else if (timelineName == "rgb2") { let timeline = new RGB2Timeline(frames, frames * 6, slotIndex); let keyMap = timelineMap[0]; let time = getValue(keyMap, "time", 0); let color = Color.fromString(keyMap.light); let color2 = Color.fromString(keyMap.dark); for (let frame = 0, bezier = 0;; frame++) { timeline.setFrame(frame, time, color.r, color.g, color.b, color2.r, color2.g, color2.b); let nextMap = timelineMap[frame + 1]; if (!nextMap) { timeline.shrink(bezier); break; } let time2 = getValue(nextMap, "time", 0); let newColor = Color.fromString(nextMap.light); let newColor2 = Color.fromString(nextMap.dark); let curve = keyMap.curve; if (curve) { bezier = readCurve(curve, timeline, bezier, frame, 0, time, time2, color.r, newColor.r, 1); bezier = readCurve(curve, timeline, bezier, frame, 1, time, time2, color.g, newColor.g, 1); bezier = readCurve(curve, timeline, bezier, frame, 2, time, time2, color.b, newColor.b, 1); bezier = readCurve(curve, timeline, bezier, frame, 3, time, time2, color2.r, newColor2.r, 1); bezier = readCurve(curve, timeline, bezier, frame, 4, time, time2, color2.g, newColor2.g, 1); bezier = readCurve(curve, timeline, bezier, frame, 5, time, time2, color2.b, newColor2.b, 1); } time = time2; color = newColor; color2 = newColor2; keyMap = nextMap; } timelines.push(timeline); } } } } // Bone timelines. if (map.bones) { for (let boneName in map.bones) { let boneMap = map.bones[boneName]; let bone = skeletonData.findBone(boneName); if (!bone) throw new Error("Bone not found: " + boneName); let boneIndex = bone.index; for (let timelineName in boneMap) { let timelineMap = boneMap[timelineName]; let frames = timelineMap.length; if (frames == 0) continue; if (timelineName === "rotate") { timelines.push(readTimeline1(timelineMap, new RotateTimeline(frames, frames, boneIndex), 0, 1)); } else if (timelineName === "translate") { let timeline = new TranslateTimeline(frames, frames << 1, boneIndex); timelines.push(readTimeline2(timelineMap, timeline, "x", "y", 0, scale)); } else if (timelineName === "translatex") { let timeline = new TranslateXTimeline(frames, frames, boneIndex); timelines.push(readTimeline1(timelineMap, timeline, 0, scale)); } else if (timelineName === "translatey") { let timeline = new TranslateYTimeline(frames, frames, boneIndex); timelines.push(readTimeline1(timelineMap, timeline, 0, scale)); } else if (timelineName === "scale") { let timeline = new ScaleTimeline(frames, frames << 1, boneIndex); timelines.push(readTimeline2(timelineMap, timeline, "x", "y", 1, 1)); } else if (timelineName === "scalex") { let timeline = new ScaleXTimeline(frames, frames, boneIndex); timelines.push(readTimeline1(timelineMap, timeline, 1, 1)); } else if (timelineName === "scaley") { let timeline = new ScaleYTimeline(frames, frames, boneIndex); timelines.push(readTimeline1(timelineMap, timeline, 1, 1)); } else if (timelineName === "shear") { let timeline = new ShearTimeline(frames, frames << 1, boneIndex); timelines.push(readTimeline2(timelineMap, timeline, "x", "y", 0, 1)); } else if (timelineName === "shearx") { let timeline = new ShearXTimeline(frames, frames, boneIndex); timelines.push(readTimeline1(timelineMap, timeline, 0, 1)); } else if (timelineName === "sheary") { let timeline = new ShearYTimeline(frames, frames, boneIndex); timelines.push(readTimeline1(timelineMap, timeline, 0, 1)); } else if (timelineName === "inherit") { let timeline = new InheritTimeline(frames, bone.index); for (let frame = 0; frame < timelineMap.length; frame++) { let aFrame = timelineMap[frame]; timeline.setFrame(frame, getValue(aFrame, "time", 0), Utils.enumValue(Inherit, getValue(aFrame, "inherit", "Normal"))); } timelines.push(timeline); } } } } // IK constraint timelines. if (map.ik) { for (let constraintName in map.ik) { let constraintMap = map.ik[constraintName]; let keyMap = constraintMap[0]; if (!keyMap) continue; let constraint = skeletonData.findIkConstraint(constraintName); if (!constraint) throw new Error("IK Constraint not found: " + constraintName); let constraintIndex = skeletonData.ikConstraints.indexOf(constraint); let timeline = new IkConstraintTimeline(constraintMap.length, constraintMap.length << 1, constraintIndex); let time = getValue(keyMap, "time", 0); let mix = getValue(keyMap, "mix", 1); let softness = getValue(keyMap, "softness", 0) * scale; for (let frame = 0, bezier = 0;; frame++) { timeline.setFrame(frame, time, mix, softness, getValue(keyMap, "bendPositive", true) ? 1 : -1, getValue(keyMap, "compress", false), getValue(keyMap, "stretch", false)); let nextMap = constraintMap[frame + 1]; if (!nextMap) { timeline.shrink(bezier); break; } let time2 = getValue(nextMap, "time", 0); let mix2 = getValue(nextMap, "mix", 1); let softness2 = getValue(nextMap, "softness", 0) * scale; let curve = keyMap.curve; if (curve) { bezier = readCurve(curve, timeline, bezier, frame, 0, time, time2, mix, mix2, 1); bezier = readCurve(curve, timeline, bezier, frame, 1, time, time2, softness, softness2, scale); } time = time2; mix = mix2; softness = softness2; keyMap = nextMap; } timelines.push(timeline); } } // Transform constraint timelines. if (map.transform) { for (let constraintName in map.transform) { let timelineMap = map.transform[constraintName]; let keyMap = timelineMap[0]; if (!keyMap) continue; let constraint = skeletonData.findTransformConstraint(constraintName); if (!constraint) throw new Error("Transform constraint not found: " + constraintName); let constraintIndex = skeletonData.transformConstraints.indexOf(constraint); let timeline = new TransformConstraintTimeline(timelineMap.length, timelineMap.length * 6, constraintIndex); let time = getValue(keyMap, "time", 0); let mixRotate = getValue(keyMap, "mixRotate", 1); let mixX = getValue(keyMap, "mixX", 1); let mixY = getValue(keyMap, "mixY", mixX); let mixScaleX = getValue(keyMap, "mixScaleX", 1); let mixScaleY = getValue(keyMap, "mixScaleY", mixScaleX); let mixShearY = getValue(keyMap, "mixShearY", 1); for (let frame = 0, bezier = 0;; frame++) { timeline.setFrame(frame, time, mixRotate, mixX, mixY, mixScaleX, mixScaleY, mixShearY); let nextMap = timelineMap[frame + 1]; if (!nextMap) { timeline.shrink(bezier); break; } let time2 = getValue(nextMap, "time", 0); let mixRotate2 = getValue(nextMap, "mixRotate", 1); let mixX2 = getValue(nextMap, "mixX", 1); let mixY2 = getValue(nextMap, "mixY", mixX2); let mixScaleX2 = getValue(nextMap, "mixScaleX", 1); let mixScaleY2 = getValue(nextMap, "mixScaleY", mixScaleX2); let mixShearY2 = getValue(nextMap, "mixShearY", 1); let curve = keyMap.curve; if (curve) { bezier = readCurve(curve, timeline, bezier, frame, 0, time, time2, mixRotate, mixRotate2, 1); bezier = readCurve(curve, timeline, bezier, frame, 1, time, time2, mixX, mixX2, 1); bezier = readCurve(curve, timeline, bezier, frame, 2, time, time2, mixY, mixY2, 1); bezier = readCurve(curve, timeline, bezier, frame, 3, time, time2, mixScaleX, mixScaleX2, 1); bezier = readCurve(curve, timeline, bezier, frame, 4, time, time2, mixScaleY, mixScaleY2, 1); bezier = readCurve(curve, timeline, bezier, frame, 5, time, time2, mixShearY, mixShearY2, 1); } time = time2; mixRotate = mixRotate2; mixX = mixX2; mixY = mixY2; mixScaleX = mixScaleX2; mixScaleY = mixScaleY2; mixScaleX = mixScaleX2; keyMap = nextMap; } timelines.push(timeline); } } // Path constraint timelines. if (map.path) { for (let constraintName in map.path) { let constraintMap = map.path[constraintName]; let constraint = skeletonData.findPathConstraint(constraintName); if (!constraint) throw new Error("Path constraint not found: " + constraintName); let constraintIndex = skeletonData.pathConstraints.indexOf(constraint); for (let timelineName in constraintMap) { let timelineMap = constraintMap[timelineName]; let keyMap = timelineMap[0]; if (!keyMap) continue; let frames = timelineMap.length; if (timelineName === "position") { let timeline = new PathConstraintPositionTimeline(frames, frames, constraintIndex); timelines.push(readTimeline1(timelineMap, timeline, 0, constraint.positionMode == PositionMode.Fixed ? scale : 1)); } else if (timelineName === "spacing") { let timeline = new PathConstraintSpacingTimeline(frames, frames, constraintIndex); timelines.push(readTimeline1(timelineMap, timeline, 0, constraint.spacingMode == SpacingMode.Length || constraint.spacingMode == SpacingMode.Fixed ? scale : 1)); } else if (timelineName === "mix") { let timeline = new PathConstraintMixTimeline(frames, frames * 3, constraintIndex); let time = getValue(keyMap, "time", 0); let mixRotate = getValue(keyMap, "mixRotate", 1); let mixX = getValue(keyMap, "mixX", 1); let mixY = getValue(keyMap, "mixY", mixX); for (let frame = 0, bezier = 0;; frame++) { timeline.setFrame(frame, time, mixRotate, mixX, mixY); let nextMap = timelineMap[frame + 1]; if (!nextMap) { timeline.shrink(bezier); break; } let time2 = getValue(nextMap, "time", 0); let mixRotate2 = getValue(nextMap, "mixRotate", 1); let mixX2 = getValue(nextMap, "mixX", 1); let mixY2 = getValue(nextMap, "mixY", mixX2); let curve = keyMap.curve; if (curve) { bezier = readCurve(curve, timeline, bezier, frame, 0, time, time2, mixRotate, mixRotate2, 1); bezier = readCurve(curve, timeline, bezier, frame, 1, time, time2, mixX, mixX2, 1); bezier = readCurve(curve, timeline, bezier, frame, 2, time, time2, mixY, mixY2, 1); } time = time2; mixRotate = mixRotate2; mixX = mixX2; mixY = mixY2; keyMap = nextMap; } timelines.push(timeline); } } } } // Physics constraint timelines. if (map.physics) { for (let constraintName in map.physics) { let constraintMap = map.physics[constraintName]; let constraintIndex = -1; if (constraintName.length > 0) { let constraint = skeletonData.findPhysicsConstraint(constraintName); if (!constraint) throw new Error("Physics constraint not found: " + constraintName); constraintIndex = skeletonData.physicsConstraints.indexOf(constraint); } for (let timelineName in constraintMap) { let timelineMap = constraintMap[timelineName]; let keyMap = timelineMap[0]; if (!keyMap)