Exemplo n.º 1
0
        private static Vector2 ProjectOBB(ref ProbeReferenceVolume.Volume a, Vector3 axis)
        {
            float min = Vector3.Dot(axis, a.corner);
            float max = min;

            for (int x = 0; x < 2; x++)
            {
                for (int y = 0; y < 2; y++)
                {
                    for (int z = 0; z < 2; z++)
                    {
                        Vector3 vert = a.corner + a.X * x + a.Y * y + a.Z * z;

                        float proj = Vector3.Dot(axis, vert);

                        if (proj < min)
                        {
                            min = proj;
                        }
                        else if (proj > max)
                        {
                            max = proj;
                        }
                    }
                }
            }

            return(new Vector2(min, max));
        }
Exemplo n.º 2
0
        static protected int RenderersToVolumes(ref Renderer[] renderers, ref ProbeReferenceVolume.Volume cellVolume, ref List <ProbeReferenceVolume.Volume> volumes, ref Dictionary <Scene, int> sceneRefs)
        {
            int num = 0;

            foreach (Renderer r in renderers)
            {
                var  flags        = GameObjectUtility.GetStaticEditorFlags(r.gameObject) & StaticEditorFlags.ContributeGI;
                bool contributeGI = (flags & StaticEditorFlags.ContributeGI) != 0;

                if (!r.enabled || !r.gameObject.activeSelf || !contributeGI)
                {
                    continue;
                }

                ProbeReferenceVolume.Volume v = ToVolume(r.bounds);

                if (ProbeVolumePositioning.OBBIntersect(ref cellVolume, ref v))
                {
                    volumes.Add(v);

                    TrackSceneRefs(r.gameObject.scene, ref sceneRefs);

                    num++;
                }
            }

            return(num);
        }
Exemplo n.º 3
0
        static IEnumerable <(ProbeReferenceVolume.Volume volume, Vector3 parentPosition)> SubdivideVolumeIntoSubVolume(GPUSubdivisionContext ctx, ProbeReferenceVolume.Volume volume)
        {
            volume.CalculateCenterAndSize(out var center, out var size);
            float maxBrickInSubCell = Mathf.Pow(3, k_MaxSubdivisionInSubCell);
            float subdivisionCount  = ctx.maxBrickCountPerAxis / (float)ctx.maxBrickCountPerAxisInSubCell;
            var   subVolumeSize     = size / subdivisionCount;

            for (int x = 0; x < (int)subdivisionCount; x++)
            {
                for (int y = 0; y < (int)subdivisionCount; y++)
                {
                    for (int z = 0; z < (int)subdivisionCount; z++)
                    {
                        var subVolume = new ProbeReferenceVolume.Volume()
                        {
                            corner = volume.corner + new Vector3(x * subVolumeSize.x, y * subVolumeSize.y, z * subVolumeSize.z),
                            X      = volume.X / subdivisionCount,
                            Y      = volume.Y / subdivisionCount,
                            Z      = volume.Z / subdivisionCount,
                            maxSubdivisionMultiplier = volume.maxSubdivisionMultiplier,
                            minSubdivisionMultiplier = volume.minSubdivisionMultiplier,
                        };
                        var parentCellPosition = new Vector3(x, y, z);

                        yield return(subVolume, parentCellPosition);
                    }
                }
            }
        }
