public CTSHeightMap DivideClamped(CTSHeightMap CTSHeightMap, float min, float max) { if (this.m_widthX != CTSHeightMap.m_widthX || this.m_depthZ != CTSHeightMap.m_depthZ) { Debug.LogError((object)"Can not divide different sized CTSHeightMap"); return(this); } for (int index1 = 0; index1 < this.m_widthX; ++index1) { for (int index2 = 0; index2 < this.m_depthZ; ++index2) { float num = this.m_heights[index1, index2] / CTSHeightMap.m_heights[index1, index2]; if ((double)num < (double)min) { num = min; } else if ((double)num > (double)max) { num = max; } this.m_heights[index1, index2] = num; } } this.m_isDirty = true; return(this); }
/// <summary> /// Copy the source CTSHeightMap and clamp it /// </summary> /// <param name="CTSHeightMap">CTSHeightMap to copy</param> /// <param name="min">Min value to clamp it to</param> /// <param name="max">Max value to clamp it to</param> /// <returns>This</returns> public CTSHeightMap CopyClamped(CTSHeightMap CTSHeightMap, float min, float max) { if (m_widthX != CTSHeightMap.m_widthX || m_depthZ != CTSHeightMap.m_depthZ) { Debug.LogError("Can not copy different sized CTSHeightMap"); return(this); } for (int x = 0; x < m_widthX; x++) { for (int z = 0; z < m_depthZ; z++) { float newValue = CTSHeightMap.m_heights[x, z]; if (newValue < min) { newValue = min; } else if (newValue > max) { newValue = max; } m_heights[x, z] = newValue; } } m_isDirty = true; return(this); }
public CTSHeightMap GetSlopeMap() { CTSHeightMap ctsHeightMap = new CTSHeightMap(this); for (int x = 0; x < this.m_widthX; ++x) { for (int z = 0; z < this.m_depthZ; ++z) { ctsHeightMap[x, z] = this.GetSlope(x, z); } } return(ctsHeightMap); }
/// <summary> /// Return a new CTSHeightMap where each point at contains the slopes of this CTSHeightMap at that point /// </summary> /// <returns></returns> public CTSHeightMap GetSlopeMap() { CTSHeightMap slopeMap = new CTSHeightMap(this); for (int x = 0; x < m_widthX; x++) { for (int z = 0; z < m_depthZ; z++) { slopeMap[x, z] = GetSlope(x, z); } } return(slopeMap); }
public CTSHeightMap Divide(CTSHeightMap CTSHeightMap) { if (this.m_widthX != CTSHeightMap.m_widthX || this.m_depthZ != CTSHeightMap.m_depthZ) { Debug.LogError((object)"Can not divide different sized CTSHeightMap"); return(this); } for (int index1 = 0; index1 < this.m_widthX; ++index1) { for (int index2 = 0; index2 < this.m_depthZ; ++index2) { this.m_heights[index1, index2] /= CTSHeightMap.m_heights[index1, index2]; } } this.m_isDirty = true; return(this); }
public CTSHeightMap(CTSHeightMap source) { this.Reset(); this.m_widthX = source.m_widthX; this.m_depthZ = source.m_depthZ; this.m_widthInvX = 1f / (float)this.m_widthX; this.m_depthInvZ = 1f / (float)this.m_depthZ; this.m_heights = new float[this.m_widthX, this.m_depthZ]; this.m_isPowerOf2 = source.m_isPowerOf2; this.m_metaData = new byte[source.m_metaData.Length]; for (int index = 0; index < source.m_metaData.Length; ++index) { this.m_metaData[index] = source.m_metaData[index]; } Buffer.BlockCopy((Array)source.m_heights, 0, (Array)this.m_heights, 0, this.m_widthX * this.m_depthZ * 4); this.m_isDirty = false; }
/// <summary> /// Add CTSHeightMap /// </summary> /// <param name="CTSHeightMap">CTSHeightMap to add</param> public CTSHeightMap Add(CTSHeightMap CTSHeightMap) { if (m_widthX != CTSHeightMap.m_widthX || m_depthZ != CTSHeightMap.m_depthZ) { Debug.LogError("Can not add different sized CTSHeightMap"); return(this); } for (int x = 0; x < m_widthX; x++) { for (int z = 0; z < m_depthZ; z++) { m_heights[x, z] += CTSHeightMap.m_heights[x, z]; } } m_isDirty = true; return(this); }
/// <summary> /// Create a height map that is a copy of another CTSHeightMap /// </summary> /// <param name="source">Source CTSHeightMap</param> public CTSHeightMap(CTSHeightMap source) { Reset(); m_widthX = source.m_widthX; m_depthZ = source.m_depthZ; m_widthInvX = 1f / (float)(m_widthX); m_depthInvZ = 1f / (float)(m_depthZ); m_heights = new float[m_widthX, m_depthZ]; m_isPowerOf2 = source.m_isPowerOf2; m_metaData = new byte[source.m_metaData.Length]; for (int idx = 0; idx < source.m_metaData.Length; idx++) { m_metaData[idx] = source.m_metaData[idx]; } Buffer.BlockCopy(source.m_heights, 0, m_heights, 0, m_widthX * m_depthZ * sizeof(float)); m_isDirty = false; }
/// <summary> /// Smooth in a given radius /// </summary> /// <param name="radius">Smoothing radius</param> /// <returns>This</returns> public CTSHeightMap SmoothRadius(int radius) { radius = Mathf.Max(5, radius); CTSHeightMap filter = new CTSHeightMap(m_widthX, m_depthZ); float factor = 1f / ((2 * radius + 1) * (2 * radius + 1)); for (int y = 0; y < m_depthZ; y++) { for (int x = 0; x < m_widthX; x++) { filter[x, y] = factor * m_heights[x, y]; } } for (int x = radius; x < m_widthX - radius; x++) { int y = radius; float sum = 0f; for (int i = -radius; i < radius + 1; i++) { for (int j = -radius; j < radius + 1; j++) { sum += filter[x + j, y + i]; } } for (y++; y < m_depthZ - radius; y++) { for (int j = -radius; j < radius + 1; j++) { sum -= filter[x + j, y - radius - 1]; sum += filter[x + j, y + radius]; } m_heights[x, y] = sum; } } m_isDirty = true; return(this); }
public CTSHeightMap SmoothRadius(int radius) { radius = Mathf.Max(5, radius); CTSHeightMap ctsHeightMap = new CTSHeightMap(this.m_widthX, this.m_depthZ); float num1 = 1f / (float)((2 * radius + 1) * (2 * radius + 1)); for (int index1 = 0; index1 < this.m_depthZ; ++index1) { for (int index2 = 0; index2 < this.m_widthX; ++index2) { ctsHeightMap[index2, index1] = num1 * this.m_heights[index2, index1]; } } for (int index1 = radius; index1 < this.m_widthX - radius; ++index1) { int num2 = radius; float num3 = 0.0f; for (int index2 = -radius; index2 < radius + 1; ++index2) { for (int index3 = -radius; index3 < radius + 1; ++index3) { num3 += ctsHeightMap[index1 + index3, num2 + index2]; } } for (int index2 = num2 + 1; index2 < this.m_depthZ - radius; ++index2) { for (int index3 = -radius; index3 < radius + 1; ++index3) { num3 = num3 - ctsHeightMap[index1 + index3, index2 - radius - 1] + ctsHeightMap[index1 + index3, index2 + radius]; } this.m_heights[index1, index2] = num3; } } this.m_isDirty = true; return(this); }