Beispiel #1
0
        protected bool FinishDisplayListEntry(ref DisplayListEntry de, ref DisplayListCamera dlc,
                                              ref LocalToWorld tx, ref PrivateTransformData ptd, bool doNotClip)
        {
            de.inSortingGroup = ptd.inSortingGroup;
#if UNITY_USE_TINYMATH
            de.finalMatrix = tinymath.mul(dlc.inverseWorld, tx.Value);
#else
            de.finalMatrix = math.mul(dlc.inverseWorld, tx.Value);
#endif
            if (doNotClip)
            {
                return(true);
            }
            // z-clip
            float z = de.finalMatrix[2][3];
            if (z < dlc.clipZNear || z > dlc.clipZFar)
            {
                return(false);
            }
            // bounds clip
#if DEBUG
            if (!(de.inBounds.width > 0.0f) || !(de.inBounds.height > 0.0f))
            {
                Debug.LogFormat("Entity {0} has zero or negative size ({1},{2})! This is checked in DEVELOPMENT builds only!", de.e, de.inBounds.width, de.inBounds.height);
                return(false);
            }
#endif
            return(true);
        }
Beispiel #2
0
        static Entity ComputeWorldTransformAndSortingGroupRec(Entity e, int maxDepth, EntityManager mgr)
        {
            PrivateTransformData ptf = mgr.GetComponentData <PrivateTransformData>(e);

            if (ptf.flags == 0)
            {
                return(ptf.inSortingGroup);
            }

            Entity        ep  = mgr.GetComponentData <Parent>(e).Value;
            LocalToParent tol = mgr.GetComponentData <LocalToParent>(e);
            LocalToWorld  tow = mgr.GetComponentData <LocalToWorld>(e);

            if (ep == Entity.Null)
            {
                // local and global the same
                ptf.inSortingGroup = Entity.Null;
                tow.Value          = tol.Value;
            }
            else
            {
                Assert.IsTrue(mgr.Exists(ep), "An entity has a Parent parent set to an entity that was deleted. Did you mean to use TransformHelpers.DestroyTree?");
                Assert.IsTrue(mgr.HasComponent <Parent>(ep), "An entity has a Parent parent set to an entity that is not a Parent.");
                Assert.IsTrue(!mgr.HasComponent <Disabled>(ep), "An entity has a Parent parent that is disabled. The child must be disabled itself to avoid rendering it. Use TransformHelpers.DisableTree to disable a hierarchy.");
                if (maxDepth <= 0)
                {
                    tow.Value = float4x4.identity;
                    Assert.IsTrue(false, "Transform hierarchy is too deep or has a cycle. Run TransformHelpers.DebugCheckAllNodes to debug.");
                    return(Entity.Null);
                }
                // we have a parent!
                Entity   ptinsg = ComputeWorldTransformAndSortingGroupRec(ep, maxDepth - 1, mgr);
                float4x4 matp   = mgr.GetComponentData <LocalToWorld>(ep).Value;
#if UNITY_USE_TINYMATH
                tow.Value = tinymath.mul(matp, tol.Value);
#else
                tow.Value = math.mul(matp, tol.Value);
#endif
                if (mgr.HasComponent <SortingGroup>(ep))
                {
                    // parent is head of a group?
                    ptf.inSortingGroup = ep;
                }
                else
                {
                    // take whatever group parent is in
                    ptf.inSortingGroup = ptinsg;
                }
            }

            ptf.flags = 0;
            mgr.SetComponentData <LocalToWorld>(e, tow);
            mgr.SetComponentData <PrivateTransformData>(e, ptf);
            return(ptf.inSortingGroup);
        }
