private void SetDefaultMaterialProperties(GeometryInstance instance, Material mtrl, SceneMaterial material)
        {
            var brdf = (BrdfTypes)(int)material["brdf_id"];
            if (brdf == BrdfTypes.BRDF_TYPE_GLOSSY_REFLECTION_MEASURED)
            {
                int th, td, pd;
                string[] names =
                {
                    "chrome-steel.binary", "gold-metallic-paint.binary", "green-acrylic.binary", "chrome-steel.binary",
                    "color-changing-paint1.binary", "colonial-maple-223.binary", "dark-blue-paint.binary",
                    "green-fabric.binary",
                    "green-plastic.binary", "hematite.binary", "nylon.binary", "pearl-paint.binary"
                };

                Random rnd = new Random();
                string fileName = string.Format(@"F:\3D\Brdfs\merl\{0}", names[rnd.Next(0, names.Length - 1)]);
                Trace.TraceInformation("Loading material data for " + fileName);
                var data =
                    OptixMaterialFactory.FileHelper.Read(fileName, out th, out td, out pd)
                        .Select(i => (float)i)
                        .ToArray();
                mtrl["nThetaH"].Set(th);
                mtrl["nThetaD"].Set(td);
                mtrl["nPhiD"].Set(pd);
                var bDesc = new BufferDesc()
                {
                    Width = (uint)data.Length,
                    Format = Format.Float,
                    Type = BufferType.Input
                };
                var viBuffer = new Buffer(Session.OptixContext, bDesc);
                viBuffer.SetData(data);
                mtrl["brdf"].Set(viBuffer);
            }
            else
            {
                var bDesc = new BufferDesc()
                {
                    Width = (uint)1,
                    Format = Format.Float,
                    Type = BufferType.Input
                };
                var viBuffer = new Buffer(Session.OptixContext, bDesc);
                viBuffer.SetData(new[] { 1u });
                mtrl["brdf"].Set(viBuffer);
            }
        }
        private void BuildMaterials(SceneMaterial[] sceneMaterials, SceneResourceReference[] sceneResources)
        {
            foreach (var sceneMaterial in sceneMaterials)
            {
                var material = new Material(Session.OptixContext);
                if (this.Materials == null)
                {
                    this.Materials = new Dictionary<string, Material>(StringComparer.InvariantCultureIgnoreCase);
                }
                this.Materials.Add(sceneMaterial.Name, material);
                sceneMaterial.ResolveReferences();
                if (OnMaterialScriptsResolve != null)
                {
                    var args = new MaterialResolveArgs { MaterialName = sceneMaterial.Name, IsVolume = MaterialManager.IsVolume(sceneMaterial.Name) };
                    var scripts = OnMaterialScriptsResolve(args).ToArray();
                    for (int n = 0; n < RayTypeCount; n++)
                    {

                        material.Programs[n] = new SurfaceProgram(Session.OptixContext, n == 0 ? RayHitType.Closest : RayHitType.Any, scripts[n].PtxPath, scripts[n].Method);

                        //material["diffuse_color"].SetFloat3((Vector3)sceneMaterial["diffuse_color"]);
                        //material["glossy_color"].SetFloat3((Vector3)sceneMaterial["gloss_color"]);
                        //material["specular_color"].SetFloat3((Vector3)sceneMaterial["specular_color"]);
                        //material["index_of_refraction"].Set(1.5f);
                        //material["phong_exp"].Set((float)sceneMaterial["exponent"]);

                        //(Vector3)sceneMaterial["emission_color"]

                        string diffuse_map = sceneMaterial.Get<string>("!diffuse_map", null);
                        string alpha_map = sceneMaterial.Get<string>("!alpha_map", null);
                        string bump_map = sceneMaterial.Get<string>("!bump_map", null);
                        var samplers = LoadTextures(diffuse_map, bump_map, alpha_map, material);

                        var materialDescription = new MaterialDescription()
                        {
                            diffuse = (Vector3)sceneMaterial["diffuse_color"],
                            gloss = (Vector3)sceneMaterial["gloss_color"],
                            specular = (Vector3)sceneMaterial["specular_color"],
                            index_of_refraction = sceneMaterial.Get("index_of_refraction", 1.5f),
                            roughness = sceneMaterial.Get("roughness", 1.0f),
                            reflectivity = sceneMaterial.Get("reflectivity", 0.75f),
                            exponent = (float)sceneMaterial["exponent"],
                            emission = (Vector3)sceneMaterial["emission_color"]
                        };
                        if (samplers[0] > 0)
                        {
                            materialDescription.diffuse_tex = samplers[0];
                            materialDescription.use_diffuse = 1;
                        }

                        if (samplers[1] > 0)
                        {
                            materialDescription.bump_tex = samplers[1];
                            materialDescription.use_bump = 1;
                        }

                        if (samplers[2] > 0)
                        {
                            materialDescription.alpha_tex = samplers[2];
                            materialDescription.use_alpha = 1;
                        }
                        if (this.MaterialDescriptions == null)
                        {
                            this.MaterialDescriptions = new List<MaterialDescription>();
                        }
                        this.MaterialDescriptions.Add(materialDescription);
                        material["material_description_id"].Set(this.MaterialDescriptions.IndexOf(materialDescription));
                        sceneMaterial["MaterialDescriptionIndex"] =
                            this.MaterialDescriptions.IndexOf(materialDescription);
                        if (args.IsVolume)
                        {
                            material["use_density_map"].Set(1);
                            if (densityMap == null)
                            {
                                uint w = 126, h = 126, d = 126;
                                byte[] voxelData = new byte[w * h * d];
                                //Random rnd = new Random();

                                //for (int i = 0; i < w; i++)
                                //    for (int j = 0; j < h; j++)
                                //        for (int k = 0; k < d; k++)
                                //        {
                                //            voxelData[i + w * (j + k * d)] = (byte)(rnd.Next(255 - i));
                                //        }

                                var voxData = VolumeDataManager.OpenSlab(@"C:\Users\aircraft\Downloads\MagicaVoxel-0.97-win-mac\export\export\maze.slab.vox");
                                //VolumeDataManager.OpenXRaw(@"C:\Users\aircraft\Downloads\MagicaVoxel-0.97-win-mac\export\export\monu1.xraw");
                                w = (uint)voxData.Width;
                                h = (uint)voxData.Height;
                                d = (uint)voxData.Depth;
                                voxelData = voxData.Data;

                                var voxDataBuffer = new Buffer(ComponentPipeline.Instance.Session.OptixContext, new BufferDesc()
                                {
                                    Width = w,
                                    Depth = d,
                                    Height = h,
                                    Format = Format.Byte,
                                    Type = BufferType.Input
                                });
                                voxDataBuffer.SetData(voxelData);
                                densityMap = new TextureSampler(ComponentPipeline.Instance.Session.OptixContext, TextureSamplerDesc.Default);
                                densityMap.SetBuffer(voxDataBuffer, 0U);
                            }
                            material["density_map"].Set(densityMap.GetId());
                        }
                    }
                }
                else
                {
                    throw new ApplicationException("Material resolve event is not initialized");
                }

                //Evaluate programs count - engine dependent
                //Set programs - dependent on prev. step

                //END TODO

                sceneMaterial.InitializeOptixEntity(material, sceneMaterial.Name + " : " + ((BrdfTypes)sceneMaterial["brdf_id"]));
            }
        }
        private Material GetLightMaterial(SceneMaterial sceneMaterial, int lightIndex)
        {
            if (lightMaterial == null)
            {
                lightMaterial = new Material(Session.OptixContext);
                if (OnLightMaterialScriptsResolve == null)
                {
                    throw new ApplicationException("Resolve light material event is not initalized");
                }
                var script = OnLightMaterialScriptsResolve(sceneMaterial.Name);
                for (int n = 0; n < RayTypeCount; n++)
                {
                    lightMaterial.Programs[n] = new SurfaceProgram(Session.OptixContext, n == 0 ? RayHitType.Closest : RayHitType.Any,
                        script[n].PtxPath, script[n].Method);
                }
                lightMaterial["emission_color"].SetFloat3((Vector3)sceneMaterial["emission_color"] + DefaultSceneEmission);
                lightMaterial["light_index"].Set(lightIndex);
                SetDefaultLightProperties(lightMaterial);
            }

            return lightMaterial;
        }
        protected virtual Material ResolveMaterial(SceneMaterial mat)
        {
            var matName = mat.Name;
            if (!string.IsNullOrWhiteSpace(matName) && Materials.ContainsKey(matName))
            {

                //if (Scene.HasLight(objName))
                //{
                //    return Materials["diffuseEmitter"];
                //}

                return Materials[matName];
            }
            return null;
        }