{
  "Name": "Grid",
  "_id": "8c26f100-347c-458e-a04f-bb415946f67c",
  "Namespace": "lib.3d.generate",
  "Description": "Generates a grid geometry",
  "Inputs": [
    {
      "Name": "Color.R",
      "MetaInstanceID": "2304152f-0705-41fd-894a-8e7ac4605b4b",
      "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": "77434885-73f7-4b61-92ab-5b6f894ef99c",
      "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": "9301204d-564f-4865-8f6f-f3e73b528e54",
      "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": "6f39bf20-3f4d-4d75-8b85-61f02d5d2c4b",
      "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": "e704ddb0-2e0c-403e-bd50-72b9c62c7bc7",
      "DefaultValue": {
        "Type": "Float",
        "Value": "1"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Required",
      "Description": "",
      "Min": "-100000",
      "Max": "100000",
      "Scale": "0.1",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Size.Height",
      "MetaInstanceID": "a5725248-88f6-4a59-8753-f8f7bebb9411",
      "DefaultValue": {
        "Type": "Float",
        "Value": "1"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Required",
      "Description": "",
      "Min": "-100000",
      "Max": "100000",
      "Scale": "0.1",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Tesselate.width",
      "MetaInstanceID": "911b2d7b-fc0c-4a73-b8ec-f0cd610b122a",
      "DefaultValue": {
        "Type": "Float",
        "Value": "10"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "2",
      "Max": "1000",
      "Scale": "1",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Tesselate.height",
      "MetaInstanceID": "01c1b6e2-4b6d-4eed-bd95-1694783f18e3",
      "DefaultValue": {
        "Type": "Float",
        "Value": "10"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "2",
      "Max": "1000",
      "Scale": "1",
      "ScaleType": "Linear",
      "EnumValues": []
    }
  ],
  "Outputs": [
    {
      "Name": "Out",
      "MetaInstanceID": "7ce6d305-b9cb-40b7-b1e2-b49d496b304d",
      "MetaID": "79122951-7bc4-4c68-b085-866eab828248"
    }
  ],
  "OperatorParts": [
    {
      "MetaInstanceID": "67649f75-63bd-429d-bf85-320922e6d1e4",
      "MetaID": "5c9afeab-5182-4521-bf37-28a351b104e8",
      "Name": "GridFunc",
      "Version": "5d5ce074-a11c-4b10-af98-78bd7680cd60",
      "Type": "Scene",
      "IsMultiInput": "True",
      "Script": [
        "using System;",
        "using System.Collections.Generic;",
        "using SharpDX;",
        "using SharpDX.Direct3D11;",
        "using Buffer = SharpDX.Direct3D11.Buffer;",
        "",
        "namespace Framefield.Core.ID5c9afeab_5182_4521_bf37_28a351b104e8",
        "{",
        "    public class Class_Grid : 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);",
        "        }",
        "",
        "        private RasterizerState _rasterizerState = null;",
        "        ",
        "        public override OperatorPartContext Eval(OperatorPartContext context, List<OperatorPart> inputs, int outputIdx)",
        "        {",
        "            try",
        "            {",
        "                UpdateMesh(context, inputs);",
        "",
        "                var rasterizerDescription = new RasterizerStateDescription();",
        "                rasterizerDescription.FillMode = (FillMode) 2; // Wireframe",
        "                rasterizerDescription.CullMode = (CullMode) 1; // None",
        "                rasterizerDescription.IsMultisampleEnabled =false;",
        "",
        "                Utilities.DisposeObj(ref _rasterizerState);",
        "                _rasterizerState = new RasterizerState(context.D3DDevice, rasterizerDescription);",
        "                ",
        "                var prevRasterizerState = context.RasterizerState;",
        "                context.RasterizerState = _rasterizerState;",
        "               ",
        "                context.Renderer.SetupEffect(context);",
        "                context.Renderer.Render(_rectangleMesh, context);",
        "                context.RasterizerState = prevRasterizerState;",
        "            }",
        "            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 tessY = (int) tesselateHeight;",
        "            int numTriangles = (tessX+1) + (tessY+1);",
        "",
        "            const int attributesSize = 76;",
        "            //int numTriangles = numQuads*1;",
        "            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 ); ++z)",
        "                //{",
        "                    //float normalizedBottom = (float) z/(float) (tessZ-1);",
        "                    //float bottom = startZ + (height*normalizedBottom);",
        "                    //float normalizedTop = (float) (z +1)/(float) (tessZ );",
        "                    //float top = startZ + (height*normalizedTop);",
        "",
        "                    float bottom= -height/2;",
        "                    float top=    height/2;",
        "                    float left = -width/2;",
        "                    float right = width/2;",
        "                    ",
        "                    for (int cx = 0; cx <= tessX; ++cx)",
        "                    {                    ",
        "                        float normalizedRight = (float)cx/tessX;",
        "                        float x= left+ width* normalizedRight;",
        "",
        "                        // tri 1 vert 1",
        "                        vertexStream.Write(new Vector4(x,top, 0, 1));",
        "                        vertexStream.Write(normal);",
        "                        vertexStream.Write(color);",
        "                        vertexStream.Write(new Vector2(normalizedRight, 1.0f ));",
        "                        vertexStream.Write(tangent);",
        "                        vertexStream.Write(binormal);",
        "",
        "                        // tri 1 vert 2",
        "                        vertexStream.Write(new Vector4(x, bottom, 0, 1));",
        "                        vertexStream.Write(normal);",
        "                        vertexStream.Write(color);",
        "                        vertexStream.Write(new Vector2(normalizedRight, 1.0f));",
        "                        vertexStream.Write(tangent);",
        "                        vertexStream.Write(binormal);",
        "",
        "                        // tri 1 vert 3",
        "                        vertexStream.Write(new Vector4(x, bottom, 0, 1));",
        "                        vertexStream.Write(normal);",
        "                        vertexStream.Write(color);",
        "                        vertexStream.Write(new Vector2(normalizedRight, 1.0f));",
        "                        vertexStream.Write(tangent);",
        "                        vertexStream.Write(binormal);",
        "                    }",
        "                    ",
        "                    for (int cy = 0; cy <= tessY  ; ++cy)",
        "                    {          ",
        "                        float normalizedBottom = (float)cy/tessY;",
        "                        float y= bottom + height* normalizedBottom;",
        "",
        "",
        "                        // tri 2 vert 1",
        "                        vertexStream.Write(new Vector4(left, y, 0, 1));",
        "                        vertexStream.Write(normal);",
        "                        vertexStream.Write(color);",
        "                        vertexStream.Write(new Vector2(0, 1.0f));",
        "                        vertexStream.Write(tangent);",
        "                        vertexStream.Write(binormal);",
        "",
        "                        // tri 2 vert 2",
        "                        vertexStream.Write(new Vector4(left, y, 0, 1));",
        "                        vertexStream.Write(normal);",
        "                        vertexStream.Write(color);",
        "                        vertexStream.Write(new Vector2(0, 1.0f - normalizedBottom));",
        "                        vertexStream.Write(tangent);",
        "                        vertexStream.Write(binormal);",
        "",
        "                        // tri 2 vert 3",
        "                        vertexStream.Write(new Vector4(right, y, 0, 1));",
        "                        vertexStream.Write(normal);",
        "                        vertexStream.Write(color);",
        "                        vertexStream.Write(new Vector2(0, 1.0f - normalizedBottom));",
        "                        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": "67649f75-63bd-429d-bf85-320922e6d1e4",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "7ce6d305-b9cb-40b7-b1e2-b49d496b304d"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "a5725248-88f6-4a59-8753-f8f7bebb9411",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "67649f75-63bd-429d-bf85-320922e6d1e4"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "e704ddb0-2e0c-403e-bd50-72b9c62c7bc7",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "67649f75-63bd-429d-bf85-320922e6d1e4"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "6f39bf20-3f4d-4d75-8b85-61f02d5d2c4b",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "67649f75-63bd-429d-bf85-320922e6d1e4"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "9301204d-564f-4865-8f6f-f3e73b528e54",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "67649f75-63bd-429d-bf85-320922e6d1e4"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "77434885-73f7-4b61-92ab-5b6f894ef99c",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "67649f75-63bd-429d-bf85-320922e6d1e4"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "2304152f-0705-41fd-894a-8e7ac4605b4b",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "67649f75-63bd-429d-bf85-320922e6d1e4"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "911b2d7b-fc0c-4a73-b8ec-f0cd610b122a",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "67649f75-63bd-429d-bf85-320922e6d1e4"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "01c1b6e2-4b6d-4eed-bd95-1694783f18e3",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "67649f75-63bd-429d-bf85-320922e6d1e4"
    }
  ]
}