internal void UpdateEntity()
 {
     int index = (int)activeMaterial["MaterialDescriptionIndex"];
     if (index >= 0)
     {
         var engine = (PTEngine) ComponentPipeline.Instance.RenderingEngine;
         var oldMd = engine.Descriptions[index];
         var md = new MaterialDescription
         {
             alpha_tex = oldMd.alpha_tex,
             bump_tex = oldMd.bump_tex,
             diffuse = oldMd.diffuse,
             emission = oldMd.emission,
             diffuse_tex = oldMd.diffuse_tex,
             exponent = MaterialModel.Exponent ?? oldMd.exponent,
             gloss = oldMd.gloss,
             id = oldMd.id,
             index_of_refraction = this.MaterialModel.IndexOfRefraction??oldMd.index_of_refraction,
             reflectivity = MaterialModel.Reflectivity??oldMd.reflectivity,
             roughness = MaterialModel.Roughness??oldMd.roughness,
             specular = oldMd.specular,
             use_alpha = oldMd.use_alpha,
             use_bump = oldMd.use_bump,
             use_diffuse = oldMd.use_diffuse
         };
         engine.Descriptions[index] = md;
         engine.UpdateDescriptions();
     }
 }
        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"]));
            }
        }