public void Equip(Equipment newItem) { CurrentInventoryUI.instance.AddItem(newItem); //gets at which index of newItem in the EqupmentEnum int slotIndex = (int)newItem.equipSlot; Equipment oldItem = null; if (currentEquipment[slotIndex] != null) { oldItem = currentEquipment[slotIndex]; inventory.Add(oldItem); } if (onEquipmentChanged != null) { onEquipmentChanged.Invoke(newItem, oldItem); } currentEquipment[slotIndex] = newItem; SkinnedMeshRenderer newMesh = Instantiate <SkinnedMeshRenderer>(newItem.mesh); newMesh.transform.parent = targerMesh.transform; newMesh.bones = targerMesh.GetComponentInParent <SkinnedMeshRenderer>().bones; newMesh.rootBone = targerMesh.GetComponentInParent <SkinnedMeshRenderer>().rootBone; currentMeshes[slotIndex] = newMesh; newMesh.transform.position = targerMesh.transform.position; newMesh.transform.rotation = targerMesh.transform.rotation; newMesh.transform.localScale = targerMesh.transform.localScale; //newMesh.transform.localScale = targerMesh.bounds.extents; //CurrentInventory'e ekle }
private void InstantiatePerUnitTypeData(UnitType type) { var minionPrefab = Spawner.GetMinionPrefab(type); var renderingData = minionPrefab.GetComponentInChildren <RenderingDataWrapper>().Value; GameObject bakingObject = GameObject.Instantiate(renderingData.BakingPrefab); SkinnedMeshRenderer renderer = bakingObject.GetComponentInChildren <SkinnedMeshRenderer>(); Material material = renderingData.Material; LodData lodData = renderingData.LodData; var dataPerUnitType = new DataPerUnitType { UnitType = type, BakedData = KeyframeTextureBaker.BakeClips(renderer, GetAllAnimationClips(renderer.GetComponentInParent <Animation>()), lodData), Material = material, }; dataPerUnitType.Drawer = new InstancedSkinningDrawer(dataPerUnitType, dataPerUnitType.BakedData.NewMesh); dataPerUnitType.Lod1Drawer = new InstancedSkinningDrawer(dataPerUnitType, dataPerUnitType.BakedData.lods.Lod1Mesh); dataPerUnitType.Lod2Drawer = new InstancedSkinningDrawer(dataPerUnitType, dataPerUnitType.BakedData.lods.Lod2Mesh); dataPerUnitType.Lod3Drawer = new InstancedSkinningDrawer(dataPerUnitType, dataPerUnitType.BakedData.lods.Lod3Mesh); perUnitTypeDataHolder.Add(type, dataPerUnitType); TransferAnimationData(type); GameObject.Destroy(bakingObject); }
private void initilize() { if (init) { return; } init = true; PreSetting setting = Resources.Load <PreSetting>("PreSetting"); var bakingObject = GameObject.Instantiate(setting.go) as GameObject; SkinnedMeshRenderer renderer = bakingObject.GetComponentInChildren <SkinnedMeshRenderer>(); LodData lodData = new LodData(); lodData.Lod1Mesh = setting.mesh0; lodData.Lod2Mesh = setting.mesh1; lodData.Lod3Mesh = setting.mesh2; lodData.Lod1Distance = 10; lodData.Lod2Distance = 20; lodData.Lod3Distance = 30; Material material = setting.material; //SkinnedMeshRenderer r = Combine.CombineIt(bakingObject.transform); bakedData = KeyframeTextureBaker.BakeClips(renderer, GetAllAnimationClips(renderer.GetComponentInParent <Animation>()), lodData); //bakedData = KeyframeTextureBaker.BakeClips(r, GetAllAnimationClips(r.GetComponentInParent<Animation>()), lodData); TransferAnimationData(); //UnityEngine.Object fGo = Resources.Load("Formation"); for (int i = 0; i < formationCount; i++) { Vector3 formationPosition = new Vector3(i % 5 * (width + 30), 1, i / 5 * (height + 20)); //GameObject instance = Object.Instantiate(fGo) as GameObject; //instance.name = "Formation_" + i; //Formation form = instance.GetComponent<Formation>(); //form.Init(width, height); //instance.transform.position = formationPosition; FormationData data = new FormationData(); data.width = width; data.height = height; data.formationPosition = formationPosition; formationList.Add(data); } lodDrawer0 = new LodedDrawerECS(bakedData.NewMesh, material, bakedData); lodDrawer1 = new LodedDrawerECS(bakedData.lods.Lod1Mesh, material, bakedData); lodDrawer2 = new LodedDrawerECS(bakedData.lods.Lod2Mesh, material, bakedData); lodDrawer3 = new LodedDrawerECS(bakedData.lods.Lod3Mesh, material, bakedData); }
public void TransferBones() { defaultMesh.rootBone.parent = transform.root; newMesh.transform.parent = defaultMesh.transform.parent; newMesh.bones = defaultMesh.bones; newMesh.rootBone = defaultMesh.rootBone; var anim = defaultMesh.GetComponentInParent <Animator>(); if (anim && newAvatar != null) { anim.avatar = newAvatar; } Debug.Log("Mesh: " + newMesh.name + "has the Bones from " + defaultMesh.name); }
void Start() { var bakingObject = GameObject.Instantiate(go); SkinnedMeshRenderer renderer = bakingObject.GetComponentInChildren <SkinnedMeshRenderer>(); lodData = new LodData(); lodData.Lod1Mesh = mesh0; lodData.Lod2Mesh = mesh1; lodData.Lod3Mesh = mesh2; lodData.Lod1Distance = 10; lodData.Lod2Distance = 20; lodData.Lod3Distance = 30; bakedData = KeyframeTextureBaker.BakeClips(renderer, GetAllAnimationClips(renderer.GetComponentInParent <Animation>()), lodData); TransferAnimationData(); UnityEngine.Object fGo = Resources.Load("Formation"); for (int i = 0; i < formationCount; i++) { int width = 12; int height = 6; Vector3 formationPosition = new Vector3(i % 5 * (width + 5), 1, i / 5 * (height + 4)); GameObject instance = Instantiate(fGo) as GameObject; instance.name = "Formation_" + i; Formation form = instance.GetComponent <Formation>(); form.Init(width, height); instance.transform.position = formationPosition; formationList.Add(form); } //SaveTextureToFile(bakedData.Texture0, "tp1.png"); //SaveTextureToFile(bakedData.Texture1, "tp2.png"); //SaveTextureToFile(bakedData.Texture2, "tp3.png"); lodDrawer0 = new LodedDrawer(bakedData.NewMesh, material, bakedData, animationClipData, formationList); lodDrawer1 = new LodedDrawer(bakedData.lods.Lod1Mesh, material, bakedData, animationClipData, formationList); lodDrawer2 = new LodedDrawer(bakedData.lods.Lod2Mesh, material, bakedData, animationClipData, formationList); lodDrawer3 = new LodedDrawer(bakedData.lods.Lod3Mesh, material, bakedData, animationClipData, formationList); }
private void InstantiatePerUnitTypeData(UnitType type) { var minionPrefab = Spawner.GetMinionPrefab(type); var renderingData = minionPrefab.GetComponentInChildren <RenderingDataWrapper>().Value; var bakingObject = GameObject.Instantiate(renderingData.BakingPrefab); SkinnedMeshRenderer renderer = bakingObject.GetComponentInChildren <SkinnedMeshRenderer>(); Material material = renderingData.Material; LodData lodData = renderingData.LodData; var dataPerUnitType = new DataPerUnitType { UnitType = type, BakedData = KeyframeTextureBaker.BakeClips(renderer, GetAllAnimationClips(renderer.GetComponentInParent <Animation>()), lodData), Material = material, }; dataPerUnitType.Drawer = new InstancedSkinningDrawer(dataPerUnitType, dataPerUnitType.BakedData.NewMesh); dataPerUnitType.Lod1Drawer = new InstancedSkinningDrawer(dataPerUnitType, dataPerUnitType.BakedData.lods.Lod1Mesh); dataPerUnitType.Lod2Drawer = new InstancedSkinningDrawer(dataPerUnitType, dataPerUnitType.BakedData.lods.Lod2Mesh); dataPerUnitType.Lod3Drawer = new InstancedSkinningDrawer(dataPerUnitType, dataPerUnitType.BakedData.lods.Lod3Mesh); #if !USE_SAFE_JOBS dataPerUnitType.BufferPointers = new NativeArray <IntPtr>(12, Allocator.Persistent); dataPerUnitType.BufferPointers[0] = dataPerUnitType.Drawer.BufferPointers[0]; dataPerUnitType.BufferPointers[1] = dataPerUnitType.Drawer.BufferPointers[1]; dataPerUnitType.BufferPointers[2] = dataPerUnitType.Drawer.BufferPointers[2]; dataPerUnitType.BufferPointers[3] = dataPerUnitType.Lod1Drawer.BufferPointers[0]; dataPerUnitType.BufferPointers[4] = dataPerUnitType.Lod1Drawer.BufferPointers[1]; dataPerUnitType.BufferPointers[5] = dataPerUnitType.Lod1Drawer.BufferPointers[2]; dataPerUnitType.BufferPointers[6] = dataPerUnitType.Lod2Drawer.BufferPointers[0]; dataPerUnitType.BufferPointers[7] = dataPerUnitType.Lod2Drawer.BufferPointers[1]; dataPerUnitType.BufferPointers[8] = dataPerUnitType.Lod2Drawer.BufferPointers[2]; dataPerUnitType.BufferPointers[9] = dataPerUnitType.Lod3Drawer.BufferPointers[0]; dataPerUnitType.BufferPointers[10] = dataPerUnitType.Lod3Drawer.BufferPointers[1]; dataPerUnitType.BufferPointers[11] = dataPerUnitType.Lod3Drawer.BufferPointers[2]; #endif perUnitTypeDataHolder.Add(type, dataPerUnitType); TransferAnimationData(type); GameObject.Destroy(bakingObject); }
private static Matrix4x4[,] SampleAnimationClip(AnimationClip clip, SkinnedMeshRenderer renderer, float framerate) { Matrix4x4[,] boneMatrices = new Matrix4x4[Mathf.CeilToInt(framerate * clip.length) + 3, renderer.bones.Length]; for (int i = 0; i < renderer.sharedMesh.bindposes.Length; i++) { var vv = renderer.sharedMesh.bindposes[i]; //Debug.Log( "=== " + vv.weight0 + " " + vv.weight1 + " " + vv.weight2 + " " + vv.weight3 + " " + vv.boneIndex0 + " " + vv.boneIndex1 + " " + vv.boneIndex2 + " " + vv.boneIndex3); } Animation animation = renderer.GetComponentInParent <Animation>(); AnimationState bakingState = animation[clip.name]; bakingState.enabled = true; bakingState.weight = 1f; for (int i = 1; i < boneMatrices.GetLength(0) - 1; i++) { float t = (float)(i - 1) / (boneMatrices.GetLength(0) - 3); bakingState.normalizedTime = t; animation.Sample(); for (int j = 0; j < renderer.bones.Length; j++) { // Put it into model space for better compression. boneMatrices[i, j] = renderer.localToWorldMatrix.inverse * renderer.bones[j].localToWorldMatrix * renderer.sharedMesh.bindposes[j]; //boneMatrices[i, j] = renderer.bones[j].localToWorldMatrix; } } for (int j = 0; j < renderer.bones.Length; j++) { boneMatrices[0, j] = boneMatrices[boneMatrices.GetLength(0) - 2, j]; boneMatrices[boneMatrices.GetLength(0) - 1, j] = boneMatrices[1, j]; } bakingState.enabled = false; bakingState.weight = 0f; return(boneMatrices); }
// Function loops through the bind poseses of the animations and stores their location matrix private static Matrix4x4[,] SampleAnimation(AnimationClip animClip, SkinnedMeshRenderer prefabSkinnedMesh) { // Get the animator in the parent the skinned mesh Animator animator = prefabSkinnedMesh.GetComponentInParent <Animator>(); // initialise the bone matrices variable Matrix4x4[,] bonePosMatrix = new Matrix4x4[Mathf.CeilToInt(60 * animClip.length) + 3, prefabSkinnedMesh.bones.Length]; // Set the delta time of this clip, this is used for gathering the positional values of the mesh float deltaTime = animClip.length / (bonePosMatrix.GetLength(0) - 1); // loop through the number of bones of the mesh in the 1st dimension for (int i = 1; i < bonePosMatrix.GetLength(0) - 1; i++) { // Play the animation located in the animator controller with the same name as the // user chosen animations. The layer and the normalised time animator.Play(animClip.name, -1, (float)(i - 1) / (bonePosMatrix.GetLength(0) - 3)); // evalute the animator position based on the data time inserted animator.Update(deltaTime); // loop through the skinned mesh renderes bones and set them equal to the animation mesh bind pose. for (int j = 0; j < prefabSkinnedMesh.bones.Length; j++) { bonePosMatrix[i, j] = prefabSkinnedMesh.localToWorldMatrix.inverse * prefabSkinnedMesh.bones[j].localToWorldMatrix * prefabSkinnedMesh.sharedMesh.bindposes[j]; } } for (int j = 0; j < prefabSkinnedMesh.bones.Length; j++) { bonePosMatrix[0, j] = bonePosMatrix[bonePosMatrix.GetLength(0) - 2, j]; bonePosMatrix[bonePosMatrix.GetLength(0) - 1, j] = bonePosMatrix[1, j]; } // Return these bone matrices return(bonePosMatrix); }
public static BakedData BakeClips(SkinnedMeshRenderer originalRenderer, AnimationClip[] animationClips, LodData lods) { BakedData bakedData = new BakedData(); bakedData.NewMesh = CreateMesh(originalRenderer, lods.Scale); var lod1Mesh = CreateMesh(originalRenderer, lods.Scale, lods.Lod1Mesh); var lod2Mesh = CreateMesh(originalRenderer, lods.Scale, lods.Lod2Mesh); var lod3Mesh = CreateMesh(originalRenderer, lods.Scale, lods.Lod3Mesh); bakedData.lods = new LodData(lod1Mesh, lod2Mesh, lod3Mesh, lods.Lod1Distance, lods.Lod2Distance, lods.Lod3Distance); bakedData.Framerate = 60f; List <Matrix4x4[, ]> sampledBoneMatrices = new List <Matrix4x4[, ]>(); int numberOfKeyFrames = 0; var animationComponent = originalRenderer.GetComponentInParent <Animation>(); for (int i = 0; i < animationClips.Length; i++) { animationComponent[animationClips[i].name].enabled = false; animationComponent[animationClips[i].name].weight = 0f; } for (int i = 0; i < animationClips.Length; i++) { Debug.Log("x " + i + " " + animationClips[i].name); var sampledMatrix = SampleAnimationClip(animationClips[i], originalRenderer, bakedData.Framerate); sampledBoneMatrices.Add(sampledMatrix); numberOfKeyFrames += sampledMatrix.GetLength(0); } int numberOfBones = sampledBoneMatrices[0].GetLength(1); bakedData.Texture0 = new Texture2D(numberOfKeyFrames, numberOfBones, TextureFormat.RGBAFloat, false); bakedData.Texture0.wrapMode = TextureWrapMode.Clamp; bakedData.Texture0.filterMode = FilterMode.Point; bakedData.Texture0.anisoLevel = 0; bakedData.Texture1 = new Texture2D(numberOfKeyFrames, numberOfBones, TextureFormat.RGBAFloat, false); bakedData.Texture1.wrapMode = TextureWrapMode.Clamp; bakedData.Texture1.filterMode = FilterMode.Point; bakedData.Texture1.anisoLevel = 0; bakedData.Texture2 = new Texture2D(numberOfKeyFrames, numberOfBones, TextureFormat.RGBAFloat, false); bakedData.Texture2.wrapMode = TextureWrapMode.Clamp; bakedData.Texture2.filterMode = FilterMode.Point; bakedData.Texture2.anisoLevel = 0; Color[] texture0Color = new Color[bakedData.Texture0.width * bakedData.Texture0.height]; Color[] texture1Color = new Color[bakedData.Texture0.width * bakedData.Texture0.height]; Color[] texture2Color = new Color[bakedData.Texture0.width * bakedData.Texture0.height]; int runningTotalNumberOfKeyframes = 0; for (int i = 0; i < sampledBoneMatrices.Count; i++) { for (int boneIndex = 0; boneIndex < sampledBoneMatrices[i].GetLength(1); boneIndex++) { //Color previousRotation = new Color(); for (int keyframeIndex = 0; keyframeIndex < sampledBoneMatrices[i].GetLength(0); keyframeIndex++) { //var rotation = GetRotation(Quaternion.LookRotation(sampledBoneMatrices[i][keyframeIndex, boneIndex].GetColumn(2), // sampledBoneMatrices[i][keyframeIndex, boneIndex].GetColumn(1))); //if (keyframeIndex != 0) //{ // if (Distance(previousRotation, rotation) > Distance(Negate(rotation), previousRotation)) // { // rotation = new Color(-rotation.r, -rotation.g, -rotation.b, -rotation.a); // } //} //var translation = GetTranslation(sampledBoneMatrices[i][keyframeIndex, boneIndex].GetColumn(3), rotation); //previousRotation = rotation; //int index = Get1DCoord(runningTotalNumberOfKeyframes + keyframeIndex, boneIndex, bakedData.TranslationTexture.width); //translations[index] = translation; //rotations[index] = rotation; int index = Get1DCoord(runningTotalNumberOfKeyframes + keyframeIndex, boneIndex, bakedData.Texture0.width); texture0Color[index] = sampledBoneMatrices[i][keyframeIndex, boneIndex].GetRow(0); texture1Color[index] = sampledBoneMatrices[i][keyframeIndex, boneIndex].GetRow(1); texture2Color[index] = sampledBoneMatrices[i][keyframeIndex, boneIndex].GetRow(2); } } AnimationClipData clipData = new AnimationClipData { Clip = animationClips[i], PixelStart = runningTotalNumberOfKeyframes + 1, PixelEnd = runningTotalNumberOfKeyframes + sampledBoneMatrices[i].GetLength(0) - 1 }; if (clipData.Clip.wrapMode == WrapMode.Default) { clipData.PixelEnd -= 1; } bakedData.Animations.Add(clipData); runningTotalNumberOfKeyframes += sampledBoneMatrices[i].GetLength(0); } bakedData.Texture0.SetPixels(texture0Color); bakedData.Texture0.Apply(false, false); bakedData.Texture1.SetPixels(texture1Color); bakedData.Texture1.Apply(false, false); bakedData.Texture2.SetPixels(texture2Color); bakedData.Texture2.Apply(false, false); runningTotalNumberOfKeyframes = 0; for (int i = 0; i < sampledBoneMatrices.Count; i++) { for (int boneIndex = 0; boneIndex < sampledBoneMatrices[i].GetLength(1); boneIndex++) { for (int keyframeIndex = 0; keyframeIndex < sampledBoneMatrices[i].GetLength(0); keyframeIndex++) { //int d1_index = Get1DCoord(runningTotalNumberOfKeyframes + keyframeIndex, boneIndex, bakedData.Texture0.width); Color pixel0 = bakedData.Texture0.GetPixel(runningTotalNumberOfKeyframes + keyframeIndex, boneIndex); Color pixel1 = bakedData.Texture1.GetPixel(runningTotalNumberOfKeyframes + keyframeIndex, boneIndex); Color pixel2 = bakedData.Texture2.GetPixel(runningTotalNumberOfKeyframes + keyframeIndex, boneIndex); if ((Vector4)pixel0 != sampledBoneMatrices[i][keyframeIndex, boneIndex].GetRow(0)) { Debug.LogError("Error at (" + (runningTotalNumberOfKeyframes + keyframeIndex) + ", " + boneIndex + ") expected " + Format(sampledBoneMatrices[i][keyframeIndex, boneIndex].GetRow(0)) + " but got " + Format(pixel0)); } if ((Vector4)pixel1 != sampledBoneMatrices[i][keyframeIndex, boneIndex].GetRow(1)) { Debug.LogError("Error at (" + (runningTotalNumberOfKeyframes + keyframeIndex) + ", " + boneIndex + ") expected " + Format(sampledBoneMatrices[i][keyframeIndex, boneIndex].GetRow(1)) + " but got " + Format(pixel1)); } if ((Vector4)pixel2 != sampledBoneMatrices[i][keyframeIndex, boneIndex].GetRow(2)) { Debug.LogError("Error at (" + (runningTotalNumberOfKeyframes + keyframeIndex) + ", " + boneIndex + ") expected " + Format(sampledBoneMatrices[i][keyframeIndex, boneIndex].GetRow(2)) + " but got " + Format(pixel2)); } } } runningTotalNumberOfKeyframes += sampledBoneMatrices[i].GetLength(0); } bakedData.AnimationsDictionary = new Dictionary <string, AnimationClipData>(); foreach (var clipData in bakedData.Animations) { bakedData.AnimationsDictionary[clipData.Clip.name] = clipData; } return(bakedData); }
/// <summary> /// Stores a reference to the model <see cref="UnityEngine.Animation"/> component. /// </summary> private void Start() { _animation = SkinnedMeshRenderer.GetComponentInParent <Animation>(); }
public void SetPlayerIndex(int index) { m_CurrentSelectedMaterial = m_PlayerIndex = index; m_MeshCharacter.GetComponentInParent <Animator>().SetBool("IsGrounded", true); m_TitleText.SetText("Player " + (index + 1).ToString()); }