Exemplo n.º 4
0
        // TODO: Add subdivision criteria here,
        // currently just keeps subdividing inside probe volumes
        internal static bool ShouldKeepBrick(ref RefTrans refTrans, Brick brick)
        {
            Renderer[] renderers = Object.FindObjectsOfType <Renderer>();
            foreach (Renderer r in renderers)
            {
                var  flags        = GameObjectUtility.GetStaticEditorFlags(r.gameObject) & StaticEditorFlags.ContributeGI;
                bool contributeGI = (flags & StaticEditorFlags.ContributeGI) != 0;

                if (!r.enabled || !contributeGI)
                {
                    continue;
                }

                ProbeReferenceVolume.Volume v = new ProbeReferenceVolume.Volume();
                v.corner = r.bounds.center - r.bounds.size * 0.5f;
                v.X      = new Vector3(r.bounds.size.x, 0, 0);
                v.Y      = new Vector3(0, r.bounds.size.y, 0);
                v.Z      = new Vector3(0, 0, r.bounds.size.z);

                if (OBBIntersect(ref refTrans, brick, ref v))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 5
0
        static public void CreateInfluenceVolumes(Vector3Int cellPos,
                                                  Renderer[] renderers, ProbeVolume[] probeVolumes,
                                                  ProbeReferenceVolumeAuthoring settings, Matrix4x4 cellTrans, out List <ProbeReferenceVolume.Volume> culledVolumes, out Dictionary <Scene, int> sceneRefs)
        {
            ProbeReferenceVolume.Volume cellVolume = new ProbeReferenceVolume.Volume();
            cellVolume.corner = new Vector3(cellPos.x * settings.cellSize, cellPos.y * settings.cellSize, cellPos.z * settings.cellSize);
            cellVolume.X      = new Vector3(settings.cellSize, 0, 0);
            cellVolume.Y      = new Vector3(0, settings.cellSize, 0);
            cellVolume.Z      = new Vector3(0, 0, settings.cellSize);
            cellVolume.Transform(cellTrans);

            // Keep track of volumes and which scene they originated from
            sceneRefs = new Dictionary <Scene, int>();

            // Extract all influencers inside the cell
            List <ProbeReferenceVolume.Volume> influenceVolumes = new List <ProbeReferenceVolume.Volume>();

            RenderersToVolumes(ref renderers, ref cellVolume, ref influenceVolumes, ref sceneRefs);
            NavPathsToVolumes(ref cellVolume, ref influenceVolumes, ref sceneRefs);
            ImportanceVolumesToVolumes(ref cellVolume, ref influenceVolumes, ref sceneRefs);
            LightsToVolumes(ref cellVolume, ref influenceVolumes, ref sceneRefs);

            // Extract all ProbeVolumes inside the cell
            List <ProbeReferenceVolume.Volume> indicatorVolumes = new List <ProbeReferenceVolume.Volume>();

            ProbeVolumesToVolumes(ref probeVolumes, ref cellVolume, ref indicatorVolumes, ref sceneRefs);

            // Cull all influencers against ProbeVolumes
            culledVolumes = new List <ProbeReferenceVolume.Volume>();
            CullVolumes(ref influenceVolumes, ref indicatorVolumes, ref culledVolumes);
        }
Exemplo n.º 6
0
 static public ProbeReferenceVolume.Volume ToVolume(Bounds bounds)
 {
     ProbeReferenceVolume.Volume v = new ProbeReferenceVolume.Volume();
     v.corner = bounds.center - bounds.size * 0.5f;
     v.X      = new Vector3(bounds.size.x, 0, 0);
     v.Y      = new Vector3(0, bounds.size.y, 0);
     v.Z      = new Vector3(0, 0, bounds.size.z);
     return(v);
 }
Exemplo n.º 7
0
        internal static bool ShouldKeepBrick(List <ProbeReferenceVolume.Volume> volumes, ProbeReferenceVolume.Volume brick)
        {
            foreach (ProbeReferenceVolume.Volume v in volumes)
            {
                ProbeReferenceVolume.Volume vol = v;
                if (ProbeVolumePositioning.OBBIntersect(ref vol, ref brick))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 8
0
        public static void Subdivide(Vector3Int cellPosGridSpace, ProbeReferenceVolume refVol, float cellSize, Vector3 translation, Quaternion rotation, List <ProbeReferenceVolume.Volume> influencerVolumes,
                                     ref Vector3[] positions, ref List <ProbeBrickIndex.Brick> bricks)
        {
            //TODO: This per-cell volume is calculated 2 times during probe placement. We should calculate it once and reuse it.
            ProbeReferenceVolume.Volume cellVolume = new ProbeReferenceVolume.Volume();
            cellVolume.corner = new Vector3(cellPosGridSpace.x * cellSize, cellPosGridSpace.y * cellSize, cellPosGridSpace.z * cellSize);
            cellVolume.X      = new Vector3(cellSize, 0, 0);
            cellVolume.Y      = new Vector3(0, cellSize, 0);
            cellVolume.Z      = new Vector3(0, 0, cellSize);
            cellVolume.Transform(Matrix4x4.TRS(translation, rotation, Vector3.one));

            // TODO move out
            var indicatorVolumes = new List <ProbeReferenceVolume.Volume>();

            foreach (ProbeVolume pv in UnityEngine.Object.FindObjectsOfType <ProbeVolume>())
            {
                if (!pv.enabled)
                {
                    continue;
                }

                indicatorVolumes.Add(new ProbeReferenceVolume.Volume(Matrix4x4.TRS(pv.transform.position, pv.transform.rotation, pv.GetExtents())));
            }

            ProbeReferenceVolume.SubdivisionDel subdivDel =
                (RefTrans refTrans, List <Brick> inBricks, List <Flags> outFlags) =>
            { SubdivisionAlgorithm(cellVolume, indicatorVolumes, influencerVolumes, refTrans, inBricks, outFlags); };

            bricks = new List <ProbeBrickIndex.Brick>();

            // get a list of bricks for this volume
            int numProbes;

            refVol.CreateBricks(new List <ProbeReferenceVolume.Volume>()
            {
                cellVolume
            }, subdivDel, bricks, out numProbes);

            positions = new Vector3[numProbes];
            refVol.ConvertBricks(bricks, positions);
        }
Exemplo n.º 9
0
        public static bool OBBIntersect(ref ProbeReferenceVolume.Volume a, ref ProbeReferenceVolume.Volume b)
        {
            m_Axes[0] = a.X.normalized;
            m_Axes[1] = a.Y.normalized;
            m_Axes[2] = a.Z.normalized;
            m_Axes[3] = b.X.normalized;
            m_Axes[4] = b.Y.normalized;
            m_Axes[5] = b.Z.normalized;

            foreach (Vector3 axis in m_Axes)
            {
                Vector2 aProj = ProjectOBB(ref a, axis);
                Vector2 bProj = ProjectOBB(ref b, axis);

                if (aProj.y < bProj.x || bProj.y < aProj.x)
                {
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 10
0
        static protected void CullVolumes(ref List <ProbeReferenceVolume.Volume> cullees, ref List <ProbeReferenceVolume.Volume> cullers, ref List <ProbeReferenceVolume.Volume> result)
        {
            foreach (ProbeReferenceVolume.Volume v in cullers)
            {
                ProbeReferenceVolume.Volume lv = v;

                foreach (ProbeReferenceVolume.Volume c in cullees)
                {
                    if (result.Contains(c))
                    {
                        continue;
                    }

                    ProbeReferenceVolume.Volume lc = c;

                    if (ProbeVolumePositioning.OBBIntersect(ref lv, ref lc))
                    {
                        result.Add(c);
                    }
                }
            }
        }
Exemplo n.º 11
0
        static protected int ProbeVolumesToVolumes(ref ProbeVolume[] probeVolumes, ref ProbeReferenceVolume.Volume cellVolume, ref List <ProbeReferenceVolume.Volume> volumes, ref Dictionary <Scene, int> sceneRefs)
        {
            int num = 0;

            foreach (ProbeVolume pv in probeVolumes)
            {
                if (!pv.enabled || !pv.gameObject.activeSelf)
                {
                    continue;
                }

                ProbeReferenceVolume.Volume indicatorVolume = new ProbeReferenceVolume.Volume(Matrix4x4.TRS(pv.transform.position, pv.transform.rotation, pv.GetExtents()));

                if (ProbeVolumePositioning.OBBIntersect(ref cellVolume, ref indicatorVolume))
                {
                    volumes.Add(indicatorVolume);
                    TrackSceneRefs(pv.gameObject.scene, ref sceneRefs);
                    num++;
                }
            }

            return(num);
        }
Exemplo n.º 12
0
        public static void SubdivisionAlgorithm(ProbeReferenceVolume.Volume cellVolume, List <ProbeReferenceVolume.Volume> probeVolumes, List <ProbeReferenceVolume.Volume> influenceVolumes, RefTrans refTrans, List <Brick> inBricks, List <Flags> outFlags)
        {
            Flags f = new Flags();

            for (int i = 0; i < inBricks.Count; i++)
            {
                ProbeReferenceVolume.Volume brickVolume = ProbeVolumePositioning.CalculateBrickVolume(ref refTrans, inBricks[i]);

                // Keep bricks that overlap at least one probe volume, and at least one influencer (mesh)
                if (ShouldKeepBrick(probeVolumes, brickVolume) && ShouldKeepBrick(influenceVolumes, brickVolume))
                {
                    f.subdivide = true;

                    // Transform into refvol space
                    brickVolume.Transform(refTrans.refSpaceToWS.inverse);
                    ProbeReferenceVolume.Volume cellVolumeTrans = new ProbeReferenceVolume.Volume(cellVolume);
                    cellVolumeTrans.Transform(refTrans.refSpaceToWS.inverse);

                    // Discard parent brick if it extends outside of the cell, to prevent duplicates
                    var brickVolumeMax = brickVolume.corner + brickVolume.X + brickVolume.Y + brickVolume.Z;
                    var cellVolumeMax  = cellVolumeTrans.corner + cellVolumeTrans.X + cellVolumeTrans.Y + cellVolumeTrans.Z;

                    f.discard = brickVolumeMax.x > cellVolumeMax.x ||
                                brickVolumeMax.y > cellVolumeMax.y ||
                                brickVolumeMax.z > cellVolumeMax.z ||
                                brickVolume.corner.x < cellVolumeTrans.corner.x ||
                                brickVolume.corner.y < cellVolumeTrans.corner.y ||
                                brickVolume.corner.z < cellVolumeTrans.corner.z;
                }
                else
                {
                    f.discard   = true;
                    f.subdivide = false;
                }
                outFlags.Add(f);
            }
        }
Exemplo n.º 13
0
        public static bool OBBIntersect(ref RefTrans refTrans, Brick brick, ref ProbeReferenceVolume.Volume volume)
        {
            var transformed = CalculateBrickVolume(ref refTrans, brick);

            return(OBBIntersect(ref transformed, ref volume));
        }
Exemplo n.º 14
0
 internal Bounds GetBounds()
 {
     ProbeReferenceVolume.Volume volume = new ProbeReferenceVolume.Volume(Matrix4x4.TRS(transform.position, transform.rotation, GetExtents()), 0, 0);
     return(volume.CalculateAABB());
 }
Exemplo n.º 15
0
 public static List <Brick> SubdivideCell(ProbeReferenceVolume.Volume cellVolume, ProbeSubdivisionContext subdivisionCtx, GPUSubdivisionContext ctx, List <(Renderer component, ProbeReferenceVolume.Volume volume)> renderers, List <(ProbeVolume component, ProbeReferenceVolume.Volume volume)> probeVolumes)
Exemplo n.º 16
0
 static protected int LightsToVolumes(ref ProbeReferenceVolume.Volume cellVolume, ref List <ProbeReferenceVolume.Volume> volumes, ref Dictionary <Scene, int> sceneRef)
 {
     // TODO
     return(0);
 }