public Terrain CreateTerrain()
    {
        TerrainData terrainData = new TerrainData();

        terrainData.heightmapResolution = 513;
        terrainData.baseMapResolution   = 513;
        terrainData.size = new Vector3(50, 50, 50);
        terrainData.alphamapResolution = 512;
        terrainData.SetDetailResolution(32, 8);
        TreePrototype treePrototype = new TreePrototype();

        treePrototype.prefab     = treePrefab;
        treePrototype.bendFactor = 1;

        // Error !!!!
        //terrainData.treePrototypes = new TreePrototype[1];
        //terrainData.treePrototypes[0] = treePrototype;

        terrainData.treePrototypes = new TreePrototype[1] {
            treePrototype
        };

        GameObject obj = Terrain.CreateTerrainGameObject(terrainData);

#if UNITY_EDITOR
        AssetDatabase.CreateAsset(terrainData, "Assets/Tutorial/Tutor_6_SimpleTerrain.asset");
        AssetDatabase.SaveAssets();
#endif
        return(obj.GetComponent <Terrain>());
    }
        private static void AutoExtractTypes(FoliagePainterEditTime painter, Terrain terrain)
        {
            TreeInstance[]  terrainTreeInstances  = terrain.terrainData.treeInstances;
            TreePrototype[] terrainTreePrototypes = terrain.terrainData.treePrototypes;

            for (int i = 0; i < terrainTreePrototypes.Length; i++)
            {
                TreePrototype proto  = terrainTreePrototypes[i];
                GameObject    prefab = proto.prefab;

                // See if we already have it
                if (painter.HasFoliageType(prefab) == false)
                {
                    // Attempt to add it
                    FoliageTypeBuilder builder;
                    bool anyError;

                    FoliageUtilitiesEditor.ConfigurePrefab(prefab, out builder, out anyError);

                    if (anyError == false)
                    {
                        painter.AddFoliageType(builder);
                    }
                    else
                    {
                        FoliageLog.e("Could not add foliage type: " + prefab.name + " due to error. Check the log for more info.");
                    }
                }
            }
        }
Esempio n. 3
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();
            }
        }
Esempio n. 4
0
 private void DoApply()
 {
     if ((Object)this.terrainData == (Object)null)
     {
         return;
     }
     TreePrototype[] treePrototypes = this.m_Terrain.terrainData.treePrototypes;
     if (this.m_PrototypeIndex == -1)
     {
         TreePrototype[] treePrototypeArray = new TreePrototype[treePrototypes.Length + 1];
         for (int index = 0; index < treePrototypes.Length; ++index)
         {
             treePrototypeArray[index] = treePrototypes[index];
         }
         treePrototypeArray[treePrototypes.Length]            = new TreePrototype();
         treePrototypeArray[treePrototypes.Length].prefab     = this.m_Tree;
         treePrototypeArray[treePrototypes.Length].bendFactor = this.m_BendFactor;
         this.m_PrototypeIndex = treePrototypes.Length;
         this.m_Terrain.terrainData.treePrototypes = treePrototypeArray;
         TreePainter.selectedTree = this.m_PrototypeIndex;
     }
     else
     {
         treePrototypes[this.m_PrototypeIndex].prefab     = this.m_Tree;
         treePrototypes[this.m_PrototypeIndex].bendFactor = this.m_BendFactor;
         this.m_Terrain.terrainData.treePrototypes        = treePrototypes;
     }
     this.m_Terrain.Flush();
     EditorUtility.SetDirty((Object)this.m_Terrain);
 }
    void CreateProtoTypes()
    {
        if (_AddTrees)
        {
            _treeData = new TreePrototype[_Trees.Length];
            for (int i = 0; i < _Trees.Length; i++)
            {
                _treeData[i]        = new TreePrototype();
                _treeData[i].prefab = _Trees[i];
            }
        }
        if (_AddTexture)
        {
            _TerrainTexture[0]         = new SplatPrototype();
            _TerrainTexture[0].texture = _TerTexture;
        }

        if (_AddGrass)
        {
            _detailData = new DetailPrototype[1];

            _detailData[0] = new DetailPrototype();
            _detailData[0].prototypeTexture = _Grass;
            _detailData[0].renderMode       = DetailRenderMode.GrassBillboard;
        }
    }
    static void Run()
    {
        Terrain terrain = Terrain.activeTerrain;

        if (terrain == null)
        {
            return;
        }

        TerrainData td = terrain.terrainData;

        GameObject parent = new GameObject("Tree Shadow Casters");

        foreach (TreeInstance tree in td.treeInstances)
        {
            Vector3       pos      = Vector3.Scale(tree.position, td.size) + terrain.transform.position;
            TreePrototype treeProt = td.treePrototypes[tree.prototypeIndex];
            GameObject    prefab   = treeProt.prefab;

            Debug.Log("tree : " + tree.rotation);
            GameObject   obj      = GameObject.Instantiate(prefab, pos, Quaternion.AngleAxis(tree.rotation * Mathf.Rad2Deg, Vector3.up)) as GameObject;
            MeshRenderer renderer = obj.GetComponentInChildren <MeshRenderer>();
            renderer.receiveShadows    = false;
            renderer.shadowCastingMode = ShadowCastingMode.On;
            GameObjectUtility.SetStaticEditorFlags(obj, StaticEditorFlags.LightmapStatic);

            Transform t = obj.transform;
            t.localScale = new Vector3(tree.widthScale, tree.heightScale, tree.widthScale);
            t.rotation   = Quaternion.AngleAxis(tree.rotation * Mathf.Rad2Deg, Vector3.up);
            t.parent     = parent.transform;
        }
    }
        protected override void GetDepsFromImpl(object obj, GetDepsFromContext context)
        {
            base.GetDepsFromImpl(obj, context);
            TreePrototype uo = (TreePrototype)obj;

            AddDep(uo.prefab, context);
        }
