726488d7e1161f844b69c33a3326bd1ba1239354284e0263fd8bcd52f0db1da2.json 16 KB

1
  1. {"ast":null,"code":"import _asyncToGenerator from \"F:/workspace/202226701027/huinongbao-app/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js\";\nimport { RawTexture } from \"../Materials/Textures/rawTexture.js\";\nimport { Texture } from \"../Materials/Textures/texture.js\";\nimport { EncodeArrayBufferToBase64, DecodeBase64ToBinary } from \"../Misc/stringTools.js\";\nimport { Skeleton } from \"../Bones/skeleton.js\";\n/**\n * Class to bake vertex animation textures.\n * @since 5.0\n */\nexport class VertexAnimationBaker {\n /**\n * Create a new VertexAnimationBaker object which can help baking animations into a texture.\n * @param scene Defines the scene the VAT belongs to\n * @param meshOrSkeleton Defines the skeleton or the mesh from which to retrieve the skeleton from.\n */\n constructor(scene, meshOrSkeleton) {\n this._scene = scene;\n if (meshOrSkeleton instanceof Skeleton) {\n this._skeleton = meshOrSkeleton;\n this._mesh = null;\n } else {\n this._mesh = meshOrSkeleton;\n this._skeleton = meshOrSkeleton.skeleton;\n }\n }\n /**\n * Bakes the animation into the texture. This should be called once, when the\n * scene starts, so the VAT is generated and associated to the mesh.\n * @param ranges Defines the ranges in the animation that will be baked.\n * @returns The array of matrix transforms for each vertex (columns) and frame (rows), as a Float32Array.\n */\n bakeVertexData(ranges) {\n var _this = this;\n return _asyncToGenerator(function* () {\n if (!_this._skeleton) {\n throw new Error(\"No skeleton provided.\");\n }\n const boneCount = _this._skeleton.bones.length;\n /** total number of frames in our animations */\n const frameCount = ranges.reduce((previous, current) => previous + current.to - current.from + 1, 0);\n if (isNaN(frameCount)) {\n throw new Error(\"Invalid animation ranges.\");\n }\n // reset our loop data\n let textureIndex = 0;\n const textureSize = (boneCount + 1) * 4 * 4 * frameCount;\n const vertexData = new Float32Array(textureSize);\n _this._scene.stopAnimation(_this._skeleton);\n _this._skeleton.returnToRest();\n // render all frames from our slices\n for (const range of ranges) {\n for (let frameIndex = range.from; frameIndex <= range.to; frameIndex++) {\n yield _this._executeAnimationFrame(vertexData, frameIndex, textureIndex++);\n }\n }\n return vertexData;\n })();\n }\n /**\n * Runs an animation frame and stores its vertex data\n *\n * @param vertexData The array to save data to.\n * @param frameIndex Current frame in the skeleton animation to render.\n * @param textureIndex Current index of the texture data.\n */\n _executeAnimationFrame(vertexData, frameIndex, textureIndex) {\n var _this2 = this;\n return _asyncToGenerator(function* () {\n return new Promise((resolve, _reject) => {\n _this2._scene.beginAnimation(_this2._skeleton, frameIndex, frameIndex, false, 1.0, () => {\n // generate matrices\n const skeletonMatrices = _this2._skeleton.getTransformMatrices(_this2._mesh);\n vertexData.set(skeletonMatrices, textureIndex * skeletonMatrices.length);\n resolve();\n });\n });\n })();\n }\n /**\n * Builds a vertex animation texture given the vertexData in an array.\n * @param vertexData The vertex animation data. You can generate it with bakeVertexData().\n * @returns The vertex animation texture to be used with BakedVertexAnimationManager.\n */\n textureFromBakedVertexData(vertexData) {\n if (!this._skeleton) {\n throw new Error(\"No skeleton provided.\");\n }\n const boneCount = this._skeleton.bones.length;\n const texture = RawTexture.CreateRGBATexture(vertexData, (boneCount + 1) * 4, vertexData.length / ((boneCount + 1) * 4 * 4), this._scene, false, false, Texture.NEAREST_NEAREST, 1);\n texture.name = \"VAT\" + this._skeleton.name;\n return texture;\n }\n /**\n * Serializes our vertexData to an object, with a nice string for the vertexData.\n * @param vertexData The vertex array data.\n * @returns This object serialized to a JS dict.\n */\n serializeBakedVertexDataToObject(vertexData) {\n if (!this._skeleton) {\n throw new Error(\"No skeleton provided.\");\n }\n // this converts the float array to a serialized base64 string, ~1.3x larger\n // than the original.\n const boneCount = this._skeleton.bones.length;\n const width = (boneCount + 1) * 4;\n const height = vertexData.length / ((boneCount + 1) * 4 * 4);\n const data = {\n vertexData: EncodeArrayBufferToBase64(vertexData),\n width,\n height\n };\n return data;\n }\n /**\n * Loads previously baked data.\n * @param data The object as serialized by serializeBakedVertexDataToObject()\n * @returns The array of matrix transforms for each vertex (columns) and frame (rows), as a Float32Array.\n */\n loadBakedVertexDataFromObject(data) {\n return new Float32Array(DecodeBase64ToBinary(data.vertexData));\n }\n /**\n * Serializes our vertexData to a JSON string, with a nice string for the vertexData.\n * Should be called right after bakeVertexData().\n * @param vertexData The vertex array data.\n * @returns This object serialized to a safe string.\n */\n serializeBakedVertexDataToJSON(vertexData) {\n return JSON.stringify(this.serializeBakedVertexDataToObject(vertexData));\n }\n /**\n * Loads previously baked data in string format.\n * @param json The json string as serialized by serializeBakedVertexDataToJSON().\n * @returns The array of matrix transforms for each vertex (columns) and frame (rows), as a Float32Array.\n */\n loadBakedVertexDataFromJSON(json) {\n return this.loadBakedVertexDataFromObject(JSON.parse(json));\n }\n}","map":{"version":3,"names":["RawTexture","Texture","EncodeArrayBufferToBase64","DecodeBase64ToBinary","Skeleton","VertexAnimationBaker","constructor","scene","meshOrSkeleton","_scene","_skeleton","_mesh","skeleton","bakeVertexData","ranges","_this","_asyncToGenerator","Error","boneCount","bones","length","frameCount","reduce","previous","current","to","from","isNaN","textureIndex","textureSize","vertexData","Float32Array","stopAnimation","returnToRest","range","frameIndex","_executeAnimationFrame","_this2","Promise","resolve","_reject","beginAnimation","skeletonMatrices","getTransformMatrices","set","textureFromBakedVertexData","texture","CreateRGBATexture","NEAREST_NEAREST","name","serializeBakedVertexDataToObject","width","height","data","loadBakedVertexDataFromObject","serializeBakedVertexDataToJSON","JSON","stringify","loadBakedVertexDataFromJSON","json","parse"],"sources":["F:/workspace/202226701027/huinongbao-app/node_modules/@babylonjs/core/BakedVertexAnimation/vertexAnimationBaker.js"],"sourcesContent":["import { RawTexture } from \"../Materials/Textures/rawTexture.js\";\nimport { Texture } from \"../Materials/Textures/texture.js\";\nimport { EncodeArrayBufferToBase64, DecodeBase64ToBinary } from \"../Misc/stringTools.js\";\n\nimport { Skeleton } from \"../Bones/skeleton.js\";\n/**\n * Class to bake vertex animation textures.\n * @since 5.0\n */\nexport class VertexAnimationBaker {\n /**\n * Create a new VertexAnimationBaker object which can help baking animations into a texture.\n * @param scene Defines the scene the VAT belongs to\n * @param meshOrSkeleton Defines the skeleton or the mesh from which to retrieve the skeleton from.\n */\n constructor(scene, meshOrSkeleton) {\n this._scene = scene;\n if (meshOrSkeleton instanceof Skeleton) {\n this._skeleton = meshOrSkeleton;\n this._mesh = null;\n }\n else {\n this._mesh = meshOrSkeleton;\n this._skeleton = meshOrSkeleton.skeleton;\n }\n }\n /**\n * Bakes the animation into the texture. This should be called once, when the\n * scene starts, so the VAT is generated and associated to the mesh.\n * @param ranges Defines the ranges in the animation that will be baked.\n * @returns The array of matrix transforms for each vertex (columns) and frame (rows), as a Float32Array.\n */\n async bakeVertexData(ranges) {\n if (!this._skeleton) {\n throw new Error(\"No skeleton provided.\");\n }\n const boneCount = this._skeleton.bones.length;\n /** total number of frames in our animations */\n const frameCount = ranges.reduce((previous, current) => previous + current.to - current.from + 1, 0);\n if (isNaN(frameCount)) {\n throw new Error(\"Invalid animation ranges.\");\n }\n // reset our loop data\n let textureIndex = 0;\n const textureSize = (boneCount + 1) * 4 * 4 * frameCount;\n const vertexData = new Float32Array(textureSize);\n this._scene.stopAnimation(this._skeleton);\n this._skeleton.returnToRest();\n // render all frames from our slices\n for (const range of ranges) {\n for (let frameIndex = range.from; frameIndex <= range.to; frameIndex++) {\n await this._executeAnimationFrame(vertexData, frameIndex, textureIndex++);\n }\n }\n return vertexData;\n }\n /**\n * Runs an animation frame and stores its vertex data\n *\n * @param vertexData The array to save data to.\n * @param frameIndex Current frame in the skeleton animation to render.\n * @param textureIndex Current index of the texture data.\n */\n async _executeAnimationFrame(vertexData, frameIndex, textureIndex) {\n return new Promise((resolve, _reject) => {\n this._scene.beginAnimation(this._skeleton, frameIndex, frameIndex, false, 1.0, () => {\n // generate matrices\n const skeletonMatrices = this._skeleton.getTransformMatrices(this._mesh);\n vertexData.set(skeletonMatrices, textureIndex * skeletonMatrices.length);\n resolve();\n });\n });\n }\n /**\n * Builds a vertex animation texture given the vertexData in an array.\n * @param vertexData The vertex animation data. You can generate it with bakeVertexData().\n * @returns The vertex animation texture to be used with BakedVertexAnimationManager.\n */\n textureFromBakedVertexData(vertexData) {\n if (!this._skeleton) {\n throw new Error(\"No skeleton provided.\");\n }\n const boneCount = this._skeleton.bones.length;\n const texture = RawTexture.CreateRGBATexture(vertexData, (boneCount + 1) * 4, vertexData.length / ((boneCount + 1) * 4 * 4), this._scene, false, false, Texture.NEAREST_NEAREST, 1);\n texture.name = \"VAT\" + this._skeleton.name;\n return texture;\n }\n /**\n * Serializes our vertexData to an object, with a nice string for the vertexData.\n * @param vertexData The vertex array data.\n * @returns This object serialized to a JS dict.\n */\n serializeBakedVertexDataToObject(vertexData) {\n if (!this._skeleton) {\n throw new Error(\"No skeleton provided.\");\n }\n // this converts the float array to a serialized base64 string, ~1.3x larger\n // than the original.\n const boneCount = this._skeleton.bones.length;\n const width = (boneCount + 1) * 4;\n const height = vertexData.length / ((boneCount + 1) * 4 * 4);\n const data = {\n vertexData: EncodeArrayBufferToBase64(vertexData),\n width,\n height,\n };\n return data;\n }\n /**\n * Loads previously baked data.\n * @param data The object as serialized by serializeBakedVertexDataToObject()\n * @returns The array of matrix transforms for each vertex (columns) and frame (rows), as a Float32Array.\n */\n loadBakedVertexDataFromObject(data) {\n return new Float32Array(DecodeBase64ToBinary(data.vertexData));\n }\n /**\n * Serializes our vertexData to a JSON string, with a nice string for the vertexData.\n * Should be called right after bakeVertexData().\n * @param vertexData The vertex array data.\n * @returns This object serialized to a safe string.\n */\n serializeBakedVertexDataToJSON(vertexData) {\n return JSON.stringify(this.serializeBakedVertexDataToObject(vertexData));\n }\n /**\n * Loads previously baked data in string format.\n * @param json The json string as serialized by serializeBakedVertexDataToJSON().\n * @returns The array of matrix transforms for each vertex (columns) and frame (rows), as a Float32Array.\n */\n loadBakedVertexDataFromJSON(json) {\n return this.loadBakedVertexDataFromObject(JSON.parse(json));\n }\n}\n"],"mappings":";AAAA,SAASA,UAAU,QAAQ,qCAAqC;AAChE,SAASC,OAAO,Q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A;AACA;EACI4B,2BAA2BA,CAACC,IAAI,EAAE;IAC9B,OAAO,IAAI,CAACL,6BAA6B,CAACE,IAAI,CAACI,KAAK,CAACD,IAAI,CAAC,CAAC;EAC/D;AACJ","ignoreList":[]},"metadata":{},"sourceType":"module","externalDependencies":[]}