• Unity3D教程宝典之地形


    前言:

    本文不是讲地形编辑器的使用,而是主要讲解:
    (1)地形相关知识
    (2)使用代码创建地形
    (3)使用AnimationCurve创建曲面地形
    (4)使用photoshop绘制地形表面,即SplatAlphaMap
    (5)使用代码为地形添加树

    本讲结构:

    一:地形的基础知识
    (1)地形编辑器的不足
    (2)地形结构
    (3)地形与SplatAlpha

    二:动态创建地形
    (1)动态创建简单平面地形
    (2)动态创建凹凸地形
    (3)利用AnimationCurve创建特殊曲线地形。
    (4)动态创建地形,并设置splatPrototypes,最后使用photoShop绘制2D图编辑地形贴图。
    (5)动态创建地形,使用photoShop绘制 多张splats

    三:地形与树
    (1)TreePrototypes
    (2)TreeInstances

    一: 地形的基础知识

    (0)基本术语
    Splat:地形所使用的贴图,即Terrain Texture。术语叫Splat或者 Splat map。
    Splat Alhpa Map:地形贴图布局图,用图的rgba各个通道来表示贴图在地形上的使用,project界面里展开地形即可看见。术语叫Splat Alpha Map,或者Alpha Map.

    (1)地形编辑器的不足
    地形Terrain是3D游戏里必不可少的一部分。Unity提供的地形编辑器也十分好用,但是依然有少许不足和缺陷。

    Unity地形编辑器的不足:
    a、地形只能是成片的抬高或者降低。如果想定制某特定斜率,或者特定曲线的地形就没法实现了。
    b、地形不能实时改变。

    不过Unity提供了强大的地形脚本接口,可以弥补上述不足。

    (2)地形结构

    首先要清楚, Terrain地形的包括Heightmap高度图,贴图信息,树信息等几乎所有数据都是储存TerrainData里,而TerrainData可以保存成地形文件,地形文件后缀为.asset。任意工程导入地形文件后,在project窗口下都会显示为地形文件。

    TerrainData的基本属性:
    1.terrainData.heightmapResolution int,高度图的长宽分辨率,一般是2的幂加1,如513
    2.terrainData.baseMapResolution int,Resolution of the base map used for rendering far patches on the terrain , 如513
    3.terrainData.size:   Vector3,地形世界里的尺寸,world unit. 如new Vector3(50, 50, 50);
    4.terrainData.alphamapResolution alphamap的分辨率,如512;

    地形贴图信息储存在Terrain之下的SplatAlpha图里。在project窗口展开一个地形,会看到之下的贴图信息,名称格式为SplatAlpha xx.
    SplatAlpha xx
     

    (3)地形与SplatAlpha

    在SplatAlpha图中
    红=第1张贴图
    绿=第2张贴图
    蓝=第3张贴图
    Alpha=第4张贴图

    第5张贴图开始,会创建新的SplatAlpha图,然后继续 红绿蓝黑 如此循环。

    alphamap:指的是纹理中某通道的颜色, refer to a grayscale image residing in a single channel of a texture
    Splat:一张纹理贴图和其对应的alphamap统称为一个splat。 主要是分块,divide in chunks.所以可以使用LOD等技术

    terrainData.splatPrototypes 就是地形包含的贴图信息
    splatPrototypes 为SplatPrototype[],
    SplatPrototype为单张贴图信息
    SplatPrototype的属性有
    SplatPrototype.texture     Texture2D,地形贴图
    SplatPrototype.tileOffset   Vector2,图块偏移
    SplatPrototype.tileSize     Vector2,图块尺寸(World Unit)

    terrainData.SetAlphamaps(int x,int y,float[,,]) ,其中x,y为起点
    float[i,j,k]为通道信息,i,j为对应的点,k为第几张图,float值储存的是该点该图的灰度值。
    terrainData.splatPrototypes的长度 = 贴图数量 = splatArray (float[,,])的第三维的长度

    二:动态创建地形

    (1)动态创建简单平面地形
    创建地形是不需要using UnityEditor的,这里使用了AssetDatabase,所以需using UnityEditor;
    创建三步:
    a) 第一步:

    TerrainData  terrainData = new TerrainData();

    b) 设置terrainData的属性
    c) 根据terrainData创建地形

    GameObject obj = Terrain.CreateTerrainGameObject(terrainData);
    具体脚本如下:
    using UnityEngine;
    using System.Collections;
    using UnityEditor;
    
    public class Tutor_1_CreateSimpleTerrain : MonoBehaviour {
        
        void Start()
        {
            CreateTerrain();
        }
        
        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);
            GameObject obj = Terrain.CreateTerrainGameObject(terrainData);
            AssetDatabase.CreateAsset(terrainData, "Assets/Tutorial/Tutor_1_SimpleTerrain.asset");
            AssetDatabase.SaveAssets();
            return  obj.GetComponent<Terrain>();
        }
    }

    (2)动态创建凹凸地形

    接下来改变地形的高度。地形高度是用heightmap存储的。

    代码里通过TerrainData.GetHeights()读取高度图里的二维高度数组,
    通过TerrainData.SetHeights()设置高度图里的二维高度数组。

    TerrainData.GetHeights(int x的起点,int y的起点,int 读取高度的宽度范围, int 读取高度的高度范围),返回float[,] ,二维高度数组
    TerrainData.SetHeights(int x的起点,int y的起点,float[,] 二维高度数组),返回void

    例子:在创建地形前,改变地形的高度,代码如下:

    using UnityEngine;
    using System.Collections;
    using UnityEditor;
    
    public class Tutor_2_CreateTerrain_ModifyHeight : MonoBehaviour {
    
        void Start()
        {
            CreateTerrain();
        }
    
        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);
            ModifyTerrainDataHeight(terrainData);
            GameObject obj = Terrain.CreateTerrainGameObject(terrainData);
            AssetDatabase.CreateAsset(terrainData,"Assets/Tutorial/Tutor_2_Terrain_ModifyHeight.asset");
            AssetDatabase.SaveAssets();
            return  obj.GetComponent<Terrain>();
        }
        
        public void ModifyTerrainDataHeight(TerrainData terrainData)
        {
            int width = terrainData.heightmapWidth;
            int height = terrainData.heightmapHeight;
            float[,] array = new float[width,height];
            print (""+ width +" height:"+ height );
            for(int i=0; i< width;i++) {
                for(int j=0; j< height;j++)
                {
                    float f1 = i;
                    float f2 = width;
                    float f3 = j;
                    float f4 = height;
                    float baseV =  (f1/f2 + f3/f4)/2 *  1;
                    array[i,j] =baseV*baseV;
                }
            }
            terrainData.SetHeights(0,0,array);
        }
    }

    (3)利用AnimationCurve创建特殊曲线地形

    之前风宇冲有一篇关于AnimationCurve的教程,现在我们就来看看如何用AnimationCurve来控制地形的起伏。
    步骤:
    1.创建一个新场景,并新建一个GameObject起名为Manager,新建一个名为Tutor_3_CreateTerrainWithAnimationCurve.cs的脚本并拖至Manager上。
    2.粘贴并覆盖如下脚本至Tutor_3_CreateTerrainWithAnimationCurve里

    using UnityEngine;
    using System.Collections;
    
    public class Tutor_3_CreateTerrainWithAnimationCurve : MonoBehaviour {
        public AnimationCurve animationCurve;
    }

    3.在Manager的Inspector面板里双击Animation Curve。在弹出曲线绘制界面后,绘制任意曲线,如下。
    curve

    之后还是设置高度,即terrainData.SetHeights(0,0,array);只是对array里高度的赋值变为如下

    array[i,j] = animationCurve.Evaluate(f1/f2);

    其中f1是array里的y,f2是整个高度图的高度。具体代码如下:

    using UnityEngine;
    using System.Collections;
    using UnityEditor;
    
    public class Tutor_3_CreateTerrainWithAnimationCurve : MonoBehaviour {
        public AnimationCurve animationCurve;
        
        void Start()
        {
            CreateTerrain();
        }
        
        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);
            ModifyTerrainDataHeight(terrainData);
            GameObject obj = Terrain.CreateTerrainGameObject(terrainData);
            AssetDatabase.CreateAsset(terrainData,"Assets/Tutorial/Tutor_3_TerrainWithAnimationCurve.asset");
            AssetDatabase.SaveAssets();
            return  obj.GetComponent<Terrain>();
        }
        
        public void ModifyTerrainDataHeight(TerrainData terrainData)
        {
            int width = terrainData.heightmapWidth;
            int height = terrainData.heightmapHeight;
            float[,] array = new float[width,height];
            print (""+ width +" height:"+ height );
            for(int i=0; i< width;i++)
            {
                for(int j=0; j< height;j++)
                {
                    float f1 = j;
                    float f2 = height;
                    array[i,j] = animationCurve.Evaluate(f1/f2);
                }
            }
            terrainData.SetHeights(0,0,array);
        }
    }

    最后创建的地形和我们之前设置的曲线一致。
    terrain_01

    (4)动态创建地形,并设置splatPrototypes,最后使用photoShop绘制2D图编辑地形贴图

    步骤:

    1.创建一个新场景并命名为Tutor_4_CreateTerrainWithSplat,并新建一个GameObject起名为Manager,新建一个名为
    Tutor_4_CreateTerrainWithSplat.cs的脚本并拖至Manager上。
    2.粘贴并覆盖如下脚本至Tutor_4_CreateTerrainWithSplat里,该脚本与上个例子的脚本基本一致。

    using UnityEngine;
    using System.Collections;
    using UnityEditor;
    
    public class Tutor_4_CreateTerrainWithSplat : MonoBehaviour {
        public AnimationCurve animationCurve;
        
        void Start()
        {
            CreateTerrain();
        }
        
        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);
            ModifyTerrainDataHeight(terrainData);
            GameObject obj = Terrain.CreateTerrainGameObject(terrainData);
            AssetDatabase.CreateAsset(terrainData, "Assets/Tutorial/Tutor_4_TerrainWithSplats.asset");
            AssetDatabase.SaveAssets();
            return  obj.GetComponent<Terrain>();
        }
        
        public void ModifyTerrainDataHeight(TerrainData terrainData)
        {
            int width = terrainData.heightmapWidth;
            int height = terrainData.heightmapHeight;
            float[,] array = new float[width,height];
            print (""+ width +" height:"+ height );
            for(int i=0; i< width;i++) {
                for(int j=0; j< height;j++)
                {
                    float f1 = j;
                    float f2 = height;
                    array[i,j] = animationCurve.Evaluate(f1/f2);
                }
            }
            terrainData.SetHeights(0,0,array);
        }
    }

    之后添加地形贴图变量,在脚本里加上

    public Texture2D[] splats;

    然后在Inspector里指定任意一张贴图。

    现在开始添加Splat了。

    核心是SplatPrototype,也就是Splat原型的信息,包含贴图信息和地形块的信息。

    SplatPrototype outSplatPrototype = new SplatPrototype();

    之后设置SplatPrototype的如下属性
    outSplatPrototype.texture: 贴图
    outSplatPrototype.tileOffset:地形块的偏移。
    outSplatPrototype.tileSize:地形块的尺寸
    组成好SplatPrototype[] outSplatPrototypes后,赋予TerrainData.splatPrototypes 即可。

    代码如下:

    using UnityEngine;
    using System.Collections;
    using UnityEditor;
    
    public class Tutor_4_CreateTerrainWithSplat : MonoBehaviour {
        public AnimationCurve animationCurve;
        public Texture2D[] splats;
        
        void Start()
        {
            CreateTerrain();
        }
        
        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);
            terrainData.splatPrototypes = CreateSplatPrototypes(splats,new Vector2(15,15),new Vector2(0,0)); 
            ModifyTerrainDataHeight(terrainData);
            GameObject obj = Terrain.CreateTerrainGameObject(terrainData);
            AssetDatabase.CreateAsset(terrainData, "Assets/Tutorial/Tutor_4_TerrainWithSplats.asset");
            AssetDatabase.SaveAssets();
            return  obj.GetComponent<Terrain>();
        }
        
        public void ModifyTerrainDataHeight(TerrainData terrainData)
        {
            int width = terrainData.heightmapWidth;
            int height = terrainData.heightmapHeight;
            float[,] array = new float[width,height];
            for(int i=0; i< width;i++)
            {
                for(int j=0; j< height;j++)
                {
                    float f1 = j;
                    float f2 = height;
                    array[i,j] = animationCurve.Evaluate(f1/f2);
                }
            }
            terrainData.SetHeights(0,0,array);
        }
        
        public SplatPrototype[] CreateSplatPrototypes(Texture2D[] tmpTextures,Vector2 tmpTileSize,Vector2 tmpOffset)
        {
            SplatPrototype[] outSplatPrototypes = new SplatPrototype[tmpTextures.Length];
            for(int i = 0; i < tmpTextures.Length; i++)
            {
                outSplatPrototypes[i] = CreateSplatPrototype(tmpTextures[i],tmpTileSize,tmpOffset);
            }
            return outSplatPrototypes;
        }
        
        public SplatPrototype CreateSplatPrototype(Texture2D tmpTexture, Vector2 tmpTileSize,Vector2 tmpOffset)
        {
            SplatPrototype outSplatPrototype = new SplatPrototype();
            outSplatPrototype.texture = tmpTexture;
            outSplatPrototype.tileOffset = tmpOffset;
            outSplatPrototype.tileSize = tmpTileSize ;
            return outSplatPrototype;
        }
    }

    之前的例子,创建出来的地形在project界面里一直不能展开,因为没有任何splat信息。而本例中,指定TerrainData.splatPrototypes,即有了splat的原型信息后,就会自动生成splat图,因此地形可以展开了。

    展开出来的SplatAlpha 01为纯红,表示整个地图铺的都是第一张地形贴图。
    SplatAlpha 01

     

    现在我们再进一步,通过Photoshop绘制splat的alpha图。
    1.选中Manager,在Inspector面板里,将Terrain Textures设置为任意两张贴图,这里用的是unity Terrain包里的“Grass&Rock”和 “Grass (Hill)” 两张贴图。
    2.然后脚本里也添加splats属性,public Texture2D[] splatAlphaMaps;
    3.打开photoshop,创建一张512x512的rgb图,先铺满红色,再绘制一些绿色,如下图
     splatAlphaMap1

    之后导出为splatAlphaMap1.png

    4.将该图拖进unity,并设置Import Settings里的 Texture Type为Advanced,然后勾选Read/Write Enable。最后把importType设置为Default(importType不能为其他的)。
    texture_setting
     

    5. Manager的Inspector面板里,splatAlphaMaps,此Texture2D数组设置为1,并指定为splatAlphaMap1

    6.在Tutor_4_CreateTerrainWithSplat脚本里补充以下函数:

    public float[,,] CreateSplatAlphaArray(Texture2D[] splatAlphaMaps,int numOfSplatPrototypes )
    {
        List cArray = new List();
        int splatAlphaMap_SizeX = splatAlphaMaps[0].width;
        int splatAlphaMap_SizeY = splatAlphaMaps[0].height;
        float[,,] outSplatAlphaArray = new float[splatAlphaMap_SizeX,splatAlphaMap_SizeY,numOfSplatPrototypes];
        
        //第几张SplatAlphaMap
        for(int splatAlphaMapIndex=0; splatAlphaMapIndex < splatAlphaMaps.Length; splatAlphaMapIndex++)
        {
            //RGBA第几个通道
             for(int alphaIndex =0;alphaIndex<4; alphaIndex ++)
            {
                //Splat ID
                int splatIndex = alphaIndex+splatAlphaMapIndex*4;
                //仅当Splat ID小于Splat的数量时
                  if(splatIndex < numOfSplatPrototypes)
                {
                    for (int index_heightmapY = 0; index_heightmapY < splatAlphaMap_SizeY; index_heightmapY++)
                    {
                        for (int index_heightmapX = 0; index_heightmapX < splatAlphaMap_SizeX; index_heightmapX++)
                        {
                            //取第splatAlphaMapIndex张SplatAlphaMap上的位于index_heightmapY,index_heightmapX的颜色值
                                Color c =  splatAlphaMaps[splatAlphaMapIndex].GetPixel(index_heightmapY,index_heightmapX);
                            cArray.Add(c);
                            //赋予outSplatAlphaArray的index_heightmapX,index_heightmapY,splatIndex对应的通道值
                                outSplatAlphaArray[index_heightmapX, index_heightmapY, splatIndex] = c[ alphaIndex ];
                        }
                    }
                }
                else
                {
                    return outSplatAlphaArray;
                }
            }
        }
        return outSplatAlphaArray;
    }
    之后在Start函数里的CreateTerrain(); 后面补充代码如下
    void Start()
    {
        CreateTerrain();
        TerrainData terData =AssetDatabase.LoadAssetAtPath("Assets/Tutorial/Tutor_4_TerrainWithSplats.asset",typeof(TerrainData)) as TerrainData;
        float[,,] splatAlphaArray = CreateSplatAlphaArray(splatAlphaMaps, terData.splatPrototypes.Length);
        terData.SetAlphamaps(0,0,splatAlphaArray);
    }
    之后点击运行,发现地形确实是我们之前在photoshop里绘制的形状,如下
    terrain_2

    注意:

    1.SetAlphamaps函数 必须在创建TerrainData文件,即xxx.asset后,再读取该TerrainData调用。

    (5)动态创建地形,使用photoShop绘制 多张splats

    三张以下的地形贴图一般不会出错,但是超过四张时就要注意了。用photoShop绘制a通道并导入unity是比较容易出错的。
    1.打开Photoshop,新建一张图(512x512大小,RGB颜色模式)。
    2.用黑色平铺整张图后,添加一个通道
    3.选中Red通道,然后用纯白画笔在顶端画一条横线
    4.用3.的方法在gba通道依次往下画横线. 最后的效果如下:
      rgba
    5.保存成splatAlpha.psd文件并拖入unity工程。
    注意:如果导入的贴图是有a通道的话,一定要保证在Preview里看到的是RGBA,即有A的字样。有时候在photoshop里即使有a,保存成png或者tga再导入unity,有时候会没有A,那么就是错的。
    rgba_a  image

    6.打开(4)里创建的Tutor_4_CreateTerrainWithSplat场景,另存为Tutor_5_CreateTerrainWithSplat2。
    7.选中Manager,然后将Splats设置为4张并指定贴图。将Splat Alpha Maps设置为一张并指定为splatAlpha.psd
    之后运行,发现新建的地形确实是按splatAlpha.psd里的色带分布。
    image
     

    之后我们再进一步,再在Splats里加第5张贴图,那么该splat图就对应第二张splatAlpha图的红色。
    8.photoShop里按上面的方法建张图,画个红色的竖条或者其他形状,并保存为splatAlpha2.psd
    image

    9.选中Manager,然后将Splats设置为5张,指定第5张贴图。将Splat Alpha Maps设置为2并指定为之前的splatAlpha.psd和splatAlpha2.psd
    之后运行,效果如下
    image

     

    三:地形与树

    地形的绘制树功能十分强大,而且不仅可以绘制树,任意物体都可以都可以被当做树来铺在地形上。
    (1)TreePrototypes
    该步风宇冲用代码做总是出错,搜了也找不到解决方法,只好用地形编辑器来添加树的原型。现在把代码贴出来,
    欢迎高手指出问题所在。
    代码如下,unity显示在terrainData.treePrototypes = new TreePrototype[1];这行有null

    using UnityEngine;
    using System.Collections;
    
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    
    public class Tutor_6_TreePrototype : MonoBehaviour {
    
        public GameObject treePrefab;
        public TerrainData terrainData;
    
        void Start()
        {
            Terrain terrain = CreateTerrain();
    
            AddTrees(terrain, 100);
        }
    
        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>();
        }
    
        public void AddTrees(Terrain terrain, int numOfTrees)
        {
            if (terrain.terrainData != null)
            {
                terrain.terrainData.treeInstances = new TreeInstance[numOfTrees];
                for (int i = 0; i < numOfTrees; i++)
                {
                    TreeInstance tmpTreeInstances = new TreeInstance();
                    tmpTreeInstances.prototypeIndex = 0; // ID of tree prototype
                    tmpTreeInstances.position = new Vector3(Random.Range(0f, 1f), 0, Random.Range(0f, 1f));   // not regular pos,  [0,1]
                    tmpTreeInstances.color = new Color(1, 1, 1, 1);
                    tmpTreeInstances.lightmapColor = new Color(1, 1, 1, 1);//must add
                    float ss = Random.Range(0.8f, 1f);
                    tmpTreeInstances.heightScale = ss; //same size as prototype
                    tmpTreeInstances.widthScale = ss;
                    terrain.AddTreeInstance(tmpTreeInstances);
                }
                TerrainCollider tc = terrain.GetComponent<TerrainCollider>();
                tc.enabled = false;
                tc.enabled = true;
            }
        }
    }

    原文代码有错误,原因在于对treePrototypes属性赋值时,必须确保数组中的元素为合法的值。

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

    (2)TreeInstances
    步骤:
    1.创建TreeInstance
    TreeInstance tmpTreeInstances = new TreeInstance();

    2.设置TreeInstance属性

    TreeInstance.prototypeindex:使用的prototype序号,从0开始
    TreeInstance.position:在地形里的相对位置(不是世界坐标的位置),范围为[0,1]
    TreeInstance.color:树的颜色
    TreeInstance.lightmapColor:树如果有lightmap的话,lightmap的颜色
    TreeInstance.heightScale:树高的缩放 即y轴上的缩放
    TreeInstance.widthScale:树宽的缩放,即xz轴上的缩放

    3.地形Terrain添加TreeInstance

    Terrain terrain;
    
    terrain.AddTreeInstance(tmpTreeInstances);

    4.重设碰撞

    TerrainCollider  tc = terrain.GetComponent();
    tc.enabled = false;
    tc.enabled = true;

    例子:在整个地形上创建100个随机位置,大小为[0.8,1]间随机的树。脚本如下:

    using UnityEngine;
    using System.Collections;
    
    public class Tutor_6_TreeInstances : MonoBehaviour {
        public Terrain terrain;
        
        void Start () {
            AddTrees(terrain,100);
        }
        
        public void AddTrees(Terrain terrain,int numOfTrees)
        {
            if(terrain.terrainData!=null)
            {
                terrain.terrainData.treeInstances = new TreeInstance[numOfTrees];
                for(int i =0;i< numOfTrees;i++)
                {
                    TreeInstance tmpTreeInstances = new TreeInstance();
                    tmpTreeInstances.prototypeIndex =0; // ID of tree prototype
                    tmpTreeInstances.position = new Vector3(Random.Range(0f,1f),0,Random.Range(0f,1f));   // not regular pos,  [0,1]
                    tmpTreeInstances.color = new Color(1,1,1,1);
                    tmpTreeInstances.lightmapColor = new Color(1,1,1,1);//must add
                    float ss= Random.Range(0.8f,1f);
                    tmpTreeInstances.heightScale =ss; //same size as prototype
                    tmpTreeInstances.widthScale =ss;
                    terrain.AddTreeInstance(tmpTreeInstances);
                }
                TerrainCollider  tc = terrain.GetComponent<TerrainCollider>();
                tc.enabled = false;
                tc.enabled = true;
            }
        }
    }
    运行效果如下:
    image

    结语:整个地形教程到此结束,希望对大家有所帮助。

    本文转载自他人博客内容,文中的图片内容已经缺失,现在已经补充上去了。 相关的资源和代码可以从 https://github.com/jungletree/terrain-sample 下载。

    Ref: https://blog.csdn.net/smilingeyes/article/details/41805475
    Ref: https://www.cnblogs.com/Uinkanade/articles/4334898.html

  • 相关阅读:
    两种&
    安装版Windows是必须的
    检验CSS3.0兼容性的有趣东东
    Quadro和Geforce系列的区别
    ECMAScript
    多拉A梦的CSS(6) :not
    多拉A梦的CSS(1) Transform
    表单
    关于a元素的伪类
    多拉A梦的CSS(4) gradient
  • 原文地址:https://www.cnblogs.com/open-coder/p/12331851.html
Copyright © 2020-2023  润新知