Large diffs are not rendered by default.

BIN -758 KB (28%) Assets/New Terrain.asset
Binary file not shown.
@@ -2,7 +2,7 @@
** Auth: ysd
** Date: 31/10/2015 13:37
** Desc: 生成Perlin噪声
** Desc: 生成Perlin噪声,1D,2D;unity自带的就是辣鸡
** Vers: v1.0
*************************************************************/
@@ -26,8 +26,6 @@ public class CoherentNoise1D
/// </summary>
private float[] _permutation;

private ushort _sampleCount;

private float _from;

private float _to;
@@ -41,7 +39,6 @@ public CoherentNoise1D (float from = 0, float to = 2 * Mathf.PI, float ampl = 1,
{
_from = Mathf.Min(from, to);
_to = Mathf.Max(from, to);
_sampleCount = sampleCount;
_scale = (float)(sampleCount - 1) / (to - from);
_permutation = new float[sampleCount];
for (ushort i = 0; i < sampleCount; i++)
@@ -50,24 +47,9 @@ public CoherentNoise1D (float from = 0, float to = 2 * Mathf.PI, float ampl = 1,
}
}

/// <summary>
/// 非线性插值
/// </summary>
private float _Lerp (float left, float right, float t)
{
t = Mathf.Min(1.0f, t);
t = Mathf.Max(0.0f, t);
float ttt = t * t * t;
t = ttt * (3.0f * t * (2.0f * t - 5.0f) + 10.0f);
return (1.0f - t) * left + t * right;
}

/// <summary>
/// 1D coherent noise
/// </summary>
/// <param name="from"></param>
/// <param name="to"></param>
/// <returns></returns>
public float GetPixel (float x)
{
x = Mathf.Min(_to, x);
@@ -81,7 +63,9 @@ public float GetPixel (float x)

int left = Mathf.FloorToInt(x);
int right = Mathf.CeilToInt(x);
return _Lerp(_permutation[left], _permutation[right], x - left);

float t = RMGUtility.SmoothCurve(x - left);
return RMGUtility.Lerp(_permutation[left], _permutation[right], t);
}

}
@@ -150,29 +134,6 @@ public CoherentNoise2D (uint m, uint n)
}
}

/// <summary>
/// 线性插值
/// </summary>
/// <param name="t">距from的距离</param>
/// <returns></returns>
private float _Lerp (float from, float to, float t)
{
return (1 - t) * from + t * to;
}

/// <summary>
/// 光滑插值
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
private float _SmoothCurve (float t)
{
t = Mathf.Min(1.0f, t);
t = Mathf.Max(0.0f, t);
float ttt = t * t * t;
return ttt * (3.0f * t * (2.0f * t - 5.0f) + 10.0f);
}

/// <summary>
/// 双线性插值
/// @---->*<--@
@@ -184,12 +145,12 @@ private float _SmoothCurve (float t)
/// <returns></returns>
private float _Lerp2D (uint x1, uint y1, uint x2, uint y2, float tx, float ty)
{
tx = _SmoothCurve(tx);
ty = _SmoothCurve(ty);
tx = RMGUtility.SmoothCurve(tx);
ty = RMGUtility.SmoothCurve(ty);

return _Lerp(
_Lerp(_permutaion[x1, y1], _permutaion[x2, y1], tx),
_Lerp(_permutaion[x1, y2], _permutaion[x2, y2], tx),
return RMGUtility.Lerp(
RMGUtility.Lerp(_permutaion[x1, y1], _permutaion[x2, y1], tx),
RMGUtility.Lerp(_permutaion[x1, y2], _permutaion[x2, y2], tx),
ty);
}

@@ -296,7 +257,7 @@ public PerlinNoise2D (uint m, uint n, byte count = 4, float maxAmpl = 1)
_amplitude = maxAmpl;

}

