/// <inheritdoc/>
        public override void Extract()
        {
            var modelObjectInfo = RootRenderFeature.RenderData.GetData(renderObjectInfoKey);

            foreach (var objectNodeReference in RootRenderFeature.ObjectNodeReferences)
            {
                var objectNode = RootRenderFeature.GetObjectNode(objectNodeReference);
                var renderMesh = objectNode.RenderObject as RenderMesh;
                if (renderMesh == null)
                {
                    continue;
                }

                int meshIndex = 0;
                for (int i = 0; i < renderMesh.RenderModel.ModelComponent.Model.Meshes.Count; i++)
                {
                    if (renderMesh.RenderModel.ModelComponent.Model.Meshes[i] == renderMesh.Mesh)
                    {
                        meshIndex = i;
                        break;
                    }
                }

                modelObjectInfo[objectNodeReference] = new PickingObjectInfo(RuntimeIdHelper.ToRuntimeId(renderMesh.RenderModel.ModelComponent), meshIndex, renderMesh.Mesh.MaterialIndex);
            }
        }
Example #2
0
        protected override void DrawCore(RenderContext context, RenderDrawContext drawContext)
        {
            if (pickingTexture == null)
            {
                // TODO: Release resources!
                pickingTexture = Texture.New2D(drawContext.GraphicsDevice, 1, 1, PickingRenderStage.Output.RenderTargetFormat0, TextureFlags.None, 1, GraphicsResourceUsage.Staging);
            }
            var inputManager = context.Services.GetSafeServiceAs <InputManager>();

            // Skip rendering if mouse position is the same
            var mousePosition = inputManager.MousePosition;

            // TODO: Use RenderFrame
            var pickingRenderTarget = PushScopedResource(context.Allocator.GetTemporaryTexture2D(PickingTargetSize, PickingTargetSize, PickingRenderStage.Output.RenderTargetFormat0));
            var pickingDepthStencil = PushScopedResource(context.Allocator.GetTemporaryTexture2D(PickingTargetSize, PickingTargetSize, PickingRenderStage.Output.DepthStencilFormat, TextureFlags.DepthStencil));

            var renderTargetSize  = new Vector2(pickingRenderTarget.Width, pickingRenderTarget.Height);
            var positionInTexture = Vector2.Modulate(renderTargetSize, mousePosition);
            int x = Math.Max(0, Math.Min((int)renderTargetSize.X - 2, (int)positionInTexture.X));
            int y = Math.Max(0, Math.Min((int)renderTargetSize.Y - 2, (int)positionInTexture.Y));

            // Render the picking stage using the current view
            using (drawContext.PushRenderTargetsAndRestore())
            {
                drawContext.CommandList.Clear(pickingRenderTarget, Color.Transparent);
                drawContext.CommandList.Clear(pickingDepthStencil, DepthStencilClearOptions.DepthBuffer);

                drawContext.CommandList.SetRenderTargetAndViewport(pickingDepthStencil, pickingRenderTarget);
                drawContext.CommandList.SetScissorRectangle(new Rectangle(x, y, 1, 1));
                context.RenderSystem.Draw(drawContext, context.RenderView, PickingRenderStage);
                drawContext.CommandList.SetScissorRectangle(new Rectangle());
            }

            // Copy results to 1x1 target
            drawContext.CommandList.CopyRegion(pickingRenderTarget, 0, new ResourceRegion(x, y, 0, x + 1, y + 1, 1), pickingTexture, 0);

            // Get data
            var data = new PickingObjectInfo[1];

            pickingTexture.GetData(drawContext.CommandList, data);
            pickingResult = data[0];
        }