Пример #1
0
        protected bool UpdateTempRenderTexture(ref CustomRenderTexture target, bool hasMips = false, bool autoGenerateMips = false,
                                               CustomRenderTextureUpdateMode updateMode     = CustomRenderTextureUpdateMode.OnDemand, bool depthBuffer = false,
                                               GraphicsFormat overrideGraphicsFormat        = GraphicsFormat.None, bool hideAsset = true)
        {
            if (graph.mainOutputTexture == null)
            {
                return(false);
            }

            bool             changed      = false;
            int              outputWidth  = settings.GetResolvedWidth(graph);
            int              outputHeight = settings.GetResolvedHeight(graph);
            int              outputDepth  = settings.GetResolvedDepth(graph);
            var              filterMode   = settings.GetResolvedFilterMode(graph);
            var              wrapMode     = settings.GetResolvedWrapMode(graph);
            GraphicsFormat   targetFormat = overrideGraphicsFormat != GraphicsFormat.None ? overrideGraphicsFormat : settings.GetGraphicsFormat(graph);
            TextureDimension dimension    = GetTempTextureDimension();

            outputWidth  = Mathf.Max(outputWidth, 1);
            outputHeight = Mathf.Max(outputHeight, 1);
            outputDepth  = Mathf.Max(outputDepth, 1);

            if (dimension != TextureDimension.Tex3D)
            {
                outputDepth = 1;
            }

            if (dimension == TextureDimension.Cube)
            {
                outputHeight = outputDepth = outputWidth;                 // we only use the width for cubemaps
            }
            if (targetFormat == GraphicsFormat.None)
            {
                targetFormat = graph.mainOutputTexture.graphicsFormat;
            }
            if (dimension == TextureDimension.None)
            {
                dimension = TextureDimension.Tex2D;
            }

            if (dimension == TextureDimension.Tex3D)
            {
                depthBuffer = false;
            }

            if (target == null)
            {
                target = new CustomRenderTexture(outputWidth, outputHeight, targetFormat)
                {
                    volumeDepth       = Math.Max(1, outputDepth),
                    depth             = depthBuffer ? 32 : 0,
                    dimension         = dimension,
                    name              = $"Mixture Temp {name}",
                    updateMode        = CustomRenderTextureUpdateMode.OnDemand,
                    doubleBuffered    = settings.doubleBuffered,
                    wrapMode          = settings.GetResolvedWrapMode(graph),
                    filterMode        = settings.GetResolvedFilterMode(graph),
                    useMipMap         = hasMips,
                    autoGenerateMips  = autoGenerateMips,
                    enableRandomWrite = true,
                    hideFlags         = hideAsset ? HideFlags.HideAndDontSave : HideFlags.None,
                    updatePeriod      = settings.GetUpdatePeriodInMilliseconds(),
                };
                target.Create();
                target.material = MixtureUtils.dummyCustomRenderTextureMaterial;

                return(true);
            }

            // TODO: check if format is supported by current system

            // Warning: here we use directly the settings from the
            if (target.width != outputWidth ||
                target.height != outputHeight ||
                target.graphicsFormat != targetFormat ||
                target.dimension != dimension ||
                target.volumeDepth != outputDepth ||
                target.doubleBuffered != settings.doubleBuffered ||
                target.useMipMap != hasMips ||
                target.autoGenerateMips != autoGenerateMips)
            {
                target.Release();
                target.width             = outputWidth;
                target.height            = outputHeight;
                target.graphicsFormat    = (GraphicsFormat)targetFormat;
                target.dimension         = dimension;
                target.volumeDepth       = outputDepth;
                target.depth             = depthBuffer ? 32 : 0;
                target.doubleBuffered    = settings.doubleBuffered;
                target.useMipMap         = hasMips;
                target.autoGenerateMips  = autoGenerateMips;
                target.enableRandomWrite = true;
                target.hideFlags         = hideAsset ? HideFlags.HideAndDontSave : HideFlags.None;
                target.Create();
                if (target.material == null)
                {
                    target.material = MixtureUtils.dummyCustomRenderTextureMaterial;
                }
                changed = true;
            }

            // Patch settings that don't require to re-create the texture
            target.updateMode   = updateMode;
            target.updatePeriod = settings.GetUpdatePeriodInMilliseconds();
            target.wrapMode     = settings.GetResolvedWrapMode(graph);
            target.filterMode   = settings.GetResolvedFilterMode(graph);

            if (target.doubleBuffered)
            {
                target.EnsureDoubleBufferConsistency();
                var rt = target.GetDoubleBufferRenderTexture();
                if (rt.enableRandomWrite != true)
                {
                    rt.Release();
                    rt.enableRandomWrite = true;
                    rt.Create();
                }
            }

            if (!target.IsCreated())
            {
                target.Create();
            }

            return(changed);
        }
