public void SaveSnapShot() { Debug.Assert(m_particleSnapShot.Count() == 0); Debug.Assert(m_collisionSnapShot.Count() == 0); foreach (GameObject gameObject in m_particleEntities.Values) { DynamicParticleComponent particle = gameObject.GetComponent <DynamicParticleComponent>(); m_particleSnapShot.Add(particle.ParticleInfo.VertInfo.VertID, gameObject.transform.position); } foreach (GameObject gameObject in m_collisionEntities.Values) { DynamicCollisionComponent collision = gameObject.GetComponent <DynamicCollisionComponent>(); CollisionInfoDefinition def = collision.CollisionInfo.CollisionInfoDefinition; m_collisionSnapShot.Add(def.Name, new CollisionSnapShotData { PositionOffset = def.PositionOffset, RotationOffset = def.RotationOffset, Length = def.Length, Radius = def.Radius }); } }
public void ApplyBlendOffsets() { BlendShapeLoader loader = BlendShapeLoader.GetComponent <BlendShapeLoader>(); Dictionary <string, float> blendValues = loader.GetBlendShapeValues(); foreach (GameObject gameObject in m_particleEntities.Values) { DynamicParticleComponent particle = gameObject.GetComponent <DynamicParticleComponent>(); BodyShapeOffSetTable offsetsTable = particle.ParticleInfo.VertInfo.BodyShapeOffsetTable; Vector3 offset = Vector3.zero; Vector3 original = m_particleSnapShot[particle.ParticleInfo.VertInfo.VertID]; offset = original; foreach (KeyValuePair <string, Vector3> kvp in offsetsTable.Definitions) { if (blendValues.TryGetValue(kvp.Key, out float blendValue)) { offset += Vector3.Lerp(original, kvp.Value, blendValue) - original; } } gameObject.transform.position = offset; } foreach (GameObject gameObject in m_collisionEntities.Values) { DynamicCollisionComponent collision = gameObject.GetComponent <DynamicCollisionComponent>(); CollisionInfoShapeOffsets offsetsTable = collision.CollisionInfo.CollisionInfoDefinition.BodyShapeOffSets; } }
public void GenerateCollisionFromConfig() { List <CollisionInfo> collisionInfo = m_config.GenerateCollisionFromConfig(); GameObject rootCollision = GameObject.Find("Collision"); if (rootCollision == null) { rootCollision = new GameObject(); rootCollision.name = "Collision"; rootCollision.transform.position = Vector3.zero; rootCollision.transform.SetParent(transform); } foreach (CollisionInfo info in collisionInfo) { GameObject gameObject = new GameObject(); DynamicCollisionComponent collision = gameObject.AddComponent <DynamicCollisionComponent>(); gameObject.name = "Collision Name : " + info.CollisionInfoDefinition.Name; collision.ClothSimEntity = this; collision.CollisionInfo = info; m_collisionEntities.Add(info.CollisionInfoDefinition.Name, gameObject); gameObject.transform.SetParent(transform); Transform boneTransform = transform.FindRecursive(info.CollisionInfoDefinition.BoneName.ToLower()); if (boneTransform != null) { GameObject dummyObject = new GameObject(); dummyObject.transform.position = boneTransform.position; dummyObject.transform.SetParent(boneTransform); dummyObject.transform.localPosition = info.CollisionInfoDefinition.PositionOffset; dummyObject.transform.localEulerAngles = info.CollisionInfoDefinition.RotationOffset; gameObject.transform.rotation = dummyObject.transform.rotation; gameObject.transform.position = dummyObject.transform.position; Vector3 euler = collision.GetEulerAnglesDegrees(gameObject.transform.localRotation); collision.DummyObject = dummyObject; } gameObject.transform.SetParent(rootCollision.transform); } }
public override void OnInspectorGUI() { DynamicCollisionComponent dynamicCollision = (DynamicCollisionComponent)target; ClothSimEntity clothSimEntity = dynamicCollision.ClothSimEntity; CollisionInfoDefinition collisionInfo = dynamicCollision.CollisionInfo.CollisionInfoDefinition; collisionInfo.Name = EditorGUILayout.TextField("Name", collisionInfo.Name); collisionInfo.BoneName = EditorGUILayout.TextField("Bone Name", collisionInfo.BoneName); int index = options.IndexOf(collisionInfo.CollisionType); index = EditorGUILayout.Popup("Collision Type", index, options.ToArray()); collisionInfo.CollisionType = options[index]; if (collisionInfo.CollisionType == "capsule") { collisionInfo.Length = EditorGUILayout.FloatField("Length", collisionInfo.Length); } collisionInfo.Radius = EditorGUILayout.FloatField("Radius", collisionInfo.Radius); collisionInfo.PositionOffset = EditorGUILayout.Vector3Field("Offset", collisionInfo.PositionOffset); Vector3 r = collisionInfo.RotationOffset; if (r.x > 180.0f) { r.x -= 360f; } if (r.y > 180.0f) { r.y -= 360f; } if (r.z > 180.0f) { r.z -= 360f; } EditorGUILayout.Vector3Field("Rotation", r); BlendShapeEditor(clothSimEntity, dynamicCollision, dynamicCollision.CollisionInfo.CollisionInfoDefinition.BodyShapeOffSets); }
public void RestoreSnapShot() { foreach (KeyValuePair <int, Vector3> kvp in m_particleSnapShot) { GameObject gameObject = m_particleEntities[kvp.Key]; gameObject.transform.position = kvp.Value; } foreach (KeyValuePair <string, CollisionSnapShotData> kvp in m_collisionSnapShot) { GameObject gameObject = m_collisionEntities[kvp.Key]; DynamicCollisionComponent collision = gameObject.GetComponent <DynamicCollisionComponent>(); CollisionInfoDefinition def = collision.CollisionInfo.CollisionInfoDefinition; def.PositionOffset = kvp.Value.PositionOffset; def.RotationOffset = kvp.Value.RotationOffset; def.Length = kvp.Value.Length; def.Radius = kvp.Value.Radius; } m_particleSnapShot.Clear(); m_collisionSnapShot.Clear(); }
private void BlendShapeEditor(ClothSimEntity clothSimEntity, DynamicCollisionComponent dynamicCollision, CollisionInfoShapeOffsets offsetsTable) { EditorGUILayout.LabelField("", GUI.skin.horizontalSlider); ShapeRenderer shapeRenderer = clothSimEntity.ShapeRenderer.GetComponent <ShapeRenderer>(); BlendShapeLoader blendShapeLoader = clothSimEntity.BlendShapeLoader.GetComponent <BlendShapeLoader>(); string[] opts = offsetsTable.BodyShapeOffsets.Keys.ToArray(); m_currentOffsetsIndex = EditorGUILayout.Popup(m_currentOffsetsIndex, opts); Material modelMaterial = clothSimEntity.Model.GetComponentInChildren <SkinnedMeshRenderer>().material; bool isCapsule = dynamicCollision.CollisionInfo.CollisionInfoDefinition.CollisionType == "capsule"; if (opts.Count() > 0) { EditorGUILayout.BeginHorizontal(); //this is Blah if (GUILayout.Button("Edit")) { if (!m_isEditMode) { if (offsetsTable.BodyShapeOffsets.TryGetValue(opts[m_currentOffsetsIndex], out CollisionInfoDefinition outDef)) { float radius = dynamicCollision.CollisionInfo.CollisionInfoDefinition.Radius; float length = isCapsule ? dynamicCollision.CollisionInfo.CollisionInfoDefinition.Length : 0.0f; shapeRenderer.Initialise(isCapsule ? Shape.ShapeType.Capsule : Shape.ShapeType.Sphere, dynamicCollision.transform.rotation, dynamicCollision.transform.position, radius, length); blendShapeLoader.ClearBlendShapes(); blendShapeLoader.SetBlendShapeActive(true); blendShapeLoader.SetBlendShapeValue(opts[m_currentOffsetsIndex], 1.0f); dynamicCollision.DummyObject.transform.localPosition = outDef.PositionOffset; //dynamicCollision.transform.position = dynamicCollision.DummyObject.transform.position; dynamicCollision.DummyObject.transform.localEulerAngles = outDef.RotationOffset; dynamicCollision.transform.rotation = dynamicCollision.DummyObject.transform.rotation; dynamicCollision.CollisionInfo.CollisionInfoDefinition.Radius = outDef.Radius; if (isCapsule) { dynamicCollision.CollisionInfo.CollisionInfoDefinition.Length = outDef.Length; } modelMaterial.color = ClothSimEntity.Translucient; m_isEditMode = true; } } else { dynamicCollision.transform.position = shapeRenderer.transform.position; dynamicCollision.transform.rotation = shapeRenderer.transform.rotation; dynamicCollision.CollisionInfo.CollisionInfoDefinition.Length = shapeRenderer.ShapeDefinition.Length; dynamicCollision.CollisionInfo.CollisionInfoDefinition.Radius = shapeRenderer.ShapeDefinition.Radius; shapeRenderer.Clear(); m_isEditMode = false; blendShapeLoader.ClearBlendShapes(); modelMaterial.color = ClothSimEntity.Opaque; } } EditorGUILayout.EndHorizontal(); if (m_isEditMode) { dynamicCollision.DummyObject.transform.localPosition = EditorGUILayout.Vector3Field("Position Offset", dynamicCollision.DummyObject.transform.localPosition); Vector3 eulerAngles = dynamicCollision.GetEulerAnglesDegrees(dynamicCollision.DummyObject.transform.localRotation); dynamicCollision.DummyObject.transform.localEulerAngles = EditorGUILayout.Vector3Field("Rotation Offset", eulerAngles); if (isCapsule) { dynamicCollision.CollisionInfo.CollisionInfoDefinition.Length = EditorGUILayout.FloatField("Length", dynamicCollision.CollisionInfo.CollisionInfoDefinition.Length); } dynamicCollision.CollisionInfo.CollisionInfoDefinition.Radius = EditorGUILayout.FloatField("Radius", dynamicCollision.CollisionInfo.CollisionInfoDefinition.Radius); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Save")) { CollisionInfoDefinition collisionInfoDefinition = offsetsTable.BodyShapeOffsets[opts[m_currentOffsetsIndex]]; collisionInfoDefinition.PositionOffset = dynamicCollision.DummyObject.transform.localPosition; Vector3 saveEulerAngles = dynamicCollision.GetEulerAnglesDegrees(dynamicCollision.DummyObject.transform.localRotation); collisionInfoDefinition.RotationOffset = saveEulerAngles; collisionInfoDefinition.Radius = dynamicCollision.CollisionInfo.CollisionInfoDefinition.Radius; if (isCapsule) { collisionInfoDefinition.Length = dynamicCollision.CollisionInfo.CollisionInfoDefinition.Length; } //restore the original values. dynamicCollision.CollisionInfo.CollisionInfoDefinition.Radius = shapeRenderer.ShapeDefinition.Radius; dynamicCollision.CollisionInfo.CollisionInfoDefinition.Length = shapeRenderer.ShapeDefinition.Length; dynamicCollision.transform.position = shapeRenderer.transform.position; dynamicCollision.transform.rotation = shapeRenderer.transform.rotation; shapeRenderer.Clear(); blendShapeLoader.ClearBlendShapes(); modelMaterial.color = ClothSimEntity.Opaque; m_isEditMode = false; } EditorGUILayout.EndHorizontal(); } } EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Add Offset")) { } if (GUILayout.Button("Remove Offset")) { } EditorGUILayout.EndHorizontal(); EditorGUILayout.LabelField("", GUI.skin.horizontalSlider); }