Group example utils into folder
This commit is contained in:
parent
c5560664ed
commit
d1f0b9fd91
8 changed files with 17 additions and 14 deletions
|
|
@ -27,7 +27,7 @@
|
||||||
|
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using static Raylib_cs.Raylib;
|
using static Raylib_cs.Raylib;
|
||||||
using static Examples.Rlights;
|
using Examples.Shared;
|
||||||
|
|
||||||
namespace Examples.Shaders;
|
namespace Examples.Shaders;
|
||||||
|
|
||||||
|
|
@ -77,7 +77,7 @@ public class BasicLighting
|
||||||
|
|
||||||
// Using 4 point lights: Color.gold, Color.red, Color.green and Color.blue
|
// Using 4 point lights: Color.gold, Color.red, Color.green and Color.blue
|
||||||
Light[] lights = new Light[4];
|
Light[] lights = new Light[4];
|
||||||
lights[0] = CreateLight(
|
lights[0] = Rlights.CreateLight(
|
||||||
0,
|
0,
|
||||||
LightType.Point,
|
LightType.Point,
|
||||||
new Vector3(-2, 1, -2),
|
new Vector3(-2, 1, -2),
|
||||||
|
|
@ -85,7 +85,7 @@ public class BasicLighting
|
||||||
Color.YELLOW,
|
Color.YELLOW,
|
||||||
shader
|
shader
|
||||||
);
|
);
|
||||||
lights[1] = CreateLight(
|
lights[1] = Rlights.CreateLight(
|
||||||
1,
|
1,
|
||||||
LightType.Point,
|
LightType.Point,
|
||||||
new Vector3(2, 1, 2),
|
new Vector3(2, 1, 2),
|
||||||
|
|
@ -93,7 +93,7 @@ public class BasicLighting
|
||||||
Color.RED,
|
Color.RED,
|
||||||
shader
|
shader
|
||||||
);
|
);
|
||||||
lights[2] = CreateLight(
|
lights[2] = Rlights.CreateLight(
|
||||||
2,
|
2,
|
||||||
LightType.Point,
|
LightType.Point,
|
||||||
new Vector3(-2, 1, 2),
|
new Vector3(-2, 1, 2),
|
||||||
|
|
@ -101,7 +101,7 @@ public class BasicLighting
|
||||||
Color.GREEN,
|
Color.GREEN,
|
||||||
shader
|
shader
|
||||||
);
|
);
|
||||||
lights[3] = CreateLight(
|
lights[3] = Rlights.CreateLight(
|
||||||
3,
|
3,
|
||||||
LightType.Point,
|
LightType.Point,
|
||||||
new Vector3(2, 1, -2),
|
new Vector3(2, 1, -2),
|
||||||
|
|
@ -138,10 +138,10 @@ public class BasicLighting
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update light values (actually, only enable/disable them)
|
// Update light values (actually, only enable/disable them)
|
||||||
UpdateLightValues(shader, lights[0]);
|
Rlights.UpdateLightValues(shader, lights[0]);
|
||||||
UpdateLightValues(shader, lights[1]);
|
Rlights.UpdateLightValues(shader, lights[1]);
|
||||||
UpdateLightValues(shader, lights[2]);
|
Rlights.UpdateLightValues(shader, lights[2]);
|
||||||
UpdateLightValues(shader, lights[3]);
|
Rlights.UpdateLightValues(shader, lights[3]);
|
||||||
|
|
||||||
// Update the light shader with the camera view position
|
// Update the light shader with the camera view position
|
||||||
Raylib.SetShaderValue(
|
Raylib.SetShaderValue(
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using static Raylib_cs.Raylib;
|
using static Raylib_cs.Raylib;
|
||||||
using static Raylib_cs.Raymath;
|
using static Raylib_cs.Raymath;
|
||||||
using static Examples.Rlights;
|
using Examples.Shared;
|
||||||
|
|
||||||
namespace Examples.Shaders;
|
namespace Examples.Shaders;
|
||||||
|
|
||||||
|
|
@ -88,7 +88,7 @@ public class Fog
|
||||||
Raylib.SetMaterialShader(ref modelC, 0, ref shader);
|
Raylib.SetMaterialShader(ref modelC, 0, ref shader);
|
||||||
|
|
||||||
// Using just 1 point lights
|
// Using just 1 point lights
|
||||||
CreateLight(0, LightType.Point, new Vector3(0, 2, 6), Vector3.Zero, Color.WHITE, shader);
|
Rlights.CreateLight(0, LightType.Point, new Vector3(0, 2, 6), Vector3.Zero, Color.WHITE, shader);
|
||||||
|
|
||||||
SetTargetFPS(60);
|
SetTargetFPS(60);
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
|
@ -13,9 +13,9 @@
|
||||||
*
|
*
|
||||||
********************************************************************************************/
|
********************************************************************************************/
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using static Raylib_cs.Raylib;
|
using static Raylib_cs.Raylib;
|
||||||
|
using Examples.Shared;
|
||||||
|
|
||||||
namespace Examples.Shaders;
|
namespace Examples.Shaders;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@
|
||||||
********************************************************************************************/
|
********************************************************************************************/
|
||||||
|
|
||||||
using static Raylib_cs.Raylib;
|
using static Raylib_cs.Raylib;
|
||||||
|
using Examples.Shared;
|
||||||
|
|
||||||
namespace Examples.Shapes;
|
namespace Examples.Shapes;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@
|
||||||
|
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using static Raylib_cs.Raylib;
|
using static Raylib_cs.Raylib;
|
||||||
|
using Examples.Shared;
|
||||||
|
|
||||||
namespace Examples.Shapes;
|
namespace Examples.Shapes;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@
|
||||||
|
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using static Raylib_cs.Raylib;
|
using static Raylib_cs.Raylib;
|
||||||
|
using Examples.Shared;
|
||||||
|
|
||||||
namespace Examples.Shapes;
|
namespace Examples.Shapes;
|
||||||
|
|
||||||
|
|
|
||||||
264
Examples/Shared/Easings.cs
Normal file
264
Examples/Shared/Easings.cs
Normal file
|
|
@ -0,0 +1,264 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Examples.Shared;
|
||||||
|
|
||||||
|
public static class Easings
|
||||||
|
{
|
||||||
|
// Linear Easing functions
|
||||||
|
public static float EaseLinearNone(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
return (c * t / d + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float EaseLinearIn(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
return (c * t / d + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float EaseLinearOut(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
return (c * t / d + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float EaseLinearInOut(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
return (c * t / d + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sine Easing functions
|
||||||
|
public static float EaseSineIn(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
return (-c * MathF.Cos(t / d * (MathF.PI / 2)) + c + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float EaseSineOut(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
return (c * MathF.Sin(t / d * (MathF.PI / 2)) + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float EaseSineInOut(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
return (-c / 2 * (MathF.Cos(MathF.PI * t / d) - 1) + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Circular Easing functions
|
||||||
|
public static float EaseCircIn(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
return (-c * (MathF.Sqrt(1 - (t /= d) * t) - 1) + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float EaseCircOut(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
return (c * MathF.Sqrt(1 - (t = t / d - 1) * t) + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float EaseCircInOut(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
if ((t /= d / 2) < 1)
|
||||||
|
{
|
||||||
|
return (-c / 2 * (MathF.Sqrt(1 - t * t) - 1) + b);
|
||||||
|
}
|
||||||
|
return (c / 2 * (MathF.Sqrt(1 - t * (t -= 2)) + 1) + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cubic Easing functions
|
||||||
|
public static float EaseCubicIn(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
return (c * (t /= d) * t * t + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float EaseCubicOut(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
return (c * ((t = t / d - 1) * t * t + 1) + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float EaseCubicInOut(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
if ((t /= d / 2) < 1)
|
||||||
|
{
|
||||||
|
return (c / 2 * t * t * t + b);
|
||||||
|
}
|
||||||
|
return (c / 2 * ((t -= 2) * t * t + 2) + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Quadratic Easing functions
|
||||||
|
public static float EaseQuadIn(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
return (c * (t /= d) * t + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float EaseQuadOut(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
return (-c * (t /= d) * (t - 2) + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float EaseQuadInOut(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
if ((t /= d / 2) < 1)
|
||||||
|
{
|
||||||
|
return (((c / 2) * (t * t)) + b);
|
||||||
|
}
|
||||||
|
return (-c / 2 * (((t - 2) * (--t)) - 1) + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exponential Easing functions
|
||||||
|
public static float EaseExpoIn(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
return (t == 0) ? b : (c * MathF.Pow(2, 10 * (t / d - 1)) + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float EaseExpoOut(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
return (t == d) ? (b + c) : (c * (-MathF.Pow(2, -10 * t / d) + 1) + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float EaseExpoInOut(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
if (t == 0)
|
||||||
|
{
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
if (t == d)
|
||||||
|
{
|
||||||
|
return (b + c);
|
||||||
|
}
|
||||||
|
if ((t /= d / 2) < 1)
|
||||||
|
{
|
||||||
|
return (c / 2 * MathF.Pow(2, 10 * (t - 1)) + b);
|
||||||
|
}
|
||||||
|
return (c / 2 * (-MathF.Pow(2, -10 * --t) + 2) + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Back Easing functions
|
||||||
|
public static float EaseBackIn(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
float s = 1.70158f;
|
||||||
|
float postFix = t /= d;
|
||||||
|
return (c * (postFix) * t * ((s + 1) * t - s) + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float EaseBackOut(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
float s = 1.70158f;
|
||||||
|
return (c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float EaseBackInOut(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
float s = 1.70158f;
|
||||||
|
if ((t /= d / 2) < 1)
|
||||||
|
{
|
||||||
|
return (c / 2 * (t * t * (((s *= (1.525f)) + 1) * t - s)) + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
float postFix = t -= 2;
|
||||||
|
return (c / 2 * ((postFix) * t * (((s *= (1.525f)) + 1) * t + s) + 2) + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bounce Easing functions
|
||||||
|
public static float EaseBounceOut(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
if ((t /= d) < (1 / 2.75f))
|
||||||
|
{
|
||||||
|
return (c * (7.5625f * t * t) + b);
|
||||||
|
}
|
||||||
|
else if (t < (2 / 2.75f))
|
||||||
|
{
|
||||||
|
float postFix = t -= (1.5f / 2.75f);
|
||||||
|
return (c * (7.5625f * (postFix) * t + 0.75f) + b);
|
||||||
|
}
|
||||||
|
else if (t < (2.5 / 2.75))
|
||||||
|
{
|
||||||
|
float postFix = t -= (2.25f / 2.75f);
|
||||||
|
return (c * (7.5625f * (postFix) * t + 0.9375f) + b);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
float postFix = t -= (2.625f / 2.75f);
|
||||||
|
return (c * (7.5625f * (postFix) * t + 0.984375f) + b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float EaseBounceIn(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
return (c - EaseBounceOut(d - t, 0, c, d) + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float EaseBounceInOut(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
if (t < d / 2)
|
||||||
|
{
|
||||||
|
return (EaseBounceIn(t * 2, 0, c, d) * 0.5f + b);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return (EaseBounceOut(t * 2 - d, 0, c, d) * 0.5f + c * 0.5f + b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Elastic Easing functions
|
||||||
|
public static float EaseElasticIn(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
if (t == 0)
|
||||||
|
{
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
if ((t /= d) == 1)
|
||||||
|
{
|
||||||
|
return (b + c);
|
||||||
|
}
|
||||||
|
|
||||||
|
float p = d * 0.3f;
|
||||||
|
float a = c;
|
||||||
|
float s = p / 4;
|
||||||
|
float postFix = a * MathF.Pow(2, 10 * (t -= 1));
|
||||||
|
|
||||||
|
return (-(postFix * MathF.Sin((t * d - s) * (2 * MathF.PI) / p)) + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float EaseElasticOut(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
if (t == 0)
|
||||||
|
{
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
if ((t /= d) == 1)
|
||||||
|
{
|
||||||
|
return (b + c);
|
||||||
|
}
|
||||||
|
|
||||||
|
float p = d * 0.3f;
|
||||||
|
float a = c;
|
||||||
|
float s = p / 4;
|
||||||
|
|
||||||
|
return (a * MathF.Pow(2, -10 * t) * MathF.Sin((t * d - s) * (2 * MathF.PI) / p) + c + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float EaseElasticInOut(float t, float b, float c, float d)
|
||||||
|
{
|
||||||
|
if (t == 0)
|
||||||
|
{
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
if ((t /= d / 2) == 2)
|
||||||
|
{
|
||||||
|
return (b + c);
|
||||||
|
}
|
||||||
|
|
||||||
|
float p = d * (0.3f * 1.5f);
|
||||||
|
float a = c;
|
||||||
|
float s = p / 4;
|
||||||
|
|
||||||
|
float postFix = 0f;
|
||||||
|
if (t < 1)
|
||||||
|
{
|
||||||
|
postFix = a * MathF.Pow(2, 10 * (t -= 1));
|
||||||
|
return -0.5f * (postFix * MathF.Sin((t * d - s) * (2 * MathF.PI) / p)) + b;
|
||||||
|
}
|
||||||
|
|
||||||
|
postFix = a * MathF.Pow(2, -10 * (t -= 1));
|
||||||
|
|
||||||
|
return (postFix * MathF.Sin((t * d - s) * (2 * MathF.PI) / p) * 0.5f + c + b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
90
Examples/Shared/Rlights.cs
Normal file
90
Examples/Shared/Rlights.cs
Normal file
|
|
@ -0,0 +1,90 @@
|
||||||
|
using System.Numerics;
|
||||||
|
using static Raylib_cs.Raylib;
|
||||||
|
|
||||||
|
namespace Examples.Shared;
|
||||||
|
|
||||||
|
public struct Light
|
||||||
|
{
|
||||||
|
public bool Enabled;
|
||||||
|
public LightType Type;
|
||||||
|
public Vector3 Position;
|
||||||
|
public Vector3 Target;
|
||||||
|
public Color Color;
|
||||||
|
|
||||||
|
public int EnabledLoc;
|
||||||
|
public int TypeLoc;
|
||||||
|
public int PosLoc;
|
||||||
|
public int TargetLoc;
|
||||||
|
public int ColorLoc;
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum LightType
|
||||||
|
{
|
||||||
|
Directorional,
|
||||||
|
Point
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Rlights
|
||||||
|
{
|
||||||
|
public static Light CreateLight(
|
||||||
|
int lightsCount,
|
||||||
|
LightType type,
|
||||||
|
Vector3 pos,
|
||||||
|
Vector3 target,
|
||||||
|
Color color,
|
||||||
|
Shader shader
|
||||||
|
)
|
||||||
|
{
|
||||||
|
Light light = new();
|
||||||
|
|
||||||
|
light.Enabled = true;
|
||||||
|
light.Type = type;
|
||||||
|
light.Position = pos;
|
||||||
|
light.Target = target;
|
||||||
|
light.Color = color;
|
||||||
|
|
||||||
|
string enabledName = "lights[" + lightsCount + "].enabled";
|
||||||
|
string typeName = "lights[" + lightsCount + "].type";
|
||||||
|
string posName = "lights[" + lightsCount + "].position";
|
||||||
|
string targetName = "lights[" + lightsCount + "].target";
|
||||||
|
string colorName = "lights[" + lightsCount + "].color";
|
||||||
|
|
||||||
|
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
|
||||||
|
Raylib.SetShaderValue(
|
||||||
|
shader,
|
||||||
|
light.EnabledLoc,
|
||||||
|
light.Enabled ? 1 : 0,
|
||||||
|
ShaderUniformDataType.SHADER_UNIFORM_INT
|
||||||
|
);
|
||||||
|
Raylib.SetShaderValue(shader, light.TypeLoc, (int)light.Type, ShaderUniformDataType.SHADER_UNIFORM_INT);
|
||||||
|
|
||||||
|
// Send to shader light target position values
|
||||||
|
Raylib.SetShaderValue(shader, light.PosLoc, light.Position, ShaderUniformDataType.SHADER_UNIFORM_VEC3);
|
||||||
|
|
||||||
|
// Send to shader light target position values
|
||||||
|
Raylib.SetShaderValue(shader, light.TargetLoc, light.Target, ShaderUniformDataType.SHADER_UNIFORM_VEC3);
|
||||||
|
|
||||||
|
// Send to shader light color values
|
||||||
|
float[] color = new[]
|
||||||
|
{
|
||||||
|
(float)light.Color.R / (float)255,
|
||||||
|
(float)light.Color.G / (float)255,
|
||||||
|
(float)light.Color.B / (float)255,
|
||||||
|
(float)light.Color.A / (float)255
|
||||||
|
};
|
||||||
|
Raylib.SetShaderValue(shader, light.ColorLoc, color, ShaderUniformDataType.SHADER_UNIFORM_VEC4);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue