public void SetDefaultWeightsSetting3(bool hasRootCopy) { //set default weights CalcWeights(); //apply weights if biped if (combinedbones.ContainsKey("Main")) { CombinedBone cb = combinedbones["Main"]; cb.SetPositionWeightRecursive(0.0f); cb.SetPositionLockRecursive(false); cb.SetRotationWeightRecursive(0.0f); cb.SetRotationLockRecursive(false); cb.SetScaleWeightRecursive(0.0f); cb.SetScaleLockRecursive(false); } string bonesUp = "Main/Root_M/BackA_M"; if (hasRootCopy) { bonesUp = "Main/RootCopy/Root_M/BackA_M"; } if (combinedbones.ContainsKey(bonesUp)) { CombinedBone cb = combinedbones[bonesUp]; cb.SetPositionWeightRecursive(1.0f); cb.SetPositionLockRecursive(true); cb.SetRotationWeightRecursive(1.0f); cb.SetRotationLockRecursive(false); cb.SetScaleWeightRecursive(1.0f); cb.SetScaleLockRecursive(false); } }
public override void OnInspectorGUI() { //base.OnInspectorGUI(); CombinedBone cb = (CombinedBone)target; GUILayout.BeginVertical("Box"); GUILayout.BeginHorizontal("Box"); cb.poslock = EditorGUILayout.Toggle("position lock", cb.poslock); GUILayout.EndHorizontal(); if (GUILayout.Button("Apply Position Lock to Childrens")) { cb.SetPositionLockRecursive(cb.poslock); } GUILayout.BeginHorizontal("Box"); cb.posweight = EditorGUILayout.Slider("position weight", cb.posweight, 0.0f, 1.0f); GUILayout.EndHorizontal(); if (GUILayout.Button("Apply Position Weight to Childrens")) { cb.SetPositionWeightRecursive(cb.posweight); } GUILayout.BeginHorizontal("Box"); cb.rotationlock = EditorGUILayout.Toggle("rotation lock", cb.rotationlock); GUILayout.EndHorizontal(); if (GUILayout.Button("Apply Position Lock to Childrens")) { cb.SetRotationLockRecursive(cb.rotationlock); } GUILayout.BeginHorizontal("Box"); cb.rotationweight = EditorGUILayout.Slider("rotation weight", cb.rotationweight, 0.0f, 1.0f); GUILayout.EndHorizontal(); if (GUILayout.Button("Apply Rotation Weight to Childrens")) { cb.SetRotationWeightRecursive(cb.rotationweight); } GUILayout.BeginHorizontal("Box"); cb.scalelock = EditorGUILayout.Toggle("scale lock", cb.scalelock); GUILayout.EndHorizontal(); if (GUILayout.Button("Apply Scale Lock to Childrens")) { cb.SetScaleLockRecursive(cb.scalelock); } GUILayout.BeginHorizontal("Box"); cb.scaleweight = EditorGUILayout.Slider("scale weight", cb.scaleweight, 0.0f, 1.0f); GUILayout.EndHorizontal(); if (GUILayout.Button("Apply Rotation Weight to Childrens")) { cb.SetScaleWeightRecursive(cb.scaleweight); } GUILayout.EndVertical(); }
public bool SaveSetting(string path) { if (path.Length <= 0) { Debug.Log("Save Bone Setting Fail, path is empty"); return(false); } if (!Application.isPlaying) { Debug.Log("Save Bone Setting Fail, please do it in playing mode!"); return(false); } CombinedBone[] cbs = this.GetComponentsInChildren <CombinedBone>(); StreamWriter sw = new StreamWriter(path, false); sw.WriteLine("#,bonename,rotationlock,rotationweight,poslock,posweight"); for (int i = 0; i < cbs.Length; i++) { CombinedBone cb = cbs[i]; string sline = "bone" + ","; string bonename = cb.name; sline += bonename + ","; int rotationlock = cb.rotationlock ? 1 : 0; sline += rotationlock.ToString() + ","; float rotationweight = cb.rotationweight; sline += rotationweight.ToString() + ","; int poslock = cb.poslock ? 1 : 0; sline += poslock.ToString() + ","; float posweight = cb.posweight; sline += posweight.ToString() + ","; int scalelock = cb.scalelock ? 1 : 0; sline += scalelock.ToString() + ","; float scaleweight = cb.scaleweight; sline += scaleweight.ToString() + ","; sw.WriteLine(sline); sw.Flush(); } sw.Close(); return(true); }
public void SetAll(bool rotationlock, float rotationweight, bool poslock, float posweight) { if (combinedbones.ContainsKey("Main")) { CombinedBone cb = combinedbones["Main"]; cb.SetPositionWeightRecursive(rotationweight); cb.SetPositionLockRecursive(rotationlock); cb.SetRotationWeightRecursive(posweight); cb.SetRotationLockRecursive(poslock); cb.SetScaleWeightRecursive(posweight); cb.SetScaleLockRecursive(poslock); } }
void BlendBoneSelf(CombinedBone cb, Transform roota, Transform rootb, Transform rootc) { if (cb != null) { if (cb.rotationlock)//local { cb.RotationA = cb.srca.localRotation; cb.RotationB = cb.srcb.localRotation; cb.RotationC = Quaternion.Slerp(cb.RotationA, cb.RotationB, cb.rotationweight); cb.destc.localRotation = cb.RotationC; } else { cb.RotationA = Quaternion.Inverse(roota.rotation) * cb.srca.rotation; cb.RotationB = Quaternion.Inverse(rootb.rotation) * cb.srcb.rotation; cb.RotationC = Quaternion.Slerp(cb.RotationA, cb.RotationB, cb.rotationweight); cb.destc.rotation = rootc.rotation * cb.RotationC; } if (cb.poslock)//local { cb.posA = cb.srca.localPosition; cb.posB = cb.srcb.localPosition; cb.posC = Vector3.Lerp(cb.posA, cb.posB, cb.posweight); cb.destc.localPosition = cb.posC; } else { cb.posA = roota.InverseTransformPoint(cb.srca.position); cb.posB = rootb.InverseTransformPoint(cb.srcb.position); cb.posC = Vector3.Lerp(cb.posA, cb.posB, cb.posweight); cb.destc.SetPosition(rootc.TransformPoint(cb.posC)); } if (cb.scalelock)//local { cb.scaleA = cb.srca.localScale; cb.scaleB = cb.srcb.localScale; cb.scaleC = Vector3.Lerp(cb.scaleA, cb.scaleB, cb.scaleweight); cb.destc.localScale = cb.scaleC; } else { cb.scaleA = cb.srca.localScale; cb.scaleB = cb.srcb.localScale; cb.scaleC = Vector3.Lerp(cb.scaleA, cb.scaleB, cb.scaleweight); cb.destc.localScale = cb.scaleC; } } }
public void Build() { GameObject goC = this.gameObject; if (goA == null || goB == null || goC == null) { return; } Dictionary <string, Transform> bonesA = BuildBoneMap(goA); Dictionary <string, Transform> bonesB = BuildBoneMap(goB); Dictionary <string, Transform> bonesC = BuildBoneMap(goC); foreach (KeyValuePair <string, Transform> keypair in bonesC) { string bonename = keypair.Key; Transform t = keypair.Value; float y = t.position.y; if (bonesA.ContainsKey(bonename) && bonesB.ContainsKey(bonename)) { CombinedBone cb = t.gameObject.AddComponent <CombinedBone>(); cb.destc = t; cb.srca = bonesA[bonename]; cb.srcb = bonesB[bonename]; combinedbones[bonename] = cb; } else { Debug.Log("error bone=" + bonename); } } BuildBoneOrder(); SetDefaultWeightsSetting1(combinedbones.ContainsKey("Main/RootCopy/Root_M/BackA_M")); BipedIK bipedik = GetComponent <BipedIK>(); if (bipedik != null) { Transform troot = gameObject.transform.Find("Main"); if (troot != null) { bipedik.references.root = troot; } } }
void BlendBone(Transform tbone, Transform roota, Transform rootb, Transform rootc) { CombinedBone cb = tbone.GetComponent <CombinedBone>(); BlendBoneSelf(cb, roota, rootb, rootc); int n = tbone.childCount; for (int i = 0; i < n; i++) { Transform tchild = tbone.GetChild(i); if (tchild != null) { BlendBone(tchild, roota, rootb, rootc); } } }
void BuildOrderRecursive(Transform t, ref List <CombinedBone> bones) { CombinedBone cb = t.GetComponent <CombinedBone>(); if (cb != null) { bones.Add(cb); } int n = t.childCount; for (int i = 0; i < n; i++) { Transform tchild = t.GetChild(i); if (tchild != null) { BuildOrderRecursive(tchild, ref bones); } } }
private void CalcWeights() { //set default weights float miny = 99999999.0f; float maxy = -99999999.0f; foreach (KeyValuePair <string, CombinedBone> keypair in combinedbones) { CombinedBone cb = keypair.Value; Transform t = cb.transform; float y = t.position.y; if (y < miny) { miny = y; } if (y > maxy) { maxy = y; } } foreach (KeyValuePair <string, CombinedBone> keypair in combinedbones) { CombinedBone cb = keypair.Value; Transform t = cb.transform; float y = t.position.y; cb.posweight = 1.0f; if (Mathf.Abs(maxy - miny) > 0.0f) { cb.posweight = (y - miny) / (maxy - miny); } cb.posweight = (cb.posweight - 0.5f) * a1 + 0.5f + a2; cb.posweight = Mathf.Clamp01(cb.posweight); cb.rotationweight = cb.posweight; cb.scaleweight = cb.posweight; } }
//BipedIK一直都是null,注释掉,浪费性能 //#if USE_TIMMER_BEHAVIOUR // public override void OnLateUpdate() //#else // public void LateUpdate() //#endif // { // if (!ikenable) // { // return; // } // BipedIK bipedik = GetComponent<BipedIK>(); // if (bipedik == null) return; // bipedik.LateUpdateEx(); // } public bool LoadSetting(string path) { if (path.Length <= 0) { Debug.Log("Load Bone Setting Fail, path is empty"); return(false); } if (!Application.isPlaying) { Debug.Log("Load Bone Setting Fail, please do it in playing mode!"); return(false); } if (File.Exists(path) == false) { return(false); } CombinedBone[] cbs = this.GetComponentsInChildren <CombinedBone>(); Dictionary <string, CombinedBone> cbmap = new Dictionary <string, CombinedBone>(); foreach (CombinedBone cb in cbs) { cbmap[cb.name] = cb; } string text = File.ReadAllText(path); ScpReader sr = new ScpReader(text, path); int n = sr.GetRecordCount(); for (int i = 0; i < n; i++) { string cmd = sr.GetString(i, 0, ""); if (cmd != "bone") { continue; } string bonename = sr.GetString(i, 1, ""); int rotationlock = sr.GetInt(i, 2, 0); float rotationweight = sr.GetFloat(i, 3, 0); int poslock = sr.GetInt(i, 4, 0); float posweight = sr.GetFloat(i, 5, 0); int scalelock = sr.GetInt(i, 6, 0); float scaleweight = sr.GetFloat(i, 7, 0); if (cbmap.ContainsKey(bonename)) { CombinedBone cb = cbmap[bonename]; cb.rotationlock = (rotationlock != 0); cb.rotationweight = rotationweight; cb.poslock = (poslock != 0); cb.posweight = posweight; cb.scalelock = (scalelock != 0); cb.scaleweight = scaleweight; } } return(true); }
private void createShadow() { BaseStateMachine machine = EffectParam.srcObject.GetComponent <BaseStateMachine>(); if (machine == null) { return; } GameObject srcObject = machine.getCurrentSkin(); if (srcObject == null) { return; } if (maxShadowCount > 0 && shadowCount >= maxShadowCount) { return; } if (EffectParam.srcObject && Initialize.mainCam) { //距离超过创建距离,不创建光效了。 //为了保证上帝视角和切换回英雄附近都能正确看到光效,这里同时考虑光效到英雄以及光效到摄像机的距离 Vector3 p0 = EffectParam.srcObject.transform.position; Vector3 p1 = Initialize.mainCam.transform.position; Vector3 p2 = Vector3.zero; if (EntityFactory.MainHero) { p2 = EntityFactory.MainHero.transform.position; } //不要Y p0.y = 0; p1.y = 0; p2.y = 0; Vector3 dis1 = p1 - p0; Vector3 dis2 = p2 - p0; float disSqr = LightingEffectFactory.CreateDistance * LightingEffectFactory.CreateDistance; if (EffectConfig.EnableDistanceOptimtized && (dis1.sqrMagnitude > disSqr) && (dis2.sqrMagnitude > disSqr)) { return; } } //距离太近就不创建了 if (shadowConfig.minDistance > 0.01f && shadowInstanceLists.Count > 0) { GameObject go = shadowInstanceLists[shadowInstanceLists.Count - 1].gameObject; if (go != null) { Vector3 lastPos = go.transform.position; Vector3 currentPos = srcObject.transform.position; Vector3 deta = currentPos - lastPos; if (deta.magnitude < shadowConfig.minDistance) { return; } } } //复制skin GameObject newObject = ResNode.InstantiateRes(srcObject, true); newObject.transform.SetPosition(srcObject.transform.position); newObject.transform.rotation = srcObject.transform.rotation; Transform cloneTransform = newObject.transform; cloneTransform.parent = EntityFactory.EffectGroup.transform; newObject.name = srcObject.name + "(effect" + getID() + ")"; //删除多余的组件 deleteComponent(cloneTransform); CombinedBone[] cbs = cloneTransform.GetComponentsInChildren <CombinedBone>(); foreach (CombinedBone bone in cbs) { CombinedBone temp = bone; ResNode.DestroyRes(ref temp); } int childIndex = 0; while (childIndex < cloneTransform.childCount) { Transform child = cloneTransform.GetChild(childIndex); if (child.name == "Main" || child.name == "MAIN" || child.name == "main") { childIndex++; } else if (child.name.Length >= 5 && child.name.Substring(0, 5) == "group") { childIndex++; } else { child.parent = null; var temp1 = child.gameObject; ResNode.DestroyRes(ref temp1); } } if (shadowConfig.DisableWeapon == true) //删武器 { Transform weapon = FindTransformRoot(cloneTransform, "W_RA"); if (weapon != null) { var temp = weapon.gameObject; ResNode.DestroyRes(ref temp); } weapon = FindTransformRoot(cloneTransform, "W_LA"); if (weapon != null) { var temp = weapon.gameObject; ResNode.DestroyRes(ref temp); } } ShadowInstance newInstance = new ShadowInstance(); newInstance.gameObject = newObject; newInstance.createTick = currentTick; newInstance.alpha = 255.0f; //改shader支持透明 newInstance.renders.AddRange(newObject.GetComponentsInChildren <SkinnedMeshRenderer>()); newInstance.renders.AddRange(newObject.GetComponentsInChildren <MeshRenderer>()); foreach (Renderer r in newInstance.renders) { if (r != null) { if (shadowConfig.alphaMaterial != null) { Material newMaterial = new Material(shadowConfig.alphaMaterial); r.material = newMaterial; if (shadowConfig.alphaAttributeType == LightingEffect_Shadow.AlphaAttributeType.Color && shadowConfig.alphaAttributeName != "") { matColor = r.material.GetColor(shadowConfig.alphaAttributeName); } } else if (r.materials != null) { if (r.materials.Length > 1) //多层材质叠加会变成白色 { Material[] newMaterials = new Material[1]; newMaterials[0] = r.materials[0]; r.materials = newMaterials; } foreach (Material mat in r.materials) { mat.shader = AssetBundleManager.EntityAlphaShader; if (mat.HasProperty("_Alpha")) { mat.SetFloat("_Alpha", shadowConfig.fStartAlpha); } } } } } shadowInstanceLists.Add(newInstance); shadowCount++; if (shadowConfig.minDistance > 0.01f && newInstance.gameObject != null) { newInstance.gameObject.SetActive(false); } }