Exemple #1
0
        public ActionResult <string> Test()
        {
            var config = new LightingConfiguration()
            {
                LedData = new List <LED>()
            };
            Random random = new Random();
            var    count  = 84;

            for (var i = 0; i < count; i++)
            {
                config.LedData.Add(new LED()
                {
                    Id = i,
                    R  = random.Next(255),
                    G  = random.Next(255),
                    B  = random.Next(255),
                });
            }
            SerialPort port = new SerialPort("COM3", 19200);

            port.Open();
            var data = "";

            config.LedData.ForEach(l =>
            {
                data += l.Serialize();
            });
            port.Write(data);
            port.Close();
            return("200");
        }
Exemple #2
0
        public void Post(LightingConfiguration lightConfig)
        {
            //Uhhh set all the leds
            SerialPort port = new SerialPort("COM4", 9600);

            port.Open();
            lightConfig.LedData.ForEach(l =>
            {
                port.WriteLine($"{l.Id} {l.R} {l.G} {l.B}");
            });
            port.Close();
        }
        private static List <ShadowUpdateInfo> CreateEffectShadowParams(LightingConfiguration config)
        {
            var configs = new List <ShadowUpdateInfo>();

            for (var i = 0; i < config.ShadowConfigurations.Groups.Count; ++i)
            {
                var group = LightingProcessorHelpers.CreateShadowUpdateInfo(i, config.ShadowConfigurations.Groups[i].CascadeCount);
                configs.Add(group);
            }

            return(configs);
        }
        private static void CreateParametersFromLightingConfiguration(LightingConfiguration config, ParameterCollection parameters)
        {
            // Apply parameters for effect change
            parameters.Set(LightingKeys.MaxDirectionalLights, config.MaxNumDirectionalLight);
            parameters.Set(LightingKeys.MaxPointLights, config.MaxNumPointLight);
            parameters.Set(LightingKeys.MaxSpotLights, config.MaxNumSpotLight);

            // TODO: cache some objects since it is done at each frame
            // TODO: try to reuse the parameter collections?

            if (config.ShadowConfigurations != null)
            {
                var groupCount = 0;
                foreach (var group in config.ShadowConfigurations.Groups)
                {
                    groupCount += (group.ShadowCount > 0 ? 1 : 0);
                }

                if (groupCount == 0)
                {
                    parameters.Remove(ShadowMapParameters.ShadowMaps);
                    return;
                }

                var shadowMapParameters = new ShadowMapParameters[groupCount];
                var index = 0;
                for (var i = 0; i < config.ShadowConfigurations.Groups.Count; ++i)
                {
                    if (config.ShadowConfigurations.Groups[i].ShadowCount > 0)
                    {
                        var shadowParams = new ShadowMapParameters();
                        shadowParams.Set(ShadowMapParameters.LightType, config.ShadowConfigurations.Groups[i].LightType);
                        shadowParams.Set(ShadowMapParameters.ShadowMapCount, config.ShadowConfigurations.Groups[i].ShadowCount);
                        shadowParams.Set(ShadowMapParameters.ShadowMapCascadeCount, config.ShadowConfigurations.Groups[i].CascadeCount);
                        shadowParams.Set(ShadowMapParameters.FilterType, config.ShadowConfigurations.Groups[i].FilterType);
                        shadowMapParameters[index] = shadowParams;
                        ++index;
                    }
                }
                parameters.Set(ShadowMapParameters.ShadowMaps, shadowMapParameters);
            }
            else
            {
                parameters.Remove(ShadowMapParameters.ShadowMaps);
            }

            //mesh.SharedParameters.Set(LightingKeys.UnrollDirectionalLightLoop, foundConfiguration.UnrollDirectionalLightLoop);
            //mesh.SharedParameters.Set(LightingKeys.UnrollPointLightLoop, foundConfiguration.UnrollPointLightLoop);
            //mesh.SharedParameters.Set(LightingKeys.UnrollSpotLightLoop, foundConfiguration.UnrollSpotLightLoop);
        }
        private void AssignGroups(LightingConfiguration config)
        {
            // TODO: optimize the groups based on the maximum number of shadow maps so that when there is a solution, it is chosen

            // TODO: add shadow group and directional light shadow group (list) if necessary
            for (var i = shadowMapGroups.Count; i < config.ShadowConfigurations.Groups.Count; ++i)
            {
                shadowMapGroups.Add(new List <ShadowMap>());
            }

            for (var i = directionalLightsWithShadowForMeshGroups.Count; i < config.ShadowConfigurations.Groups.Count; ++i)
            {
                directionalLightsWithShadowForMeshGroups.Add(new List <EntityLightShadow>());
            }
            foreach (var light in directionalLightsWithShadowForMesh)
            {
                for (var i = 0; i < config.ShadowConfigurations.Groups.Count; ++i)
                {
                    if (BelongToGroup(light.Light, light.ShadowMap, config.ShadowConfigurations.Groups[i], shadowMapGroups[i].Count, shadowMapGroups[i].Count > 0 ? shadowMapGroups[i][0].Texture.ShadowMapDepthTexture : null))
                    {
                        shadowMapGroups[i].Add(light.ShadowMap);
                        directionalLightsWithShadowForMeshGroups[i].Add(light);
                        break;
                    }
                }
            }

            for (var i = spotLightsWithShadowForMeshGroups.Count; i < config.ShadowConfigurations.Groups.Count; ++i)
            {
                spotLightsWithShadowForMeshGroups.Add(new List <EntityLightShadow>());
            }
            foreach (var light in spotLightsWithShadowForMesh)
            {
                for (var i = 0; i < config.ShadowConfigurations.Groups.Count; ++i)
                {
                    if (BelongToGroup(light.Light, light.ShadowMap, config.ShadowConfigurations.Groups[i], shadowMapGroups[i].Count, shadowMapGroups[i].Count > 0 ? shadowMapGroups[i][0].Texture.ShadowMapDepthTexture : null))
                    {
                        shadowMapGroups[i].Add(light.ShadowMap);
                        spotLightsWithShadowForMeshGroups[i].Add(light);
                        break;
                    }
                }
            }
        }
        private bool TestConfiguration(int numDirectionalLights, int numPointLights, int numSpotLights, LightingConfiguration config)
        {
            if (config.MaxNumDirectionalLight < numDirectionalLights || config.MaxNumPointLight < numPointLights || config.MaxNumSpotLight < numSpotLights)
            {
                return(false);
            }

            //TODO change hardcoded 16
            var groupCounts   = new int[16];
            var groupTextures = new Texture[16];

            // TODO: optimize OR consider that this will always be relatively small
            foreach (var light in directionalLightsWithShadowForMesh)
            {
                var notFound = true;
                if (config.ShadowConfigurations != null)
                {
                    for (var i = 0; i < config.ShadowConfigurations.Groups.Count; ++i)
                    {
                        if (BelongToGroup(light.Light, light.ShadowMap, config.ShadowConfigurations.Groups[i], groupCounts[i], groupTextures[i]))
                        {
                            groupCounts[i] += 1;
                            if (groupTextures[i] == null)
                            {
                                groupTextures[i] = light.ShadowMap.Texture.ShadowMapDepthTexture;
                            }
                            notFound = false;
                            break;
                        }
                    }
                }
                if (notFound)
                {
                    return(false);
                }
            }
            foreach (var light in spotLightsWithShadowForMesh)
            {
                var notFound = true;
                if (config.ShadowConfigurations != null)
                {
                    for (var i = 0; i < config.ShadowConfigurations.Groups.Count; ++i)
                    {
                        if (BelongToGroup(light.Light, light.ShadowMap, config.ShadowConfigurations.Groups[i], groupCounts[i], groupTextures[i]))
                        {
                            groupCounts[i] += 1;
                            if (groupTextures[i] == null)
                            {
                                groupTextures[i] = light.ShadowMap.Texture.ShadowMapDepthTexture;
                            }
                            notFound = false;
                            break;
                        }
                    }
                }
                if (notFound)
                {
                    return(false);
                }
            }
            return(true);
        }
        /// <summary>
        /// Update light lists and choose the new light configuration.
        /// </summary>
        /// <param name="context">The render context.</param>
        /// <param name="renderMesh">The current RenderMesh (the same as <seealso cref="PostEffectUpdate"/>)</param>
        public void PreEffectUpdate(RenderContext context, RenderMesh renderMesh)
        {
            // TODO:
            // light selection based on:
            //    - from the same entity?
            //    - spot & point lights distances
            // rewrite shaders to handle all the cases?
            // TODO: other criterion to choose the light (distance?)

            directionalLightsForMesh.Clear();
            directionalLightsWithShadowForMesh.Clear();
            foreach (var group in directionalLightsWithShadowForMeshGroups)
            {
                group.Clear();
            }
            foreach (var group in spotLightsWithShadowForMeshGroups)
            {
                group.Clear();
            }
            foreach (var group in shadowMapGroups)
            {
                group.Clear();
            }
            pointLightsForMesh.Clear();
            spotLightsForMesh.Clear();
            spotLightsWithShadowForMesh.Clear();

            var receiveShadows = renderMesh.Parameters.Get(LightingKeys.ReceiveShadows);
            var renderLayers   = renderMesh.Parameters.Get(RenderingParameters.RenderLayer);

            foreach (var light in directionalLights)
            {
                if ((light.Light.Layers & renderLayers) != 0)
                {
                    directionalLightsForMesh.Add(light);
                }
            }
            foreach (var light in directionalLightsWithShadows)
            {
                if ((light.Light.Layers & renderLayers) != 0)
                {
                    if (receiveShadows)
                    {
                        directionalLightsWithShadowForMesh.Add(light);
                    }
                    else
                    {
                        directionalLightsForMesh.Add(light);
                    }
                }
            }
            foreach (var light in pointLights)
            {
                if ((light.Light.Layers & renderLayers) != 0)
                {
                    pointLightsForMesh.Add(light);
                }
            }
            foreach (var light in spotLights)
            {
                if ((light.Light.Layers & renderLayers) != 0)
                {
                    spotLightsForMesh.Add(light);
                }
            }
            foreach (var light in spotLightsWithShadows)
            {
                if ((light.Light.Layers & renderLayers) != 0)
                {
                    if (receiveShadows)
                    {
                        spotLightsWithShadowForMesh.Add(light);
                    }
                    else
                    {
                        spotLightsForMesh.Add(light);
                    }
                }
            }

            var numDirectionalLights = directionalLightsForMesh.Count;
            var numPointLights       = pointLightsForMesh.Count;
            var numSpotLights        = spotLightsForMesh.Count;

            // TODO: improve detection - better heuristics
            // choose configuration
            var configurations          = renderMesh.Parameters.Get(LightingKeys.LightingConfigurations);
            var lastConfigWithoutShadow = -1;
            LightingConfiguration foundConfiguration;

            if (configurations != null)
            {
                foundConfiguration.MaxNumDirectionalLight     = 0;
                foundConfiguration.MaxNumPointLight           = 0;
                foundConfiguration.MaxNumSpotLight            = 0;
                foundConfiguration.UnrollDirectionalLightLoop = false;
                foundConfiguration.UnrollPointLightLoop       = false;
                foundConfiguration.UnrollSpotLightLoop        = false;
                var configurationIndex = -1;
                for (var i = 0; i < configurations.Configs.Length; ++i)
                {
                    if (configurations.Configs[i].ShadowConfigurations == null || configurations.Configs[i].ShadowConfigurations.Groups.Count == 0)
                    {
                        lastConfigWithoutShadow = i;
                    }

                    if (TestConfiguration(numDirectionalLights, numPointLights, numSpotLights, configurations.Configs[i]))
                    {
                        configurationIndex = i;
                        break;
                    }
                }

                // no correct configuration found
                if (configurationIndex < 0)
                {
                    if (lastConfigWithoutShadow != -1)// take the biggest one without shadow
                    {
                        configurationIndex = lastConfigWithoutShadow;
                    }
                    else // take the latest
                    {
                        configurationIndex = configurations.Configs.Length - 1;
                    }
                }

                foundConfiguration = configurations.Configs[configurationIndex];

                //create the parameters to get the correct shader
                if (configurationIndex != renderMesh.Parameters.Get(LightKeys.ConfigurationIndex))
                {
                    CreateParametersFromLightingConfiguration(foundConfiguration, renderMesh.Parameters);
                    renderMesh.Parameters.Set(LightKeys.ConfigurationIndex, configurationIndex);
                }
            }
            else
            {
                // set the configuration that perfectly matches the actual scene
                foundConfiguration = new LightingConfiguration();
                foundConfiguration.MaxNumPointLight           = numPointLights;
                foundConfiguration.MaxNumDirectionalLight     = numDirectionalLights;
                foundConfiguration.MaxNumSpotLight            = numSpotLights;
                foundConfiguration.UnrollPointLightLoop       = true;
                foundConfiguration.UnrollDirectionalLightLoop = true;
                foundConfiguration.UnrollSpotLightLoop        = true;

                if (directionalLightsWithShadowForMesh.Count > 0 ||
                    spotLightsWithShadowForMesh.Count > 0)
                {
                    foundConfiguration.ShadowConfigurations        = new ShadowConfigurationArray();
                    foundConfiguration.ShadowConfigurations.Groups = new List <ShadowConfiguration>();
                    foundConfiguration.ShadowConfigurations.Groups.AddRange(CreateShadowConfiguration(directionalLightsWithShadowForMesh, LightType.Directional));
                    foundConfiguration.ShadowConfigurations.Groups.AddRange(CreateShadowConfiguration(spotLightsWithShadowForMesh, LightType.Spot));
                }

                CreateParametersFromLightingConfiguration(foundConfiguration, renderMesh.Parameters);
            }

            var maxNumDirectionalLights = foundConfiguration.MaxNumDirectionalLight;
            var maxNumPointLights       = foundConfiguration.MaxNumPointLight;
            var maxNumSpotLights        = foundConfiguration.MaxNumSpotLight;

            // assign the shadow ligths to a specific group
            if (foundConfiguration.ShadowConfigurations != null)
            {
                AssignGroups(foundConfiguration);
            }

            var finalDirectionalLightCount = Math.Min(numDirectionalLights, maxNumDirectionalLights);
            var finalPointLightCount       = Math.Min(numPointLights, maxNumPointLights);
            var finalSpotLightCount        = Math.Min(numSpotLights, maxNumSpotLights);

            var maxLights = finalDirectionalLightCount;

            if (maxLights > finalPointLightCount)
            {
                maxLights = finalPointLightCount;
            }
            if (maxLights > finalSpotLightCount)
            {
                maxLights = finalSpotLightCount;
            }

            if (maxLights > maximumSupportedLights)
            {
                maximumSupportedLights = maxLights;
                arrayFloat             = new float[4 * maxLights];
                arrayVector3           = new Vector3[2 * maxLights];
                arrayColor3            = new Color3[maxLights];
            }

            lastConfiguration = foundConfiguration;
        }
 private void WriteConfigToFile(string configFilePath, LightingConfiguration hueShiftOptions)
 {
     File.WriteAllText(configFilePath, JsonConvert.SerializeObject(hueShiftOptions, Formatting.Indented, new StringEnumConverter()));
 }
    void OnGUI()
    {
        lightingConfigurationPath = EditorGUILayout.TextField(lightingConfigurationPath);

        if (lightSettings != null)
        {
            if (GUILayout.Button("Unload current Lighting Preset"))
            {
                if (lightSettings != null)
                {
                    lightSettings = null;
                    editorSetup   = null;
                }
            }
            else
            if (GUILayout.Button("Save current Lighting Preset to Project"))
            {
                LightingConfigurationExists();
                if (!fileExists)
                {
                    AssetDatabase.CreateAsset(lightSettings, lightingConfigurationPath);
                }
                else
                {
                    AssetDatabase.SaveAssets();
                }

                if (lightSettings != null)
                {
                    editorSetup = Editor.CreateEditor(lightSettings);
                }
            }


            scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
            if (editorSetup != null)
            {
                editorSetup.Repaint();
                editorSetup.OnInspectorGUI();
            }

            EditorGUILayout.EndScrollView();
        }
        else
        {
            if (fileExists && GUILayout.Button("Load Lighting Preset from Project path"))
            {
                lightSettings = AssetDatabase.LoadAssetAtPath <LightingConfiguration>(lightingConfigurationPath);
                if (lightSettings != null)
                {
                    editorSetup = Editor.CreateEditor(lightSettings);
                }
            }
            else
            {
                if (GUILayout.Button("Create a Lighting Preset by copying current Scene lighting settings"))
                {
                    lightSettings = new LightingConfiguration();
                    lightSettings.Save();
                    editorSetup = Editor.CreateEditor(lightSettings);
                }
            }
        }
    }