public PhysicalShader(string name = "Physical") : base(name) { VertexCode = Resources.Physical_vert; FragmentCode = Resources.Physical_frag; FragmentCode = FragmentCode.Replace("replace(POINT_LIGHT_NUM)", "#define POINT_LIGHT_NUM " + PointLightNum); FragmentCode = FragmentCode.Replace("replace(SPOT_LIGHT_NUM)", "#define SPOT_LIGHT_NUM " + SpotLightNum); RegistShaderParam <int>("skinning"); RegistShaderParam <int>("morphing"); RegistShaderParam <Matrix4>("M", "Model"); RegistShaderParam <Matrix4>("MIT", "ModelInverseTranspose"); RegistShaderParam <Matrix4>("MVP", "ModelViewProjection"); RegistShaderParam <Matrix4>("shadowMV1", "ShadowModelView1"); RegistShaderParam <Matrix4>("shadowMV2", "ShadowModelView2"); RegistShaderParam <Matrix4>("shadowMV3", "ShadowModelView3"); RegistShaderParam <Color4>("albedo", "Albedo"); RegistShaderParam <Color4>("emissive", "Emissive"); RegistShaderParam <float>("metallic", "Metallic"); RegistShaderParam <float>("roughness", "Roughness"); RegistShaderParam <Color4>("f0", "F0"); RegistShaderParam <Vector3>("wCamDir", "WorldCameraDir"); RegistShaderParam <Vector3>("wCamPos", "WorldCameraPosition"); RegistShaderParam <Vector2>("resolutionInverse", "ResolutionInverse"); RegistShaderParam <Vector3>("wDirLight.dir", "DirectionalLightDir"); RegistShaderParam <Color4>("wDirLight.color", "DirectionalLightColor"); RegistShaderParam <float>("wDirLight.intensity", "DirectionalLightIntensity"); RegistShaderParam <float>("wDirLight.radius", "DirectionalLightRadius"); RegistShaderParam <Vector3>("wDirLight.pos", "DirectionalLightPosition"); for (var i = 0; i < PointLightNum; i++) { RegistShaderParam <Color4>(string.Format("wPointLight[{0}].color", i)); RegistShaderParam <float>(string.Format("wPointLight[{0}].intensity", i)); RegistShaderParam <float>(string.Format("wPointLight[{0}].radius", i)); RegistShaderParam <Vector3>(string.Format("wPointLight[{0}].pos", i)); RegistShaderParam <Vector3>(string.Format("wPointLight[{0}].min", i)); RegistShaderParam <Vector3>(string.Format("wPointLight[{0}].max", i)); } for (var i = 0; i < SpotLightNum; i++) { RegistShaderParam <Vector3>(string.Format("wSpotLight[{0}].dir", i)); RegistShaderParam <Color4>(string.Format("wSpotLight[{0}].color", i)); RegistShaderParam <float>(string.Format("wSpotLight[{0}].intensity", i)); RegistShaderParam <float>(string.Format("wSpotLight[{0}].radius", i)); RegistShaderParam <Vector3>(string.Format("wSpotLight[{0}].pos", i)); RegistShaderParam <Vector3>(string.Format("wSpotLight[{0}].min", i)); RegistShaderParam <Vector3>(string.Format("wSpotLight[{0}].max", i)); RegistShaderParam <float>(string.Format("wSpotLight[{0}].innerAngle", i)); RegistShaderParam <float>(string.Format("wSpotLight[{0}].outerAngle", i)); } RegistShaderParam <Color4>("gAmbient", "GlobalAmbient"); RegistShaderParam <float>("iblIntensity", "IBLIntensity"); RegistShaderParam("albedoMap", "AlbedoMap", TextureUnit.Texture0); RegistShaderParam("normalMap", "NormalMap", TextureUnit.Texture1); RegistShaderParam("environmentMap", "EnvironmentMap", TextureUnit.Texture2); RegistShaderParam("shadowMap1", "ShadowMap1", TextureUnit.Texture3); RegistShaderParam("shadowMap2", "ShadowMap2", TextureUnit.Texture4); RegistShaderParam("shadowMap3", "ShadowMap3", TextureUnit.Texture5); whiteMap = MMW.GetAsset <Texture2D>("WhiteMap"); defaultNormalMap = MMW.GetAsset <Texture2D>("DefaultNormalMap"); environmentMap = MMW.GetAsset <TextureCube>("DefaultSkyBox"); }
override public Result Load() { Unload(); string add = ""; if (MMW.Configuration.IBLQuality == MMWConfiguration.IBLQualityType.VeryHigh) { add += "#define IBL_DIFF_SAMPLE_NUM 128\n"; add += "#define IBL_SPEC_SAMPLE_NUM 64\n"; } else if (MMW.Configuration.IBLQuality == MMWConfiguration.IBLQualityType.High) { add += "#define IBL_DIFF_SAMPLE_NUM 48\n"; add += "#define IBL_SPEC_SAMPLE_NUM 24\n"; } else if (MMW.Configuration.IBLQuality == MMWConfiguration.IBLQualityType.Default) { add += "#define IBL_DIFF_SAMPLE_NUM 24\n"; add += "#define IBL_SPEC_SAMPLE_NUM 12\n"; } else if (MMW.Configuration.IBLQuality == MMWConfiguration.IBLQualityType.Low) { add += "#define IBL_DIFF_SAMPLE_NUM 12\n"; add += "#define IBL_SPEC_SAMPLE_NUM 8\n"; } if (VertexCode != null) { VertexCode = VertexCode.Replace("require(functions)", add + Resources.Functions); } if (FragmentCode != null) { FragmentCode = FragmentCode.Replace("require(functions)", add + Resources.Functions); } List <ShaderLoadData> shaders = new List <ShaderLoadData>(); if (!string.IsNullOrEmpty(VertexCode)) { shaders.Add(new ShaderLoadData(ShaderType.VertexShader, VertexCode)); } if (!string.IsNullOrEmpty(FragmentCode)) { shaders.Add(new ShaderLoadData(ShaderType.FragmentShader, FragmentCode)); } if (!string.IsNullOrEmpty(GeometryCode)) { shaders.Add(new ShaderLoadData(ShaderType.GeometryShader, GeometryCode)); } if (!string.IsNullOrEmpty(TesseControlCode)) { shaders.Add(new ShaderLoadData(ShaderType.TessControlShader, TesseControlCode)); } if (!string.IsNullOrEmpty(TessEvaluationCode)) { shaders.Add(new ShaderLoadData(ShaderType.TessEvaluationShader, TessEvaluationCode)); } if (!string.IsNullOrEmpty(ComputeCode)) { shaders.Add(new ShaderLoadData(ShaderType.ComputeShader, ComputeCode)); } foreach (var data in shaders) { data.shader = LoadShader(data.type, data.code); if (data.shader == -1) { Debug.WriteLine(Name + ": " + CompileErrorMessage); //Console.WriteLine(CompileErrorMessage); return(Result.CompileError); } } program = GL.CreateProgram(); foreach (var data in shaders) { GL.AttachShader(program, data.shader); GL.DeleteShader(data.shader); } int status; GL.LinkProgram(program); GL.GetProgram(program, GetProgramParameterName.LinkStatus, out status); if (status == 0) { CompileErrorMessage = GL.GetProgramInfoLog(program); Debug.WriteLine(CompileErrorMessage); GL.DeleteProgram(program); program = -1; return(Result.CompileError); } foreach (var p in shaderParams) { p.Value.location = GetUniformLocation(p.Value.name); } Loaded = true; return(Result.Success); }