/// <inheritdoc/> protected override void OnMessageReceived(IncomingMessage msg) { var key = msg.ReadString(); var type = msg.ReadInt32(); if ((int)EnvironmentDataTypes.Float == type) { var value = msg.ReadFloat32(); m_Parameters[key] = () => value; Action <float> action; m_RegisteredActions.TryGetValue(key, out action); action?.Invoke(value); } else if ((int)EnvironmentDataTypes.Sampler == type) { int seed = msg.ReadInt32(); int samplerType = msg.ReadInt32(); Func <float> sampler = () => 0.0f; if ((int)SamplerType.Uniform == samplerType) { float min = msg.ReadFloat32(); float max = msg.ReadFloat32(); sampler = SamplerFactory.CreateUniformSampler(min, max, seed); } else if ((int)SamplerType.Gaussian == samplerType) { float mean = msg.ReadFloat32(); float stddev = msg.ReadFloat32(); sampler = SamplerFactory.CreateGaussianSampler(mean, stddev, seed); } else if ((int)SamplerType.MultiRangeUniform == samplerType) { IList <float> intervals = msg.ReadFloatList(); sampler = SamplerFactory.CreateMultiRangeUniformSampler(intervals, seed); } else { Debug.LogWarning("EnvironmentParametersChannel received an unknown data type."); } m_Parameters[key] = sampler; } else { Debug.LogWarning("EnvironmentParametersChannel received an unknown data type."); } }
// \brief Declare uniforms and set Shader replacement strings private void InitializeShaderSystem() { #if false if (RenderStack.Graphics.Configuration.useOpenRL) { InitializeOpenRL(); } #endif string fs_shadows = System.IO.File.ReadAllText("res/OpenGL3/_fs_shadows.txt"); string instancing = System.IO.File.ReadAllText("res/OpenGL3/_instancing.txt"); string utils = System.IO.File.ReadAllText("res/OpenGL3/_utils.txt"); ShaderGL3.Replace("FS_SHADOWS;", fs_shadows); ShaderGL3.Replace("INSTANCING;", instancing); ShaderGL3.Replace("UTILS;", utils); CameraUniformBlock.NearFar = "near_far"; CameraUniformBlock.FovXFovYAspect = "fovx_fovy_aspect"; CameraUniformBlock.ViewToClip = "view_to_clip_matrix"; CameraUniformBlock.ClipToView = "view_to_clip_matrix"; CameraUniformBlock.WorldToClip = "world_to_clip_matrix"; CameraUniformBlock.ClipToWorld = "clip_to_world_matrix"; CameraUniformBlock.WorldToView = "world_to_view_matrix"; CameraUniformBlock.ViewToWorld = "view_to_world_matrix"; CameraUniformBlock.Viewport = "viewport"; CameraUniformBlock.ViewPositionInWorld = "view_position_in_world"; CameraUniformBlock.Initialize("camera"); LightsUniforms.spec.Count = "count"; LightsUniforms.spec.Exposure = "exposure"; LightsUniforms.spec.Bias = "bias"; LightsUniforms.spec.AmbientLightColor = "ambient_light_color"; LightsUniforms.spec.WorldToLight = "world_to_light_matrix"; LightsUniforms.spec.WorldToShadow = "world_to_shadow_matrix"; LightsUniforms.spec.Direction = "direction"; LightsUniforms.spec.Color = "color"; LightsUniforms.Initialize("lights", Configuration.maxLightCount); System.Text.StringBuilder uniforms = new System.Text.StringBuilder(); uniforms.Append(CameraUniformBlock.UniformBlockGL.SourceGL); uniforms.Append(LightsUniforms.UniformBlockGL.SourceGL); modelsUB = new UniformBlockGL("models"); modelsUB.AddMat4("model_to_world_matrix", Configuration.instanceCount); if (RenderStack.Graphics.Configuration.useIntegerPolygonIDs) { modelsUB.AddUInt("id_offset_uint", Configuration.instanceCount); ShaderGL3.Replace("#if USE_INTEGER_POLYGON_ID", "#if 1"); } else { modelsUB.AddVec4("id_offset_vec3", Configuration.instanceCount); ShaderGL3.Replace("#if USE_INTEGER_POLYGON_ID", "#if 0"); } modelsUB.Seal(); uniforms.Append(modelsUB.SourceGL); globalUB = new UniformBlockGL("global"); globalUB.AddVec4("add_color"); globalUB.AddFloat("alpha"); globalUB.Seal(); uniforms.Append(globalUB.SourceGL); LightsUniforms.UniformBufferGL.Use(); ShaderGL3.Replace("MAX_LIGHT_COUNT", Configuration.maxLightCount.ToString()); models = UniformBufferFactory.Create(modelsUB); global = UniformBufferFactory.Create(globalUB); global.Floats("alpha").Set(1.0f); global.Floats("add_color").Set(0.0f, 0.0f, 0.4f); global.Sync(); LightsUniforms.Exposure.Set(1.0f); LightsUniforms.Bias.Set(-0.002f, 0.0f, 0.002f); LightsUniforms.AmbientLightColor.Set(0.2f, 0.2f, 0.2f); LightsUniforms.UniformBufferGL.Sync(); models.Use(); global.Use(); // GL_NEAREST - no filtering, no mipmaps // GL_LINEAR - filtering, no mipmaps // GL_NEAREST_MIPMAP_NEAREST - no filtering, sharp switching between mipmaps // GL_NEAREST_MIPMAP_LINEAR - no filtering, smooth transition between mipmaps // GL_LINEAR_MIPMAP_NEAREST - filtering, sharp switching between mipmaps // GL_LINEAR_MIPMAP_LINEAR - filtering, smooth transition between mipmaps // // GL_LINEAR - bilinear // GL_LINEAR_MIPMAP_NEAREST - bilinear with mipmaps // GL_LINEAR_MIPMAP_LINEAR - trilinear // No filtering, no mipmaps var nearest = SamplerFactory.Create(); nearest.MinFilter = TextureMinFilter.Nearest; nearest.MagFilter = TextureMagFilter.Nearest; nearest.Wrap = TextureWrapMode.ClampToEdge; nearest.CompareMode = TextureCompareMode.None; var linear = SamplerFactory.Create(); linear.MinFilter = TextureMinFilter.Linear; linear.MagFilter = TextureMagFilter.Linear; linear.Wrap = TextureWrapMode.ClampToEdge; linear.CompareMode = TextureCompareMode.None; var bilinear = SamplerFactory.Create(); bilinear.MinFilter = TextureMinFilter.LinearMipmapNearest; bilinear.MagFilter = TextureMagFilter.Linear; bilinear.Wrap = TextureWrapMode.ClampToEdge; bilinear.CompareMode = TextureCompareMode.None; var trilinear = SamplerFactory.Create(); trilinear.MinFilter = TextureMinFilter.LinearMipmapLinear; trilinear.MagFilter = TextureMagFilter.Linear; trilinear.Wrap = TextureWrapMode.ClampToEdge; trilinear.CompareMode = TextureCompareMode.None; var pcfShadow = SamplerFactory.Create(); pcfShadow.MinFilter = TextureMinFilter.Linear; pcfShadow.MagFilter = TextureMagFilter.Linear; pcfShadow.Wrap = TextureWrapMode.ClampToEdge; pcfShadow.CompareMode = TextureCompareMode.CompareRefToTexture; if (Configuration.hardwareShadowPCF) { ShaderGL3.Replace("#if HARDWARE_PCF", "#if 1"); Samplers.Global.AddSampler2DArrayShadow("t_shadowmap", pcfShadow).TextureUnitIndex = 2; } else { ShaderGL3.Replace("#if HARDWARE_PCF", "#if 0"); Samplers.Global.AddSampler2DArray("t_shadowmap", linear).TextureUnitIndex = 2; } Samplers.Global.AddSampler2D("t_font", nearest).TextureUnitIndex = 1; Samplers.Global.AddSampler2D("t_ninepatch", linear).TextureUnitIndex = 2; Samplers.Global.AddSampler2D("t_cube", trilinear); Samplers.Global.AddSampler2D("t_left", nearest); Samplers.Global.AddSampler2D("t_right", nearest).TextureUnitIndex = 1; Samplers.Global.AddSampler2D("t_surface_color", trilinear); Samplers.Global.AddSampler2D("t_particle", trilinear); Samplers.Global.AddSampler2DArray("t_shadowmap_vis", linear); Samplers.Global.Seal(); materialUB = new UniformBlockGL("material"); materialUB.AddVec4("surface_diffuse_reflectance_color"); materialUB.AddVec4("surface_specular_reflectance_color"); materialUB.AddFloat("surface_specular_reflectance_exponent"); materialUB.AddVec2("surface_rim_parameters"); materialUB.AddVec4("grid_size"); materialUB.AddVec4("main_camera_near_far"); materialUB.AddFloat("surface_roughness"); materialUB.AddFloat("surface_isotropy"); materialUB.AddFloat("contrast").Default = new Floats(1.0f); materialUB.AddFloat("deghost").Default = new Floats(0.0f); materialUB.AddFloat("saturation").Default = new Floats(1.0f); materialUB.AddFloat("t"); materialUB.AddFloat("slider_t"); materialUB.AddVec4("fill_color"); materialUB.AddVec4("point_color"); materialUB.AddFloat("point_z_offset"); materialUB.AddVec4("line_color"); materialUB.AddVec2("line_width").Default = new Floats(1.0f, 0.25f); // \todo check how to compute .y materialUB.AddFloat("bias_units"); materialUB.AddFloat("bias_factor"); materialUB.AddFloat("octaves").Default = new Floats(4.0f); materialUB.AddFloat("offset").Default = new Floats(0.0f); materialUB.AddFloat("frequency").Default = new Floats(2.2f); materialUB.AddFloat("amplitude").Default = new Floats(0.2f); materialUB.AddFloat("lacunarity").Default = new Floats(3.3f); materialUB.AddFloat("persistence").Default = new Floats(0.25f); materialUB.Seal(); uniforms.Append(MaterialUB.SourceGL); uniforms.Append(Samplers.Global.ToString()); // Have these last ShaderGL3.Replace("UNIFORMS;", uniforms.ToString()); ShaderGL3.Replace("INSTANCE_COUNT", "50"); }