/// <summary>
/// 不大于x的最大2的指数
/// </summary>
@@ -16,7 +16,7 @@
namespace RandomMapGenerator
{

public struct RasterIndex
public struct RasterIndex : IEqualityComparer<RasterIndex>
{
public uint x;
public uint y;
@@ -25,6 +25,24 @@ public RasterIndex (uint xx, uint yy)
x = xx;
y = yy;
}

#region IEqualityComparer<RasterIndex> 成员

bool IEqualityComparer<RasterIndex>.Equals (RasterIndex obj1, RasterIndex obj2)
{
return obj1.x == obj2.x && obj1.y == obj2.y;
}

/// <summary>
/// 只要 x1, x2 < 100000,可以保证 y1 * 100000 + x1 != y2 * 100000 + x2;
/// 实际上,x12,y12为栅格坐标,不会大于100000.
/// </summary>
int IEqualityComparer<RasterIndex>.GetHashCode (RasterIndex obj)
{
return (obj.x + obj.y * 100000).GetHashCode();
}

#endregion
}

public class Raster
@@ -103,7 +121,7 @@ public Raster (Rect border, uint widthNumber, float rand = 0.3f)
rand = Mathf.Max(0, rand);

_Gird();
_RandomAdjust(rand);
_RandomAdjust(rand * _squareSize);
}

/// <summary>
@@ -123,7 +141,7 @@ private void _Gird ( )
for (uint j = 0; j < n + 1; j++)
{
offset.x = width * i;
offset.y = -height * j;
offset.y = height * j;
if (i < m && j < n)
_squareCenters[i, j] = _squareCenters[0, 0] + offset;
_squareCorners[i, j] = _border.position + offset;
@@ -132,23 +150,35 @@ private void _Gird ( )

}

private void _RandomAdjust (float rand)
private void _RandomAdjust (float randOffset)
{
for (uint i = 1; i < m; i++)
{
for (uint j = 1; j < n; j++)
{
Vector2 dir = Random.insideUnitCircle;
_squareCorners[i, j] += dir;
_squareCorners[i, j] += dir * randOffset;
}
}

// 调整中心的位置
for (uint i = 0; i < m; i++)
{
for (uint j = 0; j < n; j++)
{
_squareCenters[i, j].x = (_squareCorners[i, j].x + _squareCorners[i + 1, j].x + _squareCorners[i, j + 1].x + _squareCorners[i + 1, j + 1].x) / 4;
_squareCenters[i, j].y = (_squareCorners[i, j].y + _squareCorners[i + 1, j].y + _squareCorners[i, j + 1].y + _squareCorners[i + 1, j + 1].y) / 4;
_squareCenters[i, j].x = (
_squareCorners[i, j].x +
_squareCorners[i + 1, j].x +
_squareCorners[i, j + 1].x +
_squareCorners[i + 1, j + 1].x
) / 4;

_squareCenters[i, j].y = (
_squareCorners[i, j].y +
_squareCorners[i + 1, j].y +
_squareCorners[i, j + 1].y +
_squareCorners[i + 1, j + 1].y
) / 4;
}
}
}
@@ -187,10 +217,7 @@ public RasterIndex[] GetCircleIndices (RasterIndex center, float radius)
float minSize = Mathf.Min(_border.size.x, _border.size.y);
radius = Mathf.Min(minSize * 0.9f, radius);
uint radiusNumber = (uint)(radius / _squareSize);

// 估计个数
uint num = (uint)(0.35f * radius);


List<RasterIndex> result = new List<RasterIndex>();

// 第一个栅格的位置
@@ -23,10 +23,10 @@ public class test : MonoBehaviour

void Start ( )
{
PerlinNoise noise = new PerlinNoise(to: Mathf.PI * 2, ampl:5, count: 8, freq:10);
PerlinNoise1D noise = new PerlinNoise1D(to: Mathf.PI * 2, ampl:5, count: 8, freq:10);
for (float x = 0; x <= Mathf.PI * 2; x += 0.002f)
{
_vs.Add(x, noise.PerlinNoise1D(x));
_vs.Add(x, noise.GetPixel(x));
}
}

BIN +124 Bytes (100%) Assets/test.unity
Binary file not shown.