Exemplo n.º 1
0
    public RenderTexture GetChannel(string channelName)
    {
        PainterChannel channel = null;

        if (channels.TryGetValue(channelName, out channel))
        {
            return(channel.data);
        }
        return(null);
    }
Exemplo n.º 2
0
    public void WriteChannel(string channelName, RenderTexture channelData, int channelDimension = 3)
    {
        PainterChannel targetChannel = null;

        if (!channels.TryGetValue(channelName, out targetChannel))
        {
            targetChannel = new PainterChannel();
            int dimension = 3;
            if (Painter.ChannelDimensions.TryGetValue(channelName, out dimension))
            {
                targetChannel.dimension = dimension;
            }
            else
            {
                targetChannel.dimension = channelDimension;
            }
            bool blendable;
            if (Painter.ChannelBlendable.TryGetValue(channelName, out blendable))
            {
                targetChannel.blendable = blendable;
            }
            else
            {
                targetChannel.blendable = true;
            }
            channels.Add(channelName, targetChannel);
        }

        // apply blend here, if necessary
        if ((currentPainterLayer != null) && currentPainterLayer.isBlendable && targetChannel.blendable)
        {
            // figure out if we have a mask or not
            RenderTexture maskRT = null;
            if (currentPainter.LayerHasMask(currentGenericLayer))
            {
                maskRT = GetChannel(Painter.Channels.Mask);
            }

            RenderTexture original = targetChannel.data;
            if (original == channelData)
            {
                Error("Blend-able layers must return a new RenderTarget, they cannot reuse the original");
            }
            else
            {
                RenderTexture dest = CreateRenderTexture(original.descriptor);

                // blend channelData on top of original
                m_LayerBlendingMaterial.SetTexture("_Overlay", channelData);
                m_LayerBlendingMaterial.SetFloat("_Opacity", Mathf.Clamp01(currentGenericLayer.opacity));
                if (maskRT)
                {
                    m_LayerBlendingMaterial.SetTexture("_Mask", maskRT);
                }
                else
                {
                    // Is this even cached, or does it build it on demand each time?
                    // This should really come from defaultTexture for the mask.
                    m_LayerBlendingMaterial.SetTexture("_Mask", Texture2D.whiteTexture);
                }
                Graphics.Blit(original, dest, m_LayerBlendingMaterial, (int)currentGenericLayer.blendMode);

                // discard original
                discardedRenderTextures.Add(original.descriptor, original);

                // set channel to new value
                targetChannel.data = dest;

                // no longer a temp target, remove from the temp target list
                currentLayerTempRenderTextures.Remove(dest);
            }
        }
        else
        {
            // if not the same render texture, discard the old render texture (for potential reuse later)
            if ((targetChannel.data != null) && (targetChannel.data != channelData))
            {
                discardedRenderTextures.Add(targetChannel.data.descriptor, targetChannel.data);
            }

            // assign the channel
            targetChannel.data = channelData;

            // new texture is no longer a temp target, so remove from the temp target list
            currentLayerTempRenderTextures.Remove(channelData);
        }
    }