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
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - PBR material
|
||||
*
|
||||
* 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)
|
||||
*
|
||||
* Copyright (c) 2017 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
public const int CUBEMAP_SIZE = 512;
|
||||
public const int IRRADIANCE_SIZE = 32;
|
||||
public const int PREFILTERED_SIZE = 256;
|
||||
|
||||
public const int BRDF_SIZE = 512;
|
||||
public const int MAX_LIGHTS = 4;
|
||||
public static int lightsCount = 0;
|
||||
public const float LIGHT_DISTANCE = 3.5f;
|
||||
public const float LIGHT_HEIGHT = 1.0f;
|
||||
|
||||
// PBR material loading
|
||||
//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()
|
||||
@ -83,7 +94,8 @@ public partial class models_material_pbr
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
@ -111,51 +123,7 @@ public partial class models_material_pbr
|
||||
|
||||
DrawFPS(10, 10);
|
||||
|
||||
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);
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
@ -195,13 +163,13 @@ public partial class models_material_pbr
|
||||
mat.shader.locs[(int)ShaderLocationIndex.LOC_MAP_BRDF] = GetShaderLocation(mat.shader, "brdfLUT");
|
||||
|
||||
// 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_VIEW] = GetShaderLocation(mat.shader, "view");
|
||||
mat.shader.locs[(int)ShaderLocationIndex.LOC_VECTOR_VIEW] = GetShaderLocation(mat.shader, "viewPos");
|
||||
|
||||
// Set PBR standard maps
|
||||
mat.maps[(int)TexmapIndex.MAP_ALBEDO].texture = LoadTexture("resources/pbr/trooper_albedo.png");
|
||||
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_VECTOR_VIEW] = GetShaderLocation(mat.shader, "viewPos");
|
||||
|
||||
// Set PBR standard maps
|
||||
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_ROUGHNESS].texture = LoadTexture("resources/pbr/trooper_roughness.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
|
||||
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";
|
||||
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_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(texHDR);
|
||||
|
||||
UnloadTexture(cubemap);
|
||||
UnloadTexture(texHDR);
|
||||
|
||||
// Unload already used shaders (to create specific textures)
|
||||
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);
|
||||
lightsCount++;
|
||||
}
|
||||
|
Reference in New Issue
Block a user