{
  "Name": "BlendImages",
  "_id": "5778fd1e-bd11-4b21-9277-62c946c4f80c",
  "Namespace": "lib.image.adjust",
  "Description": "Composes two images on top of each other.",
  "Inputs": [
    {
      "Name": "Code",
      "MetaInstanceID": "e0ccec82-f325-410e-9cd2-d3febb0e2dc1",
      "DefaultValue": {
        "Type": "Text",
        "Value": [
          "//>>> _common parameters\r",
          "float4x4 objectToWorldMatrix;",
          "float4x4 worldToCameraMatrix;",
          "float4x4 projMatrix;",
          "Texture2D txDiffuse;",
          "float2 RenderTargetSize;",
          "//<<< _common parameters\r",
          "\r",
          "//>>> _parameters\r",
          "Texture2D ImageA;",
          "Texture2D ImageB;",
          "float ColorMode;",
          "float AlphaMode;",
          "//<<< _parameters\r",
          " \r",
          "//>>> setup\r",
          "SamplerState samNearest\r",
          "{\r",
          "    Filter = MIN_MAG_MIP_LINEAR;\r",
          "    AddressU = Clamp;\r",
          "    AddressV = Clamp;\r",
          "};\r",
          "//<<< setup\r",
          "\r",
          "//>>> declarations\r",
          "struct VS_IN\r",
          "{\r",
          "    float4 pos : POSITION;\r",
          "    float2 texCoord : TEXCOORD;\r",
          "};\r",
          "\r",
          "struct PS_IN\r",
          "{\r",
          "    float4 pos : SV_POSITION;\r",
          "    float2 texCoord: TEXCOORD0;\t    \r",
          "    float3 worldTViewPos: TEXCOORD1;\r",
          "    float3 worldTViewDir: TEXCOORD2;\r",
          "};\r",
          "//<<< declarations\r",
          "\r",
          "//>>> _GS\r",
          "",
          "//<<< _GS\r",
          "\r",
          "//>>> _VS \r",
          "PS_IN VS( VS_IN input )",
          "{",
          "    PS_IN output = (PS_IN)0;",
          "",
          "    output.pos = mul(input.pos, worldToCameraMatrix);",
          "    output.pos = mul(output.pos, projMatrix);",
          "    output.texCoord = input.texCoord;",
          "",
          "    return output;",
          "}",
          "//<<< _VS\r",
          "\r",
          "//>>> PS\r",
          "\r",
          "\r",
          "float4 PS( PS_IN input ) : SV_Target\r",
          "{\r",
          "    float4 tA = ImageA.Sample(samNearest, input.texCoord); \r",
          "    float4 tB = ImageB.Sample(samNearest, input.texCoord);    \r",
          "\r",
          "    float a = tA.a + tB.a - tA.a*tB.a;\r",
          "    float3 rgb = (1.0 - tB.a)*tA.rgb + tB.a*tB.rgb;   \r",
          "    \r",
          "    switch( (int)ColorMode) {\r",
          "        // screen\r",
          "        case 1:\r",
          "            rgb = 1-(1-tA.rgb) * (1-tB.rgb);\r",
          "            break;\r",
          "            \r",
          "        // multiply\r",
          "        case 2:\r",
          "            rgb = tA.rgb * tB.rgb;\r",
          "            break;\r",
          "        //\r",
          "        case 3:\r",
          "            rgb = tA.rgb + tB.rgb;\r",
          "            break;\r",
          "        \r",
          "        // overlay\r",
          "        case 4:\r",
          "            rgb = float3( \r",
          "                tA.r < 0.5?(2.0 * tA.r * tB.r) : (1.0-2.0*(1.0-tA.r)*(1.0- tB.r)),\r",
          "                tA.g < 0.5?(2.0 * tA.g * tB.g) : (1.0-2.0*(1.0-tA.g)*(1.0- tB.g)),\r",
          "                tA.b < 0.5?(2.0 * tA.b * tB.b) : (1.0-2.0*(1.0-tA.b)*(1.0- tB.b)));\r",
          "            break;\r",
          "            \r",
          "        // difference\r",
          "        case 5:\r",
          "            rgb = abs(tA.rgb - tB.rgb) * tB.a + tB.rgb * (1.0 - tB.a);\r",
          "            break;        \r",
          "        case 6:\r",
          "            rgb = tA.rgb;\r",
          "            break;\r",
          "    }\r",
          "    \r",
          "    switch( (int)AlphaMode) {\r",
          "        case 1:\r",
          "            a = tA.a;\r",
          "            break;\r",
          "        case 2:\r",
          "            a = tA.a * tB.a;\r",
          "            break;\r",
          "            \r",
          "        case  3:\r",
          "            a =1;\r",
          "            break;\r",
          "    }\r",
          "    \r",
          "    return float4(rgb, a);\r",
          "\r",
          "}\r",
          "//<<< PS\r",
          "\r",
          "//>>> _technique\r",
          "technique10 Render",
          "{",
          "    pass P0",
          "    {",
          "        SetGeometryShader( 0 );",
          "        SetVertexShader( CompileShader( vs_4_0, VS() ) );",
          "        SetPixelShader( CompileShader( ps_4_0, PS() ) );",
          "    }",
          "}",
          "//<<< _technique\r",
          ""
        ]
      },
      "MetaID": "c522a66e-3260-4692-b3e3-79fd0361fa3d",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "-100000",
      "Max": "100000",
      "Scale": "0.1",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "ImageA",
      "MetaInstanceID": "31cbd2f8-2f42-403d-9142-3ad22e6b6225",
      "DefaultValue": {
        "Type": "Image",
        "Value": "Framefield.Core.Image"
      },
      "MetaID": "9848060d-fd84-45b0-b658-d0d531c61dab",
      "IsMultiInput": "False",
      "Relevance": "Required",
      "Description": "",
      "Min": "-100000",
      "Max": "100000",
      "Scale": "0.1",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "ImageB",
      "MetaInstanceID": "9d7470a5-c572-4924-b121-b6e2b1614034",
      "DefaultValue": {
        "Type": "Image",
        "Value": "Framefield.Core.Image"
      },
      "MetaID": "9848060d-fd84-45b0-b658-d0d531c61dab",
      "IsMultiInput": "False",
      "Relevance": "Required",
      "Description": "",
      "Min": "-100000",
      "Max": "100000",
      "Scale": "0.1",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "ColorMode",
      "MetaInstanceID": "cc982fe1-68b7-40f8-a1cc-cab61033d987",
      "DefaultValue": {
        "Type": "Float",
        "Value": "0"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "-10000",
      "Max": "10000",
      "Scale": "0.1",
      "ScaleType": "Linear",
      "EnumValues": [
        {
          "Name": "NormalBlend",
          "Value": "0"
        },
        {
          "Name": "Screen",
          "Value": "1"
        },
        {
          "Name": "Multiply",
          "Value": "2"
        },
        {
          "Name": "Add",
          "Value": "3"
        },
        {
          "Name": "Overlay",
          "Value": "4"
        },
        {
          "Name": "Difference",
          "Value": "5"
        },
        {
          "Name": "OnlyA",
          "Value": "6"
        }
      ]
    },
    {
      "Name": "AlphaMode",
      "MetaInstanceID": "b1d7382a-f22c-4cad-9294-2848f12136da",
      "DefaultValue": {
        "Type": "Float",
        "Value": "0"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "-10000",
      "Max": "10000",
      "Scale": "0.1",
      "ScaleType": "Linear",
      "EnumValues": [
        {
          "Name": "Normal",
          "Value": "0"
        },
        {
          "Name": "MaskWithA",
          "Value": "1"
        },
        {
          "Name": "MaskWithB",
          "Value": "2"
        },
        {
          "Name": "Opaque",
          "Value": "3"
        }
      ]
    }
  ],
  "Outputs": [
    {
      "Name": "Image",
      "MetaInstanceID": "852971d5-5a56-4b81-97de-3c33203693c3",
      "MetaID": "9848060d-fd84-45b0-b658-d0d531c61dab"
    }
  ],
  "OperatorParts": [
    {
      "MetaInstanceID": "0361546c-cc78-4e2c-a49b-6d8d12b57129",
      "MetaID": "719e0132-6622-4aba-8c92-8eb8e417529e",
      "Name": "BlendImagesFunc",
      "Version": "6df5cb0a-5cad-4ba9-bf75-68c12ddf80a4",
      "Type": "Float",
      "IsMultiInput": "True",
      "Script": [
        "//>>> _using",
        "using System;",
        "using System.Collections.Generic;",
        "using System.Linq;",
        "using System.Text;",
        "using SharpDX;",
        "using SharpDX.Direct3D11;",
        "using SharpDX.Windows;",
        "//<<< _using",
        "",
        "namespace Framefield.Core.ID719e0132_6622_4aba_8c92_8eb8e417529e",
        "{",
        "    public class Class_BlendImages : FXImageFunction",
        "    {",
        "        //>>> _inputids",
        "        private enum InputId",
        "        {",
        "            Code = 0,",
        "            ImageA = 1,",
        "            ImageB = 2,",
        "            ColorMode = 3,",
        "            AlphaMode = 4",
        "        }",
        "        //<<< _inputids",
        "",
        "        public override OperatorPartContext Eval(OperatorPartContext context, List<OperatorPart> inputs, int outputIdx)",
        "        {",
        "            return PrepareAndEvalOnChange(context, () =>",
        "            {",
        "                //>>> _params",
        "                var Code = inputs[(int)InputId.Code].Eval(context).Text;",
        "                var ImageA = inputs[(int)InputId.ImageA].Eval(context).Image; // Needs to be checked for null!",
        "                var ImageB = inputs[(int)InputId.ImageB].Eval(context).Image; // Needs to be checked for null!",
        "                var ColorMode = (int) inputs[(int)InputId.ColorMode].Eval(context).Value;",
        "                var AlphaMode = (int) inputs[(int)InputId.AlphaMode].Eval(context).Value;",
        "                //<<< _params",
        "",
        "                ClearRenderTarget(context, new SharpDX.Color4(0, 0, 0, 0));",
        "                if(ImageA == null || ImageB ==null) {",
        "                    Logger.Warn(this, \"BlendImages requires two images\");",
        "                }",
        "",
        "                //>>> _setup",
        "                using (var ImageAView = new ShaderResourceView(context.D3DDevice, ImageA))",
        "                using (var ImageBView = new ShaderResourceView(context.D3DDevice, ImageB))",
        "                {",
        "                    _effect.GetVariableByName(\"RenderTargetSize\").AsVector().Set(new Vector2(_usedViewport.Width, _usedViewport.Height));",
        "                    _effect.GetVariableByName(\"ImageA\").AsShaderResource().SetResource(ImageAView);",
        "                    _effect.GetVariableByName(\"ImageB\").AsShaderResource().SetResource(ImageBView);",
        "                    _effect.GetVariableByName(\"ColorMode\").AsScalar().Set(ColorMode);",
        "                    _effect.GetVariableByName(\"AlphaMode\").AsScalar().Set(AlphaMode);",
        "                //<<< _setup",
        "",
        "                Render(context);",
        "",
        "                //>>> _cleanup",
        "                }",
        "                //<<< _cleanup",
        "            });",
        "        }",
        "",
        "    }",
        "}",
        "",
        ""
      ],
      "AdditionalAssemblies": []
    }
  ],
  "Operators": [],
  "Connections": [
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "0361546c-cc78-4e2c-a49b-6d8d12b57129",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "852971d5-5a56-4b81-97de-3c33203693c3"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "e0ccec82-f325-410e-9cd2-d3febb0e2dc1",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "0361546c-cc78-4e2c-a49b-6d8d12b57129"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "31cbd2f8-2f42-403d-9142-3ad22e6b6225",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "0361546c-cc78-4e2c-a49b-6d8d12b57129"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "9d7470a5-c572-4924-b121-b6e2b1614034",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "0361546c-cc78-4e2c-a49b-6d8d12b57129"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "cc982fe1-68b7-40f8-a1cc-cab61033d987",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "0361546c-cc78-4e2c-a49b-6d8d12b57129"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "b1d7382a-f22c-4cad-9294-2848f12136da",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "0361546c-cc78-4e2c-a49b-6d8d12b57129"
    }
  ]
}