{
  "Name": "MaskedGaussBlur",
  "_id": "c8badc6c-1842-45bf-adb1-d428e7b42544",
  "Namespace": "lib.image.blur",
  "Description": "Blurs an image. The strength of the blur-kernel is defined by the brightness of the second image input. This can be used for a tilt-shift effect",
  "Inputs": [
    {
      "Name": "Code",
      "MetaInstanceID": "d68358cf-ccd5-44f2-8d22-9b72bc3b89f1",
      "DefaultValue": {
        "Type": "Text",
        "Value": [
          "//>>> _common parameters\r",
          "float4x4 objectToWorldMatrix;",
          "float4x4 worldToCameraMatrix;",
          "float4x4 projMatrix;",
          "Texture2D txDiffuse;",
          "float2 RenderTargetSize;",
          "//<<< _common parameters\r",
          "\r",
          "//>>> _parameters\r",
          "Texture2D Image;",
          "float Glow;",
          "float Offset;",
          "float Size;",
          "float NumberOfSamples;",
          "Texture2D Intensity;",
          "//<<< _parameters\r",
          "\r",
          "float widthToHeight;\r",
          "float2 direction;\r",
          "\r",
          "//>>> _setup\r",
          "SamplerState samLinear",
          "{",
          "    Filter = MIN_MAG_MIP_LINEAR;",
          "    AddressU = Clamp;",
          "    AddressV = Clamp;",
          "};",
          "//<<< _setup\r",
          "\r",
          "//>>> _declarations\r",
          "struct VS_IN",
          "{",
          "    float4 pos : POSITION;",
          "    float2 texCoord : TEXCOORD;",
          "};",
          "",
          "struct PS_IN",
          "{",
          "    float4 pos : SV_POSITION;",
          "    float2 texCoord: TEXCOORD0;",
          "};",
          "//<<< _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",
          "static const int NUMWT = 10;\r",
          "static const float Gauss[NUMWT] = { 0.93, 0.86, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1 };\r",
          "\r",
          "float4 PS( PS_IN input ) : SV_Target\r",
          "{\r",
          "    float2 dir = direction;\r",
          "    \r",
          "    float strength = Intensity.Sample(samLinear, input.texCoord).r;\r",
          "\r",
          "    //dir *= 0.01* Size/NumberOfSamples;\r",
          "    dir *= 0.01* strength * Size / NumberOfSamples;\r",
          "    dir.y *= widthToHeight;\r",
          "\r",
          "    float2 pos = dir;\r",
          "    float4 c = txDiffuse.Sample(samLinear, input.texCoord);\r",
          "    float totalWeight = 1;\r",
          "    for (int i = 0; i < NumberOfSamples; ++i)\r",
          "    {\r",
          "        float index = (float)i*(NUMWT - 1)/NumberOfSamples;\r",
          "        float weight = lerp(Gauss[(int)index], Gauss[(int)index + 1], frac(index));\r",
          "        c += txDiffuse.Sample(samLinear, input.texCoord + pos)*weight;\r",
          "        c += txDiffuse.Sample(samLinear, input.texCoord - pos)*weight;\r",
          "        pos += dir;\r",
          "        totalWeight += 2*weight;\r",
          "    }\r",
          "    \r",
          "\r",
          "    //c.rgb = float3(Offset, Offset, Offset) + c.rgb/totalWeight*Glow;\r",
          "    //c.a = 1.0;\r",
          "    c = float4(Offset, Offset, Offset, Offset) + c/totalWeight*Glow;\r",
          "    //c.r = Intensity.Sample(samLinear, input.texCoord).r;\r",
          "    \r",
          "    return c;\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": "Image",
      "MetaInstanceID": "4ce48bbe-0c04-4d80-a3b2-565d31441218",
      "DefaultValue": {
        "Type": "Image",
        "Value": "Framefield.Core.Image"
      },
      "MetaID": "9848060d-fd84-45b0-b658-d0d531c61dab",
      "IsMultiInput": "False",
      "Relevance": "Required",
      "Description": "",
      "Min": "-100000",
      "Max": "100000",
      "Scale": "1",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Glow",
      "MetaInstanceID": "b4a14bce-d23d-4beb-ba7d-d363b2930e91",
      "DefaultValue": {
        "Type": "Float",
        "Value": "1"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "-10",
      "Max": "10",
      "Scale": "0.001",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Offset",
      "MetaInstanceID": "f466895c-48af-4ad8-bb78-7becc087a4f6",
      "DefaultValue": {
        "Type": "Float",
        "Value": "0"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "-10",
      "Max": "10",
      "Scale": "0.001",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Size",
      "MetaInstanceID": "666fcad0-3bb0-4638-81bc-517b6cbecec9",
      "DefaultValue": {
        "Type": "Float",
        "Value": "5"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "0",
      "Max": "100",
      "Scale": "0.1",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Number Of Samples",
      "MetaInstanceID": "a5804b35-c85b-4934-b075-eb16d069f6a5",
      "DefaultValue": {
        "Type": "Float",
        "Value": "9"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "0",
      "Max": "100",
      "Scale": "1",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Text",
      "MetaInstanceID": "a7f8ca89-513f-47f5-9875-19d5830767f0",
      "DefaultValue": {
        "Type": "Text",
        "Value": ""
      },
      "MetaID": "c522a66e-3260-4692-b3e3-79fd0361fa3d",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "-100000",
      "Max": "100000",
      "Scale": "0.1",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Intensity",
      "MetaInstanceID": "a423fa3b-c6db-4812-be91-afae933cffe0",
      "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": []
    }
  ],
  "Outputs": [
    {
      "Name": "Image",
      "MetaInstanceID": "a89fae29-af93-49c8-898b-9595d0505714",
      "MetaID": "9848060d-fd84-45b0-b658-d0d531c61dab"
    }
  ],
  "OperatorParts": [
    {
      "MetaInstanceID": "22fb902b-f601-445e-b91a-a50a73d85da2",
      "MetaID": "5a20234d-e781-4931-9cad-d7305b2aa667",
      "Name": "MaskedGaussBlurFunc",
      "Version": "65d37edc-8f2b-4089-95b2-6117ca72bd4e",
      "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",
        "using SharpDX.DXGI;",
        "",
        "namespace Framefield.Core.ID5a20234d_e781_4931_9cad_d7305b2aa667",
        "{",
        "    public class Class_MaskedGaussBlur : FXImageFunction",
        "    {",
        "        //>>> _inputids",
        "        private enum InputId",
        "        {",
        "            Code = 0,",
        "            Image = 1,",
        "            Glow = 2,",
        "            Offset = 3,",
        "            Size = 4,",
        "            NumberOfSamples = 5,",
        "            Text = 6,",
        "            Intensity = 7",
        "        }",
        "        //<<< _inputids",
        "",
        "        protected override bool NeedsDepth { get { return false; } }",
        "",
        "        protected override bool BuildRenderTarget()",
        "        {",
        "            var pass1ResourceChanged = ResourceManager.ValidateRenderTargetResource(ref _pass1RenderTargetResource, OperatorPart, D3DDevice.Device,",
        "                                                                                    (int)_usedViewport.Width, (int)_usedViewport.Height);",
        "            if (pass1ResourceChanged)",
        "            {",
        "                Utilities.DisposeObj(ref _pass1RenderTargetView);",
        "                _pass1RenderTargetView = new RenderTargetView(D3DDevice.Device, _pass1RenderTargetResource.Texture);",
        "            }",
        "",
        "            return base.BuildRenderTarget() || pass1ResourceChanged;",
        "        }",
        "",
        "        public override void Dispose()",
        "        {",
        "            ResourceManager.Dispose(_pass1RenderTargetResource);",
        "            Utilities.DisposeObj(ref _pass1RenderTargetView);",
        "            base.Dispose();",
        "        }",
        "",
        "        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 Image = inputs[(int)InputId.Image].Eval(context).Image; // Needs to be checked for null!",
        "                var Glow = inputs[(int)InputId.Glow].Eval(context).Value;",
        "                var Offset = inputs[(int)InputId.Offset].Eval(context).Value;",
        "                var Size = inputs[(int)InputId.Size].Eval(context).Value;",
        "                var NumberOfSamples = inputs[(int)InputId.NumberOfSamples].Eval(context).Value;",
        "                var Text = inputs[(int)InputId.Text].Eval(context).Text;",
        "                var Intensity = inputs[(int)InputId.Intensity].Eval(context).Image; // Needs to be checked for null!",
        "                //<<< _params",
        "                ",
        "                if(Image == null) {",
        "                    Logger.Info(this, \"No image connected to blur\");",
        "                    return;",
        "                }",
        "                ",
        "                if(Intensity == null) {",
        "                    Logger.Info(this, \"No image to define blur-strength\");",
        "                    return;",
        "                }",
        "",
        "                var D3DDevice = context.D3DDevice;",
        "                var cameraProjection = Matrix.OrthoLH(1, 1, -100, 100);",
        "                var worldToCamera = Matrix.LookAtLH(new Vector3(0, 0, -5), new Vector3(0, 0, 0), new Vector3(0, 1, 0));",
        "",
        "                _effect.GetVariableByName(\"widthToHeight\").AsScalar().Set((float)_usedViewport.Width/_usedViewport.Height);",
        "                _effect.GetVariableByName(\"Size\").AsScalar().Set(Size);",
        "                _effect.GetVariableByName(\"Glow\").AsScalar().Set(Glow);",
        "                _effect.GetVariableByName(\"Offset\").AsScalar().Set(Offset);",
        "                _effect.GetVariableByName(\"NumberOfSamples\").AsScalar().Set(NumberOfSamples);",
        "",
        "                //pass 1 (horizontal)",
        "                D3DDevice.ImmediateContext.ClearRenderTargetView(_pass1RenderTargetView, new SharpDX.Color4(0, 0, 0, 1));",
        "",
        "                var pass1SubContext = new OperatorPartContext(context);",
        "                pass1SubContext.DepthStencilView = null;",
        "                pass1SubContext.RenderTargetView = _pass1RenderTargetView;",
        "                pass1SubContext.Effect = _effect;",
        "                pass1SubContext.Renderer = _renderer;",
        "                pass1SubContext.BlendState = OperatorPartContext.DefaultRenderer.DisabledBlendState;",
        "                pass1SubContext.InputLayout = context.Renderer.ScreenQuadInputLayout;",
        "                pass1SubContext.CameraProjection = cameraProjection;",
        "                pass1SubContext.WorldToCamera = worldToCamera;",
        "                pass1SubContext.ObjectTWorld = Matrix.Identity;",
        "                ",
        "                using (var textureView = new ShaderResourceView(D3DDevice, Image))",
        "                using (var intensityTextureView = new ShaderResourceView(D3DDevice, Intensity))",
        "                {",
        "                    pass1SubContext.Texture0 = textureView;",
        "                    pass1SubContext.Effect.GetVariableByName(\"Intensity\").AsShaderResource().SetResource(intensityTextureView);",
        "",
        "                    //pass1SubContext.IntensityTexture = intensityTextureView;",
        "                    pass1SubContext.Renderer.SetupEffect(pass1SubContext);",
        "",
        "                    _effect.GetVariableByName(\"direction\").AsVector().Set(new Vector2(1, 0));",
        "",
        "                    pass1SubContext.Renderer.Render(pass1SubContext.Renderer._screenQuadMesh, pass1SubContext);",
        "                }",
        "",
        "                //pass 2 (vertical)",
        "                D3DDevice.ImmediateContext.ClearRenderTargetView(_renderTargetView, new SharpDX.Color4(0, 0, 0, 1));",
        "",
        "                var pass2SubContext = new OperatorPartContext(context);",
        "                pass2SubContext.DepthStencilView = null;",
        "                pass2SubContext.RenderTargetView = _renderTargetView;",
        "                pass2SubContext.Effect = _effect;",
        "                pass2SubContext.Renderer = _renderer;",
        "                pass2SubContext.BlendState = OperatorPartContext.DefaultRenderer.DisabledBlendState;",
        "                pass2SubContext.InputLayout = context.Renderer.ScreenQuadInputLayout;",
        "                pass2SubContext.CameraProjection = cameraProjection;",
        "                pass2SubContext.WorldToCamera = worldToCamera;",
        "                pass2SubContext.ObjectTWorld = Matrix.Identity;",
        "                ",
        "                using (var textureView = new ShaderResourceView(D3DDevice, _pass1RenderTargetResource.Texture))",
        "                using (var intensityTextureView = new ShaderResourceView(D3DDevice, Intensity))",
        "                {",
        "                    pass2SubContext.Texture0 = textureView;",
        "                    pass2SubContext.Effect.GetVariableByName(\"Intensity\").AsShaderResource().SetResource(intensityTextureView);",
        "",
        "                    pass2SubContext.Renderer.SetupEffect(pass2SubContext);",
        "",
        "                    _effect.GetVariableByName(\"direction\").AsVector().Set(new Vector2(0, 1));",
        "",
        "                    pass2SubContext.Renderer.Render(pass2SubContext.Renderer._screenQuadMesh, pass2SubContext);",
        "                }",
        "            });",
        "        }",
        "",
        "        Resource _pass1RenderTargetResource = null;",
        "        RenderTargetView _pass1RenderTargetView = null;",
        "    }",
        "}",
        "",
        "",
        ""
      ],
      "AdditionalAssemblies": []
    }
  ],
  "Operators": [],
  "Connections": [
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "22fb902b-f601-445e-b91a-a50a73d85da2",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "a89fae29-af93-49c8-898b-9595d0505714"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "d68358cf-ccd5-44f2-8d22-9b72bc3b89f1",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "22fb902b-f601-445e-b91a-a50a73d85da2"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "4ce48bbe-0c04-4d80-a3b2-565d31441218",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "22fb902b-f601-445e-b91a-a50a73d85da2"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "b4a14bce-d23d-4beb-ba7d-d363b2930e91",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "22fb902b-f601-445e-b91a-a50a73d85da2"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "f466895c-48af-4ad8-bb78-7becc087a4f6",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "22fb902b-f601-445e-b91a-a50a73d85da2"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "666fcad0-3bb0-4638-81bc-517b6cbecec9",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "22fb902b-f601-445e-b91a-a50a73d85da2"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "a5804b35-c85b-4934-b075-eb16d069f6a5",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "22fb902b-f601-445e-b91a-a50a73d85da2"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "a7f8ca89-513f-47f5-9875-19d5830767f0",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "22fb902b-f601-445e-b91a-a50a73d85da2"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "a423fa3b-c6db-4812-be91-afae933cffe0",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "22fb902b-f601-445e-b91a-a50a73d85da2"
    }
  ]
}