public void CreateDistortionMesh(RuntimeGizmoDrawer drawer = null) { if (eyePerspective == null) { eyePerspective = GetComponent <Camera>(); } /// 1.12f; } eyePerspective.aspect = aspectRatio; ellipse.UpdateEllipsoid(); meshVertices.Clear(); meshUVs.Clear(); meshTriangles.Clear(); //Full range = 0f - 1f for (float i = 0; i <= meshResolution.x; i++) { for (float j = 0; j <= meshResolution.y; j++) { Vector2 RenderUV = new Vector2(i / meshResolution.x, j / meshResolution.y); meshUVs.Add(RenderUV); meshVertices.Add(RenderUVToDisplayUV(RenderUV, (i % 5 == 0 && j % 5 == 0), drawer) - (Vector2.one * 0.5f)); //drawer.DrawSphere(filter.transform.TransformPoint(meshVertices[meshVertices.Count - 1]), 0.005f); } } for (int x = 1; x <= meshResolution.x; x++) { for (int y = 1; y <= meshResolution.y; y++) { //Adds the index of the three vertices in order to make up each of the two tris meshTriangles.Add((int)meshResolution.x * x + y); //Top right meshTriangles.Add((int)meshResolution.x * x + y - 1); //Bottom right meshTriangles.Add((int)meshResolution.x * (x - 1) + y - 1); //Bottom left - First triangle meshTriangles.Add((int)meshResolution.x * (x - 1) + y - 1); //Bottom left meshTriangles.Add((int)meshResolution.x * (x - 1) + y); //Top left meshTriangles.Add((int)meshResolution.x * x + y); //Top right - Second triangle } } _distortionMesh.SetVertices(meshVertices); _distortionMesh.SetUVs(0, meshUVs); _distortionMesh.SetTriangles(meshTriangles, 0); _distortionMesh.RecalculateNormals(); if (filter != null) { filter.sharedMesh = _distortionMesh; } if (deformer != null) { deformer.InitializeMeshDeformations(); } }
public void CompleteDistortionMesh(bool onlyRecomputeVertices = false, RuntimeGizmoDrawer drawer = null) { if (previousMeshVertices.Count != vertices.Length) { previousMeshVertices = new List <Vector3>(vertices.Length); } _distortionMesh.GetVertices(previousMeshVertices); if (parallelizeRaytracing && rayUVs.IsCreated && vertices.IsCreated) { raytraceJob.Complete(); if (managedVertices == null || vertices.Length != managedVertices.Length) { managedVertices = vertices.ToArray(); } else { vertices.CopyTo(managedVertices); } for (int i = 0; i < managedVertices.Length; i++) { if (managedVertices[i].x == -0.5f) { managedVertices[i] = previousMeshVertices[i]; } } _distortionMesh.vertices = managedVertices; } else { for (int i = 0; i < meshVertices.Count; i++) { if (meshVertices[i].x == -0.5f) { meshVertices[i] = previousMeshVertices[i]; } } _distortionMesh.SetVertices(meshVertices); } if (!onlyRecomputeVertices) { _distortionMesh.RecalculateNormals(); _distortionMesh.SetUVs(0, meshUVs); } if ((!onlyRecomputeVertices || meshTriangles.Count == 0) && _distortionMesh.vertexCount > 0) { meshTriangles.Clear(); for (int x = 1; x <= meshResolution.x; x++) { for (int y = 1; y <= meshResolution.y; y++) { //Adds the index of the three vertices in order to make up each of the two tris meshTriangles.Add((int)meshResolution.x * x + y); //Top right meshTriangles.Add((int)meshResolution.x * x + y - 1); //Bottom right meshTriangles.Add((int)meshResolution.x * (x - 1) + y - 1); //Bottom left - First triangle meshTriangles.Add((int)meshResolution.x * (x - 1) + y - 1); //Bottom left meshTriangles.Add((int)meshResolution.x * (x - 1) + y); //Top left meshTriangles.Add((int)meshResolution.x * x + y); //Top right - Second triangle } } _distortionMesh.SetTriangles(meshTriangles, 0); } if (filter != null) { filter.sharedMesh = _distortionMesh; } if (deformer != null && deformer.controlPoints.Count > 0) { deformer.InitializeMeshDeformations(); } }