Пример #2
0
        void ReloadSettingsView()
        {
            // Remove all old fields
            Clear();

            if (title != null)
            {
                var titleLabel = new Label(title);
                titleLabel.AddToClassList("PropertyEditorTitle");
                this.Add(titleLabel);
            }

            var dimension = settings.GetResolvedTextureDimension(graph);

            // Wrap and Filter Modes
            smpHeader = new Label("Sampler States");
            smpHeader.AddToClassList(headerStyleClass);
            this.Add(smpHeader);

            wrapMode       = showSettingsForNode ? new EnumField(settings.wrapMode) : new EnumField((GraphOutputWrapMode)settings.wrapMode);
            wrapMode.label = "Wrap Mode";
            wrapMode.RegisterValueChangedCallback(e =>
            {
                owner.RegisterCompleteObjectUndo("Updated Wrap Mode " + e.newValue);
                settings.wrapMode = (OutputWrapMode)e.newValue;
                onChanged?.Invoke();
            });

            filterMode       = showSettingsForNode ? new EnumField(settings.filterMode) : new EnumField((GraphOutputFilterMode)settings.filterMode);
            filterMode.label = "Filter Mode";
            filterMode.RegisterValueChangedCallback(e =>
            {
                owner.RegisterCompleteObjectUndo("Updated Filter Mode " + e.newValue);
                settings.filterMode = (OutputFilterMode)e.newValue;
                onChanged?.Invoke();
            });

            this.Add(wrapMode);
            this.Add(filterMode);

            // Size Modes
            sizeHeader = new Label("Size Properties");
            sizeHeader.AddToClassList(headerStyleClass);
            this.Add(sizeHeader);

            outputSizeMode       = showSettingsForNode ? new EnumField(settings.sizeMode) : new EnumField((GraphOutputSizeMode)settings.sizeMode);
            outputSizeMode.label = "Size Mode";
            outputSizeMode.RegisterValueChangedCallback((EventCallback <ChangeEvent <Enum> >)(e => {
                owner.RegisterCompleteObjectUndo("Updated Size mode " + e.newValue);
                settings.sizeMode = (OutputSizeMode)e.newValue;
                onChanged?.Invoke();
                ReloadSettingsView();
                UpdateFieldVisibility(settings);
            }));
            this.Add(outputSizeMode);

            potSize = new EnumField(settings.potSize)
            {
                value = settings.potSize,
                label = "Resolution",
            };
            potSize.RegisterValueChangedCallback(e =>
            {
                owner.RegisterCompleteObjectUndo("Updated Size " + e.newValue);
                var size         = (POTSize)e.newValue;
                settings.potSize = size;

                if (size != POTSize.Custom)
                {
                    settings.width  = (int)size;
                    settings.height = (int)size;
                    settings.depth  = (int)size;
                }
                else
                {
                    settings.width  = outputWidth.value;
                    settings.height = outputHeight.value;
                    if (outputDepth != null)
                    {
                        settings.depth = outputDepth.value;
                    }
                }

                onChanged?.Invoke();
                ReloadSettingsView();
                UpdateFieldVisibility(settings);
            });

            this.Add(potSize);

            outputWidth = new IntegerField()
            {
                value     = settings.width,
                label     = "Width",
                isDelayed = true,
            };
            outputWidth.RegisterValueChangedCallback(e =>
            {
                owner.RegisterCompleteObjectUndo("Updated Width " + e.newValue);
                settings.width = e.newValue;
                onChanged?.Invoke();
            });
            this.Add(outputWidth);

            outputWidthScale = CreateSizeScaleSlider(settings.widthScale, (v) => settings.widthScale = v, "Width Scale");
            this.Add(outputWidthScale);

            if (dimension != TextureDimension.Cube)
            {
                outputHeight = new IntegerField()
                {
                    value     = settings.height,
                    label     = "Height",
                    isDelayed = true,
                };
                outputHeight.RegisterValueChangedCallback(e =>
                {
                    owner.RegisterCompleteObjectUndo("Updated Height " + e.newValue);
                    settings.height = e.newValue;
                    onChanged?.Invoke();
                });
                this.Add(outputHeight);

                outputHeightScale = CreateSizeScaleSlider(settings.heightScale, v => settings.heightScale = v, "Height Scale");
                this.Add(outputHeightScale);

                if (dimension == TextureDimension.Tex3D)
                {
                    outputDepth = new IntegerField()
                    {
                        value     = settings.depth,
                        label     = "Depth",
                        isDelayed = true,
                    };
                    outputDepth.RegisterValueChangedCallback(e =>
                    {
                        owner.RegisterCompleteObjectUndo("Updated Depth " + e.newValue);
                        settings.depth = e.newValue;
                        onChanged?.Invoke();
                    });
                    this.Add(outputDepth);

                    outputDepthScale = CreateSizeScaleSlider(settings.depthScale, v => settings.depthScale = v, "Depth Scale");
                    this.Add(outputDepthScale);
                }
            }

            SliderInt CreateSizeScaleSlider(float defaultValue, Action <float> setter, string propertyName)
            {
                var slider = new SliderInt(0, (int)(Mathf.Log(k_MaxSizeScale, 2) * 2))
                {
                    value = SizeScaleToInt(settings.heightScale),
                    label = propertyName,
                };

                slider.RegisterValueChangedCallback(e =>
                {
                    owner.RegisterCompleteObjectUndo(propertyName + " " + e.newValue);
                    setter(IntToSizeScale(e.newValue));
                    onChanged?.Invoke();
                });

                return(slider);
            }

            // Dimension and Pixel Format
            formatHeader = new Label("Format");
            formatHeader.AddToClassList(headerStyleClass);
            this.Add(formatHeader);

            outputDimension       = showSettingsForNode ? new EnumField(settings.dimension) : new EnumField((GraphOutputDimension)settings.dimension);
            outputDimension.label = "Dimension";
            outputDimension.RegisterValueChangedCallback(e => {
                owner.RegisterCompleteObjectUndo("Updated Texture Dimension " + e.newValue);
                // Check if the new texture is not too high res:
                settings.dimension = (OutputDimension)e.newValue;
                if (settings.dimension == OutputDimension.Texture3D)
                {
                    long pixelCount = settings.GetResolvedWidth(graph) * settings.GetResolvedHeight(graph) * settings.GetResolvedDepth(graph);

                    // Above 16M pixels in a texture3D, processing can take too long and crash the GPU when a conversion happen
                    if (pixelCount > 16777216)
                    {
                        settings.sizeMode = OutputSizeMode.Absolute;
                        settings.SetPOTSize(64);
                    }
                }
                onChanged?.Invoke();
                ReloadSettingsView();
            });

            outputChannels       = showSettingsForNode ? new EnumField(settings.outputChannels) : new EnumField((GraphOutputChannel)settings.outputChannels);
            outputChannels.label = "Output Channels";
            outputChannels.RegisterValueChangedCallback(e => {
                owner.RegisterCompleteObjectUndo("Updated Output Channels " + e.newValue);
                settings.outputChannels = (OutputChannel)e.newValue;
                onChanged?.Invoke();
            });

            outputPrecision       = showSettingsForNode ? new EnumField(settings.outputPrecision) : new EnumField((GraphOutputPrecision)settings.outputPrecision);
            outputPrecision.label = "Output Precision";
            outputPrecision.RegisterValueChangedCallback(e => {
                owner.RegisterCompleteObjectUndo("Updated Output Precision " + e.newValue);
                settings.outputPrecision = (OutputPrecision)e.newValue;
                // outputPrecision.Init();
                onChanged?.Invoke();
            });

            this.Add(outputDimension);
            this.Add(outputChannels);
            this.Add(outputPrecision);

            UpdateFieldVisibility(settings);

            if (showSettingsForNode)
            {
                // Realtime fields and refresh mode
                otherHeader = new Label("Other");
                otherHeader.AddToClassList(headerStyleClass);
                this.Add(otherHeader);

                doubleBuffered = new Toggle("Double Buffered")
                {
                    value = settings.doubleBuffered,
                };
                doubleBuffered.RegisterValueChangedCallback(e => {
                    owner.RegisterCompleteObjectUndo("Set Double Buffered " + e.newValue);
                    settings.doubleBuffered = e.newValue;
                    onChanged?.Invoke();
                });

                Add(doubleBuffered);
            }
            else if (graph.type == MixtureGraphType.Realtime)
            {
                otherHeader = new Label("Realtime");
                otherHeader.AddToClassList(headerStyleClass);
                this.Add(otherHeader);

                AddRealtimeFields(owner);
            }
        }