Beispiel #3
0
        protected int UpdateOneDisplayListCamera(Entity e, ref Camera2D cam, ref DisplayListCamera dlcCam, ref LocalToWorld tx, float primaryAspect)
        {
            var mgr = EntityManager;

            // get list and sorted list buffers
            DynamicBuffer <DisplayListEntry> dest       = mgr.GetBuffer <DisplayListEntry>(e);
            DynamicBuffer <SortedEntity>     destSorted = mgr.GetBuffer <SortedEntity>(e);

            dest.Clear();
            destSorted.Clear();
#if DEBUG
            if (!(cam.rect.x >= 0.0f && cam.rect.y >= 0.0f && cam.rect.x + cam.rect.width <= 1.0f &&
                  cam.rect.y + cam.rect.height <= 1.0f))
            {
                Debug.LogFormat("The camera {0} has an invalid rect field ({1},{2},{3},{4}). Fixing by clamping it to the unit rectangle (0,0,1,1) in DEVELOPMENT build only.",
                                e, cam.rect.x, cam.rect.y, cam.rect.width, cam.rect.height);
                cam.rect.Clamp(new Rect(0, 0, 1, 1));
            }
            if (cam.rect.IsEmpty())
            {
                Debug.LogFormat("The camera {0} has an empty rect field. Fixing by setting it to identity in DEVELOPMENT build only.", e);
                cam.rect = new Rect(0, 0, 1, 1);
            }
            if (cam.halfVerticalSize <= 0)
            {
                Debug.LogFormat("The camera {0} has an invalid halfVerticalSize size of {1}. Nothing will render for it.", e, cam.halfVerticalSize);
                return(0);
            }
            float mas = MaxAbsScale(tx.Value);
            if (!(mas > .99f && mas < 1.01f))
            {
                Debug.LogFormat("The entity {0} with a Camera2D has a maximum absolute scaling factor of {1}. Cameras can not be scaled for rendering. Rendering and picking with this camera will likely be wrong.",
                                e, mas);
            }
#endif
            // get camera sorting axis
            if (mgr.HasComponent <Camera2DAxisSort>(e))
            {
                var axissort = mgr.GetComponentData <Camera2DAxisSort>(e);
                dlcCam.sortingDot.xyz = -axissort.axis;
                dlcCam.sortingDot.w   = 0;
            }
            else
            {
                dlcCam.sortingDot.x = 0.0f;
                dlcCam.sortingDot.y = 0.0f;
                dlcCam.sortingDot.z = -1.0f;
                dlcCam.sortingDot.w = 0.0f;
            }
            // copy transform matrix
            dlcCam.world        = tx.Value;
            dlcCam.inverseWorld = math.inverse(dlcCam.world);

            // initialize 2d clipping
            if (mgr.HasComponent <Camera2DRenderToTexture>(e))
            {
                var   rtt         = mgr.GetComponentData <Camera2DRenderToTexture>(e);
                float localAspect = (float)rtt.width / (float)rtt.height;
                dlcCam.clip2D.x = cam.halfVerticalSize * localAspect;
                dlcCam.clip2D.y = cam.halfVerticalSize;
            }
            else
            {
                dlcCam.clip2D.x = cam.halfVerticalSize * primaryAspect;
                dlcCam.clip2D.y = cam.halfVerticalSize;
            }

            // initialize near/far clipping
            if (mgr.HasComponent <Camera2DClippingPlanes>(e))
            {
                var clipz = mgr.GetComponentData <Camera2DClippingPlanes>(e);
                dlcCam.clipZNear = clipz.near;
                dlcCam.clipZFar  = clipz.far;
            }
            else
            {
                dlcCam.clipZNear = float.MinValue;
                dlcCam.clipZFar  = float.MaxValue;
            }

#if DEBUG
            if (dlcCam.clipZNear >= dlcCam.clipZFar)
            {
                Debug.LogFormat("The camera {0} has an invalid z clip range [{1}...{2}]. Nothing will render for it.", e, dlcCam.clipZNear, dlcCam.clipZFar);
                return(0);
            }
#endif

            // add all items
            for (int i = 0; i < dlMakerReg.Count; i++)
            {
                AddItemsToListByType(dlMakerReg[i], dlcCam, cam, dest, destSorted);
            }

            // sort in c++
            unsafe {
                SortExternal(dest.GetUnsafePtr(), destSorted.GetUnsafePtr(), dest.Length);
            }

            return(dest.Length);
        }