mirror of
https://github.com/raylib-cs/raylib-cs
synced 2025-09-09 03:01:41 -04:00
Fixed format
This commit is contained in:
@@ -18,16 +18,16 @@
|
|||||||
********************************************************************************************/
|
********************************************************************************************/
|
||||||
|
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using static Raylib_cs.Raylib;
|
|
||||||
using Examples.Shared;
|
using Examples.Shared;
|
||||||
|
using static Raylib_cs.Raylib;
|
||||||
|
|
||||||
namespace Examples.Shaders;
|
namespace Examples.Shaders;
|
||||||
|
|
||||||
public class BasicPbr
|
public class BasicPbr
|
||||||
{
|
{
|
||||||
const int GLSL_VERSION = 330;
|
private const int GLSL_VERSION = 330;
|
||||||
|
|
||||||
public unsafe static int Main()
|
public static unsafe int Main()
|
||||||
{
|
{
|
||||||
// Initialization
|
// Initialization
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
@@ -47,10 +47,7 @@ public class BasicPbr
|
|||||||
camera.Projection = CameraProjection.Perspective;
|
camera.Projection = CameraProjection.Perspective;
|
||||||
|
|
||||||
// Load PBR shader and setup all required locations
|
// Load PBR shader and setup all required locations
|
||||||
Shader shader = LoadShader(
|
var shader = LoadShader("resources/shaders/glsl330/pbr.vs", "resources/shaders/glsl330/pbr.fs");
|
||||||
"resources/shaders/glsl330/pbr.vs",
|
|
||||||
"resources/shaders/glsl330/pbr.fs"
|
|
||||||
);
|
|
||||||
|
|
||||||
shader.Locs[(int)ShaderLocationIndex.MapAlbedo] = GetShaderLocation(shader, "albedoMap");
|
shader.Locs[(int)ShaderLocationIndex.MapAlbedo] = GetShaderLocation(shader, "albedoMap");
|
||||||
// WARNING: Metalness, roughness, and ambient occlusion are all packed into a MRA texture
|
// WARNING: Metalness, roughness, and ambient occlusion are all packed into a MRA texture
|
||||||
@@ -66,28 +63,28 @@ public class BasicPbr
|
|||||||
|
|
||||||
// Setup additional required shader locations, including lights data
|
// Setup additional required shader locations, including lights data
|
||||||
shader.Locs[(int)ShaderLocationIndex.VectorView] = GetShaderLocation(shader, "viewPos");
|
shader.Locs[(int)ShaderLocationIndex.VectorView] = GetShaderLocation(shader, "viewPos");
|
||||||
int lightCountLoc = GetShaderLocation(shader, "numOfLights");
|
var lightCountLoc = GetShaderLocation(shader, "numOfLights");
|
||||||
int maxLightCount = 4;
|
var maxLightCount = 4;
|
||||||
SetShaderValue(shader, lightCountLoc, &maxLightCount, ShaderUniformDataType.Int);
|
SetShaderValue(shader, lightCountLoc, &maxLightCount, ShaderUniformDataType.Int);
|
||||||
|
|
||||||
// Setup ambient color and intensity parameters
|
// Setup ambient color and intensity parameters
|
||||||
float ambientIntensity = 0.02f;
|
var ambientIntensity = 0.02f;
|
||||||
Color ambientColor = new Color(26, 32, 135, 255);
|
var ambientColor = new Color(26, 32, 135, 255);
|
||||||
Vector3 ambientColorNormalized = new Vector3(ambientColor.R / 255.0F, ambientColor.G / 255.0F, ambientColor.B / 255.0F);
|
var ambientColorNormalized = new Vector3(ambientColor.R / 255.0F, ambientColor.G / 255.0F, ambientColor.B / 255.0F);
|
||||||
SetShaderValue(shader, GetShaderLocation(shader, "ambientColor"), &ambientColorNormalized, ShaderUniformDataType.Vec3);
|
SetShaderValue(shader, GetShaderLocation(shader, "ambientColor"), &ambientColorNormalized, ShaderUniformDataType.Vec3);
|
||||||
SetShaderValue(shader, GetShaderLocation(shader, "ambient"), &ambientIntensity, ShaderUniformDataType.Float);
|
SetShaderValue(shader, GetShaderLocation(shader, "ambient"), &ambientIntensity, ShaderUniformDataType.Float);
|
||||||
|
|
||||||
// Get location for shader parameters that can be modified in real time
|
// Get location for shader parameters that can be modified in real time
|
||||||
int emissiveIntensityLoc = GetShaderLocation(shader, "emissivePower");
|
var emissiveIntensityLoc = GetShaderLocation(shader, "emissivePower");
|
||||||
int emissiveColorLoc = GetShaderLocation(shader, "emissiveColor");
|
var emissiveColorLoc = GetShaderLocation(shader, "emissiveColor");
|
||||||
int textureTilingLoc = GetShaderLocation(shader, "tiling");
|
var textureTilingLoc = GetShaderLocation(shader, "tiling");
|
||||||
|
|
||||||
// Load old car model using PBR maps and shader
|
// Load old car model using PBR maps and shader
|
||||||
// WARNING: We know this model consists of a single model.meshes[0] and
|
// WARNING: We know this model consists of a single model.meshes[0] and
|
||||||
// that model.materials[0] is by default assigned to that mesh
|
// that model.materials[0] is by default assigned to that mesh
|
||||||
// There could be more complex models consisting of multiple meshes and
|
// There could be more complex models consisting of multiple meshes and
|
||||||
// multiple materials defined for those meshes... but always 1 mesh = 1 material
|
// multiple materials defined for those meshes... but always 1 mesh = 1 material
|
||||||
Model car = LoadModel("resources/models/gltf/old_car_new.glb");
|
var car = LoadModel("resources/models/gltf/old_car_new.glb");
|
||||||
|
|
||||||
// Assign already setup PBR shader to model.materials[0], used by models.meshes[0]
|
// Assign already setup PBR shader to model.materials[0], used by models.meshes[0]
|
||||||
car.Materials[0].Shader = shader;
|
car.Materials[0].Shader = shader;
|
||||||
@@ -107,7 +104,7 @@ public class BasicPbr
|
|||||||
|
|
||||||
// Load floor model mesh and assign material parameters
|
// Load floor model mesh and assign material parameters
|
||||||
// NOTE: A basic plane shape can be generated instead of being loaded from a model file
|
// NOTE: A basic plane shape can be generated instead of being loaded from a model file
|
||||||
Model floor = LoadModel("resources/models/gltf/plane.glb");
|
var floor = LoadModel("resources/models/gltf/plane.glb");
|
||||||
//Mesh floorMesh = GenMeshPlane(10, 10, 10, 10);
|
//Mesh floorMesh = GenMeshPlane(10, 10, 10, 10);
|
||||||
//GenMeshTangents(&floorMesh); // TODO: Review tangents generation
|
//GenMeshTangents(&floorMesh); // TODO: Review tangents generation
|
||||||
//Model floor = LoadModelFromMesh(floorMesh);
|
//Model floor = LoadModelFromMesh(floorMesh);
|
||||||
@@ -127,18 +124,45 @@ public class BasicPbr
|
|||||||
|
|
||||||
// Models texture tiling parameter can be stored in the Material struct if required (CURRENTLY NOT USED)
|
// Models texture tiling parameter can be stored in the Material struct if required (CURRENTLY NOT USED)
|
||||||
// NOTE: Material.params[4] are available for generic parameters storage (float)
|
// NOTE: Material.params[4] are available for generic parameters storage (float)
|
||||||
Vector2 carTextureTiling = new Vector2(0.5f, 0.5f);
|
var carTextureTiling = new Vector2(0.5f, 0.5f);
|
||||||
Vector2 floorTextureTiling = new Vector2(0.5f, 0.5f);
|
var floorTextureTiling = new Vector2(0.5f, 0.5f);
|
||||||
|
|
||||||
// Create some lights
|
// Create some lights
|
||||||
PbrLight[] lights = new PbrLight[4];
|
var lights = new PbrLight[4];
|
||||||
lights[0] = PbrLights.CreateLight(0, PbrLightType.Point, new Vector3(-1.0f, 1.0f, -2.0f), new Vector3(0.0f, 0.0f, 0.0f), Color.Yellow, 4.0f, shader);
|
lights[0] = PbrLights.CreateLight(
|
||||||
lights[1] = PbrLights.CreateLight(1, PbrLightType.Point, new Vector3(2.0f, 1.0f, 1.0f), new Vector3(0.0f, 0.0f, 0.0f), Color.Green, 3.3f, shader);
|
0,
|
||||||
lights[2] = PbrLights.CreateLight(2, PbrLightType.Point, new Vector3(-2.0f, 1.0f, 1.0f), new Vector3(0.0f, 0.0f, 0.0f), Color.Red, 8.3f, shader);
|
PbrLightType.Point,
|
||||||
lights[3] = PbrLights.CreateLight(3, PbrLightType.Point, new Vector3(1.0f, 1.0f, -2.0f), new Vector3(0.0f, 0.0f, 0.0f), Color.Black, 2.0f, shader);
|
new Vector3(-1.0f, 1.0f, -2.0f),
|
||||||
|
new Vector3(0.0f, 0.0f, 0.0f),
|
||||||
|
Color.Yellow,
|
||||||
|
4.0f,
|
||||||
|
shader);
|
||||||
|
lights[1] = PbrLights.CreateLight(1,
|
||||||
|
PbrLightType.Point,
|
||||||
|
new Vector3(2.0f, 1.0f, 1.0f),
|
||||||
|
new Vector3(0.0f, 0.0f, 0.0f),
|
||||||
|
Color.Green,
|
||||||
|
3.3f,
|
||||||
|
shader);
|
||||||
|
lights[2] = PbrLights.CreateLight(
|
||||||
|
2, PbrLightType.Point,
|
||||||
|
new Vector3(-2.0f, 1.0f, 1.0f),
|
||||||
|
new Vector3(0.0f, 0.0f, 0.0f),
|
||||||
|
Color.Red,
|
||||||
|
8.3f,
|
||||||
|
shader);
|
||||||
|
lights[3] = PbrLights.CreateLight(
|
||||||
|
3,
|
||||||
|
PbrLightType.Point,
|
||||||
|
new Vector3(1.0f, 1.0f, -2.0f),
|
||||||
|
new Vector3(0.0f, 0.0f, 0.0f),
|
||||||
|
Color.Black,
|
||||||
|
2.0f,
|
||||||
|
shader);
|
||||||
|
|
||||||
// Setup material texture maps usage in shader
|
// Setup material texture maps usage in shader
|
||||||
// NOTE: By default, the texture maps are always used
|
// NOTE: By default, the texture maps are always used
|
||||||
int usage = 1;
|
var usage = 1;
|
||||||
SetShaderValue(shader, GetShaderLocation(shader, "useTexAlbedo"), &usage, ShaderUniformDataType.Int);
|
SetShaderValue(shader, GetShaderLocation(shader, "useTexAlbedo"), &usage, ShaderUniformDataType.Int);
|
||||||
SetShaderValue(shader, GetShaderLocation(shader, "useTexNormal"), &usage, ShaderUniformDataType.Int);
|
SetShaderValue(shader, GetShaderLocation(shader, "useTexNormal"), &usage, ShaderUniformDataType.Int);
|
||||||
SetShaderValue(shader, GetShaderLocation(shader, "useTexMRA"), &usage, ShaderUniformDataType.Int);
|
SetShaderValue(shader, GetShaderLocation(shader, "useTexMRA"), &usage, ShaderUniformDataType.Int);
|
||||||
@@ -155,17 +179,32 @@ public class BasicPbr
|
|||||||
UpdateCamera(&camera, CameraMode.Orbital);
|
UpdateCamera(&camera, CameraMode.Orbital);
|
||||||
|
|
||||||
// Update the shader with the camera view vector (points towards { 0.0f, 0.0f, 0.0f })
|
// Update the shader with the camera view vector (points towards { 0.0f, 0.0f, 0.0f })
|
||||||
Vector3 cameraPos = camera.Position;
|
var cameraPos = camera.Position;
|
||||||
SetShaderValue(shader, shader.Locs[(int)ShaderLocationIndex.VectorView], cameraPos, ShaderUniformDataType.Vec3);
|
SetShaderValue(shader, shader.Locs[(int)ShaderLocationIndex.VectorView], cameraPos, ShaderUniformDataType.Vec3);
|
||||||
|
|
||||||
// Check key inputs to enable/disable lights
|
// Check key inputs to enable/disable lights
|
||||||
if (IsKeyPressed(KeyboardKey.One)) { lights[2].Enabled = !lights[2].Enabled; }
|
if (IsKeyPressed(KeyboardKey.One))
|
||||||
if (IsKeyPressed(KeyboardKey.Two)) { lights[1].Enabled = !lights[1].Enabled; }
|
{
|
||||||
if (IsKeyPressed(KeyboardKey.Three)) { lights[3].Enabled = !lights[3].Enabled; }
|
lights[2].Enabled = !lights[2].Enabled;
|
||||||
if (IsKeyPressed(KeyboardKey.Four)) { lights[0].Enabled = !lights[0].Enabled; }
|
}
|
||||||
|
|
||||||
|
if (IsKeyPressed(KeyboardKey.Two))
|
||||||
|
{
|
||||||
|
lights[1].Enabled = !lights[1].Enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IsKeyPressed(KeyboardKey.Three))
|
||||||
|
{
|
||||||
|
lights[3].Enabled = !lights[3].Enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IsKeyPressed(KeyboardKey.Four))
|
||||||
|
{
|
||||||
|
lights[0].Enabled = !lights[0].Enabled;
|
||||||
|
}
|
||||||
|
|
||||||
// Update light values on shader (actually, only enable/disable them)
|
// Update light values on shader (actually, only enable/disable them)
|
||||||
for (int i = 0; i < 4; i++)
|
for (var i = 0; i < 4; i++)
|
||||||
{
|
{
|
||||||
UpdateLight(shader, lights[i]);
|
UpdateLight(shader, lights[i]);
|
||||||
}
|
}
|
||||||
@@ -181,30 +220,26 @@ public class BasicPbr
|
|||||||
|
|
||||||
// Set floor model texture tiling and emissive color parameters on shader
|
// Set floor model texture tiling and emissive color parameters on shader
|
||||||
SetShaderValue(shader, textureTilingLoc, &floorTextureTiling, ShaderUniformDataType.Vec2);
|
SetShaderValue(shader, textureTilingLoc, &floorTextureTiling, ShaderUniformDataType.Vec2);
|
||||||
Vector4 floorEmissiveColor = ColorNormalize(floor.Materials[0].Maps[(int) MaterialMapIndex.Emission].Color);
|
var floorEmissiveColor = ColorNormalize(floor.Materials[0].Maps[(int)MaterialMapIndex.Emission].Color);
|
||||||
SetShaderValue(shader, emissiveColorLoc, &floorEmissiveColor, ShaderUniformDataType.Vec4);
|
SetShaderValue(shader, emissiveColorLoc, &floorEmissiveColor, ShaderUniformDataType.Vec4);
|
||||||
|
|
||||||
DrawModel(floor, Vector3.Zero, 5.0f, Color.White); // Draw floor model
|
DrawModel(floor, Vector3.Zero, 5.0f, Color.White); // Draw floor model
|
||||||
|
|
||||||
// Set old car model texture tiling, emissive color and emissive intensity parameters on shader
|
// Set old car model texture tiling, emissive color and emissive intensity parameters on shader
|
||||||
SetShaderValue(shader, textureTilingLoc, &carTextureTiling, ShaderUniformDataType.Vec2);
|
SetShaderValue(shader, textureTilingLoc, &carTextureTiling, ShaderUniformDataType.Vec2);
|
||||||
Vector4 carEmissiveColor = ColorNormalize(car.Materials[0].Maps[(int) MaterialMapIndex.Emission].Color);
|
var carEmissiveColor = ColorNormalize(car.Materials[0].Maps[(int)MaterialMapIndex.Emission].Color);
|
||||||
SetShaderValue(shader, emissiveColorLoc, &carEmissiveColor, ShaderUniformDataType.Vec4);
|
SetShaderValue(shader, emissiveColorLoc, &carEmissiveColor, ShaderUniformDataType.Vec4);
|
||||||
float emissiveIntensity = 0.01f;
|
var emissiveIntensity = 0.01f;
|
||||||
SetShaderValue(shader, emissiveIntensityLoc, &emissiveIntensity, ShaderUniformDataType.Float);
|
SetShaderValue(shader, emissiveIntensityLoc, &emissiveIntensity, ShaderUniformDataType.Float);
|
||||||
|
|
||||||
DrawModel(car, Vector3.Zero, 0.005f, Color.White); // Draw car model
|
DrawModel(car, Vector3.Zero, 0.005f, Color.White); // Draw car model
|
||||||
|
|
||||||
// Draw spheres to show the lights positions
|
// Draw spheres to show the lights positions
|
||||||
for (int i = 0; i < 4; i++)
|
for (var i = 0; i < 4; i++)
|
||||||
{
|
{
|
||||||
var color = lights[i].Color;
|
var color = lights[i].Color;
|
||||||
var lightColor = new Color(
|
var lightColor = new Color((byte)(color.X * 255), (byte)(color.Y * 255), (byte)(color.Z * 255),
|
||||||
(byte)(color.X * 255),
|
(byte)(color.W * 255));
|
||||||
(byte)(color.Y * 255),
|
|
||||||
(byte)(color.Z * 255),
|
|
||||||
(byte)(color.W * 255)
|
|
||||||
);
|
|
||||||
|
|
||||||
if (lights[i].Enabled)
|
if (lights[i].Enabled)
|
||||||
{
|
{
|
||||||
@@ -249,7 +284,7 @@ public class BasicPbr
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void UpdateLight(Shader shader, PbrLight light)
|
private static void UpdateLight(Shader shader, PbrLight light)
|
||||||
{
|
{
|
||||||
SetShaderValue(shader, light.EnabledLoc, light.Enabled, ShaderUniformDataType.Int);
|
SetShaderValue(shader, light.EnabledLoc, light.Enabled, ShaderUniformDataType.Int);
|
||||||
SetShaderValue(shader, light.TypeLoc, light.Type, ShaderUniformDataType.Int);
|
SetShaderValue(shader, light.TypeLoc, light.Type, ShaderUniformDataType.Int);
|
||||||
|
Reference in New Issue
Block a user