{
  "Name": "Rectangle",
  "_id": "161f6915-5798-40e0-a99b-47e6205448f4",
  "Namespace": "lib.3d.generate",
  "Description": "Defines a rectangle in the x-y plane with specific extends and color.\r\nalso known as \"Plan  e\", \"Rect\" or \"Quad\".\r\n\r\nThis operators is exensively used to build more complex objects like [TextRect].",
  "Inputs": [
    {
      "Name": "Color.R",
      "MetaInstanceID": "2bae21c1-64b4-4dd0-8913-e29a9e5eaa30",
      "DefaultValue": {
        "Type": "Float",
        "Value": "1"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "0",
      "Max": "1",
      "Scale": "0.01",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Color.G",
      "MetaInstanceID": "cc353c8c-8d76-4302-98d0-d5aa2079ad8c",
      "DefaultValue": {
        "Type": "Float",
        "Value": "1"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "0",
      "Max": "1",
      "Scale": "0.01",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Color.B",
      "MetaInstanceID": "9b80fa7b-282e-4c85-8917-f36740916045",
      "DefaultValue": {
        "Type": "Float",
        "Value": "1"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "0",
      "Max": "1",
      "Scale": "0.01",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Color.A",
      "MetaInstanceID": "7403f401-0fdb-440a-aa49-cec4156a2c40",
      "DefaultValue": {
        "Type": "Float",
        "Value": "1"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "0",
      "Max": "1",
      "Scale": "0.01",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Size.Width",
      "MetaInstanceID": "3b4b22e7-c78c-41e9-b1c7-2c6ce24995cd",
      "DefaultValue": {
        "Type": "Float",
        "Value": "1"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Relevant",
      "Description": "",
      "Min": "-100000",
      "Max": "100000",
      "Scale": "0.1",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Size.Height",
      "MetaInstanceID": "53d3495a-8c76-4be2-aaf7-7c852ec1b17d",
      "DefaultValue": {
        "Type": "Float",
        "Value": "1"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Relevant",
      "Description": "",
      "Min": "-100000",
      "Max": "100000",
      "Scale": "0.1",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Tesselate.width",
      "MetaInstanceID": "3e4b420e-3f3e-464c-9c58-4b49e8b14592",
      "DefaultValue": {
        "Type": "Float",
        "Value": "2"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "2",
      "Max": "1000",
      "Scale": "1",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Tesselate.height",
      "MetaInstanceID": "d957543c-efcc-4d9a-8e0e-fcbd23a4dad3",
      "DefaultValue": {
        "Type": "Float",
        "Value": "2"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "2",
      "Max": "1000",
      "Scale": "1",
      "ScaleType": "Linear",
      "EnumValues": []
    }
  ],
  "Outputs": [
    {
      "Name": "Out",
      "MetaInstanceID": "a8a64dec-f762-4b73-a45a-c09accecb1b5",
      "MetaID": "79122951-7bc4-4c68-b085-866eab828248"
    }
  ],
  "OperatorParts": [
    {
      "MetaInstanceID": "5c8b843f-dd1c-44dc-961d-3cb6501100c3",
      "MetaID": "ba9ee4dd-6f96-40c9-bb8a-0f383d5cf166",
      "Name": "RectangleFunc",
      "Version": "09122d01-30fa-44aa-8ffd-5260a37c522f",
      "Type": "Scene",
      "IsMultiInput": "True",
      "Script": [
        "using System;",
        "using System.Collections.Generic;",
        "using SharpDX;",
        "using SharpDX.Direct3D11;",
        "using Buffer = SharpDX.Direct3D11.Buffer;",
        "",
        "namespace Framefield.Core.IDba9ee4dd_6f96_40c9_bb8a_0f383d5cf166",
        "{",
        "    public class Class_Rectangle : OperatorPart.Function,",
        "                                   Framefield.Core.OperatorPartTraits.IMeshSupplier",
        "    {",
        "        // IMeshSupplier",
        "        public void AddMeshesTo(ICollection<Mesh> meshes)",
        "        {",
        "            UpdateMesh(new OperatorPartContext(), OperatorPart.Connections);",
        "            meshes.Add(_rectangleMesh);",
        "        }",
        "",
        "        public override void Dispose()",
        "        {",
        "            Utilities.DisposeObj(ref _rectangleMesh);",
        "        }",
        "",
        "        public override OperatorPartContext Eval(OperatorPartContext context, List<OperatorPart> inputs, int outputIdx)",
        "        {",
        "            try",
        "            {",
        "                UpdateMesh(context, inputs);",
        "",
        "                context.Renderer.SetupEffect(context);",
        "                context.Renderer.Render(_rectangleMesh, context);",
        "            }",
        "            catch (Exception)",
        "            {",
        "                Logger.Error(this,\"error\");",
        "            }",
        "",
        "            return context;",
        "        }",
        "",
        "        private void UpdateMesh(OperatorPartContext context, IList<OperatorPart> inputs)",
        "        {",
        "            if (_rectangleMesh != null && !Changed)",
        "                return;",
        "",
        "            // params >>>",
        "            var height = inputs[0].Eval(context).Value;",
        "            var width = inputs[1].Eval(context).Value;",
        "            var ColorA = inputs[2].Eval(context).Value;",
        "            var ColorB = inputs[3].Eval(context).Value;",
        "            var ColorG = inputs[4].Eval(context).Value;",
        "            var ColorR = inputs[5].Eval(context).Value;",
        "            var tesselateWidth = inputs[6].Eval(context).Value;",
        "            var tesselateHeight = inputs[7].Eval(context).Value;",
        "            // <<< params",
        "",
        "            // Function>>> ",
        "            var normal = new Vector3(0.0f, 0.0f, -1.0f);",
        "            var color = new Vector4(ColorR, ColorG, ColorB, ColorA);",
        "            var tangent = new Vector3(1.0f, 0.0f, 0.0f);",
        "            var binormal = new Vector3(0.0f, -1.0f, 0.0f);",
        "",
        "            var inputElements = new InputElement[]",
        "                                    {",
        "                                        new InputElement(\"POSITION\", 0, SharpDX.DXGI.Format.R32G32B32A32_Float, 0, 0),",
        "                                        new InputElement(\"NORMAL\", 0, SharpDX.DXGI.Format.R32G32B32_Float, 16, 0),",
        "                                        new InputElement(\"COLOR\", 0, SharpDX.DXGI.Format.R32G32B32A32_Float, 28, 0),",
        "                                        new InputElement(\"TEXCOORD\", 0, SharpDX.DXGI.Format.R32G32_Float, 44, 0),",
        "                                        new InputElement(\"TANGENT\", 0, SharpDX.DXGI.Format.R32G32B32_Float, 52, 0),",
        "                                        new InputElement(\"BINORMAL\", 0, SharpDX.DXGI.Format.R32G32B32_Float, 64, 0)",
        "                                    };",
        "",
        "            int tessX = (int) tesselateWidth;",
        "            int tessZ = (int) tesselateHeight;",
        "            int numQuads = (tessX - 1)*(tessZ - 1);",
        "",
        "            const int attributesSize = 76;",
        "            int numTriangles = numQuads*2;",
        "            int streamSize = numTriangles*3*attributesSize;",
        "",
        "            if (_rectangleMesh == null || streamSize != _rectangleMesh.NumTriangles*3*_rectangleMesh.AttributesSize)",
        "            {",
        "                Utilities.DisposeObj(ref _rectangleMesh);",
        "                using (var stream = new DataStream(streamSize, true, true))",
        "                {",
        "                    var vertices = new Buffer(context.D3DDevice, stream, new BufferDescription",
        "                                                                             {",
        "                                                                                 BindFlags = BindFlags.VertexBuffer,",
        "                                                                                 CpuAccessFlags = CpuAccessFlags.Write,",
        "                                                                                 OptionFlags = ResourceOptionFlags.None,",
        "                                                                                 SizeInBytes = streamSize,",
        "                                                                                 Usage = ResourceUsage.Dynamic",
        "                                                                             });",
        "                    _rectangleMesh = new Mesh",
        "                                         {",
        "                                             InputElements = inputElements,",
        "                                             Vertices = vertices,",
        "                                             NumTriangles = numTriangles,",
        "                                             AttributesSize = attributesSize",
        "                                         };",
        "                }",
        "            }",
        "            DataStream vertexStream;",
        "            context.D3DDevice.ImmediateContext.MapSubresource(_rectangleMesh.Vertices, MapMode.WriteDiscard, MapFlags.None, out vertexStream);",
        "            using (vertexStream)",
        "            {",
        "                vertexStream.Position = 0;",
        "",
        "                float startX = -width/2.0f;",
        "                float startZ = -height/2.0f;",
        "",
        "                for (int z = 0; z < (tessZ - 1); ++z)",
        "                {",
        "                    float normalizedBottom = (float) z/(float) (tessZ - 1);",
        "                    float bottom = startZ + (height*normalizedBottom);",
        "                    float normalizedTop = (float) (z + 1)/(float) (tessZ - 1);",
        "                    float top = startZ + (height*normalizedTop);",
        "",
        "                    for (int x = 0; x < (tessX - 1); ++x)",
        "                    {",
        "                        float normalizedLeft = (float) (x)/(float) (tessX - 1);",
        "                        float left = startX + (width*normalizedLeft);",
        "                        float normalizedRight = (float) (x + 1)/(float) (tessX - 1);",
        "                        float right = startX + (width*normalizedRight);",
        "",
        "                        // tri 1 vert 1",
        "                        vertexStream.Write(new Vector4(right, top, 0, 1));",
        "                        vertexStream.Write(normal);",
        "                        vertexStream.Write(color);",
        "                        vertexStream.Write(new Vector2(normalizedRight, 1.0f - normalizedTop));",
        "                        vertexStream.Write(tangent);",
        "                        vertexStream.Write(binormal);",
        "",
        "                        // tri 1 vert 2",
        "                        vertexStream.Write(new Vector4(right, bottom, 0, 1));",
        "                        vertexStream.Write(normal);",
        "                        vertexStream.Write(color);",
        "                        vertexStream.Write(new Vector2(normalizedRight, 1.0f - normalizedBottom));",
        "                        vertexStream.Write(tangent);",
        "                        vertexStream.Write(binormal);",
        "",
        "                        // tri 1 vert 3",
        "                        vertexStream.Write(new Vector4(left, bottom, 0, 1));",
        "                        vertexStream.Write(normal);",
        "                        vertexStream.Write(color);",
        "                        vertexStream.Write(new Vector2(normalizedLeft, 1.0f - normalizedBottom));",
        "                        vertexStream.Write(tangent);",
        "                        vertexStream.Write(binormal);",
        "",
        "                        // tri 2 vert 1",
        "                        vertexStream.Write(new Vector4(left, bottom, 0, 1));",
        "                        vertexStream.Write(normal);",
        "                        vertexStream.Write(color);",
        "                        vertexStream.Write(new Vector2(normalizedLeft, 1.0f - normalizedBottom));",
        "                        vertexStream.Write(tangent);",
        "                        vertexStream.Write(binormal);",
        "",
        "                        // tri 2 vert 2",
        "                        vertexStream.Write(new Vector4(left, top, 0, 1));",
        "                        vertexStream.Write(normal);",
        "                        vertexStream.Write(color);",
        "                        vertexStream.Write(new Vector2(normalizedLeft, 1.0f - normalizedTop));",
        "                        vertexStream.Write(tangent);",
        "                        vertexStream.Write(binormal);",
        "",
        "                        // tri 2 vert 3",
        "                        vertexStream.Write(new Vector4(right, top, 0, 1));",
        "                        vertexStream.Write(normal);",
        "                        vertexStream.Write(color);",
        "                        vertexStream.Write(new Vector2(normalizedRight, 1.0f - normalizedTop));",
        "                        vertexStream.Write(tangent);",
        "                        vertexStream.Write(binormal);",
        "                    }",
        "                }",
        "            }",
        "            context.D3DDevice.ImmediateContext.UnmapSubresource(_rectangleMesh.Vertices, 0);",
        "            // <<< Function",
        "",
        "            Changed = false;",
        "        }",
        "",
        "        private Mesh _rectangleMesh;",
        "    }",
        "}"
      ],
      "AdditionalAssemblies": []
    }
  ],
  "Operators": [],
  "Connections": [
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "5c8b843f-dd1c-44dc-961d-3cb6501100c3",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "a8a64dec-f762-4b73-a45a-c09accecb1b5"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "53d3495a-8c76-4be2-aaf7-7c852ec1b17d",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "5c8b843f-dd1c-44dc-961d-3cb6501100c3"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "3b4b22e7-c78c-41e9-b1c7-2c6ce24995cd",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "5c8b843f-dd1c-44dc-961d-3cb6501100c3"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "7403f401-0fdb-440a-aa49-cec4156a2c40",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "5c8b843f-dd1c-44dc-961d-3cb6501100c3"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "9b80fa7b-282e-4c85-8917-f36740916045",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "5c8b843f-dd1c-44dc-961d-3cb6501100c3"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "cc353c8c-8d76-4302-98d0-d5aa2079ad8c",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "5c8b843f-dd1c-44dc-961d-3cb6501100c3"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "2bae21c1-64b4-4dd0-8913-e29a9e5eaa30",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "5c8b843f-dd1c-44dc-961d-3cb6501100c3"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "3e4b420e-3f3e-464c-9c58-4b49e8b14592",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "5c8b843f-dd1c-44dc-961d-3cb6501100c3"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "d957543c-efcc-4d9a-8e0e-fcbd23a4dad3",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "5c8b843f-dd1c-44dc-961d-3cb6501100c3"
    }
  ]
}