Exemple #1
0
        public static ShaderParameter[] ShaderParameters(ObjectInstance input)
        {
            List <ShaderParameter> output = new List <ShaderParameter>();

            foreach (var value in input.Properties)
            {
                ShaderUniformDataType type = ShaderUniformDataType.SHADER_UNIFORM_FLOAT;
                object property            = value.Value;
                switch (value.Value)
                {
                case double _:
                    type     = ShaderUniformDataType.SHADER_UNIFORM_FLOAT;
                    property = (float)((double)property);
                    break;

                case int _:
                    type     = ShaderUniformDataType.SHADER_UNIFORM_FLOAT;
                    property = (float)((int)property);
                    break;

                case ObjectInstance _:
                    type = ShaderUniformDataType.SHADER_UNIFORM_VEC4;
                    var vec4      = Vector4((ObjectInstance)property);
                    var floatsize = Marshal.SizeOf(typeof(float));
                    var ptr       = Marshal.AllocHGlobal(floatsize * 4);
                    Marshal.StructureToPtr(vec4, ptr, false);
                    property = ptr;
                    break;

                case string _:
                    type     = ShaderUniformDataType.SHADER_UNIFORM_SAMPLER2D;
                    property = Assets.PixelBuffer((string)property).texture;
                    break;
                }

                output.Add(new ShaderParameter(
                               (string)value.Key,
                               property,
                               type
                               ));
            }

            return(output.ToArray());
        }
Exemple #2
0
 /// <summary>Set shader uniform value</summary>
 public static void SetShaderValue <T>(Shader shader, int uniformLoc, Span <T> values, ShaderUniformDataType uniformType)
     where T : unmanaged
 {
     fixed(T *valuePtr = values)
     {
         SetShaderValue(shader, uniformLoc, valuePtr, uniformType);
     }
 }
Exemple #3
0
 /// <summary>Set shader uniform value</summary>
 public static void SetShaderValue <T>(Shader shader, int uniformLoc, T[] values, ShaderUniformDataType uniformType)
     where T : unmanaged
 {
     SetShaderValue(shader, uniformLoc, (Span <T>)values, uniformType);
 }
Exemple #4
0
 /// <summary>Set shader uniform value</summary>
 public static void SetShaderValue <T>(Shader shader, int uniformLoc, T value, ShaderUniformDataType uniformType)
     where T : unmanaged
 {
     SetShaderValue(shader, uniformLoc, &value, uniformType);
 }
Exemple #5
0
 public ShaderParameter(string name, object value, ShaderUniformDataType type)
 {
     dataType     = type;
     uniformName  = name;
     uniformValue = value;
 }
