public void Execute(int i) { // CHECK: We can't modify values inside of a struct directly? var animatorData = textureAnimatorData[i]; if (animatorData.CurrentAnimationId != animatorData.NewAnimationId) { animatorData.CurrentAnimationId = animatorData.NewAnimationId; animatorData.AnimationNormalizedTime = 0f; } AnimationClipDataBaked clip = animationClips[(int)animatorData.UnitType * 25 + animatorData.CurrentAnimationId]; float normalizedTime = textureAnimatorData[i].AnimationNormalizedTime + dt / clip.AnimationLength; if (normalizedTime > 1.0f) { if (clip.Looping) { normalizedTime = normalizedTime % 1.0f; } else { normalizedTime = 1f; } } animatorData.AnimationNormalizedTime = normalizedTime; textureAnimatorData[i] = animatorData; }
private void TransferAnimationData() { for (int i = 0; i < bakedData.Animations.Count; i++) { AnimationClipDataBaked data = new AnimationClipDataBaked(); data.AnimationLength = bakedData.Animations[i].Clip.length; GetTextureRangeAndOffset(bakedData, bakedData.Animations[i], out data.TextureRange, out data.TextureOffset, out data.OnePixelOffset, out data.TextureWidth); //data.Looping = bakedData.Animations[i].Clip.wrapMode == WrapMode.Loop; animationClipData[(int)0 * 25 + i] = data; } }
public void Execute() { //deltaTime += Time.deltaTime; for (int i = 0; i < formationList.Length; ++i) { FormationData formation = formationList[i]; float distance = math.length(CameraPosition - formation.formationPosition); for (int j = 0; j < formation.count(); j++) { float3 center = formation.formationPosition; float3 v3 = new float3(center.x + j % formation.width * 3f, 1, center.z + j / formation.width * 3); float4 v4 = new float4(v3.x, v3.y, v3.z, 1); int index = 1;//Randomizer.Range(0, 6, ref motionDeltaTime); AnimationClipDataBaked clip = animationClips[(int)0 * 25 + index]; normalizedTime = /*normalizedTimeStart*/ 0 + deltaTime / clip.AnimationLength; //Unity.Mathematics.Random r = new Unity.Mathematics.Random((uint)Time.realtimeSinceStartup); float rad = Randomizer.Float(0.1f, 0.3f, ref motionDeltaTime); normalizedTime = (normalizedTime + rad) % 1.0f; float texturePosition = normalizedTime * clip.TextureRange + clip.TextureOffset; int lowerPixelInt = (int)math.floor(texturePosition * clip.TextureWidth); float lowerPixelCenter = (lowerPixelInt * 1.0f) / clip.TextureWidth; float upperPixelCenter = lowerPixelCenter + clip.OnePixelOffset; float lerpFactor = (texturePosition - lowerPixelCenter) / clip.OnePixelOffset; float3 texturePositionData = new float3(lowerPixelCenter, upperPixelCenter, lerpFactor); if (distance < DistanceMaxLod0) { listPositionData0.Add(v4); listRotation0.Add(Quaternion.LookRotation(new Vector3(-90, 9, 0), new Vector3(0.0f, 1, 0.0f))); listTexturePosition0.Add(texturePositionData); } else if (distance < DistanceMaxLod1) { listPositionData1.Add(v4); listRotation1.Add(Quaternion.LookRotation(new Vector3(-90, 8, 0), new Vector3(0.0f, 1, 0.0f))); listTexturePosition1.Add(texturePositionData); } else if (distance < DistanceMaxLod2) { listPositionData2.Add(v4); listRotation2.Add(Quaternion.LookRotation(new Vector3(-90, 7, 0), new Vector3(0.0f, 1, 0.0f))); listTexturePosition2.Add(texturePositionData); } else { listPositionData3.Add(v4); listRotation3.Add(Quaternion.LookRotation(new Vector3(-90, 30, 0), new Vector3(0.0f, 1, 0.0f))); listTexturePosition3.Add(texturePositionData); } } } }
private void TransferAnimationData(UnitType type) { var bakedData = perUnitTypeDataHolder[type].BakedData; for (int i = 0; i < bakedData.Animations.Count; i++) { AnimationClipDataBaked data = new AnimationClipDataBaked(); data.AnimationLength = bakedData.Animations[i].Clip.length; GetTextureRangeAndOffset(bakedData, bakedData.Animations[i], out data.TextureRange, out data.TextureOffset, out data.OnePixelOffset, out data.TextureWidth); data.Looping = bakedData.Animations[i].Clip.wrapMode == WrapMode.Loop; animationClipData[(int)type * 25 + i] = data; } }
// 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; }
public void Execute() { for (int i = 0; i < unitTransformData.Length; i++) { var unitTransform = unitTransformData[i]; float distance = math.length(CameraPosition - unitTransform.Position); var animatorData = textureAnimatorData[i]; AnimationClipDataBaked clip = animationClips[(int)animatorData.UnitType * 25 + animatorData.CurrentAnimationId]; Quaternion rotation = Quaternion.LookRotation(unitTransform.Forward, new Vector3(0.0f, 1.0f, 0.0f)); float texturePosition = textureAnimatorData[i].AnimationNormalizedTime * clip.TextureRange + clip.TextureOffset; int lowerPixelInt = (int)math.floor(texturePosition * clip.TextureWidth); //float lowerPixelCenter = (lowerPixelInt + 0.5f) / clip.TextureWidth; //float upperPixelCenter = lowerPixelCenter + clip.OnePixelOffset; float lowerPixelCenter = (lowerPixelInt * 1.0f) / clip.TextureWidth; float upperPixelCenter = lowerPixelCenter + clip.OnePixelOffset; float lerpFactor = (texturePosition - lowerPixelCenter) / clip.OnePixelOffset; float3 texturePositionData = new float3(lowerPixelCenter, upperPixelCenter, lerpFactor); //float3 texturePositionData = new float3(texturePosition, 0, -1); float4 position = new float4(unitTransform.Position, unitTransform.Scale); if (distance < DistanceMaxLod0) { Lod0Positions.Add(position); Lod0Rotations.Add(rotation); Lod0TexturePositions.Add(texturePositionData); } else if (distance < DistanceMaxLod1) { Lod1Positions.Add(position); Lod1Rotations.Add(rotation); Lod1TexturePositions.Add(texturePositionData); } else if (distance < DistanceMaxLod2) { Lod2Positions.Add(position); Lod2Rotations.Add(rotation); Lod2TexturePositions.Add(texturePositionData); } else { Lod3Positions.Add(position); Lod3Rotations.Add(rotation); Lod3TexturePositions.Add(texturePositionData); } } }
public void Execute() { // Loop through the number of character transform positions for (int i = 0; i < charTransforms.Length; i++) { // Get the current animation and transform based on the character selected AnimationData animation = animData[i]; CharacterTransformData characterTransform = charTransforms[i]; // Get the animation clip from the current animation AnimationClipDataBaked animClip = animClips[animation.animationID]; // The position of the animation position float animTexturePos = animData[i].normalizedAnimationTime * animClip.textureRange + animClip.textureOfset; // The position of centre pixel locations float lowerCentreTexPos = ((int)math.floor(animTexturePos * animClip.textureWidth) * 1.0f) / animClip.textureWidth; float upperCentreTexPos = lowerCentreTexPos + animClip.texturePixelOffset; float3 texturePos = new float3(lowerCentreTexPos, upperCentreTexPos, (animTexturePos - lowerCentreTexPos) / animClip.texturePixelOffset); // Animation transition progress float transitionPercent = animation.transitionPercent; // Pre animation texture (transition) AnimationClipDataBaked preClip = animClips[animation.animationToSwitchID]; float preTexturePosition = animData[i].animationToSwitchNormTime * preClip.textureRange + preClip.textureOfset; int preLowerPixelInt = (int)math.floor(preTexturePosition * preClip.textureWidth); // Do this before the new animation float preLowerCentreTexPos = (preLowerPixelInt * 1.0f) / preClip.textureWidth; float preUpperCentreTexPos = preLowerCentreTexPos + preClip.texturePixelOffset; float3 preTexturePos = new float3(preLowerCentreTexPos, preUpperCentreTexPos, (preTexturePosition - preLowerCentreTexPos) / preClip.texturePixelOffset); // Add the positional and rotation character data to their respective native lists characterPositions.Add(new float4(characterTransform.Position, 1)); characterRotations.Add(quaternion.LookRotation(characterTransform.Forward, new Vector3(0.0f, 1.0f, 0.0f))); // Add the texture animation data to their respecitve native lists texturePositions.Add(texturePos); preAnimationTexturePositions.Add(preTexturePos); animationTransitionProgress.Add(transitionPercent); } }
void Update() { lodDrawer0.listPositionData.Clear(); lodDrawer1.listPositionData.Clear(); lodDrawer2.listPositionData.Clear(); lodDrawer3.listPositionData.Clear(); lodDrawer0.listRotationData.Clear(); lodDrawer1.listRotationData.Clear(); lodDrawer2.listRotationData.Clear(); lodDrawer3.listRotationData.Clear(); lodDrawer0.listTexturePositionData.Clear(); lodDrawer1.listTexturePositionData.Clear(); lodDrawer2.listTexturePositionData.Clear(); lodDrawer3.listTexturePositionData.Clear(); Vector3 CameraPosition = Camera.main.transform.position;; deltaTime += Time.deltaTime; for (int i = 0; i < formationList.Count; ++i) { Formation formation = formationList[i]; float distance = math.length(CameraPosition - formation.transform.position); Debug.Log("Dis: " + distance); for (int j = 0; j < formation.Count(); j++) { Vector3 center = formation.transform.position; Vector3 v3 = new Vector3(center.x + j % formation.width, 1, center.z + j / formation.width); V4 v4 = new V4(v3.x, v3.y, v3.z, 1); AnimationClipDataBaked clip = animationClipData[(int)0 * 25 + AnimationName.Attack1]; normalizedTime = normalizedTimeStart + deltaTime / clip.AnimationLength; normalizedTime = normalizedTime % 1.0f; float texturePosition = normalizedTime * clip.TextureRange + clip.TextureOffset; int lowerPixelInt = (int)math.floor(texturePosition * clip.TextureWidth); float lowerPixelCenter = (lowerPixelInt * 1.0f) / clip.TextureWidth; float upperPixelCenter = lowerPixelCenter + clip.OnePixelOffset; float lerpFactor = (texturePosition - lowerPixelCenter) / clip.OnePixelOffset; Vector3 texturePositionData = new Vector3(lowerPixelCenter, upperPixelCenter, lerpFactor); if (distance < bakedData.lods.Lod1Distance) { lodDrawer0.listPositionData.Add(v4); lodDrawer0.listRotationData.Add(Quaternion.LookRotation(new Vector3(0, UnityEngine.Random.Range(1, 50), 0), new Vector3(0.0f, 1, 0.0f))); lodDrawer0.listTexturePositionData.Add(texturePositionData); } else if (distance < bakedData.lods.Lod2Distance) { lodDrawer1.listPositionData.Add(v4); lodDrawer1.listRotationData.Add(Quaternion.LookRotation(new Vector3(0, UnityEngine.Random.Range(1, 50), 0), new Vector3(0.0f, 1, 0.0f))); lodDrawer1.listTexturePositionData.Add(texturePositionData); } else if (distance < bakedData.lods.Lod3Distance) { lodDrawer2.listPositionData.Add(v4); lodDrawer2.listRotationData.Add(Quaternion.LookRotation(new Vector3(0, UnityEngine.Random.Range(1, 50), 0), new Vector3(0.0f, 1, 0.0f))); lodDrawer2.listTexturePositionData.Add(texturePositionData); } else { lodDrawer3.listPositionData.Add(v4); lodDrawer3.listRotationData.Add(Quaternion.LookRotation(new Vector3(0, UnityEngine.Random.Range(1, 50), 0), new Vector3(0.0f, 1, 0.0f))); lodDrawer3.listTexturePositionData.Add(texturePositionData); } } } //for (int i = 0; i < formationList.Count * formationList[0].Count(); i++) //{ // lodDrawer0.listTexturePositionData.Add(texturePositionData); // lodDrawer1.listTexturePositionData.Add(texturePositionData); // lodDrawer2.listTexturePositionData.Add(texturePositionData); // lodDrawer3.listTexturePositionData.Add(texturePositionData); //} lodDrawer0.Draw(); lodDrawer1.Draw(); lodDrawer2.Draw(); lodDrawer3.Draw(); }
public void Execute(int i) { var unitTransform = unitTransformData[i]; float distance = math.length(CameraPosition - unitTransform.Position); var animatorData = textureAnimatorData[i]; AnimationClipDataBaked clip = animationClips[(int)animatorData.UnitType * 25 + animatorData.CurrentAnimationId]; Quaternion rotation = Quaternion.LookRotation(unitTransform.Forward, new Vector3(0.0f, 1.0f, 0.0f)); float texturePosition = textureAnimatorData[i].AnimationNormalizedTime * clip.TextureRange + clip.TextureOffset; float4 position = new float4(unitTransform.Position, unitTransform.Scale); if (distance < DistanceMaxLod0) { int writeIndex = BufferCounterLod0.Increment(); //unitPositions[writeIndex] = unitTransform.Position; UnsafeUtility.WriteArrayElement(BufferPointers[0], writeIndex, position); //unitRotations[writeIndex] = Quaternion.LookRotation(unitTransform.Forward, new Vector3(0.0f, 1.0f, 0.0f) /* Vector3.up */); UnsafeUtility.WriteArrayElement(BufferPointers[1], writeIndex, rotation); //textureCoordinates[writeIndex] = animationData[i].AnimationNormalizedTime * clip.TextureRange + clip.TextureOffset; UnsafeUtility.WriteArrayElement(BufferPointers[2], writeIndex, texturePosition); } else if (distance < DistanceMaxLod1) { int writeIndex = BufferCounterLod1.Increment(); //unitPositions[writeIndex] = unitTransform.Position; UnsafeUtility.WriteArrayElement(BufferPointers[3], writeIndex, position); //unitRotations[writeIndex] = Quaternion.LookRotation(unitTransform.Forward, new Vector3(0.0f, 1.0f, 0.0f) /* Vector3.up */); UnsafeUtility.WriteArrayElement(BufferPointers[4], writeIndex, rotation); //textureCoordinates[writeIndex] = animationData[i].AnimationNormalizedTime * clip.TextureRange + clip.TextureOffset; UnsafeUtility.WriteArrayElement(BufferPointers[5], writeIndex, texturePosition); } else if (distance < DistanceMaxLod2) { int writeIndex = BufferCounterLod2.Increment(); //unitPositions[writeIndex] = unitTransform.Position; UnsafeUtility.WriteArrayElement(BufferPointers[6], writeIndex, position); //unitRotations[writeIndex] = Quaternion.LookRotation(unitTransform.Forward, new Vector3(0.0f, 1.0f, 0.0f) /* Vector3.up */); UnsafeUtility.WriteArrayElement(BufferPointers[7], writeIndex, rotation); //textureCoordinates[writeIndex] = animationData[i].AnimationNormalizedTime * clip.TextureRange + clip.TextureOffset; UnsafeUtility.WriteArrayElement(BufferPointers[8], writeIndex, texturePosition); } else { int writeIndex = BufferCounterLod3.Increment(); //unitPositions[writeIndex] = unitTransform.Position; UnsafeUtility.WriteArrayElement(BufferPointers[9], writeIndex, position); //unitRotations[writeIndex] = Quaternion.LookRotation(unitTransform.Forward, new Vector3(0.0f, 1.0f, 0.0f) /* Vector3.up */); UnsafeUtility.WriteArrayElement(BufferPointers[10], writeIndex, rotation); //textureCoordinates[writeIndex] = animationData[i].AnimationNormalizedTime * clip.TextureRange + clip.TextureOffset; UnsafeUtility.WriteArrayElement(BufferPointers[11], writeIndex, texturePosition); } }
// Called when the structure is accessed public void Execute(int i) { // Pass through the animation data from the user CG_GameManager UserAnimationData = CG_GameManager.Instance; // The current texture animator data at the index provide AnimationData animData = texAnimData[i]; // if the animation id isnt the same as the new animation data if (animData.animationID != animData.newAnimationId) { // transition pre anim if (animData.animationID >= 0) { // set pre-transition data values animData.animationToSwitchID = animData.animationID; animData.animationToSwitchNormTime = animData.normalizedAnimationTime; animData.transitionPercent = 0f; // get the animation loops based on the user inputted varaibles if (animData.animationID == 1) { animData.animationToSwitchLooping = UserAnimationData.LoopAnimation2; } else { animData.animationToSwitchLooping = UserAnimationData.LoopAnimation1; } } else { animData.transitionPercent = -1f; } // set the animation id and resent the normalised time animData.animationID = animData.newAnimationId; animData.normalizedAnimationTime = 0f; } // Geth the baked clip data from the current animation index AnimationClipDataBaked bakedClip = animationClips[animData.animationID]; // Set the default normalised time of the animation float normalizedTime = animData.normalizedAnimationTime + deltaTime / (bakedClip.animationLength / UserAnimationData.Animation1Speed); // Change the speed dependant on the animation clip being played if (animData.animationID == 1) { normalizedTime = animData.normalizedAnimationTime + deltaTime / (bakedClip.animationLength / UserAnimationData.Animation2Speed); } // if the normalised time is equal to 1, the animation has if (normalizedTime > 1.0f) { // Get whether the animation should loop or not based on user variables bool animLoop = UserAnimationData.LoopAnimation1; if (animData.animationID == 1) { animLoop = UserAnimationData.LoopAnimation2; } // if the animation should loop, reset the normalised time // else hold this value at 1 if (animLoop) { normalizedTime = 0; } else { normalizedTime = 1f; } } // set the animation datas normalised time to this new normalised time animData.normalizedAnimationTime = normalizedTime; // if a transition is in progress if (animData.transitionPercent >= 0.0f) { float blendPercent = animData.transitionPercent + deltaTime / UserAnimationData.TransitionSpeed; // Transition ended if (blendPercent > 1f) { blendPercent = -1f; } else { // continue preanimation AnimationClipDataBaked preClip = animationClips[animData.animationToSwitchID]; float preNormalizedTime = animData.animationToSwitchNormTime + deltaTime / (preClip.animationLength); // if the animation should loop, reset the normalised time // else hold this value at 1 if (preNormalizedTime > 1.0f) { if (animData.animationToSwitchLooping) { preNormalizedTime = 0; } else { preNormalizedTime = 1f; } } animData.animationToSwitchNormTime = preNormalizedTime; } animData.transitionPercent = blendPercent; } texAnimData[i] = animData; }