Esempio n. 8
0
        public void ApplyTrees(TCUnityTerrain sTerrain = null)
        {
            if (!CheckValidUnityTerrain())
            {
                return;
            }
            if (sTerrain == null)
            {
                sTerrain = this;
            }

            if (sTerrain.treePrototypes.Count == 0)
            {
                ResetTrees();
            }

            List <TreePrototype> treePrototypesCleaned = new List <TreePrototype>();

            for (int i = 0; i < sTerrain.treePrototypes.Count; i++)
            {
                TC_TreePrototype s = sTerrain.treePrototypes[i];

                if (s.prefab == null)
                {
                    continue;
                }

                TreePrototype d = new TreePrototype();
                d.bendFactor = s.bendFactor;
                d.prefab     = s.prefab;
                treePrototypesCleaned.Add(d);
            }

            terrain.terrainData.treePrototypes = treePrototypesCleaned.ToArray();
        }
Esempio n. 9
0
    // Use this for initialization
    void Start()
    {
        treePrototype = new TreePrototype();

        Terrain terrain = GameObject.Find("Terrain").GetComponent <Terrain>();

        TreeInstance treeInstance = terrain.terrainData.treeInstances[0];

        print(terrain.terrainData.treePrototypes[0].prefab.GetComponent <Tree>().data);

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

        tList.Add(treeInstance);

        treeInstance.position = new Vector3(Random.value, 0, Random.value);

        treeInstance.rotation = Random.value * Mathf.PI * 2;

        tList.Add(treeInstance);

        terrain.terrainData.treeInstances = tList.ToArray();

        // print( treePrototype );
        //var td ScriptableObject.CreateInstance(System.da)
    }
Esempio n. 10
0
        /// <summary>
        /// Get index of tree prototype on terrain. If new, it will be added to the collection.
        /// </summary>
        private static int GetTreePrototypeIndex(TerrainData terrainData, GameObject prefab)
        {
            // Search existing prototype
            TreePrototype[] treePrototypes = terrainData.treePrototypes;
            for (int i = 0; i < treePrototypes.Length; i++)
            {
                if (treePrototypes[i].prefab == prefab)
                {
                    return(i);
                }
            }

            // Add new prototype
            var updatedTreePrototypes = new List <TreePrototype>(treePrototypes);
            var treePrototype         = new TreePrototype()
            {
                prefab     = prefab,
                bendFactor = 1
            };

            updatedTreePrototypes.Add(treePrototype);
            terrainData.treePrototypes = updatedTreePrototypes.ToArray();
            terrainData.RefreshPrototypes();
            return(updatedTreePrototypes.Count - 1);
        }
