private void CreateAssets(AnimationState state, List <VertInfo> infoList) { var buffer = new ComputeBuffer(infoList.Count, System.Runtime.InteropServices.Marshal.SizeOf(typeof(VertInfo))); buffer.SetData(infoList.ToArray()); var kernel = infoTexGen.FindKernel("CSMain"); uint x, y, z; infoTexGen.GetKernelThreadGroupSizes(kernel, out x, out y, out z); infoTexGen.SetInt("VertCount", vCount); infoTexGen.SetBuffer(kernel, "Info", buffer); infoTexGen.SetTexture(kernel, "OutPosition", pRt); infoTexGen.SetTexture(kernel, "OutNormal", nRt); infoTexGen.Dispatch(kernel, vCount / (int)x + 1, frames / (int)y + 1, 1); buffer.Release(); #if UNITY_EDITOR var posTex = RenderTextureToTexture2D.Convert(pRt); var normTex = RenderTextureToTexture2D.Convert(nRt); Graphics.CopyTexture(pRt, posTex); Graphics.CopyTexture(nRt, normTex); var bta = new BakedTextureAnimation(); bta.fullPathHash = Animator.StringToHash(string.Format("Base Layer.{0}", state.name)); bta.animationName = state.name; bta.positionAnimTexture = posTex; bta.normalAnimTexture = normTex; bta.texelSize = new Vector4(1.0f / posTex.width, 1.0f / posTex.height, posTex.width, posTex.height); bakedTextureAnimations.Add(bta); go.GetComponent <TextureAnimations>().SetItemSource(bakedTextureAnimations); AssetDatabase.CreateAsset(posTex, Path.Combine(folderPath, pRt.name + ".asset")); AssetDatabase.CreateAsset(normTex, Path.Combine(folderPath, nRt.name + ".asset")); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); prefab = PrefabUtility.ReplacePrefab(go, prefab); #endif }
void Update() { if (Input.GetKeyDown(KeyCode.Alpha1)) { if (Random.value > 0.5f) { animator.SetBool("OnRun", true); } } if (Input.GetKeyDown(KeyCode.Alpha2)) { animator.SetBool("OnRun", false); } if (block == null) { block = new MaterialPropertyBlock(); } var currAnimState = animator.GetCurrentAnimatorStateInfo(0); if (prev_anim == null || prev_anim.fullPathHash != currAnimState.fullPathHash) { curr_anim = textureAnimations.Find(currAnimState.fullPathHash); } else { curr_anim = prev_anim; } if (curr_anim == null) { return; } prev_anim = curr_anim; animationTime = currAnimState.normalizedTime;// Mathf.Repeat(, 1.0f); kernelShader.SetVector("TexelSize", curr_anim.texelSize); kernelShader.SetFloat("AnimationTime", animationTime); var nextAnimState = animator.GetNextAnimatorStateInfo(0); if (prev_next_anim == null || prev_next_anim.fullPathHash != nextAnimState.fullPathHash) { next_anim = textureAnimations.Find(nextAnimState.fullPathHash); } else { next_anim = prev_next_anim; } prev_next_anim = next_anim; if (next_anim != null && nextAnimState.normalizedTime > 0) { var transition = animator.GetAnimatorTransitionInfo(0); kernelShader.SetTexture(transitionAnimation_kernelIndex, "PositionAnimTexture", curr_anim.positionAnimTexture); kernelShader.SetTexture(transitionAnimation_kernelIndex, "NormalAnimTexture", curr_anim.normalAnimTexture); kernelShader.SetTexture(transitionAnimation_kernelIndex, "PositionAnimTexture_Next", next_anim.positionAnimTexture); kernelShader.SetTexture(transitionAnimation_kernelIndex, "NormalAnimTexture_Next", next_anim.normalAnimTexture); kernelShader.SetVector("TexelSize_Next", next_anim.texelSize); kernelShader.SetFloat("AnimationTime_Next", nextAnimState.normalizedTime); kernelShader.SetFloat("TransitionTime", transition.normalizedTime); kernelShader.SetBuffer(transitionAnimation_kernelIndex, "PositionBuffer", positionBuffer); kernelShader.SetBuffer(transitionAnimation_kernelIndex, "NormalBuffer", normalBuffer); kernelShader.Dispatch(transitionAnimation_kernelIndex, vertexCount / 8 + 1, 1, 1); } else { kernelShader.SetTexture(updateAnimation_kernelIndex, "PositionAnimTexture", curr_anim.positionAnimTexture); kernelShader.SetTexture(updateAnimation_kernelIndex, "NormalAnimTexture", curr_anim.normalAnimTexture); kernelShader.SetBuffer(updateAnimation_kernelIndex, "PositionBuffer", positionBuffer); kernelShader.SetBuffer(updateAnimation_kernelIndex, "NormalBuffer", normalBuffer); kernelShader.Dispatch(updateAnimation_kernelIndex, vertexCount / 8 + 1, 1, 1); } block.SetBuffer("PositionBuffer", positionBuffer); block.SetBuffer("NormalBuffer", normalBuffer); _renderer.SetPropertyBlock(block); }