Beispiel #1
0
        public override void Enable(bool fromInspector)
        {
            base.Enable(fromInspector);

            node = nodeTarget as PreviewNode;

            var colorPickerValues = new Label();

            colorPickerValues.AddToClassList("Indent");
            if (!fromInspector)
            {
                controlsContainer.Add(colorPickerValues);

                // Stop right click when the mouse is over the preview because we use it for light position
                previewContainer.RegisterCallback <ContextualMenuPopulateEvent>(e => {
                    e.StopImmediatePropagation();
                }, TrickleDown.TrickleDown);
                previewContainer.AddManipulator(new ContextualMenuManipulator(evt => {}));
            }
            else
            {
                owner.graph.afterCommandBufferExecuted += UpdateViewData;
                controlsContainer.RegisterCallback <DetachFromPanelEvent>(e => {
                    owner.graph.afterCommandBufferExecuted -= UpdateViewData;
                });
                var histogram = new HistogramView(node.histogramData, owner);
                controlsContainer.Add(histogram);
            }

            void UpdateViewData()
            {
                if (node.output != null)
                {
                    // Update histogram
                    var cmd = CommandBufferPool.Get("Update Histogram");
                    HistogramUtility.ComputeHistogram(cmd, node.output, node.histogramData);
                    Graphics.ExecuteCommandBuffer(cmd);

                    // Update color picker data
                    UpdateColorPickerValues();
                }
            }

            void UpdateColorPickerValues()
            {
                int texturePosX = Mathf.RoundToInt(node.mousePosition.x * node.output.width);
                int texturePosY = Mathf.RoundToInt(node.mousePosition.y * node.output.height);

                texturePosX = Mathf.Clamp(texturePosX, 0, node.output.width - 1);
                texturePosY = Mathf.Clamp(texturePosY, 0, node.output.height - 1);

                var a = AsyncGPUReadback.Request(node.output, 0, texturePosX, 1, texturePosY, 1, 0, 1, (data) => {
                    var colors = data.GetData <Color>();
                    if (data.hasError || colors.Length == 0)
                    {
                        return;
                    }
                    var pixel = colors[0];
                    colorPickerValues.text = $"R: {pixel.r:F3} G: {pixel.g:F3} B: {pixel.b:F3} A: {pixel.a:F3}";
                });

                schedule.Execute(() => {
                    a.Update();
                }).Until(() => a.done);
            }

            var preview = previewContainer.Q("ImGUIPreview");

            preview.RegisterCallback <MouseMoveEvent>(e => {
                var localPos       = GetPreviewMousePositionRatio(e.mousePosition);
                node.mousePosition = localPos;
                UpdateColorPickerValues();

                if (e.imguiEvent.button == 1)
                {
                    NotifyNodeChanged();
                    node.lightPosition = (new Vector2(localPos.x, 1 - localPos.y) * 2 - Vector2.one) * node.tiling;
                }
            });

            // TODO: add source mip slider

            UpdateViewData();
            UpdateColorPickerValues();
        }
Beispiel #2
0
        public override void Enable(bool fromInspector)
        {
            levelsNode = nodeTarget as Levels;

            base.Enable(fromInspector);

            var slider = new MinMaxSlider("Luminance", levelsNode.min, levelsNode.max, 0, 1);

            sliders.Add(slider);
            slider.RegisterValueChangedCallback(e => {
                owner.RegisterCompleteObjectUndo("Changed Luminance remap");
                levelsNode.min = e.newValue.x;
                levelsNode.max = e.newValue.y;
                foreach (var s in sliders)
                {
                    if (s != null && s.parent != null)
                    {
                        s.SetValueWithoutNotify(e.newValue);
                    }
                }
                NotifyNodeChanged();
            });
            controlsContainer.Add(slider);

            var mode = this.Q <EnumField>();

            mode.RegisterValueChangedCallback((m) => {
                UpdateMinMaxSliderVisibility((Levels.Mode)m.newValue);
            });
            UpdateMinMaxSliderVisibility(levelsNode.mode);

            // Compute histogram only when the inspector is selected
            if (fromInspector)
            {
                owner.graph.afterCommandBufferExecuted += UpdateHistogram;
                controlsContainer.RegisterCallback <DetachFromPanelEvent>(e => {
                    owner.graph.afterCommandBufferExecuted -= UpdateHistogram;
                });
            }

            void UpdateHistogram()
            {
                if (levelsNode.output != null)
                {
                    var cmd = CommandBufferPool.Get("Update Histogram");
                    HistogramUtility.ComputeHistogram(cmd, levelsNode.output, levelsNode.histogramData);
                    Graphics.ExecuteCommandBuffer(cmd);
                }
            }

            UpdateHistogram();

            void UpdateMinMaxSliderVisibility(Levels.Mode mode)
            {
                if (mode == Levels.Mode.Automatic)
                {
                    slider.style.display = DisplayStyle.None;
                }
                else
                {
                    slider.style.display = DisplayStyle.Flex;
                }
            }

            if (fromInspector)
            {
                var histogram = new HistogramView(levelsNode.histogramData, owner);
                controlsContainer.Add(histogram);
            }
        }