private void AddFrame(ModelObject modelObject, GameObject frame) { Mesh myMesh = modelObject.GetComponent <MeshFilter> ().sharedMesh; Mesh frameMesh = new Mesh(); int[] oldTrianges = myMesh.GetTriangles(0); int count = 0; Dictionary <int, int> dictionary = new Dictionary <int, int> (); for (int x = 0; x < oldTrianges.Length; x++) { int current = oldTrianges [x]; if (!dictionary.ContainsKey(current)) { dictionary.Add(current, count); count = count + 1; } } int[] newTriangles = new int[oldTrianges.Length]; for (int x = 0; x < oldTrianges.Length; x++) { newTriangles [x] = dictionary [oldTrianges [x]]; } Vector3[] oldVerts = myMesh.vertices; Vector3[] newVerts = new Vector3[count]; foreach (KeyValuePair <int, int> pair in dictionary) { int oldVertIndex = pair.Key; int newVertIndex = pair.Value; newVerts [newVertIndex] = oldVerts [oldVertIndex]; } frameMesh.vertices = newVerts; frameMesh.triangles = newTriangles; Vector2[] uvs = new Vector2[newVerts.Length]; for (int i = 0; i < newVerts.Length; i++) { uvs [i] = new Vector2(newVerts [i].x, newVerts [i].z); } frameMesh.uv = uvs; frameMesh.RecalculateNormals(); frameMesh.Optimize(); frame.GetComponent <MeshFilter>().mesh = frameMesh; frame.GetComponent <MeshRenderer>().material = modelObject.GetComponent <MeshRenderer>().sharedMaterials[0]; }
public override void OnInspectorGUI() { if (GUILayout.Button("Replicate materials in objects of same type")) { ModelObject gameObject = (ModelObject)target; Material[] mats = gameObject.GetComponent <MeshRenderer>().sharedMaterials; List <ModelObject> objs = new List <ModelObject>(FindObjectsOfType <ModelObject>()); objs.ForEach(o => { if (o.GetComponent <ModelObject>().Name == gameObject.Name && o.GetComponent <MeshRenderer>().sharedMaterials.Length == mats.Length) { o.GetComponent <MeshRenderer>().materials = mats; } }); } GUILayout.Space(10); DrawDefaultInspector(); }
public Vector3 SnapSgo() { if (!snap) { return(Vector3.zero); } var o1 = sgo.GetComponent <MeshOutline>(); o1.Init(); var bounds = o1.mf.renderer.bounds; bounds.Expand(3); List <Vector3> offsets = new List <Vector3>(); foreach (var o2 in FindObjectsOfType <MeshOutline>().Where(a => a != o1 && a.Init().mf.renderer.bounds.Intersects(bounds))) { o2.Init(); foreach (MyVert b in o1.meshFilter.myVerts) { Debug.DrawRay(b.v, Vector3.up, Color.blue, 10); var v = o2.meshFilter.myVerts.Where(a => (b.v - a.v).magnitude < 2).OrderBy(a => (b.v - a.v).magnitude).FirstOrDefault(); if (v != null) { offsets.Add(v.v - b.v); Debug.DrawLine(b.v, v.v, Color.red, 10); } } } var gr = offsets.GroupBy(a => a); var g = gr.OrderByDescending(a => a.Count()).FirstOrDefault(); if (g != null) { print("Found"); return(ZeroY(g.FirstOrDefault())); } return(Vector3.zero); }
private void AddComponentDoorPanel(ModelObject modelObject, GameObject panel, int submeshIndex) { GameObject component = new GameObject("Component " + submeshIndex); component.transform.parent = panel.transform; component.transform.localScale = new Vector3(1, 1, 1); component.transform.localPosition = new Vector3(0, 0, 0); component.transform.localRotation = new Quaternion(); MeshFilter meshFilter = component.AddComponent <MeshFilter>(); MeshCollider meshCollider = component.AddComponent <MeshCollider>(); MeshRenderer meshRenderer = component.AddComponent <MeshRenderer>(); Mesh myMesh = modelObject.GetComponent <MeshFilter>().sharedMesh; // List<Vector2> myUVs = new List<Vector2> (); // myMesh.GetUVs(submeshIndex,myUVs); // Mesh panelMesh = new Mesh(); int[] oldTrianges = myMesh.GetTriangles(submeshIndex); int count = 0; Dictionary <int, int> dictionary = new Dictionary <int, int>(); for (int x = 0; x < oldTrianges.Length; x++) { int current = oldTrianges[x]; if (!dictionary.ContainsKey(current)) { dictionary.Add(current, count); count = count + 1; } } int[] newTriangles = new int[oldTrianges.Length]; for (int x = 0; x < oldTrianges.Length; x++) { newTriangles[x] = dictionary[oldTrianges[x]]; } Vector3[] oldVerts = myMesh.vertices; Vector3[] newVerts = new Vector3[count]; foreach (KeyValuePair <int, int> pair in dictionary) { int oldVertIndex = pair.Key; int newVertIndex = pair.Value; newVerts[newVertIndex] = oldVerts[oldVertIndex]; } panelMesh.vertices = newVerts; panelMesh.triangles = newTriangles; // panelMesh.SetUVs(0, myUVs); Vector2[] uvs = new Vector2[newVerts.Length]; for (int i = 0; i < newVerts.Length; i++) { uvs [i] = new Vector2(newVerts [i].x, newVerts [i].z); } panelMesh.uv = uvs; //panelMesh.RecalculateBounds(); panelMesh.RecalculateNormals(); panelMesh.Optimize(); meshRenderer.material = modelObject.GetComponent <MeshRenderer>().sharedMaterials[submeshIndex]; meshFilter.mesh = panelMesh; meshCollider.sharedMesh = panelMesh; }
public void UpdateDoorElement(GameObject obj) { ModelObject modelObject = obj.GetComponent <ModelObject>(); if (modelObject.Name.Contains("Double") || modelObject.Name.Contains("Dbl")) { return; } int frameCounting = 0; if (modelObject.HasFrame()) { GameObject frame = new GameObject("Door Frame [" + modelObject.Id + "]"); frame.transform.parent = modelObject.transform; frame.transform.localScale = new Vector3(1, 1, 1); frame.transform.localPosition = new Vector3(0, 0, 0); frame.transform.localRotation = new Quaternion(); frame.AddComponent <MeshFilter>(); frame.AddComponent <MeshCollider>(); frame.AddComponent <MeshRenderer>(); AddFrame(modelObject, frame); frameCounting = 1; } GameObject panel = new GameObject("Door Panel [" + modelObject.Id + "]"); panel.transform.parent = modelObject.transform; panel.transform.localScale = new Vector3(1, 1, 1); panel.transform.localPosition = new Vector3(0, 0, 0); panel.transform.localRotation = new Quaternion(); BoxCollider box = panel.GetComponent <BoxCollider> (); if (box == null) { box = panel.AddComponent <BoxCollider> (); } box.isTrigger = true; box.size = new Vector3(1, 1, 2); //box.center = new Vector3 (0, 0, 0); for (int i = 0 + frameCounting; i < modelObject.GetComponent <MeshFilter>().sharedMesh.subMeshCount; i++) { AddComponentDoorPanel(modelObject, panel, i); } DoorPanel doorPanel = panel.GetComponent <DoorPanel>(); if (doorPanel == null) { doorPanel = panel.AddComponent <DoorPanel>(); } modelObject.GetComponent <MeshRenderer>().enabled = false; modelObject.GetComponent <MeshCollider>().enabled = false; doorPanel.UpdateOpeningDirection(); if (panel.GetComponent <DoorScript> () == null) { panel.AddComponent <DoorScript> (); } }
// Start is called before the first frame update void Start() { bFrozen = true; jointPoints = new JointPoint[(int)JointIndex.COUNT]; for (var i = 0; i < (int)JointIndex.COUNT; i++) { jointPoints[i] = new JointPoint(); } //choose active model GameObject ModelObject; Renderer[] renderers1, renderers2, renderers3; switch (activeModel) { case 1: ModelObject = ModelObject1; renderers1 = ModelObject2.GetComponentsInChildren <Renderer>(); renderers2 = ModelObject3.GetComponentsInChildren <Renderer>(); renderers3 = ModelObject4.GetComponentsInChildren <Renderer>(); break; case 2: ModelObject = ModelObject2; renderers1 = ModelObject1.GetComponentsInChildren <Renderer>(); renderers2 = ModelObject3.GetComponentsInChildren <Renderer>(); renderers3 = ModelObject4.GetComponentsInChildren <Renderer>(); break; case 3: ModelObject = ModelObject3; renderers1 = ModelObject2.GetComponentsInChildren <Renderer>(); renderers2 = ModelObject1.GetComponentsInChildren <Renderer>(); renderers3 = ModelObject4.GetComponentsInChildren <Renderer>(); break; case 4: ModelObject = ModelObject4; renderers1 = ModelObject2.GetComponentsInChildren <Renderer>(); renderers2 = ModelObject3.GetComponentsInChildren <Renderer>(); renderers3 = ModelObject1.GetComponentsInChildren <Renderer>(); break; default: ModelObject = ModelObject1; renderers1 = ModelObject2.GetComponentsInChildren <Renderer>(); renderers2 = ModelObject3.GetComponentsInChildren <Renderer>(); renderers3 = ModelObject4.GetComponentsInChildren <Renderer>(); break; } foreach (Renderer r in renderers1) { r.enabled = false; } foreach (Renderer r in renderers2) { r.enabled = false; } foreach (Renderer r in renderers3) { r.enabled = false; } // anim = ModelObject.GetComponent <Animator>(); // Right Arm jointPoints[(int)JointIndex.RIGHT_SHOULDER].Transform = anim.GetBoneTransform(HumanBodyBones.RightUpperArm); jointPoints[(int)JointIndex.RIGHT_ELBOW].Transform = anim.GetBoneTransform(HumanBodyBones.RightLowerArm); jointPoints[(int)JointIndex.RIGHT_WRIST].Transform = anim.GetBoneTransform(HumanBodyBones.RightHand); jointPoints[(int)JointIndex.RIGHT_THUMB].Transform = anim.GetBoneTransform(HumanBodyBones.RightThumbIntermediate); jointPoints[(int)JointIndex.RIGHT_MID].Transform = anim.GetBoneTransform(HumanBodyBones.RightMiddleProximal); // Left Arm jointPoints[(int)JointIndex.LEFT_SHOULDER].Transform = anim.GetBoneTransform(HumanBodyBones.LeftUpperArm); jointPoints[(int)JointIndex.LEFT_ELBOW].Transform = anim.GetBoneTransform(HumanBodyBones.LeftLowerArm); jointPoints[(int)JointIndex.LEFT_WRIST].Transform = anim.GetBoneTransform(HumanBodyBones.LeftHand); jointPoints[(int)JointIndex.LEFT_THUMB].Transform = anim.GetBoneTransform(HumanBodyBones.LeftThumbIntermediate); jointPoints[(int)JointIndex.LEFT_MID].Transform = anim.GetBoneTransform(HumanBodyBones.LeftMiddleProximal); // Face jointPoints[(int)JointIndex.LEFT_EAR].Transform = anim.GetBoneTransform(HumanBodyBones.Head); jointPoints[(int)JointIndex.LEFT_EYE].Transform = anim.GetBoneTransform(HumanBodyBones.LeftEye); jointPoints[(int)JointIndex.RIGHT_EAR].Transform = anim.GetBoneTransform(HumanBodyBones.Head); jointPoints[(int)JointIndex.RIGHT_EYE].Transform = anim.GetBoneTransform(HumanBodyBones.RightEye); jointPoints[(int)JointIndex.NOSE].Transform = Nose.transform; // Right Leg jointPoints[(int)JointIndex.RIGHT_HIP].Transform = anim.GetBoneTransform(HumanBodyBones.RightUpperLeg); jointPoints[(int)JointIndex.RIGHT_KNEE].Transform = anim.GetBoneTransform(HumanBodyBones.RightLowerLeg); jointPoints[(int)JointIndex.RIGHT_ANKLE].Transform = anim.GetBoneTransform(HumanBodyBones.RightFoot); jointPoints[(int)JointIndex.RIGHT_TOE].Transform = anim.GetBoneTransform(HumanBodyBones.RightToes); // Left Leg jointPoints[(int)JointIndex.LEFT_HIP].Transform = anim.GetBoneTransform(HumanBodyBones.LeftUpperLeg); jointPoints[(int)JointIndex.LEFT_KNEE].Transform = anim.GetBoneTransform(HumanBodyBones.LeftLowerLeg); jointPoints[(int)JointIndex.LEFT_ANKLE].Transform = anim.GetBoneTransform(HumanBodyBones.LeftFoot); jointPoints[(int)JointIndex.LEFT_TOE].Transform = anim.GetBoneTransform(HumanBodyBones.LeftToes); // etc jointPoints[(int)JointIndex.ABDOMEN_UPPER].Transform = anim.GetBoneTransform(HumanBodyBones.Spine); jointPoints[(int)JointIndex.HIP].Transform = anim.GetBoneTransform(HumanBodyBones.Hips); jointPoints[(int)JointIndex.HEAD].Transform = anim.GetBoneTransform(HumanBodyBones.Head); jointPoints[(int)JointIndex.NECK].Transform = anim.GetBoneTransform(HumanBodyBones.Neck); jointPoints[(int)JointIndex.SPINE].Transform = anim.GetBoneTransform(HumanBodyBones.Spine); // Child Settings // Right Arm jointPoints[(int)JointIndex.RIGHT_SHOULDER].Child = jointPoints[(int)JointIndex.RIGHT_ELBOW]; jointPoints[(int)JointIndex.RIGHT_ELBOW].Child = jointPoints[(int)JointIndex.RIGHT_WRIST]; jointPoints[(int)JointIndex.RIGHT_ELBOW].Parent = jointPoints[(int)JointIndex.RIGHT_SHOULDER]; // Left Arm jointPoints[(int)JointIndex.LEFT_SHOULDER].Child = jointPoints[(int)JointIndex.LEFT_ELBOW]; jointPoints[(int)JointIndex.LEFT_ELBOW].Child = jointPoints[(int)JointIndex.LEFT_WRIST]; jointPoints[(int)JointIndex.LEFT_ELBOW].Parent = jointPoints[(int)JointIndex.LEFT_SHOULDER]; // Right Leg jointPoints[(int)JointIndex.RIGHT_HIP].Child = jointPoints[(int)JointIndex.RIGHT_KNEE]; jointPoints[(int)JointIndex.RIGHT_KNEE].Child = jointPoints[(int)JointIndex.RIGHT_ANKLE]; jointPoints[(int)JointIndex.RIGHT_ANKLE].Child = jointPoints[(int)JointIndex.RIGHT_TOE]; jointPoints[(int)JointIndex.RIGHT_ANKLE].Parent = jointPoints[(int)JointIndex.RIGHT_KNEE]; // Left Leg jointPoints[(int)JointIndex.LEFT_HIP].Child = jointPoints[(int)JointIndex.LEFT_KNEE]; jointPoints[(int)JointIndex.LEFT_KNEE].Child = jointPoints[(int)JointIndex.LEFT_ANKLE]; jointPoints[(int)JointIndex.LEFT_ANKLE].Child = jointPoints[(int)JointIndex.LEFT_TOE]; //jointPoints[(int)JointIndex.LEFT_ANKLE].Parent = jointPoints[(int)JointIndex.LEFT_KNEE]; // etc jointPoints[(int)JointIndex.SPINE].Child = jointPoints[(int)JointIndex.NECK]; jointPoints[(int)JointIndex.NECK].Child = jointPoints[(int)JointIndex.HEAD]; useSkeleton = ShowSkeleton; if (useSkeleton) { // Line Child Settings // Right Arm AddSkeleton(JointIndex.RIGHT_SHOULDER, JointIndex.RIGHT_ELBOW); AddSkeleton(JointIndex.RIGHT_ELBOW, JointIndex.RIGHT_WRIST); AddSkeleton(JointIndex.RIGHT_WRIST, JointIndex.RIGHT_THUMB); AddSkeleton(JointIndex.RIGHT_WRIST, JointIndex.RIGHT_MID); // Left Arm AddSkeleton(JointIndex.LEFT_SHOULDER, JointIndex.LEFT_ELBOW); AddSkeleton(JointIndex.LEFT_ELBOW, JointIndex.LEFT_WRIST); AddSkeleton(JointIndex.LEFT_WRIST, JointIndex.LEFT_THUMB); AddSkeleton(JointIndex.LEFT_WRIST, JointIndex.LEFT_MID); // Fase AddSkeleton(JointIndex.LEFT_EAR, JointIndex.NOSE); AddSkeleton(JointIndex.RIGHT_EAR, JointIndex.NOSE); // Right Leg AddSkeleton(JointIndex.RIGHT_HIP, JointIndex.RIGHT_KNEE); AddSkeleton(JointIndex.RIGHT_KNEE, JointIndex.RIGHT_ANKLE); AddSkeleton(JointIndex.RIGHT_ANKLE, JointIndex.RIGHT_TOE); // Left Leg AddSkeleton(JointIndex.LEFT_HIP, JointIndex.LEFT_KNEE); AddSkeleton(JointIndex.LEFT_KNEE, JointIndex.LEFT_ANKLE); AddSkeleton(JointIndex.LEFT_ANKLE, JointIndex.LEFT_TOE); // etc AddSkeleton(JointIndex.SPINE, JointIndex.NECK); AddSkeleton(JointIndex.NECK, JointIndex.HEAD); AddSkeleton(JointIndex.HEAD, JointIndex.NOSE); AddSkeleton(JointIndex.NECK, JointIndex.RIGHT_SHOULDER); AddSkeleton(JointIndex.NECK, JointIndex.LEFT_SHOULDER); AddSkeleton(JointIndex.RIGHT_HIP, JointIndex.RIGHT_SHOULDER); AddSkeleton(JointIndex.LEFT_HIP, JointIndex.LEFT_SHOULDER); AddSkeleton(JointIndex.RIGHT_SHOULDER, JointIndex.ABDOMEN_UPPER); AddSkeleton(JointIndex.LEFT_SHOULDER, JointIndex.ABDOMEN_UPPER); AddSkeleton(JointIndex.RIGHT_HIP, JointIndex.ABDOMEN_UPPER); AddSkeleton(JointIndex.LEFT_HIP, JointIndex.ABDOMEN_UPPER); AddSkeleton(JointIndex.LEFT_HIP, JointIndex.RIGHT_HIP); } // Set Inverse var forward = TriangleNormal(jointPoints[(int)JointIndex.HIP].Transform.position, jointPoints[(int)JointIndex.LEFT_HIP].Transform.position, jointPoints[(int)JointIndex.RIGHT_HIP].Transform.position); foreach (var jointPoint in jointPoints) { if (jointPoint.Transform != null) { jointPoint.InitRotation = jointPoint.Transform.rotation; } if (jointPoint.Child != null) { jointPoint.Inverse = GetInverse(jointPoint, jointPoint.Child, forward); jointPoint.InverseRotation = jointPoint.Inverse * jointPoint.InitRotation; } } var hip = jointPoints[(int)JointIndex.HIP]; initPosition = jointPoints[(int)JointIndex.HIP].Transform.position; hip.Inverse = Quaternion.Inverse(Quaternion.LookRotation(forward)); hip.InverseRotation = hip.Inverse * hip.InitRotation; // For Head Rotation var head = jointPoints[(int)JointIndex.HEAD]; head.InitRotation = jointPoints[(int)JointIndex.HEAD].Transform.rotation; var gaze = jointPoints[(int)JointIndex.NOSE].Transform.position - jointPoints[(int)JointIndex.HEAD].Transform.position; head.Inverse = Quaternion.Inverse(Quaternion.LookRotation(gaze)); head.InverseRotation = head.Inverse * head.InitRotation; var lHand = jointPoints[(int)JointIndex.LEFT_WRIST]; var lf = TriangleNormal(lHand.Pos3D, jointPoints[(int)JointIndex.LEFT_MID].Pos3D, jointPoints[(int)JointIndex.LEFT_THUMB].Pos3D); lHand.InitRotation = lHand.Transform.rotation; lHand.Inverse = Quaternion.Inverse(Quaternion.LookRotation(jointPoints[(int)JointIndex.LEFT_THUMB].Transform.position - jointPoints[(int)JointIndex.LEFT_MID].Transform.position, lf)); lHand.InverseRotation = lHand.Inverse * lHand.InitRotation; var rHand = jointPoints[(int)JointIndex.RIGHT_WRIST]; var rf = TriangleNormal(rHand.Pos3D, jointPoints[(int)JointIndex.RIGHT_THUMB].Pos3D, jointPoints[(int)JointIndex.RIGHT_MID].Pos3D); rHand.InitRotation = rHand.Transform.rotation; rHand.Inverse = Quaternion.Inverse(Quaternion.LookRotation(jointPoints[(int)JointIndex.RIGHT_THUMB].Transform.position - jointPoints[(int)JointIndex.RIGHT_MID].Transform.position, rf)); rHand.InverseRotation = rHand.Inverse * rHand.InitRotation; }
protected virtual void Awake() { ModelObject = transform.GetChild(0).gameObject; RigidBody = ModelObject.GetComponent <Rigidbody2D>(); Hitbox = ModelObject.GetComponent <CapsuleCollider2D>(); }