private MaleIndex GetMaleIndex(Mesh mesh, BranchIndex fromBranchIndex, GameObjectInfo objectInfo) { MaleIndex maleIndex = new MaleIndex(); maleIndex.Index = GetIndexWithSameType(OrganType.Flower, fromBranchIndex); maleIndex.MaleMesh = mesh; maleIndex.Radius = objectInfo.Radius; maleIndex.Rotation = objectInfo.Rotation; maleIndex.From = fromBranchIndex; return(maleIndex); }
private void CreateFlowerModel(MaleIndex index, Vector3 position) { /* * 计算形态数据:长度 * 用于后续生成GameObject */ index.MorphologicalSim(); /* * 创建空对象 * 后续用于包含所有创建的flower模型 */ GameObject parentModel = new GameObject("flower"); parentModel.tag = "Organ"; parentModel.transform.position = position; //设置父节点 parentModel.transform.SetParent(BranchModel.transform); GameObject flowerInternode = GameObject.CreatePrimitive(PrimitiveType.Cylinder); flowerInternode.transform.position = position + new Vector3(0, 0.075f) / MaizeParams.SCALE; flowerInternode.transform.localScale = new Vector3(BranchIndexes[BranchIndexes.Count - 1].Radius / DEFAULT_RADIUS, 15 / DEFAULT_HEIGHT, BranchIndexes[BranchIndexes.Count - 1].Radius / DEFAULT_RADIUS) * SCALE; //尺寸 flowerInternode.GetComponent <MeshRenderer>().material = BranchModel.GetComponent <MeshRenderer>().material; flowerInternode.transform.SetParent(parentModel.transform); /* * 获取模板 * 后续用于实例化 */ GameObject instance = index.MaleMesh.Instance; /* * 实例化 * 绘制以及求尺寸 */ GameObject flowerModel = GameObject.Instantiate(instance); /* * 获取该物体的包围盒 * 根据包围盒的大小以及计算出的形态数据 * 计算出绘制时的scale */ Bounds modelBounds = Mesh.GetBoundsInParent(flowerModel); /* * 绘制直立的雌蕊 */ float scale = index.Length / (modelBounds.size.z * MaizeParams.SCALE); flowerModel.transform.localScale = Vector3.one * scale; flowerModel.transform.position = position + new Vector3(0, 0.15f, 0) / MaizeParams.SCALE; flowerModel.transform.parent = parentModel.transform; flowerModel.transform.Rotate(new Vector3(-90, 0, 0)); /* * 绘制倾斜的6个雌蕊 */ for (int i = 0; i < 6; i++) { GameObject model = GameObject.Instantiate(instance); model.transform.localScale = Vector3.one * scale; model.transform.position = position + new Vector3(0, 0.15f, 0) / MaizeParams.SCALE; model.transform.parent = parentModel.transform; model.transform.Rotate(new Vector3(-35, i * 60, 0)); } SetTagInParent(parentModel.transform, "Organ"); /* * 信息补齐 */ index.Belong = parentModel; m_listOrganModels.Add(parentModel); }
public void MorphologicalSim(double biomass, bool isClear = true, bool isSameGC = false) { DataCheck(); //数据检查 /* * 包含枝干和器官的索引 * 其每一个索引包含了后续生成GameObject所需的形态信息(旋转角度)以及 * 生成形态信息所需的数据(生物量、年龄) */ List <OrganIndex> indexes = GetIndexes(isClear, isSameGC); /* * 分配生物量给各个器官 * 累积的生物量为后续形态模拟提供参考依据 */ FunctionSim.PhotosynthateAllocation(this, biomass, FunctionSim.GrowthPeriodJudgment(BranchIndexes, OrganIndexes)); /* * 遍历每个Index * 计算形态数据 * 根据形态数据绘制GameObject */ int curLevel = 0; Vector3 curPosition = Vector3.zero; Stack <Vector3> positionStack = new Stack <Vector3>(); foreach (OrganIndex index in indexes) { switch (index.Type) { case OrganType.Branch: BranchIndex branchIndex = index as BranchIndex; if (branchIndex.Level > curLevel) { positionStack.Push(curPosition); } else if (branchIndex.Level < curLevel) { curPosition = positionStack.Pop(); } curLevel = branchIndex.Level; curPosition = RecordBranchModel(branchIndex, curPosition); break; case OrganType.Leaf: LeafIndex leafIndex = index as LeafIndex; CreateLeafModel(leafIndex, curPosition); break; case OrganType.Flower: MaleIndex maleIndex = index as MaleIndex; CreateFlowerModel(maleIndex, curPosition); break; case OrganType.Fruit: FemaleIndex femaleIndex = index as FemaleIndex; CreateFruitModel(femaleIndex, curPosition); break; } } CreateBranchModel(); /* * 病虫害模拟 * 测试 */ InsectSimulation(); PostProcessing(); }