{
  "Name": "PhongCubeMapping",
  "_id": "05f26bdd-8b6d-4983-a30c-470cc0e9cdb6",
  "Namespace": "lib.3d.material",
  "Description": "",
  "Inputs": [
    {
      "Name": "Code",
      "MetaInstanceID": "a660e3d0-08be-482b-8b7e-5324adc380ea",
      "DefaultValue": {
        "Type": "Text",
        "Value": [
          "float4x4 objectToWorldMatrix;\r",
          "float4x4 worldToCameraMatrix;\r",
          "float4x4 cameraToObjectMatrix; // modelview inverse\r",
          "float4x4 projMatrix;\r",
          "float4x4 textureMatrix;\r",
          "float Base;\r",
          "float Reflection;\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",
          "TextureCube CubeMap;\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, input.texCoord);\r",
          "    float4 v = mul(cameraToObjectMatrix[3], objectToWorldMatrix) - input.posInWorld;\r",
          "    float3 incident = normalize(v.xyz);\r",
          "    float3 r = reflect(-incident, norm);\r",
          "    float4 cubeMapColor = CubeMap.Sample(samLinear, r);\r",
          "    \r",
          "    baseColor += cubeMapColor * Base;\r",
          "\r",
          "    float4 color = float4(0, 0, 0, 1);\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",
          "    color += cubeMapColor * Reflection;\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_4_0, VS() ) );\r",
          "        SetPixelShader( CompileShader( ps_4_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": "0a5edabc-ce3a-45cb-a8d7-fd251f508793",
      "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": "CubeMap",
      "MetaInstanceID": "8916814a-e0cb-4ea0-9f2c-e462f769f3e1",
      "DefaultValue": {
        "Type": "Image",
        "Value": "Framefield.Core.Image"
      },
      "MetaID": "9848060d-fd84-45b0-b658-d0d531c61dab",
      "IsMultiInput": "False",
      "Relevance": "Relevant",
      "Description": "",
      "Min": "-100000",
      "Max": "100000",
      "Scale": "0.1",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Base",
      "MetaInstanceID": "3fa9f173-cadd-4d23-b0dd-b2b7f20788cf",
      "DefaultValue": {
        "Type": "Float",
        "Value": "1"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "-100000",
      "Max": "100000",
      "Scale": "0.01",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "Reflection",
      "MetaInstanceID": "fac19078-536a-44b4-a954-f0da2915140f",
      "DefaultValue": {
        "Type": "Float",
        "Value": "0"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "-100000",
      "Max": "100000",
      "Scale": "0.01",
      "ScaleType": "Linear",
      "EnumValues": []
    }
  ],
  "Outputs": [
    {
      "Name": "Scene",
      "MetaInstanceID": "5744a9ac-0faf-43e6-903a-4643e4299811",
      "MetaID": "79122951-7bc4-4c68-b085-866eab828248"
    }
  ],
  "OperatorParts": [
    {
      "MetaInstanceID": "82e73f8d-7768-4055-a9f4-2d8722331148",
      "MetaID": "0574156e-8742-48fb-9273-27f44ef3b3bf",
      "Name": "PhongCubeMappingFunc",
      "Version": "22b5fda1-bdfb-4836-a554-f8bff3b64a8e",
      "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;",
        "",
        "namespace Framefield.Core.ID0574156e_8742_48fb_9273_27f44ef3b3bf",
        "{",
        "    public class Class_PhongCubeMapping : FXSourceCodeFunction",
        "    {",
        "        //>>> _inputids",
        "        private enum InputId",
        "        {",
        "            Code = 0,",
        "            Scene = 1,",
        "            CubeMap = 2,",
        "            Base = 3,",
        "            Reflection = 4",
        "        }",
        "        //<<< _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_PhongCubeMapping()",
        "        {",
        "            _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)",
        "        {",
        "            //>>> _params",
        "            var Code = inputs[(int)InputId.Code].Eval(context).Text;",
        "            var Scene = inputs[(int)InputId.Scene];",
        "            var CubeMap = inputs[(int)InputId.CubeMap].Eval(context).Image; // Needs to be checked for null!",
        "            var Base = inputs[(int)InputId.Base].Eval(context).Value;",
        "            var Reflection = inputs[(int)InputId.Reflection].Eval(context).Value;",
        "            //<<< _params",
        "",
        "            if (_firstEval)",
        "            {",
        "                for (int i = 0; i < NumCodes(); ++i)",
        "                    Compile(i);",
        "                _firstEval = false;",
        "                Changed = true;",
        "            }",
        "",
        "            //>>> __setup",
        "            using (var CubeMapView = new ShaderResourceView(context.D3DDevice, CubeMap))",
        "            {",
        "                _effect.GetVariableByName(\"CubeMap\").AsShaderResource().SetResource(CubeMapView);",
        "            //<<< __setup",
        "    ",
        "                using (new PropertyStasher<OperatorPartContext>(context, \"Effect\", \"Renderer\"))",
        "                {",
        "                    _effect.GetVariableByName(\"Base\").AsScalar().Set(Base);",
        "                    _effect.GetVariableByName(\"Reflection\").AsScalar().Set(Reflection);",
        "                    context.Effect = _effect;",
        "                    context.Renderer = _renderer;",
        "    ",
        "                    Scene.Eval(context);",
        "                }",
        "            }",
        "",
        "            return context;",
        "        }",
        "",
        "        Renderer _renderer;",
        "    }",
        "}",
        "",
        "",
        ""
      ],
      "AdditionalAssemblies": []
    }
  ],
  "Operators": [],
  "Connections": [
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "82e73f8d-7768-4055-a9f4-2d8722331148",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "5744a9ac-0faf-43e6-903a-4643e4299811"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "a660e3d0-08be-482b-8b7e-5324adc380ea",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "82e73f8d-7768-4055-a9f4-2d8722331148"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "0a5edabc-ce3a-45cb-a8d7-fd251f508793",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "82e73f8d-7768-4055-a9f4-2d8722331148"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "8916814a-e0cb-4ea0-9f2c-e462f769f3e1",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "82e73f8d-7768-4055-a9f4-2d8722331148"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "3fa9f173-cadd-4d23-b0dd-b2b7f20788cf",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "82e73f8d-7768-4055-a9f4-2d8722331148"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "fac19078-536a-44b4-a954-f0da2915140f",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "82e73f8d-7768-4055-a9f4-2d8722331148"
    }
  ]
}