Example #1
0
        public override void Crossfade(int index, float speed)
        {
            if (currentAnimIndex < 0)
            {
                Play(index);
                return;
            }
            ShaderMeshAnimation currentAnimation = this.currentAnimation as ShaderMeshAnimation;

            CreatePropertyBlock();
            meshRenderer.GetPropertyBlock(materialPropertyBlock);
            Vector4 crossfadeInfo = new Vector4(propertyBlockData.x, propertyBlockData.y, propertyBlockData.z, propertyBlockData.w);

            int framesPerTexture = Mathf.FloorToInt((textureSizeX * textureSizeY) / (currentAnimation.vertexCount * 2));
            int localOffset      = Mathf.FloorToInt(currentFrame / (float)framesPerTexture);
            int textureIndex     = textureStartIndex + localOffset;
            int frameOffset      = Mathf.FloorToInt(currentFrame % framesPerTexture);
            int pixelOffset      = currentAnimation.vertexCount * 2 * frameOffset;

            crossfadeInfo.x = pixelOffset;
            var shaderTime = _shaderTime;

            materialPropertyBlock.SetFloat(_crossfadeAnimTextureIndexProp, textureIndex);
            materialPropertyBlock.SetVector(_crossfadeAnimScalarProp, currentAnimation.animScalar);
            materialPropertyBlock.SetVector(_crossfadeAnimInfoProp, crossfadeInfo);
            materialPropertyBlock.SetFloat(_crossfadeStartTimeProp, shaderTime.y);
            materialPropertyBlock.SetFloat(_crossfadeEndTimeProp, shaderTime.y + speed);
            meshRenderer.SetPropertyBlock(materialPropertyBlock);
            Play(index);
        }
Example #2
0
        protected override void OnCurrentAnimationChanged(IMeshAnimation meshAnimation)
        {
            ShaderMeshAnimation currentAnimation = meshAnimation as ShaderMeshAnimation;

            CreatePropertyBlock();
            meshRenderer.GetPropertyBlock(materialPropertyBlock);
            materialPropertyBlock.SetVector(_animScalarProp, currentAnimation.animScalar);
            propertyBlockData.y = currentAnimation.vertexCount;
            pixelsPerTexture    = currentAnimation.textureSize.x * currentAnimation.textureSize.y;
            textureStartIndex   = 0;
            for (int i = 0; i < currentAnimIndex; i++)
            {
                textureStartIndex += meshAnimations[i].textureCount;
            }
            textureSizeX = currentAnimation.textureSize.x;
            textureSizeY = currentAnimation.textureSize.y;
            // set animation info
            propertyBlockData.x = textureStartIndex;
            propertyBlockData.z = textureSizeX;
            propertyBlockData.w = textureSizeY;
            materialPropertyBlock.SetVector(_animInfoProp, propertyBlockData);
            // set texture index
            materialPropertyBlock.SetFloat(_animTextureIndexProp, textureStartIndex);
            // set time info
            float startTime = _shaderTime.y;

            timeBlockData = new Vector4(
                0,
                currentAnimation.TotalFrames,
                startTime,
                startTime + (currentAnimation.length / (speed * currentAnimation.playbackSpeed)));
            materialPropertyBlock.SetVector(_animTimeProp, timeBlockData);
            // set property block
            meshRenderer.SetPropertyBlock(materialPropertyBlock);
        }
Example #3
0
 public override void SetAnimations(IMeshAnimation[] meshAnimations)
 {
     this.meshAnimations = new ShaderMeshAnimation[meshAnimations.Length];
     for (int i = 0; i < meshAnimations.Length; i++)
     {
         this.meshAnimations[i] = meshAnimations[i] as ShaderMeshAnimation;
     }
     if (meshAnimations.Length > 0 && defaultMeshAnimation == null)
     {
         defaultMeshAnimation = this.meshAnimations[0];
     }
 }