{
  "Name": "WireFrameShader",
  "_id": "cf898d05-a7b7-4ef5-89f9-e7051d032fce",
  "Namespace": "lib.geometry.material",
  "Description": "",
  "Inputs": [
    {
      "Name": "Code",
      "MetaInstanceID": "fc0a6c56-148d-420b-8b70-d3af53479331",
      "DefaultValue": {
        "Type": "Text",
        "Value": [
          "float4x4 objectToWorldMatrix;\r",
          "float4x4 worldToCameraMatrix;\r",
          "float4x4 cameraToObjectMatrix; // modelview inverse\r",
          "float4x4 projMatrix;\r",
          "float4x4 textureMatrix;\r",
          "\r",
          "float4 Color;\r",
          "\r",
          "struct PointLight\r",
          "{\r",
          "    float4 position;\r",
          "    float4 ambient;\r",
          "    float4 diffuse;\r",
          "    float4 specular;\r",
          "    float4 attenuation;\r",
          "};\r",
          "\r",
          "struct SpotLight\r",
          "{\r",
          "    float4 ambient;\r",
          "    float4 diffuse;\r",
          "    float4 specular;\r",
          "    float constantAttenuation;\r",
          "    float linearAttenuation;\r",
          "    float quadraticAttenuation;\r",
          "    float spotCutoff;\r",
          "    float spotCosCutoff;\r",
          "    float spotExponent;\r",
          "};\r",
          "\r",
          "cbuffer FogSettings\r",
          "{\r",
          "    float4 fogColor;\r",
          "    float fogStart;\r",
          "    float fogEnd;\r",
          "    float fogScale;\r",
          "}\r",
          "\r",
          "cbuffer MaterialBuffer\r",
          "{\r",
          "    float4 materialAmbient;\r",
          "    float4 materialDiffuse;\r",
          "    float4 materialSpecular;\r",
          "    float4 materialEmission;\r",
          "    float materialShininess;\r",
          "};\r",
          "\r",
          "\r",
          "cbuffer PointLightsBuffer\r",
          "{\r",
          "    int numPointLights;\r",
          "    PointLight pointLights[3];\r",
          "};\r",
          "\r",
          "\r",
          "Texture2D txDiffuse;\r",
          "\r",
          "SamplerState samLinear\r",
          "{\r",
          "    Filter = MIN_MAG_MIP_LINEAR;\r",
          "    AddressU = Wrap;\r",
          "    AddressV = Wrap;\r",
          "};\r",
          "\r",
          "\r",
          "struct VS_IN\r",
          "{\r",
          "    float4 pos : POSITION;\r",
          "    float3 normal : NORMAL;\r",
          "    float4 col : COLOR;\r",
          "    float2 texCoord : TEXCOORD0;\r",
          "    float3 tangent : TANGENT0;\r",
          "    float3 binormal : BINORMAL0;\r",
          "    \r",
          "};\r",
          "\r",
          "\r",
          "struct PS_IN\r",
          "{\r",
          "    float4 pos : SV_POSITION;\r",
          "    float4 posInWorld  : WORLD_POS;\r",
          "    float3 normal : NORMAL;   \r",
          "    float2 texCoord : TEXCOORD0;\r",
          "    float4 vertexColor : COLOR;\r",
          "    float3 fragPosToCamPos : POS;\r",
          "    float fogFragCoord : FALLO;\r",
          "};\r",
          "\r",
          "\r",
          "float mod(float a, float b)\r",
          "{\r",
          "    return a - b*floor(a/b);\r",
          "}\r",
          "\r",
          "float3 mod(float3 a, float b)\r",
          "{\r",
          "    return a - b*floor(a/b);\r",
          "}\r",
          "\r",
          "\r",
          "\r",
          "PS_IN VS( VS_IN input )",
          "{",
          "    PS_IN output = (PS_IN)0;",
          "",
          "    output.posInWorld = mul(input.pos, objectToWorldMatrix);",
          "    output.pos = mul(output.posInWorld, worldToCameraMatrix);",
          "    output.normal = mul(input.normal, (float3x3)objectToWorldMatrix);",
          "    output.fogFragCoord = abs(output.pos.z / input.pos.w);",
          "    output.pos = mul(output.pos, projMatrix);",
          "    output.texCoord = mul(float4(input.texCoord, 0, 1), textureMatrix).xy;",
          "    output.fragPosToCamPos = normalize(mul(cameraToObjectMatrix[3], objectToWorldMatrix) - output.posInWorld).rgb;",
          "    output.vertexColor = input.col;",
          "      ",
          "    return output;",
          "}",
          "\r",
          "//>>> PS\r",
          "float4 PS( PS_IN input ) : SV_Target\r",
          "{\r",
          "    float2 newTexCoords = input.texCoord;\r",
          "    float3 norm = normalize(input.normal);\r",
          "    float4 baseColor = txDiffuse.Sample(samLinear, newTexCoords);\r",
          "\r",
          "    float4 color = float4(1,1,1, 0.7) * Color;\r",
          "    color *= baseColor;\r",
          "    color += materialEmission;\r",
          "\r",
          "    float fog = (fogEnd - input.fogFragCoord) * fogScale;\r",
          "    fog = clamp(fog, 0.0, 1.0);    \r",
          "    return float4(lerp(fogColor.rgb, color.rgb, fog), materialDiffuse.a * baseColor.a);\r",
          "}\r",
          "\r",
          "float4 PS_BACK( PS_IN input ) : SV_Target\r",
          "{\r",
          "    float2 newTexCoords = input.texCoord;\r",
          "    float3 norm = normalize(input.normal);\r",
          "    float4 baseColor = txDiffuse.Sample(samLinear, newTexCoords);\r",
          "\r",
          "    float4 color = float4(0.2,0.2, 0.2, 0.3) * Color;\r",
          "    color += materialEmission*baseColor;\r",
          "\r",
          "    float fog = (fogEnd - input.fogFragCoord) * fogScale;\r",
          "    fog = clamp(fog, 0.0, 1.0);    \r",
          "    return float4(lerp(fogColor.rgb, color.rgb, fog), 0.4);\r",
          "}\r",
          "//<<< PS\r",
          "\r",
          "\r",
          "\r",
          "RasterizerState rsWireframeFront { \r",
          "    FillMode = WireFrame; \r",
          "    CullMode = BACK;\r",
          "    DepthBiasClamp=10;\r",
          "    SlopeScaledDepthBias=-1;\r",
          "    DepthBias = -20;\r",
          "    AntialiasedLineEnable=true;\r",
          "};\r",
          "\r",
          "RasterizerState rsWireframeBack { \r",
          "    FillMode = WireFrame; \r",
          "    CullMode = FRONT;\r",
          "    DepthBiasClamp=10;\r",
          "    SlopeScaledDepthBias=-1;\r",
          "    DepthBias = -20;\r",
          "    AntialiasedLineEnable=true;\r",
          "};\r",
          "\r",
          "\r",
          "DepthStencilState EnableDepth\r",
          "{\r",
          "    DepthEnable = TRUE;\r",
          "    DepthWriteMask = ALL;\r",
          "    DepthFunc = LESS_EQUAL;\r",
          "};\r",
          "\r",
          "\r",
          "DepthStencilState DisableDepth\r",
          "{\r",
          "    DepthEnable = FALSE;\r",
          "    DepthWriteMask = ALL;\r",
          "    DepthFunc = LESS_EQUAL;\r",
          "};\r",
          "\r",
          "technique10 Render\r",
          "{\r",
          "    pass P1\r",
          "    {\r",
          "        SetGeometryShader( 0 );\r",
          "        SetVertexShader( CompileShader( vs_4_0, VS() ) );\r",
          "        SetPixelShader( CompileShader( ps_4_0, PS_BACK() ) );\r",
          "        SetRasterizerState( rsWireframeBack );\r",
          "        SetDepthStencilState( DisableDepth,0 );\r",
          "        \r",
          "    }\r",
          "    pass P0\r",
          "    {\r",
          "        SetGeometryShader( 0 );\r",
          "        SetVertexShader( CompileShader( vs_4_0, VS() ) );\r",
          "        SetPixelShader( CompileShader( ps_4_0, PS() ) );\r",
          "        SetRasterizerState( rsWireframeFront );\r",
          "        SetDepthStencilState( EnableDepth, 0 );\r",
          "            \r",
          "    }\r",
          "    \r",
          "\r",
          "    \r",
          "    \r",
          "} "
        ]
      },
      "MetaID": "c522a66e-3260-4692-b3e3-79fd0361fa3d",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "-100000",
      "Max": "100000",
      "Scale": "0.1",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Scene",
      "MetaInstanceID": "f4025d39-ef71-4a0a-aa6b-567a7072d3de",
      "DefaultValue": {
        "Type": "Scene",
        "Value": "Framefield.Core.Scene"
      },
      "MetaID": "79122951-7bc4-4c68-b085-866eab828248",
      "IsMultiInput": "False",
      "Relevance": "Required",
      "Description": "",
      "Min": "-100000",
      "Max": "100000",
      "Scale": "0.1",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Color.R",
      "MetaInstanceID": "71f597fc-2dfd-4895-b17f-2ba5212d5280",
      "DefaultValue": {
        "Type": "Float",
        "Value": "1"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "0",
      "Max": "1",
      "Scale": "0.1",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Color.G",
      "MetaInstanceID": "ad2e9231-3fc9-4b13-b1c1-1653621dc1f4",
      "DefaultValue": {
        "Type": "Float",
        "Value": "1"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "0",
      "Max": "1",
      "Scale": "0.1",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Color.B",
      "MetaInstanceID": "56753b4c-75cd-405e-8d83-43436d617b2b",
      "DefaultValue": {
        "Type": "Float",
        "Value": "1"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "0",
      "Max": "1",
      "Scale": "0.1",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Color.A",
      "MetaInstanceID": "8d8c0477-9b9f-4efe-bb19-d0492d6edbf1",
      "DefaultValue": {
        "Type": "Float",
        "Value": "1"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "0",
      "Max": "1",
      "Scale": "0.1",
      "ScaleType": "Linear",
      "EnumValues": []
    }
  ],
  "Outputs": [
    {
      "Name": "Scene",
      "MetaInstanceID": "1a255bcf-2bc4-4a1d-a5e4-fc5a7292f920",
      "MetaID": "79122951-7bc4-4c68-b085-866eab828248"
    }
  ],
  "OperatorParts": [
    {
      "MetaInstanceID": "52b92d6e-fed0-46b0-a036-156b090f468c",
      "MetaID": "9295eb8f-37da-4d4a-935f-86a7f8cb315b",
      "Name": "WireFrameShaderFunc",
      "Version": "b90bbce1-9157-4999-b123-6231c2fa0bf9",
      "Type": "Float",
      "IsMultiInput": "True",
      "Script": [
        "using System;",
        "using System.Collections.Generic;",
        "using Framefield.Core.Rendering;",
        "using SharpDX;",
        "",
        "namespace Framefield.Core.ID9295eb8f_37da_4d4a_935f_86a7f8cb315b",
        "{",
        "",
        "    public class Class_WireFrameShader : FXSourceCodeFunction",
        "    {",
        "        // >>> _inputids",
        "        private enum InputId",
        "        {",
        "            Code = 0,",
        "            Scene = 1,",
        "            ColorR = 2,",
        "            ColorG = 3,",
        "            ColorB = 4,",
        "            ColorA = 5",
        "        }",
        "        // <<< _inputids",
        "    ",
        "    ",
        "        #region Renderer",
        "        public class Renderer : BaseRenderer",
        "        {",
        "            public override void SetupEffect(OperatorPartContext context) {",
        "                base.SetupEffect(context);",
        "",
        "                try {",
        "                    SetupMaterialConstBuffer(context);",
        "                    SetupFogSettingsConstBuffer(context);",
        "                    SetupPointLightsConstBuffer(context);",
        "                }",
        "                catch (Exception e) {",
        "                    Logger.Error(ParentFunc, \"Error building constant buffer: {0} - Source: {1}\", e.Message, e.Source);",
        "                }",
        "            }",
        "            public OperatorPart.Function ParentFunc {get;set;}",
        "        }",
        "        #endregion",
        "",
        "        public Class_WireFrameShader() {",
        "            _renderer = new Renderer(){ParentFunc = this};",
        "        }",
        "",
        "        public override void Dispose() {",
        "            Utilities.DisposeObj(ref _renderer);",
        "            base.Dispose();",
        "        }",
        "",
        "        bool _firstEval = true;",
        "        public override OperatorPartContext Eval(OperatorPartContext context, List<OperatorPart> inputs, int outputIdx) {",
        "        ",
        "        ",
        "            if (_firstEval) {",
        "                for (int i = 0; i < NumCodes(); ++i)",
        "                    Compile(i);",
        "                _firstEval = false;",
        "                Changed = true;",
        "            }",
        "",
        "            //var Scene = inputs[1];",
        "            ",
        "            //>>> _params",
        "            var Code = inputs[(int)InputId.Code].Eval(context).Text;",
        "            var Scene = inputs[(int)InputId.Scene];",
        "            var ColorR = inputs[(int)InputId.ColorR].Eval(context).Value;",
        "            var ColorG = inputs[(int)InputId.ColorG].Eval(context).Value;",
        "            var ColorB = inputs[(int)InputId.ColorB].Eval(context).Value;",
        "            var ColorA = inputs[(int)InputId.ColorA].Eval(context).Value;",
        "            var Color = new Color4(ColorR, ColorG, ColorB, ColorA);",
        "            //<<< _params",
        "",
        "            using (new PropertyStasher<OperatorPartContext>(context, \"Effect\", \"Renderer\")) {",
        "            ",
        "                //_effect.GetVariableByName(\"Base\").AsScalar().Set(Base);",
        "                SetColor(\"Color\",Color);",
        "                context.Effect = _effect;",
        "                context.Renderer = _renderer;",
        "",
        "                Scene.Eval(context);",
        "            }",
        "",
        "            return context;",
        "        }",
        "",
        "        Renderer _renderer;",
        "    }",
        "}",
        "",
        "",
        ""
      ],
      "AdditionalAssemblies": []
    }
  ],
  "Operators": [],
  "Connections": [
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "52b92d6e-fed0-46b0-a036-156b090f468c",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "1a255bcf-2bc4-4a1d-a5e4-fc5a7292f920"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "fc0a6c56-148d-420b-8b70-d3af53479331",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "52b92d6e-fed0-46b0-a036-156b090f468c"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "f4025d39-ef71-4a0a-aa6b-567a7072d3de",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "52b92d6e-fed0-46b0-a036-156b090f468c"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "71f597fc-2dfd-4895-b17f-2ba5212d5280",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "52b92d6e-fed0-46b0-a036-156b090f468c"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "ad2e9231-3fc9-4b13-b1c1-1653621dc1f4",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "52b92d6e-fed0-46b0-a036-156b090f468c"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "56753b4c-75cd-405e-8d83-43436d617b2b",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "52b92d6e-fed0-46b0-a036-156b090f468c"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "8d8c0477-9b9f-4efe-bb19-d0492d6edbf1",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "52b92d6e-fed0-46b0-a036-156b090f468c"
    }
  ]
}