Esempio n. 11
0
    /// <summary>
    ///
    /// </summary>
    /// <param name="terrainData"></param>
    private void ApplyTrees(TerrainData terrainData)
    {
        if (Setting.Trees.Count > 0)
        {
            int maxCounts = 5;
            int treeIndex = Random.Range(0, Setting.Trees.Count - 1);
            for (int i = 0; i < ChopPoint.Count; i++)
            {
                Vector3 treePoint = ChopPoint[i];

                for (int j = 0; j < maxCounts; j++)
                {
                    Vector3 random = new Vector3(Random.Range(treePoint.x - 0.1f, treePoint.x + 0.1f),
                                                 -0.1f, Random.Range(treePoint.y - 0.1f, treePoint.y + 0.1f));

                    Vector3 position = Vector3.Scale(random, TerrainChunk.terrainData.size) + TerrainChunk.transform.position;
                    position.y = TerrainChunk.SampleHeight(position);

                    GameObject tree = GameObject.Instantiate(Setting.Trees[treeIndex]);
                    tree.transform.position = position;
                    tree.transform.parent   = TerrainChunk.transform;
                }
            }


            List <TreePrototype> trees = new List <TreePrototype>();
            for (int i = 0; i < Setting.Trees.Count; i++)
            {
                TreePrototype tree = new TreePrototype();
                tree.prefab = Setting.Trees[i];

                trees.Add(tree);
            }

            terrainData.treePrototypes = trees.ToArray();

            for (int i = 0; i < TreePoint.Count; i++)
            {
                Vector3 vpos = TreePoint[i];

                for (int j = 0; j < 10; j++)
                {
                    TreeInstance tmpTreeInstances = new TreeInstance();
                    tmpTreeInstances.prototypeIndex = 3;
                    tmpTreeInstances.position       = new Vector3(Random.Range(vpos.x - 0.1f, vpos.x + 0.1f), -0.1f, Random.Range(vpos.y - 0.1f, vpos.y + 0.1f));
                    tmpTreeInstances.color          = new Color(1, 1, 1, 1);
                    tmpTreeInstances.lightmapColor  = new Color(1, 1, 1, 1);

                    float scale = Random.Range(0.8f, 1f);
                    tmpTreeInstances.heightScale = scale;
                    tmpTreeInstances.widthScale  = scale;
                    TerrainChunk.AddTreeInstance(tmpTreeInstances);
                }
            }

            TerrainCollider tc = TerrainChunk.GetComponent <TerrainCollider>();
            tc.enabled = false;
            tc.enabled = true;
        }
    }
Esempio n. 12
0
        public static int FindTreePrototype(Terrain terrain, Terrain sourceTerrain, int sourceTree)
        {
            if (sourceTree == PaintTreesTool.kInvalidTree ||
                sourceTree >= sourceTerrain.terrainData.treePrototypes.Length)
            {
                return(PaintTreesTool.kInvalidTree);
            }

            if (terrain == sourceTerrain)
            {
                return(sourceTree);
            }

            TreePrototype sourceTreePrototype = sourceTerrain.terrainData.treePrototypes[sourceTree];

            for (int i = 0; i < terrain.terrainData.treePrototypes.Length; ++i)
            {
                if (sourceTreePrototype.Equals(terrain.terrainData.treePrototypes[i]))
                {
                    return(i);
                }
            }

            return(PaintTreesTool.kInvalidTree);
        }
