{
  "Name": "PhongShader",
  "_id": "274c7da9-c728-4a9d-b5a3-dadb06e2715b",
  "Namespace": "lib.3d.material",
  "Description": "",
  "Inputs": [
    {
      "Name": "Code",
      "MetaInstanceID": "5f563b77-c865-4d13-bbdd-41d599c853ab",
      "DefaultValue": {
        "Type": "Text",
        "Value": [
          "float4x4 objectToWorldMatrix;\r",
          "float4x4 worldToCameraMatrix;\r",
          "float4x4 cameraToObjectMatrix; // modelview inverse\r",
          "float4x4 projMatrix;\r",
          "float4x4 textureMatrix;\r",
          "\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",
          "\r",
          "StructuredBuffer<PointLight> pointLights;\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",
          "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",
          "//>>>> VS2\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);",
          "    output.vertexColor = input.col;",
          "      ",
          "    return output;",
          "}",
          "//<<< VS\r",
          "\r",
          "float4 calcLightSource(float3 fragPosInWorld, int lightIdx, float3 cameraVector, float3 norm, float4 baseColor)\r",
          "{\r",
          "    float3 lightVector = pointLights[lightIdx].position - fragPosInWorld;\r",
          "    float dist = length(lightVector);\r",
          "    lightVector = normalize(lightVector);\r",
          "    float nxDir = max(0.0, dot(norm, lightVector));\r",
          "    float4 diffuse = pointLights[lightIdx].diffuse * nxDir;\r",
          "    float specularPower = 0.0;\r",
          "    if (nxDir > 0.0)\r",
          "    {\r",
          "        float3 r = reflect(-lightVector, norm);\r",
          "        float rl = max(0.0, dot(r, cameraVector));\r",
          "        specularPower = pow(rl, materialShininess);\r",
          "    }\r",
          "\r",
          "    float attenuation = 1.0 / (pointLights[lightIdx].attenuation.x +\r",
          "                               pointLights[lightIdx].attenuation.y * dist +\r",
          "                               pointLights[lightIdx].attenuation.z * dist * dist);\r",
          "    float4 color = materialAmbient * pointLights[lightIdx].ambient * attenuation +\r",
          "                   materialDiffuse * (diffuse * baseColor * attenuation) +\r",
          "                   materialSpecular * pointLights[lightIdx].specular * specularPower * attenuation;\r",
          "\r",
          "    return color;\r",
          "}\r",
          "\r",
          "\r",
          "\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) * input.vertexColor;\r",
          "\r",
          "    float4 color = float4(0, 0, 0, 1);\r",
          "    uint numPointLights, pointLightSize;\r",
          "    pointLights.GetDimensions(numPointLights, pointLightSize);\r",
          "    for (int lightIdx = 0; lightIdx < numPointLights; ++lightIdx)\r",
          "    {\r",
          "        color += calcLightSource(input.posInWorld, lightIdx, input.fragPosToCamPos, norm, baseColor);\r",
          "    }\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), materialDiffuse.a * baseColor.a);\r",
          "}\r",
          "//<<< PS\r",
          "\r",
          "technique10 Render\r",
          "{\r",
          "    pass P0\r",
          "    {\r",
          "        SetGeometryShader( 0 );\r",
          "        SetVertexShader( CompileShader( vs_5_0, VS() ) );\r",
          "        SetPixelShader( CompileShader( ps_5_0, PS() ) );\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": "ac7e1779-c095-4e9a-b876-0457bd7ab200",
      "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": []
    }
  ],
  "Outputs": [
    {
      "Name": "Scene",
      "MetaInstanceID": "129634fa-d2b6-43a2-b9c5-a94a050e6cff",
      "MetaID": "79122951-7bc4-4c68-b085-866eab828248"
    }
  ],
  "OperatorParts": [
    {
      "MetaInstanceID": "c321054b-b5e9-47d5-b123-2a8174864304",
      "MetaID": "4b030b99-246c-443b-972e-4b9200c29848",
      "Name": "PhongShaderFunc",
      "Version": "750dd441-9fb3-4a9e-950c-b1355e033d2a",
      "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 Framefield.Core.Rendering;",
        "",
        "using Buffer = SharpDX.Direct3D11.Buffer;",
        "",
        "namespace Framefield.Core.ID4b030b99_246c_443b_972e_4b9200c29848",
        "{",
        "    public class Class_PhongShader : FXSourceCodeFunction",
        "    {",
        "        //>>> _inputids",
        "        private enum InputId",
        "        {",
        "            Code = 0,",
        "            Scene = 1",
        "        }",
        "        //<<< _inputids",
        "",
        "        #region Renderer",
        "        public class Renderer : BaseRenderer",
        "        {",
        "            public override void Dispose()",
        "            {",
        "                Utilities.DisposeObj(ref _pointLightsBuffer);",
        "                Utilities.DisposeObj(ref _pointLightsSRV);",
        "                base.Dispose();",
        "            }",
        "        ",
        "            public override void SetupEffect(OperatorPartContext context)",
        "            {",
        "                base.SetupEffect(context);",
        "                try",
        "                {",
        "                    SetupMaterialConstBuffer(context);",
        "                    SetupFogSettingsConstBuffer(context);",
        "                    SetupPointLightsStructuredBufferForEffect(context, \"pointLights\", ref _pointLightsBuffer, ref _pointLightsSRV);",
        "                }",
        "                catch (Exception e)",
        "                {",
        "                    Logger.Error(ParentFunc, \"Error building constant buffer: {0} - Source: {1}\", e.Message, e.Source);",
        "                }",
        "            }",
        "            public OperatorPart.Function ParentFunc {get;set;}",
        "",
        "            Buffer _pointLightsBuffer;",
        "            ShaderResourceView _pointLightsSRV;",
        "        }",
        "        #endregion",
        "",
        "        public Class_PhongShader()",
        "        {",
        "            _renderer = new Renderer(){ParentFunc = this};",
        "        }",
        "",
        "        public override void Dispose()",
        "        {",
        "            Utilities.DisposeObj(ref _renderer);",
        "            base.Dispose();",
        "        }",
        "",
        "        public override OperatorPartContext Eval(OperatorPartContext context, List<OperatorPart> inputs, int outputIdx)",
        "        {",
        "            //>>> _params",
        "            var Code = inputs[(int)InputId.Code].Eval(context).Text;",
        "            var Scene = inputs[(int)InputId.Scene];",
        "            //<<< _params",
        "           ",
        "            if (_effect == null)",
        "            {",
        "                for (int i = 0; i < NumCodes(); ++i)",
        "                    Compile(i);",
        "                Changed = true;",
        "            }",
        "",
        "            var prevEffect = context.Effect;",
        "            var prevRenderer = context.Renderer;",
        "            ",
        "            context.Effect = _effect;",
        "            context.Renderer = _renderer;",
        "",
        "            Scene.Eval(context);",
        "",
        "            context.Effect = prevEffect;",
        "            context.Renderer = prevRenderer;",
        "",
        "            return context;",
        "        }",
        "",
        "        Renderer _renderer;",
        "    }",
        "}",
        "",
        "",
        ""
      ],
      "AdditionalAssemblies": []
    }
  ],
  "Operators": [],
  "Connections": [
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "c321054b-b5e9-47d5-b123-2a8174864304",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "129634fa-d2b6-43a2-b9c5-a94a050e6cff"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "5f563b77-c865-4d13-bbdd-41d599c853ab",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "c321054b-b5e9-47d5-b123-2a8174864304"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "ac7e1779-c095-4e9a-b876-0457bd7ab200",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "c321054b-b5e9-47d5-b123-2a8174864304"
    }
  ]
}