0

我在 Unity 中创建了一个简单的 Texture2D,如下所示(简化示例):

   Texture2D texture2d = new Texture2D(256,256);
   for (int h = 0; h < texture2d.height; h++) {
        for (int w = 0; w < texture2d.width ; w++) {
            texture2d.SetPixel(w,h, Color.green);
        }
    }

如何在现有地形上将此创建的 Texture2D 设置为地面纹理?

4

1 回答 1

1

因为内置Terrain着色器没有_MainTexture属性,所以您必须使用自定义着色器:例如http://wiki.unity3d.com/index.php/TerrainFourLayerToon(放在.shader文件中)

从那里你只需要适当的代码

//your code here
texture2d.Apply(); //missing in your code!

Material mat = new Material(Shader.Find("Terrain/Four Layer Toon Compat")); //from link
mat.SetTexture("_MainTex" , texture2d); //set the texture properties of the shader
mat.SetTexture("_MainTex2", texture2d);
mat.SetTexture("_MainTex3", texture2d);
mat.SetTexture("_MainTex4", texture2d);

Terrain terr = this.gameObject.GetComponent<Terrain>(); //get the terrain
terr.materialType = Terrain.MaterialType.Custom; //tell the terrain you want to manually assign its material
terr.materialTemplate = mat; //finally assign the material (and texture)

这段代码应该在 Unity 5 中工作

如果您对这些函数的作用感到好奇,请查看https://docs.unity3d.com/Manual/SL-Reference.html中的ShaderLabCG.shader文件类型

http://docs.unity3d.com/ScriptReference/UnityEngine->ClassesGameObject, Material, Shader,TerrainTexture2D.

于 2015-08-30T23:32:59.333 回答