Esempio n. 13
0
        public override void OnEnterToolMode()
        {
            Terrain terrain = null;

            if (Selection.activeGameObject != null)
            {
                terrain = Selection.activeGameObject.GetComponent <Terrain>();
            }

            if (terrain != null &&
                terrain.terrainData != null &&
                m_LastSelectedTreePrototype != null)
            {
                for (int i = 0; i < terrain.terrainData.treePrototypes.Length; ++i)
                {
                    if (m_LastSelectedTreePrototype.Equals(terrain.terrainData.treePrototypes[i]))
                    {
                        selectedTree = i;
                        break;
                    }
                }
            }

            m_TargetTerrain = terrain;

            m_LastSelectedTreePrototype = null;
        }
        // Token: 0x060027D2 RID: 10194 RVA: 0x001B4B24 File Offset: 0x001B2D24
        private void CollectTreeMeshes(Terrain terrain, List <RasterizationMesh> result)
        {
            TerrainData terrainData = terrain.terrainData;

            for (int i = 0; i < terrainData.treeInstances.Length; i++)
            {
                TreeInstance  treeInstance  = terrainData.treeInstances[i];
                TreePrototype treePrototype = terrainData.treePrototypes[treeInstance.prototypeIndex];
                if (!(treePrototype.prefab == null))
                {
                    Collider component = treePrototype.prefab.GetComponent <Collider>();
                    Vector3  pos       = terrain.transform.position + Vector3.Scale(treeInstance.position, terrainData.size);
                    if (component == null)
                    {
                        Bounds            bounds = new Bounds(terrain.transform.position + Vector3.Scale(treeInstance.position, terrainData.size), new Vector3(treeInstance.widthScale, treeInstance.heightScale, treeInstance.widthScale));
                        Matrix4x4         matrix = Matrix4x4.TRS(pos, Quaternion.identity, new Vector3(treeInstance.widthScale, treeInstance.heightScale, treeInstance.widthScale) * 0.5f);
                        RasterizationMesh item   = new RasterizationMesh(RecastMeshGatherer.BoxColliderVerts, RecastMeshGatherer.BoxColliderTris, bounds, matrix);
                        result.Add(item);
                    }
                    else
                    {
                        Vector3           s = new Vector3(treeInstance.widthScale, treeInstance.heightScale, treeInstance.widthScale);
                        RasterizationMesh rasterizationMesh = this.RasterizeCollider(component, Matrix4x4.TRS(pos, Quaternion.identity, s));
                        if (rasterizationMesh != null)
                        {
                            rasterizationMesh.RecalculateBounds();
                            result.Add(rasterizationMesh);
                        }
                    }
                }
            }
        }
        public JsonTreePrototype(TreePrototype src_, ResourceMapper resMap)
        {
            src            = src_;
            prefabId       = resMap.getRootPrefabId(src.prefab, true);
            prefabObjectId = resMap.getPrefabObjectId(src.prefab, true);

            meshIdData = new MeshIdData(src.prefab, resMap);

            /*
             * meshId = -1;
             * meshMaterials.Clear();
             *
             * var srcObj = src.prefab as GameObject;
             * if (!srcObj)
             *      return;
             * var filter = srcObj.GetComponent<MeshFilter>();
             * if (filter && filter.sharedMesh){
             *      meshId = resMap.getMeshId(filter.sharedMesh);
             * }
             * var renderer = srcObj.GetComponent<Renderer>();
             * if (renderer){
             *      foreach(var cur in renderer.sharedMaterials){
             *              meshMaterials.Add(resMap.getMaterialId(cur));
             *      }
             * }
             */
        }
Esempio n. 16
0
 private void DoApply()
 {
     if (base.terrainData == null)
     {
         return;
     }
     TreePrototype[] treePrototypes = this.m_Terrain.terrainData.treePrototypes;
     if (this.m_PrototypeIndex == -1)
     {
         TreePrototype[] array = new TreePrototype[treePrototypes.Length + 1];
         for (int i = 0; i < treePrototypes.Length; i++)
         {
             array[i] = treePrototypes[i];
         }
         array[treePrototypes.Length]            = new TreePrototype();
         array[treePrototypes.Length].prefab     = this.m_Tree;
         array[treePrototypes.Length].bendFactor = this.m_BendFactor;
         this.m_PrototypeIndex = treePrototypes.Length;
         this.m_Terrain.terrainData.treePrototypes = array;
     }
     else
     {
         treePrototypes[this.m_PrototypeIndex].prefab     = this.m_Tree;
         treePrototypes[this.m_PrototypeIndex].bendFactor = this.m_BendFactor;
         this.m_Terrain.terrainData.treePrototypes        = treePrototypes;
     }
     this.m_Terrain.Flush();
     EditorUtility.SetDirty(this.m_Terrain);
 }
