【问题标题】:how to change terrain texture in code如何在代码中更改地形纹理
【发布时间】:2016-11-16 15:31:58
【问题描述】:

我想通过代码改变地形纹理的偏移量(2)。 我在地形上添加了道路图像作为纹理。 我在网上找到了相关代码,但我无法弄清楚这种情况下渲染器的作用。

不仅仅是代码,我只想知道为了通过代码改变纹理应该采取的第一步。 (基本上是设置)。 并请提及渲染器的作用。

【问题讨论】:

    标签: c# unity3d textures renderer terrain


    【解决方案1】:

    在 Unity Terrains 中,纹理由 SplatPrototype 类处理。 See documentation

    Splat 原型只是 TerrainData 使用的纹理。

    所以如果你想改变TerrainTexture你必须创建一个新的SplatPrototype并将它设置为splatPrototype TerrainData 的变量。

    您可以在此处设置您选择的metallicnormalMapsmoothnesstexturetileSizetileOffset 的值。

    您可以使用以下方法:

    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;
    }
    

    【讨论】:

    • 谢谢。我会试一试,然后告诉你。
    • @ConnorIglesias 好的,完成后别忘了标记为已解决
    • @ConnorIglesias 我认为在运行时更改地形纹理是一项繁重的操作,应该实时避免。我个人在关卡加载期间使用此方法。
    • 好的。我实际上将它用于无限运行游戏。我会想别的办法。不想提高批次和 setpasscalls。
    • @ConnorIglesias 你试过这个解决方案了吗?也许它可以工作,我从未尝试过实时但我很想知道它是否有效。
    【解决方案2】:

    这对我来说很重要

    splat[i].tileOffset = new Vector2(tar.splatPrototypes[i].tileOffset.x, tar.splatPrototypes[i].tileOffset.y+5f);
    

    【讨论】:

      【解决方案3】:

      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();
      

      【讨论】:

        猜你喜欢
        • 2013-12-04
        • 1970-01-01
        • 2021-11-02
        • 2019-07-13
        • 1970-01-01
        • 2014-06-20
        • 1970-01-01
        • 1970-01-01
        • 2013-08-03
        相关资源
        最近更新 更多