using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour
{
public Vector3 point;
public Terrain TerrainS;
public float BiSuanDaXiao = 200;
public Projector BlobShadowProjector;
protected int hmWidth; // heightmap width 高度圖的寬度
protected int hmHeight; // heightmap height 高度圖的高度
float[,] heightMapBackup;
public float Hight = 1;
public bool ISGG;
void Start()
{
TerrainS = transform.GetComponent<Terrain>();
BlobShadowProjector = GameObject.Find("Cube").GetComponent<Projector>();
//獲取高度圖的寬度
hmWidth = TerrainS.terrainData.heightmapWidth;
//獲取高度圖的高度
hmHeight = TerrainS.terrainData.heightmapHeight;
heightMapBackup = TerrainS.terrainData.GetHeights(0, 0, hmWidth, hmHeight);
}
void OnGUI()
{
if (GUILayout.Button(ISGG.ToString())) { ISGG = !ISGG; }
}
void Update()
{
BlobShadowProjector.orthographicSize = BiSuanDaXiao - 20;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
point = hit.point;
//BlobShadowProjector.transform.position = new Vector3(point.x, BlobShadowProjector.transform.position.y, point.z);
}
if (Input.GetMouseButton(1))
{
DeformTerrain(point, BiSuanDaXiao);
}
if (Input.GetMouseButtonDown(0))
{ ISGG = !ISGG; }
}
//變形的
protected void DeformTerrain(Vector3 pos, float craterSizeInMeters)
{
//獲得點
Vector3 terrainPos = GetRelativeTerrainPositionFromPos(pos, TerrainS, hmWidth, hmHeight);
//高度圖的寬度采樣圖世界單位的地形的
int heightMapCraterWidth = (int)(craterSizeInMeters * (hmWidth / TerrainS.terrainData.size.x));
//高度圖的長度
int heightMapCraterLength = (int)(craterSizeInMeters * (hmHeight / TerrainS.terrainData.size.z));
//高度圖開始點X
int heightMapStartPosX = (int)(terrainPos.x - (heightMapCraterWidth / 2)); //高度圖開始點Z
int heightMapStartPosZ = (int)(terrainPos.z - (heightMapCraterLength / 2)); //地圖高度陣列
float[,] heights = TerrainS.terrainData.GetHeights(heightMapStartPosX, heightMapStartPosZ, heightMapCraterWidth, heightMapCraterLength);
// 圓心x
float circlePosX;
// 圓心y
float circlePosY;
//中心距
float distanceFromCenter;
//我們組的大小地形每個樣品所需的高度
for (int i = 0; i < heightMapCraterLength; i++) //width
{
for (int j = 0; j < heightMapCraterWidth; j++) //height
{//圓判斷
circlePosX = (j - (heightMapCraterWidth / 2)) / (hmWidth / TerrainS.terrainData.size.x);
circlePosY = (i - (heightMapCraterLength / 2)) / (hmHeight / TerrainS.terrainData.size.z);
distanceFromCenter = Mathf.Abs(Mathf.Sqrt(circlePosX * circlePosX + circlePosY * circlePosY));
//如果在圓內(nèi)
if (distanceFromCenter < (craterSizeInMeters / 2))
{
if (ISGG)
{
heights[i, j] = heightMapBackup[i + heightMapStartPosZ, j + heightMapStartPosX];
}
else
{
heights[i, j] = Hight;
}
}
}
}
//修改高度
TerrainS.terrainData.SetHeights(heightMapStartPosX, heightMapStartPosZ, heights);
}
//的到正確的地形位置
protected Vector3 GetNormalizedPositionRelativeToTerrain(Vector3 pos, Terrain terrain)
{
Vector3 tempCoord = (pos - terrain.gameObject.transform.position);
Vector3 coord;
coord.x = tempCoord.x / TerrainS.terrainData.size.x;
coord.y = tempCoord.y / TerrainS.terrainData.size.y;
coord.z = tempCoord.z / TerrainS.terrainData.size.z;
return coord;
}
//得到相對的地形位置
protected Vector3 GetRelativeTerrainPositionFromPos(Vector3 pos, Terrain terrain, int mapWidth, int mapHeight)
{
Vector3 coord = GetNormalizedPositionRelativeToTerrain(pos, terrain);
// 獲取這個游戲?qū)ο蟮匦胃叨葓D的位置
return new Vector3((coord.x * mapWidth), 0, (coord.z * mapHeight));
}
void OnApplicationQuit()
{
//退出程序地形還原
TerrainS.terrainData.SetHeights(0, 0, heightMapBackup);
}
}