protected void UpdateAnimations() { //Update particle frame progress for (int i = 0; i < _instanceData.Length; i++) { if (IsObjectActive(ref _instanceData[i])) { float prevFrame = _instanceData[i]._currentFrame; //Progress current animation AnimationTexture.Animation animation = GetAnimation(_instanceData[i]._animationIndex); //Update current frame _instanceData[i]._currentFrame += Time.deltaTime * animation._fps * _instanceData[i]._animationSpeed; //Is animation finished? if (Mathf.FloorToInt(_instanceData[i]._currentFrame) >= animation._totalFrames - 2) { Animation newAnimation = PickRandomAnimation(); _instanceData[i]._animationIndex = newAnimation._animationIndex; _instanceData[i]._currentFrame = 0f; _instanceData[i]._animationSpeed = newAnimation._speedRange.GetRandomValue();; } } } }
public GameObject SpawnObject(Vector3 position) { for (int i = 0; i < _instanceData.Length; i++) { if (!IsObjectActive(ref _instanceData[i])) { if (_instanceData[i]._gameObject == null) { _instanceData[i]._gameObject = Instantiate(_referencePrefab, this.transform); _instanceData[i]._gameObject.transform.position = position; _instanceData[i]._skinnedMeshes = _instanceData[i]._gameObject.GetComponentsInChildren <SkinnedMeshRenderer>(); for (int j = 0; j < _instanceData[i]._skinnedMeshes.Length; j++) { _instanceData[i]._skinnedMeshes[j].enabled = false; } _instanceData[i]._gameObject.SetActive(true); } Animation animation = PickRandomAnimation(); AnimationTexture.Animation textureAnim = GetAnimation(animation._animationIndex); _instanceData[i]._animationIndex = animation._animationIndex; _instanceData[i]._currentFrame = Random.Range(0, textureAnim._totalFrames - 2); _instanceData[i]._animationSpeed = animation._speedRange.GetRandomValue(); OnSpawnObject(ref _instanceData[i]); return(_instanceData[i]._gameObject); } } return(null); }
private Vector4 StartNewAnimation(Vector4 oldData, bool randomOffset) { Vector4 data; ParticleAnimation anim = PickRandomAnimation(); AnimationTexture.Animation animation = GetAnimation(anim._animationIndex); data.x = anim._animationIndex; data.y = randomOffset ? Random.Range(0, animation._totalFrames - 2) : 0; data.z = anim._speedRange.GetRandomValue(); data.w = 1.0f; return(data); }
private void Update() { AnimationTexture.Animation animation = _animationTexture.GetAnimations()[0]; _currentFrame += Time.deltaTime * animation._fps; if (_currentFrame + 1 > animation._totalFrames) { _currentFrame = 0.0f; } _material.GetMaterial().SetFloat("frameIndex", _currentFrame); _material.GetMaterial().SetFloat("preFrameIndex", -1.0f); _material.GetMaterial().SetFloat("transitionProgress", -1.0f); Graphics.DrawMeshInstanced(_skinnedMesh.sharedMesh, 0, _material, new Matrix4x4[] { this.transform.localToWorldMatrix }, 1); }
protected override void UpdateProperties() { #if UNITY_EDITOR for (int i = 0; i < _materials.Length; i++) { _animationTexture.SetMaterialProperties(_materials[i]); } #endif //Update property block for (int i = 0; i < GetNumRenderedParticles(); i++) { int index = GetRenderedParticlesIndex(i); AnimationTexture.Animation animation = GetAnimation(Mathf.RoundToInt(_particleCustomData[index].x)); _particleCurrentFrame[i] = animation._startFrameOffset + _particleCustomData[index].y; } //Update property block _propertyBlock.SetFloatArray("frameIndex", _particleCurrentFrame); }
private void UpdateAnimations() { int numParticles = _particleSystem.GetCustomParticleData(_particleCustomData, _customDataChannel); //Update particle frame progress for (int i = 0; i < numParticles; i++) { Vector4 customData = _particleCustomData[i]; float prevFrame = customData.y; //New particle if (customData.w == 0.0f) { _particleCustomData[i] = StartNewAnimation(customData, true); } //Progress current animation else { AnimationTexture.Animation animation = GetAnimation(Mathf.RoundToInt(customData.x)); //Update current frame customData.y += Time.deltaTime * animation._fps * customData.z; //Is animation finished? if (Mathf.FloorToInt(customData.y) < animation._totalFrames - 2) { _particleCustomData[i] = customData; } else { _particleCustomData[i] = StartNewAnimation(customData, false); } } } //Update custom data _particleSystem.SetCustomParticleData(_particleCustomData, _customDataChannel); }
private AnimationTexture.Animation GetAnimation(int index) { AnimationTexture.Animation[] animations = _animationTexture.GetAnimations(); AnimationTexture.Animation animation = animations[Mathf.Clamp(index, 0, animations.Length)]; return(animation); }
private static AnimationTexture CreateAnimationTexture(GameObject gameObject, Transform[] bones, Matrix4x4[] bindposes, AnimationClip[] animationClips, int bakeFPS) { int numBones = bones.Length; AnimationTexture.Animation[] animations = new AnimationTexture.Animation[animationClips.Length]; //3d array - animation / frame / bones Matrix4x4[][][] boneWorldMatrix = new Matrix4x4[animations.Length][][]; Matrix4x4 rootMat = gameObject.transform.worldToLocalMatrix; int startOffset = 0; for (int animIndex = 0; animIndex < animations.Length; animIndex++) { int fps = bakeFPS; string name = animationClips[animIndex].name; int totalFrames = Mathf.CeilToInt(animationClips[animIndex].length * fps) + 1; WrapMode wrapMode = animationClips[animIndex].wrapMode; animations[animIndex] = new AnimationTexture.Animation(name, startOffset, totalFrames, fps, wrapMode); startOffset += totalFrames; //Sample animation boneWorldMatrix[animIndex] = new Matrix4x4[totalFrames][]; for (int frame = 0; frame < totalFrames; frame++) { float bakeDelta = Mathf.Clamp01((float)frame / totalFrames); float animationTime = bakeDelta * animationClips[animIndex].length; //Sample animation bool wasLegacy = animationClips[animIndex].legacy; animationClips[animIndex].legacy = true; animationClips[animIndex].SampleAnimation(gameObject, animationTime); animationClips[animIndex].legacy = wasLegacy; //Save bone matrices boneWorldMatrix[animIndex][frame] = new Matrix4x4[numBones]; for (int boneIndex = 0; boneIndex < numBones; boneIndex++) { boneWorldMatrix[animIndex][frame][boneIndex] = rootMat * bones[boneIndex].localToWorldMatrix * bindposes[boneIndex]; } } } //Create and save texture! Work out width! TextureFormat format = TextureFormat.RGBAHalf; int textureSize; if (!CalculateTextureSize(boneWorldMatrix, out textureSize)) { return(null); } Texture2D texture = new Texture2D(textureSize, textureSize, format, false); texture.filterMode = FilterMode.Point; //Loop through animations / frames / bones setting pixels for each bone matrix int pixelx = 0; int pixely = 0; //Foreach animation for (int animIndex = 0; animIndex < boneWorldMatrix.Length; animIndex++) { //For each frame for (int j = 0; j < boneWorldMatrix[animIndex].Length; j++) { //Convert all frame bone matrices to colors Color[] matrixPixels = ConvertMatricesToColor(boneWorldMatrix[animIndex][j]); texture.SetPixels(pixelx, pixely, 4, numBones, matrixPixels); //Shift to next frame position pixelx += 4; //If less than 4 pixels from edge, move to next row if (textureSize - pixelx < 4) { pixelx = 0; pixely += numBones; } } } texture.Apply(); return(new AnimationTexture(animations, numBones, texture)); }