【发布时间】:2016-11-16 15:31:58
【问题描述】:
我想通过代码改变地形纹理的偏移量(2)。 我在地形上添加了道路图像作为纹理。 我在网上找到了相关代码,但我无法弄清楚这种情况下渲染器的作用。
不仅仅是代码,我只想知道为了通过代码改变纹理应该采取的第一步。 (基本上是设置)。 并请提及渲染器的作用。
【问题讨论】:
标签: c# unity3d textures renderer terrain
我想通过代码改变地形纹理的偏移量(2)。 我在地形上添加了道路图像作为纹理。 我在网上找到了相关代码,但我无法弄清楚这种情况下渲染器的作用。
不仅仅是代码,我只想知道为了通过代码改变纹理应该采取的第一步。 (基本上是设置)。 并请提及渲染器的作用。
【问题讨论】:
标签: c# unity3d textures renderer terrain
在 Unity Terrains 中,纹理由 SplatPrototype 类处理。 See documentation
Splat 原型只是 TerrainData 使用的纹理。
所以如果你想改变Terrain的Texture你必须创建一个新的SplatPrototype并将它设置为splatPrototype TerrainData 的变量。
您可以在此处设置您选择的metallic、normalMap、smoothness、texture、tileSize 和tileOffset 的值。
您可以使用以下方法:
private void SetTerrainSplatMap(Terrain terrain, Texture2D[] textures)
{
var terrainData = terrain.terrainData;
// The Splat map (Textures)
SplatPrototype[] splatPrototype = new SplatPrototype[terrainData.splatPrototypes.Length];
for (int i = 0; i < terrainData.splatPrototypes.Length; i++)
{
splatPrototype[i] = new SplatPrototype();
splatPrototype[i].texture = textures[i]; //Sets the texture
splatPrototype[i].tileSize = new Vector2(terrainData.splatPrototypes[i].tileSize.x, terrainData.splatPrototypes[i].tileSize.y); //Sets the size of the texture
splatPrototype[i].tileOffset = new Vector2(terrainData.splatPrototypes[i].tileOffset.x, terrainData.splatPrototypes[i].tileOffset.y); //Sets the size of the texture
}
terrainData.splatPrototypes = splatPrototype;
}
【讨论】:
这对我来说很重要
splat[i].tileOffset = new Vector2(tar.splatPrototypes[i].tileOffset.x, tar.splatPrototypes[i].tileOffset.y+5f);
【讨论】:
Splat 原型已弃用。我改用 TerrainLayers 来编辑纹理的平铺大小。
float[,,] splatMapData = terrain.terrainData.GetAlphamaps(0, 0, 100, 100);
for (int i = 26; i < 100; i++)
{
for (int j=0; j < 100; j++)
{
splatMapData[i, j, 0] = 0;
splatMapData[i, j, 1] = 0;
splatMapData[i, j, 2] = 1;
}
}
TerrainLayer[] layers = terrain.terrainData.terrainLayers;
layers[2].tileSize = new Vector2(100, 100);
terrain.terrainData.SetAlphamaps(0, 0, splatMapData);
terrain.Flush();
【讨论】: