Beispiel #1
0
 private static void DrawGizmoOnSelection(MLPVolume volume, GizmoType gizmoType)
 {
     if (volume.showGizmoSelected)
     {
         DrawVolumeWithBounds(volume, new Color(1, 1, 0, 0.5f), Color.yellow);
     }
 }
Beispiel #2
0
        private void OnSceneGUI()
        {
            MLPVolume mlpVolume = (MLPVolume)target;

            EventType currentEvent = Event.current.type;

            if (currentEvent == EventType.MouseUp)
            {
                mlpVolume.parentRootComponent.changed = true;
            }

            boxBoundsHandle.axes           = PrimitiveBoundsHandle.Axes.X | PrimitiveBoundsHandle.Axes.Y | PrimitiveBoundsHandle.Axes.Z;
            boxBoundsHandle.wireframeColor = Color.yellow;
            boxBoundsHandle.center         = mlpVolume.transform.position;
            boxBoundsHandle.size           = mlpVolume.transform.localScale;

            //Handles.matrix = mlpVolume.transform.localToWorldMatrix;

            EditorGUI.BeginChangeCheck();

            boxBoundsHandle.DrawHandle();

            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObject(mlpVolume, "MLP Change Bounds");

                mlpVolume.transform.position   = boxBoundsHandle.center;
                mlpVolume.transform.localScale = boxBoundsHandle.size;
            }
        }
Beispiel #3
0
        private static void DrawVolumeWithOnlyBounds(MLPVolume volume, Color boundsColor)
        {
            //Gizmos.matrix = volume.transform.localToWorldMatrix;

            Gizmos.color = boundsColor;
            Gizmos.DrawWireCube(volume.transform.parent.position, volume.transform.parent.localScale);
        }
Beispiel #4
0
        public override void OnInspectorGUI()
        {
            MLPVolume mlpVolume = (MLPVolume)target;

            GUILayout.BeginVertical(GUI.skin.box);
            GUILayout.BeginHorizontal();

            GUILayout.Label("Sub-Volumes Count", GUILayout.MinWidth(200));
            GUILayout.Label(mlpVolume.parentRootComponent.subVolumesDivided.Count.ToString());

            GUILayout.EndHorizontal();

            if (mlpVolume.parentRootComponent.tooManySubVolumes)
            {
                GUILayout.BeginHorizontal();

                EditorGUILayout.HelpBox("The main volume contains too many sub-volumes. \r\n\r\n" +
                                        "What can you do: \r\n" +
                                        " - Increase the \"Probes Count Limit\"\r\n" +
                                        " - Change the spacing settings (\"Probe Spacing\" or \"Corners Detection Threshold\") \r\n" +
                                        " - Reduce volume sizes)", MessageType.Error);

                GUILayout.EndHorizontal();
            }
            GUILayout.EndVertical();
        }
Beispiel #5
0
        private static void DrawGizmoAlways(MLPVolume volume, GizmoType gizmoType)
        {
            if (volume.parentRootComponent.calculatingVolume)
            {
                if (volume.parentRootComponent.subVolumesDivided.Count > 0)
                {
                    if (volume.isPartVolume)
                    {
                        if (volume.skipped)
                        {
                            DrawVolumeWithBounds(volume, Color.red, Color.red);
                            return;
                        }

                        if (volume.isCalculated)
                        {
                            DrawVolumeWithOnlyBounds(volume, Color.yellow);
                        }
                        else
                        {
                            if (volume.isInProcess)
                            {
                                DrawVolumeWithBounds(volume, Color.yellow, Color.red);
                            }
                            else
                            {
                                DrawVolumeWithOnlyBounds(volume, Color.yellow);
                            }
                        }
                    }
                }
                else
                {
                    if (volume.isInProcess)
                    {
                        DrawVolumeWithBounds(volume, Color.yellow, Color.yellow);
                    }
                    else
                    {
                        DrawVolumeWithBounds(volume, Color.red, Color.yellow);
                    }
                }
            }
            else
            {
                if (volume.showGizmo)
                {
                    DrawVolumeWithBounds(volume, new Color(0, 1, 0, 0.5f), Color.yellow);
                }
            }
        }
        public IEnumerator ExecutePass(MagicLightProbes parent, MLPVolume currentVolume)
        {
            currentVolume.resultLocalCornerPointsPositions.Clear();

            ComputeBuffer inputArray;
            ComputeBuffer exitArray;
            ComputeBuffer directionsArray;

            inputArray      = new ComputeBuffer(currentVolume.localCornerPointsPositions.Count, 3 * sizeof(float), ComputeBufferType.Default);
            exitArray       = new ComputeBuffer(currentVolume.localCornerPointsPositions.Count, 3 * sizeof(float), ComputeBufferType.Default);
            directionsArray = new ComputeBuffer(currentVolume.localCornerPointsPositions.Count, 3 * sizeof(float), ComputeBufferType.Default);

            inputArray.SetData(currentVolume.localCornerPointsPositions.ToArray());
            exitArray.SetData(currentVolume.localCornerPointsPositions.ToArray());
            directionsArray.SetData(currentVolume.localAvaragedDirections.ToArray());

            parent.calculateDistanceFromGeometry.SetBuffer(parent.calculateDistanceFromGeometry.FindKernel("CSMain"), "inputArray", inputArray);
            parent.calculateDistanceFromGeometry.SetBuffer(parent.calculateDistanceFromGeometry.FindKernel("CSMain"), "exitArray", exitArray);
            parent.calculateDistanceFromGeometry.SetBuffer(parent.calculateDistanceFromGeometry.FindKernel("CSMain"), "directionsArray", directionsArray);
            parent.calculateDistanceFromGeometry.SetFloat("distance", parent.unlitVolumeFillingRate);

            parent.calculateDistanceFromGeometry.Dispatch(parent.calculateDistanceFromGeometry.FindKernel("CSMain"), 256, 1, 1);

            Vector3[] exit = new Vector3[inputArray.count];
            exitArray.GetData(exit);

            inputArray.Dispose();
            exitArray.Dispose();

            currentVolume.localCornerPointsPositions.Clear();

            for (int i = 0; i < exit.Length; i++)
            {
                if (exit[i] == Vector3.zero)
                {
                    continue;
                }

                currentVolume.localCornerPointsPositions.Add(exit[i]);

                if (parent.UpdateProgress(exit.Length, 1000))
                {
                    yield return(null);
                }
            }
        }
Beispiel #7
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 #8
0
        public IEnumerator ExecutePass(MagicLightProbes parent, MLPVolume currentVolume = null, bool realtimeEditing = false)
        {
            parent.currentPass = "******";
            parent.currentPassProgressCounter      = 0;
            parent.currentPassProgressFrameSkipper = 0;

            if (parent.debugMode)
            {
                parent.tmpSharedPointsArray.Clear();
                parent.tmpSharedPointsArray.AddRange(parent.debugAcceptedPoints);
                parent.debugAcceptedPoints.Clear();
            }

            List <MLPPointData> tempList = new List <MLPPointData>();

            if (realtimeEditing)
            {
                parent.tmpSharedPointsArray.Clear();

                for (int i = 0; i < tempList.Count; i++)
                {
                    tempList[i].equalIntensity = false;
                }
            }
            else
            {
                tempList.AddRange(parent.tmpSharedPointsArray);
            }

            int equivalent;

            //Parallel.For(0, parent.acceptedPoints.Count, (i, state) =>
            for (int i = 0; i < tempList.Count; i++)
            {
                lock (tempList)
                {
                    if (tempList[i].nearbyPoints.Count > 0)
                    {
                        if (!tempList[i].inSaveRange &&
                            !tempList[i].contrastOnShadingArea &&
                            !tempList[i].contrastOnOutOfRangeArea &&
                            !tempList[i].savedNearGeometry)
                        {
                            if (!realtimeEditing)
                            {
                                //currentVolume.savedPointsForLightIntensityThresholdEditing.Add(tempList[i]);
                            }

                            if (tempList[i].lightIntensity > 0)
                            {
                                equivalent = 0;

                                foreach (MLPPointData neigboringPoint in tempList[i].nearbyPoints)
                                {
                                    if (neigboringPoint != null)
                                    {
                                        if (neigboringPoint.lightIntensity == 0)
                                        {
                                            equivalent = 0;
                                            break;
                                        }
                                        else
                                        {
                                            if (neigboringPoint.lightIntensity.EqualsApproximately(tempList[i].lightIntensity, tempList[i].lightIntensity * (1 - parent.lightIntensityTreshold)))
                                            {
                                                equivalent++;
                                            }
                                        }
                                    }
                                }

                                if (equivalent == tempList[i].nearbyPoints.Count)
                                {
                                    tempList[i].equalIntensity = true;
                                }
                                else
                                {
                                    tempList[i].contrastOnShadingArea = true;
                                }
                            }
                            else
                            {
                                equivalent = 0;

                                foreach (MLPPointData neigboringPoint in tempList[i].nearbyPoints)
                                {
                                    if (neigboringPoint != null)
                                    {
                                        if (neigboringPoint.lightIntensity > 0)
                                        {
                                            equivalent = 0;
                                            break;
                                        }
                                        else
                                        {
                                            if (!tempList[i].inSaveRange && !tempList[i].savedNearGeometry)
                                            {
                                                equivalent++;
                                            }
                                        }
                                    }
                                }

                                if (equivalent == tempList[i].nearbyPoints.Count)
                                {
                                    tempList[i].equalIntensity = true;
                                }
                                else
                                {
                                    tempList[i].contrastOnShadingArea = true;
                                }
                            }
                        }
                    }
                }
            }//);

            if (realtimeEditing)
            {
                parent.tmpEqualPoints.Clear();
                parent.tmpSharedPointsArray.Clear();
                parent.tmpSharedPointsArray.AddRange(tempList);
            }

            parent.currentPass = "******";
            parent.currentPassProgressCounter      = 0;
            parent.currentPassProgressFrameSkipper = 0;

            foreach (var point in tempList)
            {
                if (point.equalIntensity &&
                    !point.inSaveRange &&
                    !point.contrastOnOutOfRangeArea &&
                    !point.contrastOnShadingArea &&
                    !point.inCorner &&
                    !point.savedNearGeometry)
                {
                    if (point.inShadowForLights.Count == parent.lights.Count || point.isUnlit)
                    {
                        parent.tmpSharedPointsArray.Remove(point);
                    }
                    else
                    {
                        parent.tmpSharedPointsArray.Remove(point);
                        parent.tmpEqualPoints.Add(point);
                        currentVolume.localEquivalentPointsPositions.Add(point.position);
                    }
                }
                else if (point.contrastOnOutOfRangeArea || point.contrastOnShadingArea)
                {
                    currentVolume.localContrastPoints.Add(point);
                }

                if (!parent.isInBackground)
                {
                    if (parent.UpdateProgress(tempList.Count, 1000))
                    {
                        yield return(null);
                    }
                }
            }

            tempList.Clear();

            if (!parent.debugMode)
            {
                if (!realtimeEditing)
                {
                    //currentVolume.contrastPointsForQuickEditing.AddRange(parent.acceptedPoints);
                    parent.totalProbesInSubVolume = parent.tmpSharedPointsArray.Count;
                }
            }
            else
            {
                parent.debugAcceptedPoints.AddRange(parent.tmpEqualPoints);
                parent.totalProbesInSubVolume = parent.debugAcceptedPoints.Count;
            }

            parent.calculatingVolumeSubPass = false;
        }
        public IEnumerator ExecutePass(MagicLightProbes parent, TargetPoint targetPoint, MLPVolume currentVolume = null, bool realtimeEditing = false)
        {
            List <MLPPointData> tempList            = new List <MLPPointData>();
            List <Vector3>      realTimeEditingList = new List <Vector3>();
            List <Vector3>      targetPoints        = new List <Vector3>();
            float fillingRate = 0;

            switch (targetPoint)
            {
            case TargetPoint.Unlit:
                parent.currentPass = "******";
                targetPoints.AddRange(currentVolume.localUnlitPointsPositions);
                currentVolume.resultLocalFreePointsPositions.Clear();
                currentVolume.resultLocalUnlitPointsPositions.Clear();
                fillingRate = parent.unlitVolumeFillingRate;
                tempList.AddRange(parent.tmpUnlitPoints);
                break;

            case TargetPoint.Equivalent:
                parent.currentPass = "******";
                targetPoints.AddRange(currentVolume.localEquivalentPointsPositions);
                currentVolume.resultLocalFreePointsPositions.Clear();
                currentVolume.resultLocalEquivalentPointsPositions.Clear();
                fillingRate = parent.equivalentVolumeFillingRate;
                tempList.AddRange(parent.tmpEqualPoints);
                break;

            case TargetPoint.Free:
                parent.currentPass = "******";
                targetPoints.AddRange(currentVolume.localFreePointsPositions);
                currentVolume.resultLocalFreePointsPositions.Clear();
                currentVolume.resultLocalEquivalentPointsPositions.Clear();
                currentVolume.resultLocalUnlitPointsPositions.Clear();
                fillingRate = parent.freeVolumeFillingRate;
                tempList.AddRange(parent.tmpFreePoints);
                break;
            }

            if (!realtimeEditing)
            {
                //tempList.Clear
            }

            realTimeEditingList.AddRange(targetPoints);

            parent.currentPassProgressCounter      = 0;
            parent.currentPassProgressFrameSkipper = 0;

            if (targetPoints.Count > 0)
            {
                if (SystemInfo.supportsComputeShaders)
                {
                    ComputeBuffer inputArray;
                    ComputeBuffer exitArray;

                    inputArray = new ComputeBuffer(targetPoints.Count, 3 * sizeof(float), ComputeBufferType.Default);
                    exitArray  = new ComputeBuffer(targetPoints.Count, 3 * sizeof(float), ComputeBufferType.Default);

                    inputArray.SetData(targetPoints.ToArray());
                    exitArray.SetData(targetPoints.ToArray());

                    parent.calculateVolumeFilling.SetBuffer(parent.calculateVolumeFilling.FindKernel("CSMain"), "inputArray", inputArray);
                    parent.calculateVolumeFilling.SetBuffer(parent.calculateVolumeFilling.FindKernel("CSMain"), "exitArray", exitArray);
                    parent.calculateVolumeFilling.SetFloat("threshold", fillingRate);

                    parent.calculateVolumeFilling.Dispatch(parent.calculateVolumeFilling.FindKernel("CSMain"), 256, 1, 1);

                    Vector3[] exit = new Vector3[inputArray.count];
                    exitArray.GetData(exit);

                    inputArray.Dispose();
                    exitArray.Dispose();

                    for (int i = 0; i < exit.Length; i++)
                    {
                        if (exit[i] == Vector3.zero)
                        {
                            continue;
                        }

                        if (!realtimeEditing)
                        {
                            tempList[i].position = exit[i];
                            parent.tmpSharedPointsArray.Add(tempList[i]);
                        }

                        switch (targetPoint)
                        {
                        case TargetPoint.Unlit:
                            currentVolume.resultLocalUnlitPointsPositions.Add(exit[i]);
                            break;

                        case TargetPoint.Equivalent:
                            currentVolume.resultLocalEquivalentPointsPositions.Add(exit[i]);
                            break;

                        case TargetPoint.Free:
                            currentVolume.resultLocalFreePointsPositions.Add(exit[i]);
                            break;
                        }

                        if (parent.UpdateProgress(exit.Length, 1000))
                        {
                            yield return(null);
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < Mathf.RoundToInt(targetPoints.Count * (1 - fillingRate)); i++)
                    {
                        realTimeEditingList.Remove(realTimeEditingList[UnityEngine.Random.Range(0, realTimeEditingList.Count)]);

                        if (parent.UpdateProgress(Mathf.RoundToInt(targetPoints.Count * (1 - fillingRate))))
                        {
                            yield return(null);
                        }
                    }

                    if (!realtimeEditing)
                    {
                        parent.tmpSharedPointsArray.AddRange(tempList);

                        for (int i = 0; i < tempList.Count; i++)
                        {
                            switch (targetPoint)
                            {
                            case TargetPoint.Unlit:
                                currentVolume.resultLocalUnlitPointsPositions.Add(tempList[i].position);
                                break;

                            case TargetPoint.Equivalent:
                                currentVolume.resultLocalEquivalentPointsPositions.Add(tempList[i].position);
                                break;

                            case TargetPoint.Free:
                                currentVolume.resultLocalFreePointsPositions.Add(tempList[i].position);
                                break;
                            }
                        }
                    }
                    else
                    {
                        for (int i = 0; i < realTimeEditingList.Count; i++)
                        {
                            switch (targetPoint)
                            {
                            case TargetPoint.Unlit:
                                currentVolume.resultLocalUnlitPointsPositions.Add(realTimeEditingList[i]);
                                break;

                            case TargetPoint.Equivalent:
                                currentVolume.resultLocalEquivalentPointsPositions.Add(realTimeEditingList[i]);
                                break;

                            case TargetPoint.Free:
                                currentVolume.resultLocalFreePointsPositions.Add(realTimeEditingList[i]);
                                break;
                            }
                        }
                    }
                }

                if (targetPoint == TargetPoint.Unlit)
                {
                    if (!realtimeEditing)
                    {
                        parent.currentPass = "******";
                        parent.currentPassProgressCounter      = 0;
                        parent.currentPassProgressFrameSkipper = 0;

                        for (int i = 0; i < tempList.Count; i++)
                        {
                            if (!tempList[i].lockForCull)
                            {
                                parent.CheckForNearContrast(tempList[i]);
                            }

                            if (!parent.isInBackground)
                            {
                                if (parent.UpdateProgress(tempList.Count))
                                {
                                    yield return(null);
                                }
                            }
                        }
                    }
                }
            }

            parent.totalProbesInSubVolume   = parent.tmpSharedPointsArray.Count;
            parent.calculatingVolumeSubPass = false;
        }
        public IEnumerator ExecutePass(MagicLightProbes parent, MLPVolume currentVolume = null)
        {
            if (parent.debugMode)
            {
                parent.tmpSharedPointsArray.Clear();
                parent.tmpSharedPointsArray.AddRange(parent.debugAcceptedPoints);
                parent.debugAcceptedPoints.Clear();
                parent.debugCulledPoints.Clear();
            }

            List <MLPPointData> candidatesList = new List <MLPPointData>();

            foreach (var light in parent.lights)
            {
                parent.currentPass = "******"" + light.name + "\"";
                parent.currentPassProgressCounter      = 0;
                parent.currentPassProgressFrameSkipper = 0;

                if (!parent.isInBackground)
                {
                    if (parent.UpdateProgress(parent.tmpSharedPointsArray.Count, 1))
                    {
                        yield return(null);
                    }
                }

                if (
                    light.lightType == MLPLight.MLPLightType.Directional ||
                    light.parentVolume == parent ||
                    (parent.parentVolume != null && light.parentVolume == parent.gameObject))
                {
                    for (int i = 0; i < parent.tmpSharedPointsArray.Count; i++)
                    {
                        switch (light.calculationType)
                        {
                        case MLPLight.CalculationType.AccurateShadows:
                            if (light.accurateTrace)
                            {
                                if (parent.tmpSharedPointsArray[i].inRangeForLights.Contains(light))
                                {
                                    int fullShaded = 0;

                                    foreach (var tracePoint in light.tracePointsData)
                                    {
                                        if (parent.CheckIfInShadow(light, tracePoint, parent.tmpSharedPointsArray[i]))
                                        {
                                            fullShaded++;
                                        }
                                        else
                                        {
                                            fullShaded--;
                                        }
                                    }

                                    if (fullShaded == light.tracePointsData.Count)
                                    {
                                        foreach (var tracePoint in light.tracePointsData)
                                        {
                                            for (int np = 0; np < parent.tmpSharedPointsArray[i].nearbyPoints.Count; np++)
                                            {
                                                if (parent.tmpSharedPointsArray[i].nearbyPoints[np] != null)
                                                {
                                                    if (!parent.CheckIfInShadow(light, tracePoint, parent.tmpSharedPointsArray[i].nearbyPoints[np]))
                                                    {
                                                        if (light.lightMode == LightmapBakeType.Mixed)
                                                        {
                                                            if (parent.tmpSharedPointsArray[i].savedNearGeometry)
                                                            {
                                                                parent.tmpSharedPointsArray[i].nearbyPoints[np].contrastOnShadingArea = true;

                                                                if (!parent.tmpContrastShadingBordersPoints.Contains(parent.tmpSharedPointsArray[i].nearbyPoints[np]))
                                                                {
                                                                    parent.tmpContrastShadingBordersPoints.Add(parent.tmpSharedPointsArray[i].nearbyPoints[np]);
                                                                }

                                                                for (int n = 0; n < parent.tmpSharedPointsArray[i].nearbyPoints[np].nearbyPoints.Count; n++)
                                                                {
                                                                    if (!parent.tmpSharedPointsArray[i].nearbyPoints[np].nearbyPoints[n].savedNearGeometry)
                                                                    {
                                                                        parent.tmpSharedPointsArray[i].nearbyPoints[np].nearbyPoints[n].contrastOnShadingArea = true;

                                                                        if (!parent.tmpContrastShadingBordersPoints.Contains(parent.tmpSharedPointsArray[i].nearbyPoints[np].nearbyPoints[n]))
                                                                        {
                                                                            parent.tmpContrastShadingBordersPoints.Add(parent.tmpSharedPointsArray[i].nearbyPoints[np].nearbyPoints[n]);
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        else
                                                        {
                                                            parent.tmpSharedPointsArray[i].contrastOnShadingArea = true;
                                                            parent.tmpSharedPointsArray[i].nearbyPoints[np].contrastOnShadingArea = true;

                                                            if (!parent.tmpContrastShadingBordersPoints.Contains(parent.tmpSharedPointsArray[i].nearbyPoints[np]))
                                                            {
                                                                parent.tmpContrastShadingBordersPoints.Add(parent.tmpSharedPointsArray[i].nearbyPoints[np]);
                                                            }

                                                            if (!parent.tmpContrastShadingBordersPoints.Contains(parent.tmpSharedPointsArray[i]))
                                                            {
                                                                parent.tmpContrastShadingBordersPoints.Add(parent.tmpSharedPointsArray[i]);
                                                            }
                                                        }
                                                    }
                                                    else
                                                    {
                                                        if (!candidatesList.Contains(parent.tmpSharedPointsArray[i]))
                                                        {
                                                            candidatesList.Add(parent.tmpSharedPointsArray[i]);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    if (!candidatesList.Contains(parent.tmpSharedPointsArray[i]))
                                    {
                                        candidatesList.Add(parent.tmpSharedPointsArray[i]);
                                    }
                                }
                            }
                            else
                            {
                                if (parent.tmpSharedPointsArray[i].inRangeForLights.Contains(light))
                                {
                                    if (parent.tmpSharedPointsArray[i].inShadowForLights.Contains(light) && !parent.tmpSharedPointsArray[i].inCorner)
                                    {
                                        Vector3 shadedPointStartPosition = parent.tmpSharedPointsArray[i].position;

                                        for (int np = 0; np < parent.tmpSharedPointsArray[i].nearbyPoints.Count; np++)
                                        {
                                            if (parent.tmpSharedPointsArray[i].nearbyPoints[np] != null && parent.tmpSharedPointsArray[i].nearbyPoints[np].inRangeForLights.Contains(light))
                                            {
                                                if (!parent.tmpSharedPointsArray[i].nearbyPoints[np].inShadowForLights.Contains(light) && !parent.tmpSharedPointsArray[i].nearbyPoints[np].inCorner)
                                                {
                                                    if (light.lightMode == LightmapBakeType.Mixed)
                                                    {
                                                        if (parent.tmpSharedPointsArray[i].savedNearGeometry)
                                                        {
                                                            if (parent.tmpSharedPointsArray[i].nearbyPoints[np].savedNearGeometry)
                                                            {
                                                                if (
                                                                    (parent.tmpSharedPointsArray[i].position - parent.tmpSharedPointsArray[i].nearbyPoints[np].position).normalized !=
                                                                    (parent.tmpSharedPointsArray[i].contactPoint - parent.tmpSharedPointsArray[i].nearbyPoints[np].position).normalized)
                                                                {
                                                                    parent.tmpSharedPointsArray[i].contrastOnShadingArea = true;
                                                                    parent.tmpSharedPointsArray[i].nearbyPoints[np].contrastOnShadingArea = true;
                                                                    break;
                                                                }
                                                            }
                                                        }
                                                    }
                                                    else
                                                    {
                                                        if ((parent.tmpSharedPointsArray[i].position - parent.tmpSharedPointsArray[i].nearbyPoints[np].position).normalized !=
                                                            (parent.tmpSharedPointsArray[i].contactPoint - parent.tmpSharedPointsArray[i].nearbyPoints[np].position).normalized)
                                                        {
                                                            parent.tmpSharedPointsArray[i].contrastOnShadingArea = true;
                                                            parent.tmpSharedPointsArray[i].nearbyPoints[np].contrastOnShadingArea = true;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            break;
                        }
                    }
                }
            }

            if (parent.debugMode)
            {
                parent.currentPass = "******";
                parent.currentPassProgressCounter      = 0;
                parent.currentPassProgressFrameSkipper = 0;

                foreach (var point in parent.tmpSharedPointsArray)
                {
                    if (point.contrastOnShadingArea)
                    {
                        if (!parent.tmpContrastShadingBordersPoints.Contains(point))
                        {
                            parent.tmpContrastShadingBordersPoints.Add(point);
                        }
                    }

                    if (!parent.isInBackground)
                    {
                        if (parent.UpdateProgress(parent.tmpSharedPointsArray.Count, 1000))
                        {
                            yield return(null);
                        }
                    }
                }

                switch (parent.debugPass)
                {
                case MagicLightProbes.DebugPasses.ShadingBorders:
                    parent.debugAcceptedPoints.AddRange(parent.tmpContrastShadingBordersPoints);
                    parent.totalProbesInSubVolume = parent.debugAcceptedPoints.Count;
                    break;

                case MagicLightProbes.DebugPasses.ContrastAreas:
                    parent.debugAcceptedPoints.AddRange(parent.tmpContrastOnOutOfRangePoints);
                    parent.debugAcceptedPoints.AddRange(parent.tmpContrastShadingBordersPoints);
                    parent.totalProbesInSubVolume = parent.debugAcceptedPoints.Count;
                    break;

                case MagicLightProbes.DebugPasses.LightIntensity:
                case MagicLightProbes.DebugPasses.EqualProbes:
                    parent.debugAcceptedPoints.AddRange(parent.tmpContrastOnOutOfRangePoints);
                    parent.debugAcceptedPoints.AddRange(parent.tmpContrastShadingBordersPoints);
                    parent.debugAcceptedPoints.AddRange(parent.tmpSharedPointsArray);
                    parent.totalProbesInSubVolume = parent.debugAcceptedPoints.Count;
                    break;

                case MagicLightProbes.DebugPasses.UnlitProbes:
                    parent.debugAcceptedPoints.AddRange(parent.tmpSharedPointsArray);
                    parent.totalProbesInSubVolume = parent.debugAcceptedPoints.Count;
                    break;
                }
            }
            else
            {
                parent.currentPass = "******";
                parent.currentPassProgressCounter      = 0;
                parent.currentPassProgressFrameSkipper = 0;

                List <MLPPointData> tempList = new List <MLPPointData>();

                tempList.AddRange(parent.tmpSharedPointsArray);

                foreach (var point in tempList)
                {
                    if (point.contrastOnShadingArea)
                    {
                        parent.tmpContrastShadingBordersPoints.Add(point);
                        parent.tmpSharedPointsArray.Remove(point);
                        //currentVolume.localContrastPoints.Add(point);
                    }

                    if (point.savedNearGeometry)
                    {
                        parent.tmpSharedPointsArray.Remove(point);
                    }

                    if (!parent.isInBackground)
                    {
                        if (parent.UpdateProgress(tempList.Count, 1000))
                        {
                            yield return(null);
                        }
                    }
                }

                parent.totalProbesInSubVolume = parent.tmpSharedPointsArray.Count;
            }

            parent.calculatingVolumeSubPass = false;
        }
Beispiel #11
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;
        }
Beispiel #12
0
        private void CalculateProbeSpacing(MagicLightProbes parent, MLPVolume currentVolume, bool realtimeEditing = false)
        {
            probeSpacingCalculating = true;

            #region Test Implementations

            //if (currentVolume.localCornerPointsPositions.Count > 0)
            //{
            //    if (realtimeEditing)
            //    {
            //        parent.currentPass = "******";
            //        parent.currentPassProgressCounter = 0;
            //        parent.currentPassProgressFrameSkipper = 0;
            //    }

            //    currentVolume.resultLocalCornerPointsPositions.Clear();

            //    ComputeBuffer inputArray;
            //    ComputeBuffer exitArray;
            //    //ComputeBuffer collisionNormals;

            //    inputArray = new ComputeBuffer(currentVolume.localCornerPointsPositions.Count, 3 * sizeof(float), ComputeBufferType.Default);
            //    exitArray = new ComputeBuffer(currentVolume.localCornerPointsPositions.Count, 3 * sizeof(float), ComputeBufferType.Default);
            //    //collisionNormals = new ComputeBuffer(currentVolume.localCornerPointsPositions.Count, 3 * sizeof(float), ComputeBufferType.Default);

            //    inputArray.SetData(currentVolume.localCornerPointsPositions.ToArray());
            //    exitArray.SetData(currentVolume.localCornerPointsPositions.ToArray());
            //    //collisionNormals.SetData(tmpCollisionNormals.ToArray());

            //    parent.calculateProbeSpacing.SetBuffer(parent.calculateProbeSpacing.FindKernel("CSMain"), "inputArray", inputArray);
            //    parent.calculateProbeSpacing.SetBuffer(parent.calculateProbeSpacing.FindKernel("CSMain"), "exitArray", exitArray);
            //    //parent.calculateProbeSpacing.SetBuffer(parent.calculateProbeSpacing.FindKernel("CSMain"), "collisionNormals", collisionNormals);
            //    parent.calculateProbeSpacing.SetFloat("probeSpacing", parent.cornerProbesSpacing);
            //    parent.calculateProbeSpacing.SetInt("arrayCount", currentVolume.localCornerPointsPositions.Count);

            //    parent.calculateProbeSpacing.Dispatch(parent.calculateProbeSpacing.FindKernel("CSMain"), 256, 1, 1);

            //    Vector3[] exit = new Vector3[inputArray.count];
            //    exitArray.GetData(exit);

            //    inputArray.Dispose();
            //    exitArray.Dispose();
            //    //collisionNormals.Dispose();

            //    List<MLPPointData> tempList = new List<MLPPointData>();
            //    tempList.AddRange(parent.tmpPointsNearGeometryIntersections);
            //    parent.tmpPointsNearGeometryIntersections.Clear();

            //    for (int i = 0; i < exit.Length; i++)
            //    {
            //        if (exit[i] == Vector3.zero)
            //        {
            //            continue;
            //        }

            //        if (!realtimeEditing)
            //        {
            //            tempList[i].position = exit[i];
            //            parent.tmpPointsNearGeometryIntersections.Add(tempList[i]);
            //        }

            //        currentVolume.resultLocalCornerPointsPositions.Add(exit[i]);

            //        parent.UpdateProgress(exit.Length, 1000);
            //    }

            //    tempList.Clear();
            //}

            //for (int i = 0; i < parent.tmpPointsNearGeometryIntersections.Count; i++)
            //{
            //    for (int j = 0; j < parent.tmpPointsNearGeometryIntersections.Count; j++)
            //    {
            //        if (parent.tmpPointsNearGeometryIntersections[j] != parent.tmpPointsNearGeometryIntersections[i])
            //        {
            //            float distance = Vector3.Distance(parent.tmpPointsNearGeometryIntersections[i].position, parent.tmpPointsNearGeometryIntersections[j].position);
            //            float angle = Vector3.Dot(parent.tmpPointsNearGeometryIntersections[i].collisionNormal, parent.tmpPointsNearGeometryIntersections[j].collisionNormal);

            //            if (distance <= parent.cornerProbesSpacing && angle > 0.9f)
            //            {
            //                parent.tmpPointsNearGeometryIntersections.RemoveAt(i);
            //            }
            //        }
            //    }
            //}

            #endregion

            List <MLPPointData> cullList = new List <MLPPointData>();

            for (int i = 0; i < currentVolume.localCornerPoints.Count; i++)
            {
                currentVolume.localCornerPoints[i].lockForCull = false;
                currentVolume.localCornerPoints[i].removed     = false;
            }

            for (int i = 0; i < currentVolume.localCornerPoints.Count; i++)
            {
                if (!currentVolume.localCornerPoints[i].removed)
                {
                    for (int j = 0; j < currentVolume.localCornerPoints.Count; j++)
                    {
                        if (currentVolume.localCornerPoints[j] != currentVolume.localCornerPoints[i])
                        {
                            float distance = Vector3.Distance(currentVolume.localCornerPoints[i].position, currentVolume.localCornerPoints[j].position);
                            float angle    = Vector3.Dot(currentVolume.localCornerPoints[i].collisionNormal, currentVolume.localCornerPoints[j].collisionNormal);

                            if (distance <= parent.cornerProbesSpacing && angle > 0.9f && !currentVolume.localCornerPoints[j].lockForCull)
                            {
                                currentVolume.localCornerPoints[i].lockForCull = true;
                                currentVolume.localCornerPoints[j].removed     = true;
                                cullList.Add(currentVolume.localCornerPoints[j]);
                            }
                        }
                    }
                }
            }

            currentVolume.resultLocalCornerPointsPositions.Clear();

            for (int i = 0; i < currentVolume.localCornerPoints.Count; i++)
            {
                if (!cullList.Contains(currentVolume.localCornerPoints[i]))
                {
                    currentVolume.resultLocalCornerPointsPositions.Add(currentVolume.localCornerPoints[i].position);

                    if (!realtimeEditing)
                    {
                        parent.tmpPointsNearGeometryIntersections.Add(currentVolume.localCornerPoints[i]);
                    }
                }
            }

            probeSpacingCalculating = false;
        }
Beispiel #13
0
        public IEnumerator ExecutePass(MagicLightProbes parent, MLPVolume currentVolume = null, bool realtimeEditing = false)
        {
            parent.currentPass = "******";
            parent.currentPassProgressCounter      = 0;
            parent.currentPassProgressFrameSkipper = 0;

            if (parent.debugMode)
            {
                parent.tmpSharedPointsArray.Clear();
                parent.debugAcceptedPoints.Clear();
            }

            if (currentVolume.localEquivalentPointsPositions.Count > 0)
            {
                currentVolume.resultLocalEquivalentPointsPositions.Clear();

                ComputeBuffer inputArray;
                ComputeBuffer exitArray;

                inputArray = new ComputeBuffer(currentVolume.localEquivalentPointsPositions.Count, 3 * sizeof(float), ComputeBufferType.Default);
                exitArray  = new ComputeBuffer(currentVolume.localEquivalentPointsPositions.Count, 3 * sizeof(float), ComputeBufferType.Default);

                inputArray.SetData(currentVolume.localEquivalentPointsPositions.ToArray());
                exitArray.SetData(currentVolume.localEquivalentPointsPositions.ToArray());

                parent.calculateVolumeFilling.SetBuffer(parent.calculateVolumeFilling.FindKernel("CSMain"), "inputArray", inputArray);
                parent.calculateVolumeFilling.SetBuffer(parent.calculateVolumeFilling.FindKernel("CSMain"), "exitArray", exitArray);
                parent.calculateVolumeFilling.SetFloat("threshold", parent.equivalentVolumeFillingRate);

                parent.calculateVolumeFilling.Dispatch(parent.calculateVolumeFilling.FindKernel("CSMain"), 256, 1, 1);

                Vector3[] exit = new Vector3[inputArray.count];
                exitArray.GetData(exit);

                inputArray.Dispose();
                exitArray.Dispose();

                List <MLPPointData> tempList = new List <MLPPointData>();
                tempList.AddRange(parent.tmpEqualPoints);

                for (int i = 0; i < exit.Length; i++)
                {
                    if (exit[i] == Vector3.zero)
                    {
                        continue;
                    }

                    if (!realtimeEditing)
                    {
                        tempList[i].position = exit[i];
                        parent.tmpSharedPointsArray.Add(tempList[i]);
                    }

                    currentVolume.resultLocalEquivalentPointsPositions.Add(exit[i]);

                    if (!parent.isInBackground)
                    {
                        if (parent.UpdateProgress(exit.Length, 1000))
                        {
                            yield return(null);
                        }
                    }
                }

                if (parent.debugMode)
                {
                    parent.debugAcceptedPoints.AddRange(parent.tmpSharedPointsArray);
                }
            }

            parent.totalProbesInSubVolume   = parent.tmpSharedPointsArray.Count;
            parent.calculatingVolumeSubPass = false;
        }
Beispiel #14
0
        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;
        }
Beispiel #15
0
        public IEnumerator ExecutePass(MagicLightProbes parent, MLPVolume currentVolume = null)
        {
            parent.currentPass = "******";
            parent.currentPassProgressCounter      = 0;
            parent.currentPassProgressFrameSkipper = 0;

            if (parent.debugMode)
            {
                parent.tmpSharedPointsArray.Clear();
                parent.tmpSharedPointsArray.AddRange(parent.debugAcceptedPoints);
                parent.debugAcceptedPoints.Clear();
            }

            for (int i = 0; i < parent.tmpSharedPointsArray.Count; i++)
            {
                float resultIntensity = 0;

                foreach (var light in parent.lights)
                {
                    RaycastHit hitInfo;
                    Ray        rayToLight = new Ray();

                    switch (light.lightType)
                    {
                    case MLPLight.MLPLightType.Directional:
                        rayToLight = new Ray(parent.tmpSharedPointsArray[i].position, -light.transform.forward);

                        if (!Physics.Raycast(rayToLight, out hitInfo, Mathf.Infinity, parent.layerMask))
                        {
                            if (light.lightMode == LightmapBakeType.Mixed)
                            {
                                if (parent.tmpSharedPointsArray[i].savedNearGeometry)
                                {
                                    resultIntensity += light.intensity;
                                }
                            }
                            else
                            {
                                resultIntensity += light.intensity;
                            }
                        }
                        break;

                    default:
                        if (light.accurateTrace)
                        {
                            if (parent.tmpSharedPointsArray[i].inRangeForLights.Contains(light) && !parent.tmpSharedPointsArray[i].inShadowForLights.Contains(light))
                            {
                                float tracePointWeight = 1f / light.tracePoints.Count;

                                foreach (var tracePoint in light.tracePointsData)
                                {
                                    tracePoint.pointGameObject.SetActive(true);
                                    rayToLight = new Ray(parent.tmpSharedPointsArray[i].position, (tracePoint.position - parent.tmpSharedPointsArray[i].position).normalized);

                                    if (Physics.Raycast(rayToLight, out hitInfo, Mathf.Infinity, parent.layerMask))
                                    {
                                        if (hitInfo.collider.name == tracePoint.name)
                                        {
                                            //resultIntensity += tracePointWeight;
                                            resultIntensity += 1.0F / (1.0F + 25.0F * (tracePoint.position - parent.tmpSharedPointsArray[i].position).sqrMagnitude / (light.range * light.range));
                                            parent.tmpSharedPointsArray[i].distancesToLights.Add(hitInfo.distance);
                                        }
                                    }

                                    tracePoint.pointGameObject.SetActive(false);
                                }
                            }
                        }
                        else
                        {
                            if (parent.tmpSharedPointsArray[i].inRangeForLights.Contains(light) && !parent.tmpSharedPointsArray[i].inShadowForLights.Contains(light))
                            {
                                rayToLight = new Ray(parent.tmpSharedPointsArray[i].position, (light.transform.position - parent.tmpSharedPointsArray[i].position).normalized);

                                if (Physics.Raycast(rayToLight, out hitInfo, Mathf.Infinity, parent.layerMask))
                                {
                                    if (hitInfo.collider.name == light.gameObject.name)
                                    {
                                        if (light.calculationType == MLPLight.CalculationType.AccurateShadows)
                                        {
                                            if (parent.tmpSharedPointsArray[i].inRangeForLights.Contains(light) && !parent.tmpSharedPointsArray[i].inShadowForLights.Contains(light))
                                            {
                                                if (light.lightMode == LightmapBakeType.Mixed)
                                                {
                                                    if (parent.tmpSharedPointsArray[i].savedNearGeometry)
                                                    {
                                                        //resultIntensity += light.intensity;
                                                        resultIntensity += 1.0F / (1.0F + 25.0F * (light.position - parent.tmpSharedPointsArray[i].position).sqrMagnitude / (light.range * light.range));
                                                    }
                                                }
                                                else
                                                {
                                                    //resultIntensity += light.intensity;
                                                    resultIntensity += 1.0F / (1.0F + 25.0F * (light.position - parent.tmpSharedPointsArray[i].position).sqrMagnitude / (light.range * light.range));
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if (light.lightMode == LightmapBakeType.Mixed)
                                            {
                                                if (parent.tmpSharedPointsArray[i].savedNearGeometry)
                                                {
                                                    //resultIntensity += light.intensity;
                                                    resultIntensity += 1.0F / (1.0F + 25.0F * (light.position - parent.tmpSharedPointsArray[i].position).sqrMagnitude / (light.range * light.range));
                                                }
                                            }
                                            else
                                            {
                                                //resultIntensity += light.intensity;
                                                resultIntensity += 1.0F / (1.0F + 25.0F * (light.position - parent.tmpSharedPointsArray[i].position).sqrMagnitude / (light.range * light.range));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        break;
                    }
                }

                parent.tmpSharedPointsArray[i].SetLightIntensity(resultIntensity);

                if (parent.tmpSharedPointsArray[i].lightIntensity == 0 &&
                    !parent.tmpSharedPointsArray[i].contrastOnOutOfRangeArea &&
                    !parent.tmpSharedPointsArray[i].contrastOnShadingArea)
                {
                    parent.tmpSharedPointsArray[i].isUnlit = true;
                    parent.tmpUnlitPoints.Add(parent.tmpSharedPointsArray[i]);
                    currentVolume.localUnlitPointsPositions.Add(parent.tmpSharedPointsArray[i].position);
                }

                if (!parent.isInBackground)
                {
                    if (parent.UpdateProgress(parent.tmpSharedPointsArray.Count, 1000))
                    {
                        yield return(null);
                    }
                }
            }

            if (parent.debugMode)
            {
                switch (parent.debugPass)
                {
                case MagicLightProbes.DebugPasses.OutOfRangeBorders:
                case MagicLightProbes.DebugPasses.ShadingBorders:
                case MagicLightProbes.DebugPasses.ContrastAreas:
                case MagicLightProbes.DebugPasses.EqualProbes:
                    parent.debugAcceptedPoints.AddRange(parent.tmpSharedPointsArray);
                    parent.totalProbesInSubVolume = parent.debugAcceptedPoints.Count;
                    break;

                case MagicLightProbes.DebugPasses.LightIntensity:
                    parent.debugAcceptedPoints.AddRange(parent.tmpSharedPointsArray);
                    parent.totalProbesInSubVolume = parent.debugAcceptedPoints.Count;
                    break;

                case MagicLightProbes.DebugPasses.UnlitProbes:
                    parent.debugCulledPoints.AddRange(parent.tmpUnlitPoints);
                    parent.totalProbesInSubVolume = parent.debugCulledPoints.Count;
                    break;
                }
            }

            parent.calculatingVolumeSubPass = false;
        }