Exemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RenderPassSubscription" /> struct.
 /// </summary>
 /// <param name="layerViewSubset">The ViewRelatedSceneLayerSubset object this subscription belongs to.</param>
 /// <param name="renderPass">The render pass on which to register.</param>
 /// <param name="sceneObject">The scene object which should be registered.</param>
 /// <param name="renderMethod">The render method which is to be registered.</param>
 /// <param name="zOrder">The z-order for sorting if the subscriptions of this pass get sorted by it.</param>
 internal RenderPassSubscription(
     ViewRelatedSceneLayerSubset layerViewSubset, RenderPassInfo renderPass,
     SceneObject sceneObject, Action <RenderState> renderMethod,
     int zOrder)
 {
     LayerViewSubset   = layerViewSubset;
     RenderPass        = renderPass;
     SceneObject       = sceneObject;
     RenderMethod      = renderMethod;
     IsSubscribed      = true;
     SubscriptionIndex = 0;
     ZOrder            = zOrder;
 }
        /// <summary>
        /// Subscribes the given object to the given render pass.
        /// </summary>
        internal RenderPassSubscription SubscribeForPass(
            RenderPassInfo passInfo,
            SceneObject sceneObject, Action <RenderState> renderMethod,
            int zOrder)
        {
            if (!_isSubscribeUnsubscribeAllowed)
            {
                throw new SeeingSharpException("Subscription is not allowed currently!");
            }

            var subscriptionProperties = _objectsPerPassDict[passInfo];

            // Append new subscription to subscription list
            var subscriptions      = subscriptionProperties.Subscriptions;
            var subscriptionsCount = subscriptions.Count;
            var newSubscription    = new RenderPassSubscription(this, passInfo, sceneObject, renderMethod, zOrder);

            if (!passInfo.IsSorted)
            {
                // No sort, so put the new subscription to the end of the collection
                newSubscription.SubscriptionIndex = subscriptionsCount;
                subscriptions.Add(newSubscription);
            }
            else
            {
                // Perform BinaryInsert to the correct position
                var newIndex = SeeingSharpUtil.BinaryInsert(subscriptions, newSubscription, SubscriptionZOrderComparer.Instance);

                // Increment all subscription indices after the inserted position
                subscriptionsCount++;
                for (var loop = newIndex; loop < subscriptionsCount; loop++)
                {
                    var actSubscription = subscriptions[loop];
                    if (actSubscription.SubscriptionIndex != loop)
                    {
                        actSubscription.SubscriptionIndex = loop;
                        subscriptions[loop] = actSubscription;

                        actSubscription.SceneObject.UpdateSubscription(actSubscription, this);
                    }
                }
                newSubscription = subscriptions[newIndex];
            }

            return(newSubscription);
        }