Esempio n. 17
0
    public static void GenerateFoliage(this TerrainData terrainData)
    {
        int TreeCount = Random.Range(10, 20);
        int ScaleRand = Random.Range(5, 15);

        GameObject[]    Trees         = Resources.LoadAll <GameObject>("Terrain/Scripts/Terrain/Trees");
        TreeInstance[]  treeInstance  = new TreeInstance[TreeCount];
        TreePrototype[] treePrototype = new TreePrototype[Trees.Length];
        for (int p = 0; p < treePrototype.Length; p++)
        {
            treePrototype[p]        = new TreePrototype();
            treePrototype[p].prefab = Trees[p];
        }
        terrainData.treePrototypes = treePrototype;
        float[,] TerrainHeights    = terrainData.GetHeights(0, 0, terrainData.heightmapResolution, terrainData.heightmapResolution);
        for (int t = 0; t < treeInstance.Length; t++)
        {
            int     RandomIndex  = Random.Range(0, treePrototype.Length);
            float   x            = Random.Range(0, 100);
            float   z            = Random.Range(0, 100);
            Vector3 RandPosition = new Vector3(x / 100f, TerrainHeights[(int)x, (int)z], z / 100f);
            float   RandRot      = Random.Range(0, 360);
            treeInstance[t] = new TreeInstance();
            treeInstance[t].prototypeIndex = RandomIndex;
            treeInstance[t].heightScale    = ScaleRand;
            treeInstance[t].widthScale     = ScaleRand;
            treeInstance[t].position       = RandPosition;
            treeInstance[t].rotation       = RandRot;
        }
        terrainData.treeInstances = treeInstance;
    }
    bool ShowTreePrototype(TreePrototype treePrototype, int id)
    {
        bool removeThis = false;

        EGL.BeginVertical(GuiUtils.Skin.box);
        {
            GUILayout.Label(id.ToString() + ". " + (treePrototype.prefab != null ? treePrototype.prefab.name : ""));

            EGL.BeginHorizontal();
            {
                treePrototype.prefab = EGL.ObjectField(treePrototype.prefab, typeof(GameObject), false) as GameObject;

                EGL.BeginVertical();
                {
                    treePrototype.bendFactor = EGL.FloatField("Bend Factor", treePrototype.bendFactor);
                }
                EGL.EndVertical();

                if (GUILayout.Button("Remove", GUILayout.Width(64f), GUILayout.Height(64f)))
                {
                    removeThis = true;
                }
            }
            EGL.EndHorizontal();
        }
        EGL.EndVertical();

        return(removeThis);
    }
Esempio n. 19
0
        /// <summary>
        /// Custom equals operator to take into account treePrototypes.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(prototypeObject == null);
            }

            System.Type instanceType  = obj.GetType();
            bool        prototypeType = instanceType == typeof(TreePrototype);

            if (prototypeType)
            {
                TreePrototype instance = obj as TreePrototype;

                if (instance == null)
                {
                    return(false);
                }

                return(instance.prefab == this.prototypeObject);
            }
            else
            {
                UNTreePrototype instance = obj as UNTreePrototype;

                if (instance == null)
                {
                    return(false);
                }

                return(this.prototypeObject == instance.prototypeObject);
            }
        }
Esempio n. 20
0
        public void RefreshTreePrefabs()
        {
            List <TreePrototype> treePrototypeCollection = new List <TreePrototype>();

            int index = 0;

            foreach (TreeType item in treeTypes)
            {
                foreach (TreePrefab p in item.prefabs)
                {
                    if (p.prefab == null)
                    {
                        continue;
                    }

                    TreePrototype treePrototype = new TreePrototype();
                    treePrototype.prefab = p.prefab;
                    treePrototypeCollection.Add(treePrototype);
                    p.index = treePrototypeCollection.Count - 1;
                }

                index++;
            }
            foreach (Terrain terrain in terrains)
            {
                terrain.terrainData.treePrototypes = treePrototypeCollection.ToArray();

                //Ensures prototypes are persistent
                terrain.terrainData.RefreshPrototypes();
            }
        }
