public IEnumerator ExecutePass(MagicLightProbes parent, int volumePartIndex, MLPVolume currentVolume = null, bool realtimeEditing = false) { parent.currentPass = "******"; parent.currentPassProgressCounter = 0; parent.currentPassProgressFrameSkipper = 0; if (parent.debugMode) { parent.tmpNearbyGeometryPoints.Clear(); parent.tmpNearbyGeometryPoints.AddRange(parent.debugAcceptedPoints); parent.debugAcceptedPoints.Clear(); } List <TempPointData> tempPointDatas = new List <TempPointData>(); List <MLPPointData> tempList = new List <MLPPointData>(); List <MLPPointData> lockedForCullList = new List <MLPPointData>(); string dirPath = parent.assetEditorPath + "/Scene Data/" + SceneManager.GetActiveScene().name + "/" + parent.name + "/ColorThresholdData"; string fullFilePath = dirPath + "/" + parent.name + "_" + "vol_" + volumePartIndex + "_ColorThresholdData.mlpdat"; if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } if (realtimeEditing) { tempPointDatas = MLPDataSaver.LoadData(tempPointDatas, fullFilePath); if (tempPointDatas.Count > 0) { for (int i = 0; i < tempPointDatas.Count; i++) { MLPPointData pointData = new MLPPointData(); pointData.position = new Vector3(tempPointDatas[i].xPos, tempPointDatas[i].yPos, tempPointDatas[i].zPos); pointData.averagedColor = new Color(tempPointDatas[i].colorR, tempPointDatas[i].colorG, tempPointDatas[i].colorB); for (int j = 0; j < tempPointDatas[i].nearbyAvaragedColors.Length; j++) { MLPPointData nerabyPoint = new MLPPointData(); nerabyPoint.avaragedColorValue = tempPointDatas[i].nearbyAvaragedColors[j]; pointData.nearbyPoints.Add(nerabyPoint); } tempList.Add(pointData); } parent.tmpSharedPointsArray.Clear(); for (int i = 0; i < tempList.Count; i++) { tempList[i].equalColor = false; } } } else { tempList.AddRange(parent.tmpNearbyGeometryPoints); } for (int i = 0; i < tempList.Count; i++) { if (!realtimeEditing) { if ((tempList[i].collisionObject != null && tempList[i].collisionObject.gameObject.GetComponent <MLPForceSaveProbes>() != null) || tempList[i].onGeometryEdge || tempList[i].lightLeakLocked) { TempPointData tempPoint = new TempPointData(tempList[i].position, tempList[i].averagedColor, new List <float>()); tempPointDatas.Add(tempPoint); continue; } else { if (tempList[i].contrastOnShadingArea || tempList[i].contrastOnOutOfRangeArea) { if (parent.forceSaveProbesOnShadingBorders) { TempPointData tempPoint = new TempPointData(tempList[i].position, tempList[i].averagedColor, new List <float>()); tempPointDatas.Add(tempPoint); continue; } } } } int equivalent = 0; List <float> tempAvaragedColors = new List <float>(); for (int j = 0; j < tempList[i].nearbyPoints.Count; j++) { if (tempList[i].nearbyPoints[j] != null) { if (tempList[i].nearbyPoints[j].avaragedColorValue == 0) { float averagedColorValue = (tempList[i].nearbyPoints[j].averagedColor.r + tempList[i].nearbyPoints[j].averagedColor.g + tempList[i].nearbyPoints[j].averagedColor.b) / 3; tempList[i].nearbyPoints[j].avaragedColorValue = averagedColorValue; } if (tempList[i].avaragedColorValue == 0) { float averagedColorValueCompared = (tempList[i].averagedColor.r + tempList[i].averagedColor.g + tempList[i].averagedColor.b) / 3; tempList[i].avaragedColorValue = averagedColorValueCompared; } if (tempList[i].nearbyPoints[j].avaragedColorValue.EqualsApproximately(tempList[i].avaragedColorValue, parent.colorTreshold)) { equivalent++; } tempAvaragedColors.Add(tempList[i].nearbyPoints[j].avaragedColorValue); } } if (tempList[i].nearbyPoints.Count > 0 && equivalent == tempList[i].nearbyPoints.Count) { tempList[i].equalColor = true; } if (!realtimeEditing) { TempPointData tempPoint = new TempPointData(tempList[i].position, tempList[i].averagedColor, tempAvaragedColors); tempPointDatas.Add(tempPoint); //currentVolume.localColorThresholdEditingPoints.Add(tempList[i]); } if (!parent.isInBackground) { if (parent.UpdateProgress(tempList.Count, 1000)) { yield return(null); } } } if (!realtimeEditing) { MLPDataSaver.SaveData(tempPointDatas, fullFilePath); } if (!parent.debugMode) { if (realtimeEditing) { currentVolume.localNearbyGeometryPoints.Clear(); currentVolume.localNearbyGeometryPoints.AddRange(lockedForCullList); currentVolume.localNearbyGeometryPoints.AddRange(tempList); } parent.currentPass = "******"; parent.currentPassProgressCounter = 0; parent.currentPassProgressFrameSkipper = 0; foreach (var point in tempList) { if (point.equalColor) { if (parent.forceSaveProbesOnShadingBorders) { if (!point.contrastOnShadingArea && !point.contrastOnOutOfRangeArea) { if (realtimeEditing) { currentVolume.localNearbyGeometryPoints.Remove(point); } else { parent.tmpNearbyGeometryPoints.Remove(point); } } } else { if (realtimeEditing) { currentVolume.localNearbyGeometryPoints.Remove(point); } else { parent.tmpNearbyGeometryPoints.Remove(point); } } } else { if (!realtimeEditing) { currentVolume.resultNearbyGeometryPointsPositions.Add(point.position); } } if (!parent.isInBackground) { if (parent.UpdateProgress(tempList.Count, 1000)) { yield return(null); } } } tempList.Clear(); parent.totalProbesInSubVolume = parent.tmpSharedPointsArray.Count; } else { parent.debugAcceptedPoints.AddRange(parent.tmpNearbyGeometryPoints); parent.totalProbesInSubVolume = parent.debugAcceptedPoints.Count; } parent.calculatingVolumeSubPass = false; }
public IEnumerator ExecutePass(MagicLightProbes parent, int volumePartIndex, float cornersDetectionThreshold, MLPVolume currentVolume = null, bool realtimeEditing = false) { parent.currentPass = "******"; parent.currentPassProgressCounter = 0; parent.currentPassProgressFrameSkipper = 0; List <TempPointData> tempPointDatas = new List <TempPointData>(); string dirPath = parent.assetEditorPath + "/Scene Data/" + SceneManager.GetActiveScene().name + "/" + parent.name + "/GeometryIntersectionsData"; string fullFilePath = dirPath + "/" + parent.name + "_" + "vol_" + volumePartIndex + "_GeometryIntersectionsData.mlpdat"; if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } if (!realtimeEditing) { List <MLPPointData> removedPoints = new List <MLPPointData>(); List <Vector3> collisionNormals = new List <Vector3>(); if (parent.debugMode) { parent.tmpSharedPointsArray.Clear(); parent.tmpSharedPointsArray.AddRange(parent.debugAcceptedPoints); parent.debugAcceptedPoints.Clear(); } for (int i = 0; i < parent.tmpSharedPointsArray.Count; i++) { Ray[] checkRays = { new Ray(parent.tmpSharedPointsArray[i].position, Vector3.forward), new Ray(parent.tmpSharedPointsArray[i].position, -Vector3.forward), new Ray(parent.tmpSharedPointsArray[i].position, Vector3.right), new Ray(parent.tmpSharedPointsArray[i].position, -Vector3.right), new Ray(parent.tmpSharedPointsArray[i].position, Vector3.up), new Ray(parent.tmpSharedPointsArray[i].position, -Vector3.up), }; SortedList <float, Vector3> results = new SortedList <float, Vector3>(); RaycastHit hitInfo; foreach (var ray in checkRays) { if (Physics.Raycast(ray, out hitInfo, cornersDetectionThreshold + 0.1f, parent.layerMask)) { if (parent.CheckIfStatic(hitInfo.collider.gameObject)) { if (!results.Keys.Contains(hitInfo.distance)) { results.Add(hitInfo.distance, hitInfo.point); } } } } if (results.Count > 1) { Vector3 targetPoint = new Vector3(); Vector3 avaragedDirection = new Vector3(); foreach (var result in results) { targetPoint.x += result.Value.x; targetPoint.y += result.Value.y; targetPoint.z += result.Value.z; avaragedDirection.x += result.Value.x - parent.tmpSharedPointsArray[i].position.x; avaragedDirection.y += result.Value.y - parent.tmpSharedPointsArray[i].position.y; avaragedDirection.z += result.Value.z - parent.tmpSharedPointsArray[i].position.z; } targetPoint.x /= results.Count; targetPoint.y /= results.Count; targetPoint.z /= results.Count; avaragedDirection.x /= results.Count; avaragedDirection.y /= results.Count; avaragedDirection.z /= results.Count; avaragedDirection = Vector3.Normalize(avaragedDirection); Ray rayToPoint = new Ray(parent.tmpSharedPointsArray[i].position, (targetPoint - parent.tmpSharedPointsArray[i].position).normalized); MLPPointData newPoint = new MLPPointData(); if (Physics.Raycast(rayToPoint, out hitInfo, cornersDetectionThreshold * 2, parent.layerMask)) { newPoint.position = hitInfo.point; newPoint.position -= avaragedDirection * parent.distanceFromNearbyGeometry; newPoint.collisionNormal = hitInfo.normal; newPoint.inCorner = true; newPoint.contactPoint = targetPoint; currentVolume.localCornerPoints.Add(newPoint); currentVolume.localCornerPointsPositions.Add(newPoint.position); currentVolume.localAvaragedDirections.Add(avaragedDirection); tempPointDatas.Add(new TempPointData(newPoint.position, newPoint.collisionNormal)); //if (Vector3.Distance(parent.tmpSharedPointsArray[i].position, hitInfo.point) < parent.cornersDetectionThreshold / 2) //{ // removedPoints.Add(parent.tmpSharedPointsArray[i]); // //collisionNormals.Add(newPoint.collisionNormal); //} } } if (!parent.isInBackground) { if (parent.UpdateProgress(parent.tmpSharedPointsArray.Count, 1000)) { yield return(null); } } } if (!parent.debugMode) { MLPDataSaver.SaveData(tempPointDatas, fullFilePath); } parent.currentPass = "******"; parent.currentPassProgressCounter = 0; parent.currentPassProgressFrameSkipper = 0; CalculateProbeSpacing(parent, currentVolume, realtimeEditing); while (probeSpacingCalculating) { yield return(null); } if (parent.debugMode) { removedPoints.Clear(); parent.tmpSharedPointsArray.Clear(); switch (parent.debugPass) { case MagicLightProbes.DebugPasses.GeometryIntersections: parent.debugAcceptedPoints.AddRange(parent.tmpPointsNearGeometryIntersections); break; case MagicLightProbes.DebugPasses.NearGeometry: parent.debugAcceptedPoints.AddRange(parent.tmpPointsNearGeometryIntersections); break; } parent.totalProbesInSubVolume = parent.debugAcceptedPoints.Count; } } else { tempPointDatas = MLPDataSaver.LoadData(tempPointDatas, fullFilePath); currentVolume.localCornerPoints.Clear(); if (tempPointDatas.Count > 0) { for (int i = 0; i < tempPointDatas.Count; i++) { MLPPointData tempPoint = new MLPPointData(); tempPoint.position = new Vector3(tempPointDatas[i].xPos, tempPointDatas[i].yPos, tempPointDatas[i].zPos); tempPoint.collisionNormal = new Vector3(tempPointDatas[i].collisionNormalX, tempPointDatas[i].collisionNormalY, tempPointDatas[i].collisionNormalZ); currentVolume.localCornerPoints.Add(tempPoint); } } CalculateProbeSpacing(parent, currentVolume, realtimeEditing); if (!parent.isInBackground) { while (probeSpacingCalculating) { yield return(null); } } } parent.calculatingVolumeSubPass = false; }
public IEnumerator ExecutePass(MagicLightProbes parent) { parent.currentPass = "******"; parent.currentPassProgressCounter = 0; parent.currentPassProgressFrameSkipper = 0; int steps = Mathf.RoundToInt(parent.verticalDublicatingHeight / parent.verticalDublicatingStep); List <MLPPointData> candidates = new List <MLPPointData>(); List <MLPPointData> savedNearGeometry = new List <MLPPointData>(); parent.tmpSharedPointsArray.AddRange(parent.tmpNearbyGeometryPoints); for (int i = 0; i < parent.tmpSharedPointsArray.Count; i++) { if (parent.tmpSharedPointsArray[i].savedNearGeometry) { if (Vector3.Dot(-Vector3.up, (parent.tmpSharedPointsArray[i].contactPoint - parent.tmpSharedPointsArray[i].position).normalized) == 1) { candidates.Add(parent.tmpSharedPointsArray[i]); } else { savedNearGeometry.Add(parent.tmpSharedPointsArray[i]); } } if (parent.UpdateProgress(parent.tmpSharedPointsArray.Count, 1000)) { yield return(null); } } parent.tmpSharedPointsArray.Clear(); for (int i = 0; i < steps; i++) { parent.currentPass = "******" + i + "/" + steps; parent.currentPassProgressCounter = 0; parent.currentPassProgressFrameSkipper = 0; foreach (var point in candidates) { MLPPointData newPoint = new MLPPointData(); newPoint.position = new Vector3(point.position.x, point.position.y + (parent.verticalDublicatingStep * i), point.position.z); if (parent.probesVolume.GetComponent <MeshRenderer>().bounds.Contains(newPoint.position)) { parent.tmpSharedPointsArray.Add(newPoint); } if (!parent.isInBackground) { if (parent.UpdateProgress(parent.tmpSharedPointsArray.Count, 1000)) { yield return(null); } } } } parent.tmpSharedPointsArray.AddRange(candidates); parent.tmpSharedPointsArray.AddRange(savedNearGeometry); parent.tmpSharedPointsArray.AddRange(parent.tmpPointsNearGeometryIntersections); parent.calculatingVolumeSubPass = false; }
public IEnumerator ExecutePass(MagicLightProbes parent, float spacing, MagicLightProbes.VolumeParameters volumePart, MagicLightProbes.CalculationTarget calculationTarget) { parent.currentPass = "******"; parent.currentPassProgressCounter = 0; parent.currentPassProgressFrameSkipper = 0; MLPPointData pointData; parent.recalculationRequired = false; var offset = volumePart.position; var stepX = Mathf.FloorToInt(volumePart.demensions.x / spacing); var stepY = Mathf.FloorToInt(volumePart.demensions.y / spacing); var stepZ = Mathf.FloorToInt(volumePart.demensions.z / spacing) + 1; parent.xPointsCount = stepX; parent.yPointsCount = stepY; parent.zPointsCount = stepZ; offset -= volumePart.demensions * 0.5f; offset.x += (volumePart.demensions.x - stepX * spacing) * 0.5f; offset.y += (volumePart.demensions.y - stepY * spacing) * 0.5f; offset.z += (volumePart.demensions.z - stepZ * spacing) * 0.5f; for (int x = 0; x < stepX; x++) { for (int y = 0; y < stepY; y++) { for (int z = 0; z < stepZ; z++) { var pointPosition = offset + new Vector3(x * spacing, y * spacing, z * spacing); pointData = new MLPPointData(); pointData.position = pointPosition; pointData.col = x; pointData.row = y; pointData.depth = z; if (parent.innerVolumes.Count > 0) { int containsIn = 0; foreach (var volume in parent.innerVolumes) { if (parent.CheckIfInside(volume, pointPosition)) { containsIn++; } } if (containsIn == 0) { parent.tmpSharedPointsArray.Add(pointData); parent.totalProbes++; } } else { parent.tmpSharedPointsArray.Add(pointData); parent.totalProbes++; } parent.UpdateProgress(stepX * stepY * stepZ); } } } if (!parent.isInBackground) { yield return(null); } parent.calculatingVolumeSubPass = false; }
public IEnumerator ExecutePass(MagicLightProbes parent, float volumeSpacing, float cornersDetectionThreshold, MLPVolume currentVolume = null) { parent.currentPass = "******"; parent.currentPassProgressCounter = 0; parent.currentPassProgressFrameSkipper = 0; List <MLPPointData> addedPoints = new List <MLPPointData>(); List <MLPPointData> removedPoints = new List <MLPPointData>(); GameObject cameraObject = new GameObject("Temp Camera"); Camera renderCamera = cameraObject.AddComponent <Camera>(); renderCamera.cullingMask = parent.layerMask; Texture2D tex = new Texture2D(128, 128, TextureFormat.RGB24, false); RenderTexture renderTexture = new RenderTexture(128, 128, 24); if (parent.debugMode) { parent.tmpSharedPointsArray.Clear(); parent.tmpSharedPointsArray.AddRange(parent.debugAcceptedPoints); parent.debugAcceptedPoints.Clear(); } for (int i = 0; i < parent.tmpSharedPointsArray.Count; i++) { SortedList <float, Vector3> results = new SortedList <float, Vector3>(); RaycastHit hitInfo; Ray[] checkRays = { new Ray(parent.tmpSharedPointsArray[i].position, Vector3.forward), new Ray(parent.tmpSharedPointsArray[i].position, -Vector3.forward), new Ray(parent.tmpSharedPointsArray[i].position, Vector3.right), new Ray(parent.tmpSharedPointsArray[i].position, -Vector3.right), new Ray(parent.tmpSharedPointsArray[i].position, Vector3.up), new Ray(parent.tmpSharedPointsArray[i].position, -Vector3.up), }; foreach (var ray in checkRays) { if (Physics.Raycast(ray, out hitInfo, volumeSpacing + (volumeSpacing * 0.5f), parent.layerMask)) // parent.volumeSpacing + 0.1f? { if (!results.Keys.Contains(hitInfo.distance)) { if (parent.CheckIfInside(parent.probesVolume.transform, hitInfo.point)) { bool inSubVolume = false; foreach (var volume in parent.innerVolumes) { if (parent.CheckIfInside(volume, hitInfo.point)) { inSubVolume = true; break; } } if (!inSubVolume) { results.Add(hitInfo.distance, hitInfo.point); } } } } } if (results.Count > 0) { foreach (var result in results) { MLPPointData newPoint = new MLPPointData(); Vector3 closestPoint = result.Value; Ray rayToPoint = new Ray(parent.tmpSharedPointsArray[i].position, (closestPoint - parent.tmpSharedPointsArray[i].position).normalized); if (Physics.Raycast(rayToPoint, out hitInfo, volumeSpacing + 0.1f, parent.layerMask)) { if (parent.CheckIfStatic(hitInfo.collider.gameObject)) { if (hitInfo.collider.gameObject.GetComponent <MLPForceNoProbes>() != null) { removedPoints.Add(parent.tmpSharedPointsArray[i]); continue; } removedPoints.Add(parent.tmpSharedPointsArray[i]); newPoint.position = hitInfo.point; Vector3 direction = (parent.tmpSharedPointsArray[i].position - newPoint.position).normalized; newPoint.position += direction * parent.distanceFromNearbyGeometry; bool tooClose = false; foreach (var point in currentVolume.localCornerPoints) { if (Vector3.Distance(point.position, newPoint.position) < cornersDetectionThreshold) { tooClose = true; break; } } if (!tooClose) { renderCamera.targetTexture = renderTexture; renderCamera.nearClipPlane = 0.01f; renderCamera.farClipPlane = volumeSpacing * 2; renderCamera.fieldOfView = 1; renderCamera.transform.position = newPoint.position; renderCamera.transform.LookAt(hitInfo.point); renderCamera.Render(); RenderTexture.active = renderTexture; tex.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0); Color pixel; float red = 0; float green = 0; float blue = 0; int count = 0; for (int x = 0; x < renderTexture.width; x++) { for (int y = 0; y < renderTexture.height; y++) { pixel = tex.GetPixel(x, y); red += pixel.r; green += pixel.g; blue += pixel.b; count++; } } red /= count; green /= count; blue /= count; Color average = new Color(red, green, blue, 1); newPoint.col = parent.tmpSharedPointsArray[i].col; newPoint.row = parent.tmpSharedPointsArray[i].row; newPoint.depth = parent.tmpSharedPointsArray[i].depth; newPoint.collisionNormal = hitInfo.normal; newPoint.savedNearGeometry = true; newPoint.contactPoint = closestPoint; newPoint.collisionObject = hitInfo.collider.gameObject; newPoint.averagedColor = average; newPoint.xStartPoint = parent.tmpSharedPointsArray[i].xStartPoint; newPoint.yStartPoint = parent.tmpSharedPointsArray[i].yStartPoint; newPoint.zStartPoint = parent.tmpSharedPointsArray[i].zStartPoint; newPoint.xEndPoint = parent.tmpSharedPointsArray[i].xEndPoint; newPoint.yEndPoint = parent.tmpSharedPointsArray[i].yEndPoint; newPoint.zEndPoint = parent.tmpSharedPointsArray[i].zEndPoint; parent.tmpNearbyGeometryPoints.Add(newPoint); //if (currentVolume != null) //{ // currentVolume.localDirections.Add(direction); // currentVolume.localNearbyGeometryPointsPositions.Add(newPoint.position); //} RenderTexture.active = null; } } } } } if (!parent.isInBackground) { if (parent.UpdateProgress(parent.tmpSharedPointsArray.Count, 1000)) { yield return(null); } } } Object.DestroyImmediate(cameraObject); if (parent.debugMode) { switch (parent.debugPass) { case MagicLightProbes.DebugPasses.NearGeometry: case MagicLightProbes.DebugPasses.GeometryEdges: case MagicLightProbes.DebugPasses.EqualColor: parent.debugAcceptedPoints.AddRange(parent.tmpNearbyGeometryPoints); parent.totalProbesInSubVolume = parent.debugAcceptedPoints.Count; break; default: parent.debugAcceptedPoints.AddRange(parent.tmpSharedPointsArray); parent.debugAcceptedPoints.AddRange(parent.tmpNearbyGeometryPoints); parent.totalProbesInSubVolume = parent.debugAcceptedPoints.Count; break; } } else { parent.currentPass = "******"; parent.currentPassProgressCounter = 0; parent.currentPassProgressFrameSkipper = 0; Parallel.For(0, removedPoints.Count, (i, state) => { lock (parent.tmpSharedPointsArray) { parent.tmpSharedPointsArray.Remove(removedPoints[i]); } }); parent.tmpFreePoints.AddRange(parent.tmpSharedPointsArray); for (int i = 0; i < parent.tmpSharedPointsArray.Count; i++) { currentVolume.localFreePointsPositions.Add(parent.tmpSharedPointsArray[i].position); } parent.currentPass = "******"; parent.currentPassProgressCounter = 0; parent.currentPassProgressFrameSkipper = 0; List <MLPPointData> removeList = new List <MLPPointData>(); foreach (var point in parent.tmpNearbyGeometryPoints) { foreach (var checkPoint in parent.tmpPointsNearGeometryIntersections) { if (Vector3.Distance(point.position, checkPoint.position) <= cornersDetectionThreshold * 2.0f) { removeList.Add(point); break; } } if (!parent.isInBackground) { if (parent.UpdateProgress(parent.tmpNearbyGeometryPoints.Count, 1000)) { yield return(null); } } } Parallel.For(0, removeList.Count, (i, state) => { lock (parent.tmpNearbyGeometryPoints) { parent.tmpNearbyGeometryPoints.Remove(removeList[i]); } }); //currentVolume.localNearbyGeometryPoints.AddRange(parent.tmpNearbyGeometryPoints); parent.tmpSharedPointsArray.AddRange(parent.tmpNearbyGeometryPoints); parent.totalProbesInSubVolume += parent.tmpNearbyGeometryPoints.Count; } parent.calculatingVolumeSubPass = false; }