private void OnDrawGizmos() { if (dispMapSphere != null) { Gizmos.color = Color.red; for (int x = 0; x < dispMapSphere.GetLength(0); x++) { for (int y = 0; y < dispMapSphere.GetLength(1); y++) { DispSample sample = dispMapSphere[x, y]; if (sample.Empty) { continue; } Gizmos.DrawLine(sample.Point, sample.Point + (sample.Normal * 0.01f)); } } } foreach (MeshSample currentSample in samples) { Gizmos.color = Color.white; Gizmos.DrawWireSphere(currentSample.Point, 0.01f); Gizmos.DrawLine(currentSample.Point, currentSample.Point + currentSample.Normal * 0.2f); } }
public static DispSample Lerp(DispSample s1, DispSample s2, float t) { s1.Point = Vector3.Lerp(s1.Point, s2.Point, t); s1.Normal = Vector3.Lerp(s2.Normal, s2.Normal, t); s1.UV = Vector2.Lerp(s1.UV, s2.UV, t); return(s1); }
public void CreateSphereDispMap(bool transformPoint = false) { DigestMesh(); int xRes = dispMapResX; int yRes = dispMapResY; DispSample sample = new DispSample(); dispMapSphere = new DispSample[xRes, yRes]; for (int x = 0; x < xRes; x++) { for (int y = 0; y < yRes; y++) { Vector2 uv = new Vector2(1f / xRes * x, 1f / yRes * y); Vector3 meshPos = Vector3.zero; sample.Empty = true; sample.UV = uv; int numTris = tris.Length / 3; for (int triIndex = 0; triIndex < numTris; triIndex++) { if (GetMeshPointFromUV(uv, triIndex, out meshPos)) { sample.Empty = false; sample.Point = transformPoint ? transform.TransformPoint(meshPos) : meshPos; sample.Normal = meshPos.normalized; break; } } dispMapSphere[x, y] = sample; } } }
public DispSample SampleRandomSphereDistMap() { DispSample sample = default(DispSample); int maxIterations = 500; for (int i = 0; i < maxIterations; i++) { int randomX = Random.Range(0, dispMapResX); int randomY = Random.Range(0, dispMapResY); sample = dispMapSphere[randomX, randomY]; if (!sample.Empty) { break; } } return(sample); }
internal DispSample GetSphereDispMapSampleSmooth(Vector2 uv) { uv.x = Mathf.Clamp01(uv.x); uv.y = Mathf.Clamp01(uv.y); int xFloor = Mathf.Clamp(Mathf.FloorToInt(uv.x * dispMapResX - 1), 0, dispMapResX - 1); int yFloor = Mathf.Clamp(Mathf.FloorToInt(uv.y * dispMapResY - 1), 0, dispMapResY - 1); int xCeil = Mathf.Clamp(Mathf.CeilToInt(uv.x * dispMapResX - 1), 0, dispMapResX - 1); int yCeil = Mathf.Clamp(Mathf.CeilToInt(uv.y * dispMapResY - 1), 0, dispMapResY - 1); DispSample s1 = dispMapSphere[xFloor, yFloor]; DispSample s2 = dispMapSphere[xCeil, yFloor]; DispSample s3 = dispMapSphere[xCeil, yCeil]; DispSample s4 = dispMapSphere[xFloor, yCeil]; float xBlockSize = 1f / dispMapResX; float yBlockSize = 1f / dispMapResY; float xCeilNorm = (float)xCeil / dispMapResX; float yCeilNorm = (float)yCeil / dispMapResY; float xDiffFloor = (uv.x - xCeilNorm) / xBlockSize; float yDiffFloor = (uv.y - yCeilNorm) / yBlockSize; float xDiffCeil = 1f - xDiffFloor; float yDiffCeil = 1f - xDiffFloor; Debug.Log(xDiffFloor + " : " + yDiffFloor); float s2Weight = (xDiffCeil + yDiffFloor) / 2; float s3Weight = (xDiffCeil + yDiffCeil) / 2; float s4Weight = (xDiffFloor + yDiffCeil) / 2; s1 = DispSample.Lerp(s1, s2, s2Weight); s1 = DispSample.Lerp(s1, s3, s3Weight); s1 = DispSample.Lerp(s1, s4, s4Weight); return(s1); }
internal static DispSample Blend(DispSample s1, DispSample s2, DispSample s3, Vector3 bary) { s1.Point = ((bary.x * s1.Point) + (bary.y * s2.Point) + (bary.z * s3.Point)); s1.Normal = ((bary.x * s1.Normal) + (bary.y * s2.Normal) + (bary.z * s3.Normal)); return(s1); }