Esempio n. 21
0
 private void DoApply()
 {
     if (base.terrainData != null)
     {
         TreePrototype[] treePrototypes = base.m_Terrain.terrainData.treePrototypes;
         if (this.m_PrototypeIndex == -1)
         {
             TreePrototype[] prototypeArray2 = new TreePrototype[treePrototypes.Length + 1];
             for (int i = 0; i < treePrototypes.Length; i++)
             {
                 prototypeArray2[i] = treePrototypes[i];
             }
             prototypeArray2[treePrototypes.Length]            = new TreePrototype();
             prototypeArray2[treePrototypes.Length].prefab     = this.m_Tree;
             prototypeArray2[treePrototypes.Length].bendFactor = this.m_BendFactor;
             this.m_PrototypeIndex = treePrototypes.Length;
             base.m_Terrain.terrainData.treePrototypes = prototypeArray2;
             TreePainter.selectedTree = this.m_PrototypeIndex;
         }
         else
         {
             treePrototypes[this.m_PrototypeIndex].prefab     = this.m_Tree;
             treePrototypes[this.m_PrototypeIndex].bendFactor = this.m_BendFactor;
             base.m_Terrain.terrainData.treePrototypes        = treePrototypes;
         }
         base.m_Terrain.Flush();
         EditorUtility.SetDirty(base.m_Terrain);
     }
 }
Esempio n. 22
0
        void DoApply()
        {
            if (terrainData == null)
            {
                return;
            }
            TreePrototype[] trees = m_Terrain.terrainData.treePrototypes;
            if (m_PrototypeIndex == -1)
            {
                TreePrototype[] newTrees = new TreePrototype[trees.Length + 1];
                for (int i = 0; i < trees.Length; i++)
                {
                    newTrees[i] = trees[i];
                }
                newTrees[trees.Length]            = new TreePrototype();
                newTrees[trees.Length].prefab     = m_Tree;
                newTrees[trees.Length].bendFactor = m_BendFactor;
                m_PrototypeIndex = trees.Length;
                m_Terrain.terrainData.treePrototypes = newTrees;
                PaintTreesTool.instance.selectedTree = m_PrototypeIndex;
            }
            else
            {
                trees[m_PrototypeIndex].prefab     = m_Tree;
                trees[m_PrototypeIndex].bendFactor = m_BendFactor;

                m_Terrain.terrainData.treePrototypes = trees;
            }
            m_Terrain.Flush();
            EditorUtility.SetDirty(m_Terrain);
        }
