/// <summary> /// 利用笔刷设置贴图 /// </summary> public async void SetTextureWithBrush(Terrain terrain, Vector3 point, int index, float radius, float[,] brush) { Vector3 down = new Vector3(point.x - radius, 0, point.z - radius); if (terrain != null) { Vector2Int mapIndex = TerrainUtility.GetAlphaMapIndex(terrain, down); int mapRadius = (int)(radius / terrain.terrainData.size.x * terrain.terrainData.alphamapResolution); float[,,] map = terrain.terrainData.GetAlphamaps(mapIndex.x, mapIndex.y, 2 * mapRadius, 2 * mapRadius); float[,] realBrush = await Math2d.ZoomBilinearInterpAsync(brush, 2 *mapRadius, 2 *mapRadius); // 这么计算所附加的贴图实际上是有问题的,但是我懒得继续算了 for (int i = 0; i < map.GetLength(0); i++) { for (int j = 0; j < map.GetLength(1); j++) { float temp = (1 - realBrush[i, j]) / (terrain.terrainData.alphamapLayers - 1); for (int k = 0; k < terrain.terrainData.alphamapLayers; k++) { map[i, j, k] = temp; } map[i, j, index] = 1 - temp; } } terrain.terrainData.SetAlphamaps(mapIndex.x, mapIndex.y, map); } }
/// <summary> /// 设置贴图 /// </summary> /// <param name="center">中心点</param> /// <param name="radius">半径</param> /// <param name="index">layer索引</param> /// <param name="strength">力度</param> /// <param name="isMix">是否混合, 如果为false 则strength无效</param> private void InternalSetTexture(Vector3 center, float radius, int index, float strength, bool isMix, bool regesterUndo) { Vector3 leftDown = new Vector3(center.x - radius, 0, center.z - radius); Terrain terrain = Utility.SendRayDown(leftDown, LayerMask.GetMask("Terrain")).collider?.GetComponent <Terrain>(); if (terrain != null) { // 获取数据 TerrainData terrainData = terrain.terrainData; int mapRadius = (int)(radius / terrainData.size.x * terrainData.alphamapResolution); Vector2Int mapIndex = TerrainUtility.GetAlphaMapIndex(terrain, leftDown); float[,,] alphaMap = TerrainUtility.GetAlphaMap(terrain, mapIndex.x, mapIndex.y, 2 * mapRadius, 2 * mapRadius); if (regesterUndo) { TextureCmdData textureCmdData = new TextureCmdData(terrain, mapIndex, alphaMap); RegisterUndo(new TextureCmd(textureCmdData)); } // 修改数据 if (isMix) { ChangeAlphaMap(alphaMap, index, strength); } else { ChangeAlphaMapNoMix(alphaMap, index); } // 设置数据 TerrainUtility.SetAlphaMap(terrain, alphaMap, mapIndex.x, mapIndex.y); } }
/// <summary> /// 设置贴图 /// </summary> /// <param name="point"></param> /// <param name="index"></param> public void InternalSetTexture(Vector3 center, float radius, int index, float strength) { Vector3 leftDown = new Vector3(center.x - radius, 0, center.z - radius); Terrain terrain = Utility.SendRayDown(leftDown, LayerMask.GetMask("Terrain")).collider?.GetComponent <Terrain>(); if (terrain != null) { // 获取数据 TerrainData terrainData = terrain.terrainData; int mapRadius = (int)(radius / terrainData.size.x * terrainData.alphamapResolution); Vector2Int mapIndex = TerrainUtility.GetAlphaMapIndex(terrain, leftDown); float[,,] alphaMap = TerrainUtility.GetAlphaMap(terrain, mapIndex.x, mapIndex.y, 2 * mapRadius, 2 * mapRadius); // 修改数据 ChangeAlphaMap(alphaMap, index, strength); // 设置数据 TerrainUtility.SetAlphaMap(terrain, alphaMap, mapIndex.x, mapIndex.y); } }