/** * 製作基座 */ private PlatFormStruct CreatePlatform(GameObject parent, int sides, Vector3 pos, float rotateAngle = 0) { PlatFormStruct platFormStruct = new PlatFormStruct(); GameObject platformBody = new GameObject("PlatformBody"); //platformBody.transform.position = pos; platformBody.transform.parent = parent.transform; MeshFilter meshFilter = platformBody.AddComponent <MeshFilter>(); MeshRenderer meshRenderer = platformBody.AddComponent <MeshRenderer>(); meshRenderer.material.color = Color.white; List <Vector3> controlPointPosList = new List <Vector3>(); //初始值****************************************************************************** float platformRadius = (platWidth / 2.0f) / Mathf.Sin(2f * Mathf.PI / (sides * 2)); //*********************************************************************************** if (sides == (int)MainController.FormFactorSideType.FourSide) { controlPointPosList = MeshCenter.Instance.CreateCubeMesh(pos, platWidth, platHeight, platLength, rotateAngle, meshFilter); } else { controlPointPosList = MeshCenter.Instance.CreateRegularRingMesh(pos, sides, platformRadius, platHeight, rotateAngle, meshFilter); } //計算底部與頂部位置 platFormStruct.bottomPointPosList.Clear(); platFormStruct.topPointPosList.Clear(); for (int i = 0; i < sides; i++) { platFormStruct.bottomPointPosList.Add(controlPointPosList[i]); platFormStruct.topPointPosList.Add(controlPointPosList[(controlPointPosList.Count - 1) - ((sides - 1)) + i]); } MainController.ShowPos(platFormStruct.bottomPointPosList[0], platformBody, Color.blue, 1.0f); MainController.ShowPos(platFormStruct.bottomPointPosList[1], platformBody, Color.red, 1.0f); MainController.ShowPos(platFormStruct.bottomPointPosList[2], platformBody, Color.yellow, 1.0f); //計算facadeDir與stair位置 platFormStruct.facadeDir.Clear(); platFormStruct.stairPosList.Clear(); for (int index = 0; index < platFormStruct.topPointPosList.Count; index++) { Vector3 dir = Vector3.Cross(Vector3.up, platFormStruct.topPointPosList[(index + 1) % platFormStruct.topPointPosList.Count] - platFormStruct.topPointPosList[index]).normalized; Vector3 stairPos = (platFormStruct.topPointPosList[index] + platFormStruct.topPointPosList[(index + 1) % platFormStruct.topPointPosList.Count]) / 2.0f; stairPos.y = (platFormStruct.topPointPosList[index].y + platFormStruct.bottomPointPosList[index].y) / 2.0f; platFormStruct.facadeDir.Add(dir); platFormStruct.stairPosList.Add(stairPos); } return(platFormStruct); }
/** * 製作基座 */ private PlatFormStruct CreatePlatform(GameObject parent, List <Vector3> platTopPosList, float enlargeOffset = 0) { PlatFormStruct platFormStruct = new PlatFormStruct(); GameObject platformBody = new GameObject("PlatformBody"); platformBody.transform.parent = parent.transform; MeshFilter meshFilter = platformBody.AddComponent <MeshFilter>(); MeshRenderer meshRenderer = platformBody.AddComponent <MeshRenderer>(); meshRenderer.material.color = Color.white; //enlarge List <Vector2> enlargePosV2List = new List <Vector2>(); List <Vector3> enlargePosV3List = new List <Vector3>(); for (int i = 0; i < platTopPosList.Count; i++) { Vector2 newPos = new Vector2(platTopPosList[i].x, platTopPosList[i].z); enlargePosV2List.Add(newPos); } enlargePosV2List = GetEnlargedPolygon(enlargePosV2List, enlargeOffset); for (int i = 0; i < enlargePosV2List.Count; i++) { Vector3 newPos = new Vector3(enlargePosV2List[i].x, platTopPosList[i].y, enlargePosV2List[i].y); enlargePosV3List.Add(newPos); } MeshCenter.Instance.CreateEarClippingMesh(enlargePosV3List, platHeight, meshFilter); //計算底部與頂部位置 platFormStruct.bottomPointPosList.Clear(); platFormStruct.topPointPosList.Clear(); platFormStruct.topPointPosList = enlargePosV3List; for (int i = 0; i < platFormStruct.topPointPosList.Count; i++) { Vector3 btmPos = platFormStruct.topPointPosList[i] - Vector3.up * platHeight; platFormStruct.bottomPointPosList.Add(btmPos); } //計算facadeDir與stair位置 platFormStruct.facadeDir.Clear(); platFormStruct.stairPosList.Clear(); for (int index = 0; index < platFormStruct.topPointPosList.Count; index++) { Vector3 dir = Vector3.Cross(Vector3.up, platFormStruct.topPointPosList[(index + 1) % platFormStruct.topPointPosList.Count] - platFormStruct.topPointPosList[index]).normalized; Vector3 stairPos = (platFormStruct.topPointPosList[index] + platFormStruct.topPointPosList[(index + 1) % platFormStruct.topPointPosList.Count]) / 2.0f; stairPos.y = (platFormStruct.topPointPosList[index].y + platFormStruct.bottomPointPosList[index].y) / 2.0f; platFormStruct.facadeDir.Add(dir); platFormStruct.stairPosList.Add(stairPos); } return(platFormStruct); }
/** * 重製樓梯(長寬) */ public void ResetStair(float StairLength, float StairWidth) { PlatFormStruct stairInfo = platformController.platFormStruct; this.platformController.stairLength = StairLength; this.platformController.stairWidth = StairWidth; for (int iIndex = 0; iIndex < stairInfo.stairList.Count; iIndex++) { Destroy(stairInfo.stairList[iIndex]); } stairInfo.stairList = this.platformController.CreateRingStair(this.platform, stairInfo.borderColumnList, stairInfo.stairPosList, stairInfo.facadeDir, this.platformController.stairHeight, this.platformController.stairLength); }
/** * 初始化基座 */ public void InitFunction(BuildingObj buildingObj, float platformFrontWidth, float platformFrontLength, float platformHeight, bool isStair, float rotateAngle = 0) { this.buildingObj = buildingObj; this.platWidth = platformFrontWidth; this.platLength = platformFrontLength; this.platHeight = platformHeight; this.isStair = isStair; stairHeight = platformHeight; this.parentObj = buildingObj.platform; this.sides = (int)buildingObj.sides; this.entraneIndexList = buildingObj.entraneIndexList; //*********************************************************************** platFormStruct = CreatePlatform(buildingObj.platform, (int)buildingObj.sides, buildingObj.platformCenter, rotateAngle); StartCreateBorder(isBorder); StartCreateStair(isStair); }