// Calculates the texture range, offset, pixel offset and width of the animation data private void DataRangeOffsetWidth(CG_KeyframeBaker.AnimationData animData, CG_KeyframeBaker.AnimationClipData animClipData, out float range, out float textureOfset, out float texturePixelOffset, out int textureWidth) { float start = (float)animClipData.textureStart / animData.textures[0].width + (1f / animData.textures[0].width) * 0.5f; float end = (float)animClipData.textureEnd / animData.textures[0].width + (1f / animData.textures[0].width) * 0.5f; texturePixelOffset = (1f / animData.textures[0].width); textureWidth = animData.textures[0].width; range = end - start; textureOfset = start; }
// Initialise the texture animator private void InitializeTextureAnimator() { // if initialised, return to stop multiple initialisationss if (bInit) { return; } // Create the clip baked data variable with a persistant allocator animationClipBakedData = new NativeArray <AnimationClipDataBaked>(AnimArraySize, Allocator.Persistent); // Get the characterSkining prefab located in the game manager GameObject charPrefab = CG_GameManager.Instance.GetCharacterPrefab(); // if a character prefab exists if (charPrefab) { // Get the character data CG_CharacterSkinner charSkinner = charPrefab.GetComponentInChildren <CG_CharacterSkinner>(); GameObject bakingChar = GameObject.Instantiate(charSkinner.characterToSkin); SkinnedMeshRenderer characterSkinnedMesh = bakingChar.GetComponentInChildren <SkinnedMeshRenderer>(); // Get the baking data from the skinned mesh renderer of the character and the material BakedData = CG_KeyframeBaker.BakeAnimation(characterSkinnedMesh); Material = charSkinner.characterMat; // initilise the character drawer class Drawer = new CG_CharacterDrawer(this, BakedData.NewMesh); // loop through the number of animation clips for (int i = 0; i < BakedData.animationClipData.Count; i++) { // set the animation clip data to each animation AnimationClipDataBaked data = new AnimationClipDataBaked(); data.animationLength = BakedData.animationClipData[i].Clip.length; // Get the texture range and offset and return the range, offset, pixel offset and widths of the texture DataRangeOffsetWidth(BakedData, BakedData.animationClipData[i], out data.textureRange, out data.textureOfset, out data.texturePixelOffset, out data.textureWidth); // Get whether the animation should loop data.bAnimationLoops = BakedData.animationClipData[i].Clip.isLooping; animationClipBakedData[(int)0 * 25 + i] = data; } // Destroy the baking character GameObject.Destroy(bakingChar); } // Initialisation is complete bInit = true; }