static void ConvertSelectionToDclPrimitives() { if (EditorUtility.DisplayDialog("Convert To DCL Primitives", "This operation will traverse all your selected objects and convert Unity Primitives to DCL Primitives. Are you sure to continue?", "Confirm", "Abort")) { foreach (var gameObject in Selection.gameObjects) { var meshFilter = gameObject.GetComponent <MeshFilter>(); if (meshFilter) { var converted = false; PrimitiveType primitiveType = PrimitiveType.Cube; if (meshFilter.sharedMesh == PrimitiveHelper.GetPrimitiveMesh(PrimitiveType.Cube)) { primitiveType = PrimitiveType.Cube; converted = true; } else if (meshFilter.sharedMesh == PrimitiveHelper.GetPrimitiveMesh(PrimitiveType.Sphere)) { primitiveType = PrimitiveType.Sphere; converted = true; } else if (meshFilter.sharedMesh == PrimitiveHelper.GetPrimitiveMesh(PrimitiveType.Quad)) { primitiveType = PrimitiveType.Quad; converted = true; } else if (meshFilter.sharedMesh == PrimitiveHelper.GetPrimitiveMesh(PrimitiveType.Cylinder)) { primitiveType = PrimitiveType.Cylinder; converted = true; } if (converted) { var dclObj = gameObject.GetComponent <DclObject>(); if (!dclObj) { dclObj = gameObject.AddComponent <DclObject>(); } DclPrimitiveHelper.ConvertToDclPrimitive(dclObj, primitiveType); dclObj.withCollision = gameObject.GetComponent <Collider>() != null; EditorSceneManager.MarkSceneDirty(gameObject.scene); } } } //TODO:注册撤销功能 } }
void Update()// OnDrawGizmos() { if (parcels.Count > 0) { var baseParcel = parcels[0]; var mtr = new Matrix4x4(); foreach (var parcel in parcels) { var pos = new Vector3((parcel.x - baseParcel.x) * 16, -0.1f, (parcel.y - baseParcel.y) * 16); pos += parcelPosOffset; mtr.SetTRS(pos, Quaternion.identity, new Vector3(1.6f, 1f, 1.6f)); Graphics.DrawMesh(PrimitiveHelper.GetPrimitiveMesh(PrimitiveType.Plane), mtr, m_GroundMaterial, 0); } } }
void Update()// OnDrawGizmos() { if (parcels.Count > 0) { var baseParcel = parcels[0]; // Gizmos.color = new Color(0.7, 0); foreach (var parcel in parcels) { var pos = new Vector3((parcel.x - baseParcel.x) * 10, 0, (parcel.y - baseParcel.y) * 10); //Gizmos.DrawWireCube(pos, new Vector3(10, 0f, 10)); //Gizmos.DrawCube(pos, new Vector3(10, 0f, 10)); //Gizmos.DrawMesh(PrimitiveHelper.GetPrimitiveMesh(PrimitiveType.Plane), pos); Graphics.DrawMesh(PrimitiveHelper.GetPrimitiveMesh(PrimitiveType.Plane), pos, Quaternion.identity, m_GroundMaterial, 0); } } }
static void RecursivelyTraverseTransform(Transform tra, StringBuilder xmlBuilder, List <GameObject> meshesToExport, int indentLevel, SceneStatistics statistics) { if (!tra.gameObject.activeInHierarchy) { return; } //TODO: Hide empty statistics.entityCount += 1; var components = tra.GetComponents <Component>(); string nodeName = null; var position = tra.localPosition; var scale = tra.localScale; var eulerAngles = tra.localEulerAngles; string pColor = null; var extraProperties = new StringBuilder(); foreach (var component in components) { if (component is Transform) { continue; } //Primitive if (component is MeshFilter) { var meshFilter = component as MeshFilter; if (meshFilter.sharedMesh == PrimitiveHelper.GetPrimitiveMesh(PrimitiveType.Cube)) { nodeName = "box"; } else if (meshFilter.sharedMesh == PrimitiveHelper.GetPrimitiveMesh(PrimitiveType.Sphere)) { nodeName = "sphere"; } else if (meshFilter.sharedMesh == PrimitiveHelper.GetPrimitiveMesh(PrimitiveType.Quad)) { nodeName = "plane"; } else if (meshFilter.sharedMesh == PrimitiveHelper.GetPrimitiveMesh(PrimitiveType.Cylinder)) { nodeName = "cylinder"; extraProperties.Append(" radius={0.5}"); } if (nodeName != null) { //read color var rdrr = tra.GetComponent <MeshRenderer>(); if (rdrr) { var matColor = rdrr.sharedMaterial.color; pColor = ToHexString(matColor); } //Statistics statistics.triangleCount += meshFilter.sharedMesh.triangles.LongLength / 3; } } //Other Model if (nodeName == null) { if (component is MeshFilter) { var meshFilter = component as MeshFilter; //export as a glTF model if (meshesToExport != null) { meshesToExport.Add(tra.gameObject); } nodeName = "gltf-model"; position = Vector3.zero; eulerAngles = Vector3.zero; scale = Vector3.zero; extraProperties.AppendFormat(" src=\"./unity_assets/{0}.gltf\"", tra.name); //Statistics statistics.triangleCount += meshFilter.sharedMesh.triangles.LongLength / 3; statistics.bodyCount += 1; } } //TextMesh if (component is TextMesh) { nodeName = "text"; var tm = component as TextMesh; extraProperties.AppendFormat(" value=\"{0}\"", tm.text); scale *= tm.fontSize * 0.5f; //extraProperties.AppendFormat(" fontS=\"{0}\"", 100); pColor = ToHexString(tm.color); var rdrr = tra.GetComponent <MeshRenderer>(); if (rdrr) { var width = rdrr.bounds.extents.x * 2; extraProperties.AppendFormat(" width={{{0}}}", width); } } if (component is MeshRenderer) { var meshRenderer = component as MeshRenderer; //Statistics var curHeight = meshRenderer.bounds.max.y; if (curHeight > statistics.maxHeight) { statistics.maxHeight = curHeight; } } } if (nodeName == null) { nodeName = "entity"; } if (pColor != null) { extraProperties.AppendFormat(" color=\"{0}\"", pColor); } StringBuilder xmlNode = null; StringBuilder xmlNodeTail = null; StringBuilder childrenXmlBuilder = null; if (xmlBuilder != null) { xmlNode = new StringBuilder(); xmlNode.AppendIndent(indentUnit, indentLevel); xmlNode.AppendFormat("<{0} position={{{1}}} scale={{{2}}} rotation={{{3}}}{4}>", nodeName, Vector3ToJSONString(position), Vector3ToJSONString(scale), Vector3ToJSONString(eulerAngles), extraProperties); xmlNodeTail = new StringBuilder().AppendFormat("</{0}>\n", nodeName); childrenXmlBuilder = new StringBuilder(); } Debug.Log(tra.name); foreach (Transform child in tra) { RecursivelyTraverseTransform(child, childrenXmlBuilder, meshesToExport, indentLevel + 1, statistics); } if (xmlBuilder != null) { if (childrenXmlBuilder.Length > 0) { xmlNode.Append("\n"); xmlNode.Append(childrenXmlBuilder); xmlNode.AppendIndent(indentUnit, indentLevel); } xmlNode.Append(xmlNodeTail); xmlBuilder.Append(xmlNode); } }