Exemple #6
0
        // Load PBR material (Supports: ALBEDO, NORMAL, METALNESS, ROUGHNESS, AO, EMMISIVE, HEIGHT maps)
        // NOTE: PBR shader is loaded inside this function
        unsafe public static Material LoadMaterialPBR(Color albedo, float metalness, float roughness)
        {
            // NOTE: All maps textures are set to { 0 )
            Material mat = Raylib.LoadMaterialDefault();

            string shaderPath  = "resources/shaders/glsl330";
            string PATH_PBR_VS = $"{shaderPath}/pbr.vs";
            string PATH_PBR_FS = $"{shaderPath}/pbr.fs";

            mat.shader = LoadShader(PATH_PBR_VS, PATH_PBR_FS);

            // Temporary unsafe pointers into material arrays.
            MaterialMap *maps = (MaterialMap *)mat.maps.ToPointer();
            int *        locs = (int *)mat.shader.locs.ToPointer();

            // Get required locations points for PBR material
            // NOTE: Those location names must be available and used in the shader code
            locs[(int)ShaderLocationIndex.LOC_MAP_ALBEDO]     = GetShaderLocation(mat.shader, "albedo.sampler");
            locs[(int)ShaderLocationIndex.LOC_MAP_METALNESS]  = GetShaderLocation(mat.shader, "metalness.sampler");
            locs[(int)ShaderLocationIndex.LOC_MAP_NORMAL]     = GetShaderLocation(mat.shader, "normals.sampler");
            locs[(int)ShaderLocationIndex.LOC_MAP_ROUGHNESS]  = GetShaderLocation(mat.shader, "roughness.sampler");
            locs[(int)ShaderLocationIndex.LOC_MAP_OCCLUSION]  = GetShaderLocation(mat.shader, "occlusion.sampler");
            locs[(int)ShaderLocationIndex.LOC_MAP_IRRADIANCE] = GetShaderLocation(mat.shader, "irradianceMap");
            locs[(int)ShaderLocationIndex.LOC_MAP_PREFILTER]  = GetShaderLocation(mat.shader, "prefilterMap");
            locs[(int)ShaderLocationIndex.LOC_MAP_BRDF]       = GetShaderLocation(mat.shader, "brdfLUT");

            // Set view matrix location
            locs[(int)ShaderLocationIndex.LOC_MATRIX_MODEL] = GetShaderLocation(mat.shader, "matModel");
            locs[(int)ShaderLocationIndex.LOC_VECTOR_VIEW]  = GetShaderLocation(mat.shader, "viewPos");

            // Set PBR standard maps
            maps[(int)MaterialMapType.MAP_ALBEDO].texture    = LoadTexture("resources/pbr/trooper_albedo.png");
            maps[(int)MaterialMapType.MAP_NORMAL].texture    = LoadTexture("resources/pbr/trooper_normals.png");
            maps[(int)MaterialMapType.MAP_METALNESS].texture = LoadTexture("resources/pbr/trooper_metalness.png");
            maps[(int)MaterialMapType.MAP_ROUGHNESS].texture = LoadTexture("resources/pbr/trooper_roughness.png");
            maps[(int)MaterialMapType.MAP_OCCLUSION].texture = LoadTexture("resources/pbr/trooper_ao.png");

            // Set textures filtering for better quality
            SetTextureFilter(maps[(int)MaterialMapType.MAP_ALBEDO].texture, FILTER_BILINEAR);
            SetTextureFilter(maps[(int)MaterialMapType.MAP_NORMAL].texture, FILTER_BILINEAR);
            SetTextureFilter(maps[(int)MaterialMapType.MAP_METALNESS].texture, FILTER_BILINEAR);
            SetTextureFilter(maps[(int)MaterialMapType.MAP_ROUGHNESS].texture, FILTER_BILINEAR);
            SetTextureFilter(maps[(int)MaterialMapType.MAP_OCCLUSION].texture, FILTER_BILINEAR);

            // Enable sample usage in shader for assigned textures
            ShaderUniformDataType uniformType = ShaderUniformDataType.UNIFORM_INT;

            Utils.SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "albedo.useSampler"), 1, uniformType);
            Utils.SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "normals.useSampler"), 1, uniformType);
            Utils.SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "metalness.useSampler"), 1, uniformType);
            Utils.SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "roughness.useSampler"), 1, uniformType);
            Utils.SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "occlusion.useSampler"), 1, uniformType);

            int renderModeLoc = GetShaderLocation(mat.shader, "renderMode");

            Utils.SetShaderValue(mat.shader, renderModeLoc, 0, uniformType);

            // Set up material properties color
            maps[(int)MaterialMapType.MAP_ALBEDO].color    = albedo;
            maps[(int)MaterialMapType.MAP_NORMAL].color    = new Color(128, 128, 255, 255);
            maps[(int)MaterialMapType.MAP_METALNESS].value = metalness;
            maps[(int)MaterialMapType.MAP_ROUGHNESS].value = roughness;
            maps[(int)MaterialMapType.MAP_OCCLUSION].value = 1.0f;
            maps[(int)MaterialMapType.MAP_EMISSION].value  = 0.5f;
            maps[(int)MaterialMapType.MAP_HEIGHT].value    = 0.5f;

            // Load shaders for material
            string PATH_CUBEMAP_VS    = $"{shaderPath}/cubemap.vs";    // Path to equirectangular to cubemap vertex shader
            string PATH_CUBEMAP_FS    = $"{shaderPath}/cubemap.fs";    // Path to equirectangular to cubemap fragment shader
            string PATH_SKYBOX_VS     = $"{shaderPath}/skybox.vs";     // Path to skybox vertex shader
            string PATH_IRRADIANCE_FS = $"{shaderPath}/irradiance.fs"; // Path to irradiance (GI) calculation fragment shader
            string PATH_PREFILTER_FS  = $"{shaderPath}/prefilter.fs";  // Path to reflection prefilter calculation fragment shader
            string PATH_BRDF_VS       = $"{shaderPath}/brdf.vs";       // Path to bidirectional reflectance distribution function vertex shader
            string PATH_BRDF_FS       = $"{shaderPath}/brdf.fs";       // Path to bidirectional reflectance distribution function fragment shader

            Shader shdrCubemap    = LoadShader(PATH_CUBEMAP_VS, PATH_CUBEMAP_FS);
            Shader shdrIrradiance = LoadShader(PATH_SKYBOX_VS, PATH_IRRADIANCE_FS);
            Shader shdrPrefilter  = LoadShader(PATH_SKYBOX_VS, PATH_PREFILTER_FS);
            Shader shdrBRDF       = LoadShader(PATH_BRDF_VS, PATH_BRDF_FS);

            // Generate cubemap from panorama texture
            //--------------------------------------------------------------------------------------------------------
            Texture2D panorama = LoadTexture("resources/dresden_square_1k.hdr");
            Texture2D cubemap  = GenTextureCubemap(shdrCubemap, panorama, CUBEMAP_SIZE, PixelFormat.UNCOMPRESSED_R32G32B32);

            Utils.SetShaderValue(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), 0, uniformType);

            // Generate irradiance map from cubemap texture
            //--------------------------------------------------------------------------------------------------------
            Utils.SetShaderValue(shdrIrradiance, GetShaderLocation(shdrIrradiance, "environmentMap"), 0, uniformType);
            maps[(int)MaterialMapType.MAP_IRRADIANCE].texture = GenTextureIrradiance(shdrIrradiance, cubemap, IRRADIANCE_SIZE);

            // Generate prefilter map from cubemap texture
            //--------------------------------------------------------------------------------------------------------
            Utils.SetShaderValue(shdrPrefilter, GetShaderLocation(shdrPrefilter, "environmentMap"), 0, uniformType);
            maps[(int)MaterialMapType.MAP_PREFILTER].texture = GenTexturePrefilter(shdrPrefilter, cubemap, PREFILTERED_SIZE);

            // Generate BRDF (bidirectional reflectance distribution function) texture (using shader)
            //--------------------------------------------------------------------------------------------------------
            maps[(int)MaterialMapType.MAP_BRDF].texture = GenTextureBRDF(shdrBRDF, BRDF_SIZE);

            // Unload temporary shaders and textures
            UnloadShader(shdrCubemap);
            UnloadShader(shdrIrradiance);
            UnloadShader(shdrPrefilter);
            UnloadShader(shdrBRDF);

            UnloadTexture(panorama);
            UnloadTexture(cubemap);

            return(mat);
        }
Exemple #7
0
        public static void SetShaderValueV(Shader shader, int uniformLoc, float[] value, ShaderUniformDataType uniformType, int count)
        {
            IntPtr valuePtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(float)) * value.Length);

            Marshal.Copy(value, 0, valuePtr, value.Length);
            Raylib.SetShaderValueV(shader, uniformLoc, valuePtr, uniformType, count);
            Marshal.FreeHGlobal(valuePtr);
        }
Exemple #8
0
 public static void SetShaderValue(Shader shader, int uniformLoc, float value, ShaderUniformDataType uniformType = ShaderUniformDataType.UNIFORM_FLOAT)
 {
     Raylib.SetShaderValue(shader, uniformLoc, ref value, uniformType);
 }
        public static void SetShaderValueV <T>(Shader shader, int uniformLoc, T[] value, ShaderUniformDataType uniformType, int count)
        {
            GCHandle pinnedData = GCHandle.Alloc(value, GCHandleType.Pinned);

            Raylib.SetShaderValueV(shader, uniformLoc, pinnedData.AddrOfPinnedObject(), uniformType, count);
            pinnedData.Free();
        }