簡體   English   中英

在Unity中的地形上設置生成的Texture2D嗎?

[英]Set generated Texture2D on terrain in Unity?

我在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設置為地面紋理?

因為內置的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 - > ClassesGameObjectMaterialShaderTerrain ,和Texture2D

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM