private void PostGenerate(MeshFilter mf) { if (m_ForEditor) { m_MeshMgr.m_ColliderDirty = true; } else { if (m_CreateBoxCollider) { BoxCollider bc = mf.gameObject.GetComponent <BoxCollider>(); if (bc != null) { BoxCollider.DestroyImmediate(bc); } RecalcCreationMeshBounds(mf.mesh); bc = mf.gameObject.AddComponent <BoxCollider>(); bc.size *= WhiteCat.PEVCConfig.instance.creationColliderScale; bc.material = WhiteCat.PEVCConfig.instance.physicMaterial; } // particle VCParticlePlayer pp = mf.GetComponent <VCParticlePlayer>(); pp.LocalPosition = VCUtils.RandPosInBoundingBox(mf.mesh.bounds); } var cc = mf.GetComponentInParent <WhiteCat.CreationController>(); if (cc) { cc.OnNewMeshBuild(mf); } }
void SetupDisplayModel(Mesh mesh) { displayModel.transform.localPosition = Vector3.zero; displayModel.transform.localScale = Vector3.one; displayModel.transform.localRotation = Quaternion.identity; MeshRenderer displayRenderer = displayModel.GetComponent <MeshRenderer>(); displayModel.sharedMesh = mesh; Bounds bounds = displayRenderer.bounds; if (bounds.size.y > bounds.size.z) { displayModel.transform.rotation = Quaternion.Euler(-90, 0, 0); bounds = displayRenderer.bounds; } Bounds startBounds = bounds; Bounds fitBounds = displayModel.GetComponentInParent <BoxCollider>().bounds; Vector3 ratios = Vector3.zero; ratios.x = bounds.size.x / fitBounds.size.x; ratios.y = bounds.size.y / fitBounds.size.y; ratios.z = bounds.size.z / fitBounds.size.z; float maxRatio = Mathf.Max(ratios.x, ratios.y, ratios.z); bounds.size /= maxRatio; displayModel.transform.localScale = Vector3.one / maxRatio; bounds.center = fitBounds.center + Vector3.up * (bounds.size.y / 2 - fitBounds.size.y / 2); displayModel.transform.position += bounds.center - startBounds.center; }
public void Highlight(MeshFilter filter, SelectionType selectionType) { if (filter == null) { return; } RigidBody rb = filter.GetComponentInParent <RigidBody>(); Color color; if (rb != null) { Colorize(rb); color = ColorizeMeshFilters(rb); } else { color = MeshFilterColor; } m_meshColors[filter] = selectionType == SelectionType.ConstantColor ? ChangeColorHSVDelta(color, HSVDeltaData.SelectedMeshFilter) : TimeInterpolator.Lerp(color, ChangeColorHSVDelta(color, HSVDeltaData.HighlightedMeshFilterMax)); }
public void BuildDecal() { vertToIndexMap.Clear(); tempCorners.Clear(); Vector3 center = Vector3.zero; for (int i = 0; i < cornerOffsets.Length; i++) { Vector3 corner = transform.TransformPoint(cornerOffsets[i]); center += corner; tempCorners.Add(corner); } Bounds objectBounds = new Bounds(); objectBounds.center = center / (float)tempCorners.Count; for (int i = 0; i < tempCorners.Count; i++) { objectBounds.Encapsulate(tempCorners[i]); } decalTris.Clear(); decalVerts.Clear(); MeshFilter[] meshFilters = FindObjectsOfType <MeshFilter>(); for (int i = 0; i < meshFilters.Length; i++) { MeshFilter mf = meshFilters[i]; if (mf.sharedMesh == null) { continue; } MeshRenderer mr = mf.GetComponent <MeshRenderer>(); if (mr == null || !mr.enabled) { continue; } Decal decal = mf.GetComponent <Decal>(); if (decal != null) { continue; } Rigidbody rigidbody = mf.GetComponentInParent <Rigidbody>(); if (rigidbody && !rigidbody.isKinematic) { continue; } Bounds otherBounds = mr.bounds; if (!objectBounds.Intersects(otherBounds) && !objectBounds.Contains(otherBounds.center)) { continue; } tempCorners.Clear(); Vector3 otherCenter = Vector3.zero; Bounds otherLocalBounds = mf.sharedMesh.bounds; for (int j = 0; j < cornerOffsets.Length; j++) { Vector3 scaledCornerOffset = cornerOffsets[j]; scaledCornerOffset.Scale(otherLocalBounds.size); Vector3 otherCorner = mf.transform.TransformPoint(scaledCornerOffset); otherCorner += mf.transform.TransformPoint(otherLocalBounds.center) - mf.transform.position; tempCorners.Add(otherCorner); otherCenter += otherCorner; } Bounds otherObjectBounds = new Bounds(); otherObjectBounds.center = otherCenter / (float)cornerOffsets.Length; for (int j = 0; j < tempCorners.Count; j++) { otherObjectBounds.Encapsulate(tempCorners[j]); } if (!objectBounds.Intersects(otherObjectBounds) && !objectBounds.Contains(otherObjectBounds.center)) { continue; } tris.Clear(); verts.Clear(); localVerts.Clear(); if (!mf.sharedMesh.isReadable) { Debug.LogWarning("Mesh not marked readable: " + mf.sharedMesh.name, mf); continue; } for (int s = 0; s < mf.sharedMesh.subMeshCount; s++) { mf.sharedMesh.GetTriangles(tris, s); mf.sharedMesh.GetVertices(verts); for (int j = 0; j < verts.Count; j++) { Vector3 worldPos = mf.transform.TransformPoint(verts[j]); Vector3 decalPos = transform.InverseTransformPoint(worldPos); localVerts.Add(decalPos); } for (int j = 0; j < tris.Count; j += 3) { Vector3 a = localVerts[tris[j]]; Vector3 b = localVerts[tris[j + 1]]; Vector3 c = localVerts[tris[j + 2]]; Vector3 normal = Vector3.Cross(b - a, c - a).normalized; if (Vector3.Angle(normal, Vector3.up) > maxAngle) { continue; } checkVerts.Clear(); checkVerts.Add(a); checkVerts.Add(b); checkVerts.Add(c); clippedVerts.Clear(); for (int k = 0; k < planes.Length; k++) { for (int l = 0; l < checkVerts.Count; l++) { Vector3 pos = checkVerts[l]; Vector3 next = checkVerts[(l + 1) % checkVerts.Count]; if (planes[k].GetSide(pos)) { clippedVerts.Add(pos); } if (planes[k].GetSide(pos) != planes[k].GetSide(next)) { Ray ray = new Ray(pos, next - pos); float dist = 0f; planes[k].Raycast(ray, out dist); clippedVerts.Add(ray.GetPoint(dist)); } } checkVerts.Clear(); checkVerts.AddRange(clippedVerts); clippedVerts.Clear(); } if (checkVerts.Count >= 3) { int index = GetIndex(checkVerts[0], normal); for (int k = 1; k < checkVerts.Count - 1; k++) { decalTris.Add(index); int index2 = GetIndex(checkVerts[k], normal); decalTris.Add(index2); int index3 = GetIndex(checkVerts[k + 1], normal); decalTris.Add(index3); } } } } } Mesh mesh = new Mesh(); mesh.name = "DecalMesh"; mesh.hideFlags = HideFlags.HideAndDontSave; if (decalVerts.Count > 0 && decalTris.Count > 0) { if (sprite != null && sprite.texture != null) { Vector2 min = sprite.textureRect.min; min.x /= sprite.texture.width; min.y /= sprite.texture.height; Vector2 max = sprite.textureRect.max; max.x /= sprite.texture.width; max.y /= sprite.texture.height; spriteUVs[0] = new Vector2(min.x, max.y); spriteUVs[1] = new Vector2(max.x, max.y); spriteUVs[2] = new Vector2(max.x, min.y); spriteUVs[3] = new Vector2(min.x, min.y); } decalUVs.Clear(); decalNormals.Clear(); for (int k = 0; k < decalVerts.Count; k++) { Vector3 vert = decalVerts[k]; Vector2 uv = new Vector2(vert.x + 0.5f, vert.z + 0.5f); uv = BilinearInterpolation(spriteUVs[0], spriteUVs[1], spriteUVs[2], spriteUVs[3], uv.x, 1f - uv.y); decalUVs.Add(uv); decalNormals.Add(Vector3.zero); } for (int k = 0; k < decalTris.Count; k += 3) { int index = decalTris[k]; int index2 = decalTris[k + 1]; int index3 = decalTris[k + 2]; Vector3 a = decalVerts[index]; Vector3 b = decalVerts[index2]; Vector3 c = decalVerts[index3]; Vector3 normal = Vector3.Cross(b - a, c - a).normalized; decalNormals[index] += normal; decalNormals[index2] += normal; decalNormals[index3] += normal; } for (int k = 0; k < decalNormals.Count; k++) { decalNormals[k] = decalNormals[k].normalized; } float worldOffset = surfaceOffset / transform.lossyScale.y; for (int k = 0; k < decalNormals.Count; k++) { decalVerts[k] += decalNormals[k] * worldOffset; } mesh.SetVertices(decalVerts); mesh.SetNormals(decalNormals); mesh.SetTriangles(decalTris, 0); mesh.SetUVs(0, decalUVs); } MeshFilter.sharedMesh = serializedMesh = mesh; MeshRenderer.sharedMaterial = material; }