Beispiel #1
0
        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;
        }
Beispiel #2
0
        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;
        }