Ejemplo n.º 1
0
        public void updateTerrainTree(Terrain terr)
        {
            if (terr != null)
            {
                List <TreePrototype> treeList = new List <TreePrototype>();
                for (int i = 0; i < nodeContainers.Count; i++)
                {
                    if (nodeContainers[i].node is TreeOutput)
                    {
                        TreeOutput treeNode = (TreeOutput)(nodeContainers[i].node);
                        treeNode.startTreePropertyIndex = treeList.Count;
                        for (int p = 0; p < treeNode.prefabs.Length; p++)
                        {
                            if (treeNode.prefabs[p] != null && !treeNode.isEntity)  //排除Entity
                            {
                                TreePrototype tp = new TreePrototype();
                                tp.prefab     = treeNode.prefabs[p];
                                tp.bendFactor = treeNode.bendFactor;
                                treeList.Add(tp);
                            }
                        }
                    }
                }
                terr.terrainData.treePrototypes = treeList.ToArray();

                List <TreeInstance> instances = GetTreeInstance(terr, false);

                terr.terrainData.treeInstances = instances.ToArray();
            }
        }
Ejemplo n.º 2
0
        public void updateTerrainEntity(Terrain terr, Func <GameObject, GameObject> funNewObj = null)
        {
            if (terr != null)
            {
                List <TreePrototype> treeList = new List <TreePrototype>();
                for (int i = 0; i < nodeContainers.Count; i++)
                {
                    if (nodeContainers[i].node is TreeOutput)
                    {
                        TreeOutput treeNode = (TreeOutput)(nodeContainers[i].node);
                        treeNode.startTreePropertyIndex = treeList.Count;
                        for (int p = 0; p < treeNode.prefabs.Length; p++)
                        {
                            if (treeNode.prefabs[p] != null && treeNode.isEntity)  ///选取所有Entity
                            {
                                TreePrototype tp = new TreePrototype();
                                tp.prefab     = treeNode.prefabs[p];
                                tp.bendFactor = treeNode.bendFactor;
                                treeList.Add(tp);
                            }
                        }
                    }
                }

                GameObject container = entityContainer;
                if (container == null)
                {
                    container = new GameObject("auto_tree");
                }

                {
                    Transform[] childs = container.GetComponentsInChildren <Transform>();
                    foreach (Transform child in childs)
                    {
                        if (child != container.transform)
                        {
                            if (child != null)
                            {
                                GameObject.DestroyImmediate(child.gameObject);
                            }
                        }
                    }
                }


                List <TreeInstance> instances = GetTreeInstance(terr, true);

                for (int i = 0; i < instances.Count; i++)
                {
                    GameObject prefab = treeList[instances[i].prototypeIndex].prefab;
                    GameObject obj    = (funNewObj == null) ? GameObject.Instantiate(prefab) : funNewObj(prefab);
                    obj.transform.SetParent(container.transform, true);
                    obj.transform.position   = Vector3.Scale(instances[i].position, terr.terrainData.size) + container.transform.position;
                    obj.transform.rotation   = prefab.transform.rotation * Quaternion.AngleAxis(instances[i].rotation, Vector3.up);
                    obj.transform.localScale = new Vector3(instances[i].widthScale, instances[i].heightScale, instances[i].widthScale);
                    obj.transform.name       = prefab.name;
                }
            }
        }
Ejemplo n.º 3
0
        private List <TreeInstance> GetTreeInstance(Terrain terr, bool bEntity)
        {
            int   terrainWidth  = terr.terrainData.heightmapWidth;
            int   terrainHeight = terr.terrainData.heightmapHeight;
            float realWidth     = terr.terrainData.size.x;
            float realHeight    = terr.terrainData.size.z;

            float pixelSize = terr.terrainData.size.x / terrainWidth;

            List <TreeInstance> instances = new List <TreeInstance>();

            for (int n = 0; n < nodeContainers.Count; n++)
            {
                if (nodeContainers[n].node is TreeOutput)
                {
                    TreeOutput treeNode = nodeContainers[n].node as TreeOutput;
                    if (bEntity == treeNode.isEntity)
                    {
                        float[,] values = treeNode.update(seed, terrainWidth, terrainHeight, new Rect(baseX, baseY, realWidth, realHeight));
                        List <Vector3> treePos      = new List <Vector3>();
                        List <float>   treeAngles   = new List <float>();
                        List <float>   treeScales   = new List <float>();
                        List <int>     prefabIndexs = new List <int>();
                        //TreePrototype prot = terr.terrainData.treePrototypes[texNode.treeIndex];
                        for (int x = 0; x < terrainWidth; x++)
                        {
                            for (int y = 0; y < terrainHeight; y++)
                            {
                                int treeIndexForHash = treeNode.startTreePropertyIndex + (bEntity ? 999 : 0);
                                int treeNum          = treeNode.getTreeNum(baseX + x, baseY + y, values[x, y], treeNode.density, treeIndexForHash);
                                for (int t = 0; t < treeNum; t++)
                                {
                                    Vector2 offset = (treeNode.getTreePos(baseX + x, baseY + y, t, pixelSize * 2f, treeIndexForHash));
                                    Vector2 pos    = new Vector2(y + offset.y, x + offset.x);//翻转x,y.
                                    float   height = terr.terrainData.GetInterpolatedHeight(pos.x / terrainWidth, pos.y / terrainHeight) / terr.terrainData.size.y;
                                    Vector3 newPos = new Vector3(pos.x / terrainWidth, height, pos.y / terrainHeight);
                                    treePos.Add(newPos);
                                    treeAngles.Add(treeNode.GetAngle(baseX + x, baseY + y, t, pixelSize * 2f, treeIndexForHash));
                                    treeScales.Add(treeNode.GetScale(baseX + x, baseY + y, t, pixelSize * 2f, treeIndexForHash));
                                    prefabIndexs.Add(treeNode.GetPrefabIndex(baseX + x, baseY + y, t, pixelSize * 2f, treeIndexForHash));
                                }
                            }
                        }
                        //
                        for (int i = 0; i < treePos.Count; i++)
                        {
                            TreeInstance ins = new TreeInstance();
                            ins.position       = treePos[i];
                            ins.prototypeIndex = treeNode.startTreePropertyIndex + prefabIndexs[i];
                            ins.color          = Color.white;
                            ins.lightmapColor  = Color.white;
                            float s = Mathf.Lerp(treeNode.minSize, treeNode.maxSize, treeScales[i]);
                            ins.heightScale = s;
                            ins.widthScale  = s;
                            ins.rotation    = 360 * treeAngles[i];
                            instances.Add(ins);
                        }
                    }
                }
            }

            return(instances);
        }