mirror of
https://github.com/raylib-cs/raylib-cs
synced 2025-06-30 19:03:42 -04:00
Models closer to working, just lights being weird
This commit is contained in:
@ -29,25 +29,36 @@ public struct Light
|
|||||||
|
|
||||||
public partial class models_material_pbr
|
public partial class models_material_pbr
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*******************************************************************************************
|
/*******************************************************************************************
|
||||||
*
|
*
|
||||||
* raylib [models] example - PBR material
|
* raylib [models] example - PBR material
|
||||||
*
|
*
|
||||||
* This example has been created using raylib 1.8 (www.raylib.com)
|
* This example has been created using raylib 1.8 (www.raylib.com)
|
||||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2017 Ramon Santamaria (@raysan5)
|
||||||
|
*
|
||||||
|
********************************************************************************************/
|
||||||
|
|
||||||
public const int CUBEMAP_SIZE = 512;
|
public const int CUBEMAP_SIZE = 512;
|
||||||
public const int IRRADIANCE_SIZE = 32;
|
public const int IRRADIANCE_SIZE = 32;
|
||||||
public const int PREFILTERED_SIZE = 256;
|
public const int PREFILTERED_SIZE = 256;
|
||||||
|
public const int BRDF_SIZE = 512;
|
||||||
public const int MAX_LIGHTS = 4;
|
public const int MAX_LIGHTS = 4;
|
||||||
public static int lightsCount = 0;
|
public static int lightsCount = 0;
|
||||||
public const float LIGHT_DISTANCE = 3.5f;
|
public const float LIGHT_DISTANCE = 3.5f;
|
||||||
public const float LIGHT_HEIGHT = 1.0f;
|
public const float LIGHT_HEIGHT = 1.0f;
|
||||||
|
|
||||||
// PBR material loading
|
// PBR material loading
|
||||||
//private static Material LoadMaterialPBR(Color albedo, float metalness, float roughness);
|
|
||||||
|
|
||||||
//private static Material LoadMaterialPBR(Color albedo, float metalness, float roughness);
|
//private static Material LoadMaterialPBR(Color albedo, float metalness, float roughness);
|
||||||
|
|
||||||
public unsafe static int Main()
|
public unsafe static int Main()
|
||||||
@ -83,23 +94,24 @@ public partial class models_material_pbr
|
|||||||
|
|
||||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
// Main game loop
|
// Main game loop
|
||||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||||
{
|
{
|
||||||
|
// Update
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
UpdateCamera(ref camera); // Update camera
|
UpdateCamera(ref camera); // Update camera
|
||||||
UpdateCamera(ref camera); // Update camera
|
|
||||||
// Send to material PBR shader camera view position
|
// Send to material PBR shader camera view position
|
||||||
// Send to material PBR shader camera view position
|
float[] cameraPos = { camera.position.x, camera.position.y, camera.position.z };
|
||||||
SetShaderValue(model.material.shader, model.material.shader.locs[(int)ShaderLocationIndex.LOC_VECTOR_VIEW], cameraPos, 3);
|
SetShaderValue(model.material.shader, model.material.shader.locs[(int)ShaderLocationIndex.LOC_VECTOR_VIEW], cameraPos, 3);
|
||||||
SetShaderValue(model.material.shader, 0, cameraPos, 3); //(int)model.material.shader.locs, cameraPos, 3);
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Draw
|
// Draw
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
|
|
||||||
ClearBackground(RAYWHITE);
|
ClearBackground(RAYWHITE);
|
||||||
|
|
||||||
BeginMode3D(camera);
|
BeginMode3D(camera);
|
||||||
|
|
||||||
@ -111,51 +123,7 @@ public partial class models_material_pbr
|
|||||||
|
|
||||||
DrawFPS(10, 10);
|
DrawFPS(10, 10);
|
||||||
|
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
//----------------------------------------------------------------------------------
|
|
||||||
}
|
|
||||||
|
|
||||||
// De-Initialization
|
|
||||||
//--------------------------------------------------------------------------------------
|
|
||||||
UnloadModel(model); // Unload skybox model
|
|
||||||
|
|
||||||
CloseWindow(); // Close window and OpenGL context
|
|
||||||
//--------------------------------------------------------------------------------------
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Light CreateLight(LightType type, Vector3 pos, Vector3 targ, Color color, Shader shader)
|
|
||||||
{
|
|
||||||
Light light = new Light() {
|
|
||||||
enabled = true,
|
|
||||||
type = type,
|
|
||||||
position = pos,
|
|
||||||
target = targ,
|
|
||||||
color = color,
|
|
||||||
};
|
|
||||||
|
|
||||||
string enabledName = "lights[x].enabled\0";
|
|
||||||
string typeName = "lights[x].type\0";
|
|
||||||
string posName = "lights[x].position\0";
|
|
||||||
string targetName = "lights[x].target\0";
|
|
||||||
string colorName = "lights[x].color\0";
|
|
||||||
|
|
||||||
light.enabledLoc = GetShaderLocation(shader, enabledName);
|
|
||||||
light.typeLoc = GetShaderLocation(shader, typeName);
|
|
||||||
light.posLoc = GetShaderLocation(shader, posName);
|
|
||||||
light.targetLoc = GetShaderLocation(shader, targetName);
|
|
||||||
light.colorLoc = GetShaderLocation(shader, colorName);
|
|
||||||
|
|
||||||
UpdateLightValues(shader, light);
|
|
||||||
|
|
||||||
return light;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void UpdateLightValues(Shader shader, Light light)
|
|
||||||
{
|
|
||||||
// Send to shader light enabled state and type
|
|
||||||
SetShaderValuei(shader, light.enabledLoc, new int[] { Convert.ToInt32(light.enabled) }, 1);
|
|
||||||
SetShaderValuei(shader, light.typeLoc, new int[] { Convert.ToInt32(light.type) }, 1);
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -192,16 +160,16 @@ public partial class models_material_pbr
|
|||||||
//mat.shader.locs[LOC_MAP_HEIGHT] = GetShaderLocation(mat.shader, "height.sampler");
|
//mat.shader.locs[LOC_MAP_HEIGHT] = GetShaderLocation(mat.shader, "height.sampler");
|
||||||
mat.shader.locs[(int)ShaderLocationIndex.LOC_MAP_IRRADIANCE] = GetShaderLocation(mat.shader, "irradianceMap");
|
mat.shader.locs[(int)ShaderLocationIndex.LOC_MAP_IRRADIANCE] = GetShaderLocation(mat.shader, "irradianceMap");
|
||||||
mat.shader.locs[(int)ShaderLocationIndex.LOC_MAP_PREFILTER] = GetShaderLocation(mat.shader, "prefilterMap");
|
mat.shader.locs[(int)ShaderLocationIndex.LOC_MAP_PREFILTER] = GetShaderLocation(mat.shader, "prefilterMap");
|
||||||
mat.shader.locs[(int)ShaderLocationIndex.LOC_MAP_PREFILTER] = GetShaderLocation(mat.shader, "prefilterMap");
|
mat.shader.locs[(int)ShaderLocationIndex.LOC_MAP_BRDF] = GetShaderLocation(mat.shader, "brdfLUT");
|
||||||
mat.shader.locs[(int)ShaderLocationIndex.LOC_MAP_BRDF] = GetShaderLocation(mat.shader, "brdfLUT");
|
|
||||||
// Set view matrix location
|
// Set view matrix location
|
||||||
// Set view matrix location
|
mat.shader.locs[(int)ShaderLocationIndex.LOC_MATRIX_MODEL] = GetShaderLocation(mat.shader, "matModel");
|
||||||
mat.shader.locs[(int)ShaderLocationIndex.LOC_MATRIX_MODEL] = GetShaderLocation(mat.shader, "matModel");
|
mat.shader.locs[(int)ShaderLocationIndex.LOC_MATRIX_VIEW] = GetShaderLocation(mat.shader, "view");
|
||||||
mat.shader.locs[(int)ShaderLocationIndex.LOC_MATRIX_VIEW] = GetShaderLocation(mat.shader, "view");
|
mat.shader.locs[(int)ShaderLocationIndex.LOC_VECTOR_VIEW] = GetShaderLocation(mat.shader, "viewPos");
|
||||||
mat.shader.locs[(int)ShaderLocationIndex.LOC_VECTOR_VIEW] = GetShaderLocation(mat.shader, "viewPos");
|
|
||||||
|
// Set PBR standard maps
|
||||||
// Set PBR standard maps
|
mat.maps[(int)TexmapIndex.MAP_ALBEDO].texture = LoadTexture("resources/pbr/trooper_albedo.png");
|
||||||
mat.maps[(int)TexmapIndex.MAP_ALBEDO].texture = LoadTexture("resources/pbr/trooper_albedo.png");
|
mat.maps[(int)TexmapIndex.MAP_NORMAL].texture = LoadTexture("resources/pbr/trooper_normals.png");
|
||||||
mat.maps[(int)TexmapIndex.MAP_METALNESS].texture = LoadTexture("resources/pbr/trooper_metalness.png");
|
mat.maps[(int)TexmapIndex.MAP_METALNESS].texture = LoadTexture("resources/pbr/trooper_metalness.png");
|
||||||
mat.maps[(int)TexmapIndex.MAP_ROUGHNESS].texture = LoadTexture("resources/pbr/trooper_roughness.png");
|
mat.maps[(int)TexmapIndex.MAP_ROUGHNESS].texture = LoadTexture("resources/pbr/trooper_roughness.png");
|
||||||
mat.maps[(int)TexmapIndex.MAP_OCCLUSION].texture = LoadTexture("resources/pbr/trooper_ao.png");
|
mat.maps[(int)TexmapIndex.MAP_OCCLUSION].texture = LoadTexture("resources/pbr/trooper_ao.png");
|
||||||
@ -209,7 +177,9 @@ public partial class models_material_pbr
|
|||||||
// Set environment maps
|
// Set environment maps
|
||||||
const string PATH_CUBEMAP_VS = "resources/shaders/cubemap.vs"; // Path to equirectangular to cubemap vertex shader
|
const string PATH_CUBEMAP_VS = "resources/shaders/cubemap.vs"; // Path to equirectangular to cubemap vertex shader
|
||||||
const string PATH_CUBEMAP_FS = "resources/shaders/cubemap.fs"; // Path to equirectangular to cubemap fragment shader
|
const string PATH_CUBEMAP_FS = "resources/shaders/cubemap.fs"; // Path to equirectangular to cubemap fragment shader
|
||||||
const string PATH_CUBEMAP_FS = "resources/shaders/cubemap.fs";
|
const string PATH_SKYBOX_VS = "resources/shaders/skybox.vs"; // Path to skybox vertex shader
|
||||||
|
const string PATH_IRRADIANCE_FS = "resources/shaders/irradiance.fs"; // Path to irradiance (GI) calculation fragment shader
|
||||||
|
const string PATH_PREFILTER_FS = "resources/shaders/prefilter.fs"; // Path to reflection prefilter calculation fragment shader
|
||||||
const string PATH_BRDF_VS = "resources/shaders/brdf.vs"; // Path to bidirectional reflectance distribution function vertex shader
|
const string PATH_BRDF_VS = "resources/shaders/brdf.vs"; // Path to bidirectional reflectance distribution function vertex shader
|
||||||
const string PATH_BRDF_FS = "resources/shaders/brdf.fs"; // Path to bidirectional reflectance distribution function fragment shader
|
const string PATH_BRDF_FS = "resources/shaders/brdf.fs"; // Path to bidirectional reflectance distribution function fragment shader
|
||||||
|
|
||||||
@ -231,9 +201,66 @@ public partial class models_material_pbr
|
|||||||
UnloadTexture(cubemap);
|
UnloadTexture(cubemap);
|
||||||
UnloadTexture(texHDR);
|
UnloadTexture(texHDR);
|
||||||
|
|
||||||
UnloadTexture(cubemap);
|
// Unload already used shaders (to create specific textures)
|
||||||
UnloadTexture(texHDR);
|
UnloadShader(shdrCubemap);
|
||||||
|
UnloadShader(shdrIrradiance);
|
||||||
|
UnloadShader(shdrPrefilter);
|
||||||
|
UnloadShader(shdrBRDF);
|
||||||
|
|
||||||
|
// Set textures filtering for better quality
|
||||||
|
SetTextureFilter(mat.maps[(int)TexmapIndex.MAP_ALBEDO].texture, (int)TextureFilterMode.FILTER_BILINEAR);
|
||||||
|
SetTextureFilter(mat.maps[(int)TexmapIndex.MAP_NORMAL].texture, (int)TextureFilterMode.FILTER_BILINEAR);
|
||||||
|
SetTextureFilter(mat.maps[(int)TexmapIndex.MAP_METALNESS].texture, (int)TextureFilterMode.FILTER_BILINEAR);
|
||||||
|
SetTextureFilter(mat.maps[(int)TexmapIndex.MAP_ROUGHNESS].texture, (int)TextureFilterMode.FILTER_BILINEAR);
|
||||||
|
SetTextureFilter(mat.maps[(int)TexmapIndex.MAP_OCCLUSION].texture, (int)TextureFilterMode.FILTER_BILINEAR);
|
||||||
|
|
||||||
|
// Enable sample usage in shader for assigned textures
|
||||||
|
SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "albedo.useSampler"), new int[] { 1 }, 1);
|
||||||
|
SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "normals.useSampler"), new int[] { 1 }, 1);
|
||||||
|
SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "metalness.useSampler"), new int[] { 1 }, 1);
|
||||||
|
SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "roughness.useSampler"), new int[] { 1 }, 1);
|
||||||
|
SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "occlusion.useSampler"), new int[] { 1 }, 1);
|
||||||
|
|
||||||
|
int renderModeLoc = GetShaderLocation(mat.shader, "renderMode");
|
||||||
|
SetShaderValuei(mat.shader, renderModeLoc, new int[] { 0 }, 1);
|
||||||
|
|
||||||
|
// Set up material properties color
|
||||||
|
mat.maps[(int)TexmapIndex.MAP_ALBEDO].color = albedo;
|
||||||
|
mat.maps[(int)TexmapIndex.MAP_NORMAL].color = new Color(128, 128, 255, 255);
|
||||||
|
mat.maps[(int)TexmapIndex.MAP_METALNESS].value = metalness;
|
||||||
|
mat.maps[(int)TexmapIndex.MAP_ROUGHNESS].value = roughness;
|
||||||
|
mat.maps[(int)TexmapIndex.MAP_OCCLUSION].value = 1.0f;
|
||||||
|
mat.maps[(int)TexmapIndex.MAP_EMISSION].value = 0.5f;
|
||||||
|
mat.maps[(int)TexmapIndex.MAP_HEIGHT].value = 0.5f;
|
||||||
|
|
||||||
|
return mat;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Light CreateLight(LightType type, Vector3 pos, Vector3 targ, Color color, Shader shader)
|
||||||
|
{
|
||||||
|
|
||||||
|
Light light = new Light();
|
||||||
|
|
||||||
|
if (lightsCount < MAX_LIGHTS)
|
||||||
|
{
|
||||||
|
light.enabled = true;
|
||||||
|
light.type = type;
|
||||||
|
light.position = pos;
|
||||||
|
light.target = targ;
|
||||||
|
light.color = color;
|
||||||
|
|
||||||
|
string enabledName = $"lights[{lightsCount}].enabled\0";
|
||||||
|
string typeName = $"lights[{lightsCount}].type\0";
|
||||||
|
string posName = $"lights[{lightsCount}].position\0";
|
||||||
|
string targetName = $"lights[{lightsCount}].target\0";
|
||||||
|
string colorName = $"lights[{lightsCount}].color\0";
|
||||||
|
|
||||||
|
light.enabledLoc = GetShaderLocation(shader, enabledName);
|
||||||
|
light.typeLoc = GetShaderLocation(shader, typeName);
|
||||||
|
light.posLoc = GetShaderLocation(shader, posName);
|
||||||
|
light.targetLoc = GetShaderLocation(shader, targetName);
|
||||||
|
light.colorLoc = GetShaderLocation(shader, colorName);
|
||||||
|
|
||||||
UpdateLightValues(shader, light);
|
UpdateLightValues(shader, light);
|
||||||
lightsCount++;
|
lightsCount++;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user