Esempio n. 23
0
        void CreateProtoTypes()
        {
            m_treeProtoTypes = new TreePrototype[terrainTrees.Length];
            for (int i = 0; i < terrainTrees.Length; i++)
            {
                m_treeProtoTypes[i]        = new TreePrototype();
                m_treeProtoTypes[i].prefab = terrainTrees[i];
            }

            m_detailProtoTypes = new DetailPrototype[details.Length];
            for (int i = 0; i < details.Length; i++)
            {
                Texture2D tex = new Texture2D(1, 1, TextureFormat.ARGB32, false);
                Color     c   = new Color(i * .5f, i * .25f, i * .1f, i * .1f);
                tex.SetPixel(0, 0, c);
                tex.Apply();
                DetailPrototype prot = new DetailPrototype();
                prot.prototypeTexture = details[i].texture;
                prot.renderMode       = DetailRenderMode.Grass;
                prot.healthyColor     = m_grassHealthyColor;
                prot.dryColor         = m_grassDryColor;
                prot.minHeight        = details[i].Height.x + detailMaster.Height.x;
                prot.maxHeight        = details[i].Height.y + detailMaster.Height.y;
                prot.minWidth         = details[i].Width.x + detailMaster.Width.y;
                prot.maxWidth         = details[i].Width.x + detailMaster.Width.y;
                if (i + detailBillboards - details.Length >= 0)
                {
                    prot.renderMode = DetailRenderMode.GrassBillboard;
                }
                m_detailProtoTypes[i] = prot;
            }
        }
    void AddTree()
    {
        TreePrototype[] trees = new TreePrototype[treeDataList.Count];

        for (int i = 0; i < treeDataList.Count; i++)
        {
            trees[i]        = new TreePrototype();
            trees[i].prefab = treeDataList[i].treeMesh;
        }
        terrainData.treePrototypes = trees;

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

        if (AddTrees)
        {
            for (int z = 0; z < terrainData.size.z; z += treeSpacing)
            {
                for (int x = 0; x < terrainData.size.x; x += treeSpacing)
                {
                    for (int treePrototypeIndex = 0; treePrototypeIndex < trees.Length; treePrototypeIndex++)
                    {
                        if (treeInstancesList.Count < maxTrees)
                        {
                            float currentHeight = terrainData.GetHeight(x, z) / terrainData.size.y;
                            if (currentHeight >= treeDataList[treePrototypeIndex].minHeight &&
                                currentHeight <= treeDataList[treePrototypeIndex].maxHeight)
                            {
                                float randomX = (x + Random.Range(-randomXRange, randomXRange)) / terrainData.size.x;
                                float randomZ = (z + Random.Range(-randomZRange, randomZRange)) / terrainData.size.z;

                                TreeInstance treeInstance = new TreeInstance();
                                treeInstance.position = new Vector3(randomX, currentHeight, randomZ);
                                Vector3 treePosition = new Vector3(treeInstance.position.x * terrainData.size.x,
                                                                   treeInstance.position.y * terrainData.size.y,
                                                                   treeInstance.position.z * terrainData.size.z) + this.transform.position;
                                RaycastHit raycastHit;
                                int        layerMask = 1 << terrainLayerIndex;

                                if (Physics.Raycast(treePosition, Vector3.down, out raycastHit, 100, layerMask) ||
                                    Physics.Raycast(treePosition, Vector3.up, out raycastHit, 100, layerMask))
                                {
                                    float treeHeight = (raycastHit.point.y - this.transform.position.y) / terrainData.size.y;
                                    treeInstance.position       = new Vector3(treeInstance.position.x, treeHeight, treeInstance.position.z);
                                    treeInstance.rotation       = Random.Range(0, 360);
                                    treeInstance.prototypeIndex = treePrototypeIndex;
                                    treeInstance.color          = Color.white;
                                    treeInstance.lightmapColor  = Color.white;
                                    treeInstance.heightScale    = 0.95f;
                                    treeInstance.widthScale     = 0.95f;
                                    treeInstancesList.Add(treeInstance);
                                }
                            }
                        }
                    }
                }
            }
        }
        terrainData.treeInstances = treeInstancesList.ToArray();
    }
Esempio n. 25
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;
                }
            }
        }
Esempio n. 26
0
 public static TreePrototypeConfiguration Serialize(TreePrototype prototype)
 {
     return(new TreePrototypeConfiguration()
     {
         _prefabPath = AssetDatabase.GetAssetPath(prototype.prefab),
         _bendFactor = prototype.bendFactor
     });
 }
Esempio n. 27
0
            public TreePrototype getTreePrototype()
            {
                TreePrototype tree = new TreePrototype();

                tree.bendFactor = bendFactor;
                tree.prefab     = (GameObject)Resources.Load("Prefabs/" + prefabName);
                return(tree);
            }
Esempio n. 28
0
    public static void ClearTrees()
    {
        Terrain     t  = Terrain.activeTerrain;
        TerrainData td = t.terrainData;

        TreePrototype[] treeprototypes = new TreePrototype[0];
        td.treePrototypes = treeprototypes;
    }
Esempio n. 29
0
    private TreePrototype createTreePrototype(GameObject treePrefab)
    {
        TreePrototype tp = new TreePrototype();

        tp.prefab     = treePrefab;
        tp.bendFactor = .2f;
        return(tp);
    }
        protected override void ReadFromImpl(object obj)
        {
            base.ReadFromImpl(obj);
            TreePrototype uo = (TreePrototype)obj;

            prefab     = ToID(uo.prefab);
            bendFactor = uo.bendFactor;
        }