WASM examples (+ backports of new official examples) (#344)
* chg: New build system that uses the officially distributed binaries, bumped version to 8.0.0, simplified git workflow, removed deprecated OpenGL 1.1 functionality. * chg: Modernize CI workflow, enable SourceLink - Bump workflow actions to latest majors (Node 24); drop deprecated softprops/action-gh-release@v1 - Trigger push builds on main instead of master - Create local nuget feed dir before pack (fixes NU1301) - Enable Microsoft.SourceLink.GitHub for debugging symbols (ref PR #340) * fix: centralized version data in Directory.build.props, and fixed various interop details that had incorrect function signatures * chore: updated readme * fix: version the native extract marker and chain download via DependsOnTargets The .extracted marker now includes the raylib package name, so bumping TargetRaylibTag re-extracts the new archive instead of silently keeping (and packing/copying) the previous version's files. _PrepareNativeLibrary and _StageWasmNative now depend directly on _DownloadAndExtractInternal instead of CallTarget-ing it; dependency targets run in the same project instance, so the resolved properties (RaylibPackageName etc.) propagate naturally. * fix: let the binding build for browser-wasm on both net8.0 and net10.0 The net8-era wasm workload (Microsoft.NET.Runtime.WebAssembly.Sdk 8.0.x, auto-imported for RID browser-wasm) treats every browser-wasm project as a wasm app: it forces OutputType=Exe after project evaluation (CS5001 for a classlib) and hooks its app-bundle build after Build, which errors because a library has no assemblies to bundle. Opt Raylib-cs out via DisableAutoWasmBuildApp (props time, before the workload defaults its trigger) and pin OutputType back to Library in Directory.Build.targets (evaluated after the workload props, so the assignment wins). net10's wasm SDK needs neither workaround. * chore: readme updated * chg: simplifying build logic - a simple line in the documentation should save us the code here * fix: Wrong signature of FrameBufferComplete * chore: readme update * feat: samples default to local project reference, and can optionally use the nuget package * feat: backporting existing raylib-cs examples and new official raylib examples to WASM, adopting raylib's original code style * chore: readme, gitignore, and targets backport. * fix: Examples.csproj runs the download task when building locally * feat: backporting existing raylib-cs examples and new official raylib examples to WASM, adopting raylib's original code style * chore: readme, gitignore, and targets backport. * chore: clean up linter warnings * feat: html harness focuses the example and allows quick navigation with J/K instead. * chore: readme mentions the property to use nuget vs. the local project reference * feat: replaced the J/K navigation with good old HTML buttons * chore: run dotnet format scoped default (was previously scoped to just 'style')
This commit is contained in:
parent
b207e633ae
commit
8c22e68c2a
236 changed files with 40405 additions and 10896 deletions
|
|
@ -1,65 +1,76 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [core] example - VR Simulator (Oculus Rift CV1 parameters)
|
||||
* raylib [core] example - vr simulator
|
||||
*
|
||||
* This example has been created using raylib 1.7 (www.raylib.com)
|
||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||
* Example complexity rating: [★★★☆] 3/4
|
||||
*
|
||||
* Copyright (c) 2017 Ramon Santamaria (@raysan5)
|
||||
* Example originally created with raylib 2.5, last time updated with raylib 4.0
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2017-2025 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Core;
|
||||
|
||||
public class VrSimulator
|
||||
public partial class VrSimulator : IExample
|
||||
{
|
||||
public static int Main()
|
||||
private const int screenWidth = 800;
|
||||
private const int screenHeight = 450;
|
||||
|
||||
#if BROWSER
|
||||
const int GlslVersion = 100; // WebGL1 needs GLSL ES 100
|
||||
#else
|
||||
private const int GlslVersion = 330;
|
||||
#endif
|
||||
|
||||
public string Name => "Core / VR Simulator";
|
||||
|
||||
public string Title => "raylib [core] example - vr simulator";
|
||||
|
||||
public bool CursorDisabled => true;
|
||||
|
||||
private VrStereoConfig config;
|
||||
private Shader distortion;
|
||||
private RenderTexture2D target;
|
||||
private Rectangle sourceRec;
|
||||
private Rectangle destRec;
|
||||
private Camera3D camera;
|
||||
private Vector3 cubePosition;
|
||||
|
||||
public unsafe void Init()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 1080;
|
||||
const int screenHeight = 600;
|
||||
|
||||
SetConfigFlags(ConfigFlags.Msaa4xHint);
|
||||
InitWindow(screenWidth, screenHeight, "raylib [core] example - vr simulator");
|
||||
|
||||
// VR device parameters definition
|
||||
VrDeviceInfo device = new VrDeviceInfo
|
||||
var device = new VrDeviceInfo
|
||||
{
|
||||
// Oculus Rift CV1 parameters for simulator
|
||||
HResolution = 2160,
|
||||
VResolution = 1200,
|
||||
HScreenSize = 0.133793f,
|
||||
VScreenSize = 0.0669f,
|
||||
EyeToScreenDistance = 0.041f,
|
||||
LensSeparationDistance = 0.07f,
|
||||
InterpupillaryDistance = 0.07f,
|
||||
HResolution = 2160, // Horizontal resolution in pixels
|
||||
VResolution = 1200, // Vertical resolution in pixels
|
||||
HScreenSize = 0.133793f, // Horizontal size in meters
|
||||
VScreenSize = 0.0669f, // Vertical size in meters
|
||||
EyeToScreenDistance = 0.041f, // Distance between eye and display in meters
|
||||
LensSeparationDistance = 0.07f, // Lens separation distance in meters
|
||||
InterpupillaryDistance = 0.07f, // IPD (distance between pupils) in meters
|
||||
};
|
||||
|
||||
// NOTE: CV1 uses a Fresnel-hybrid-asymmetric lenses with specific distortion compute shaders.
|
||||
// Following parameters are an approximation to distortion stereo rendering but results differ from actual
|
||||
// device.
|
||||
unsafe
|
||||
{
|
||||
device.LensDistortionValues[0] = 1.0f;
|
||||
device.LensDistortionValues[1] = 0.22f;
|
||||
device.LensDistortionValues[2] = 0.24f;
|
||||
device.LensDistortionValues[3] = 0.0f;
|
||||
device.ChromaAbCorrection[0] = 0.996f;
|
||||
device.ChromaAbCorrection[1] = -0.004f;
|
||||
device.ChromaAbCorrection[2] = 1.014f;
|
||||
device.ChromaAbCorrection[3] = 0.0f;
|
||||
}
|
||||
// NOTE: CV1 uses fresnel-hybrid-asymmetric lenses with specific compute shaders
|
||||
// Following parameters are just an approximation to CV1 distortion stereo rendering
|
||||
device.LensDistortionValues[0] = 1.0f; // Lens distortion constant parameter 0
|
||||
device.LensDistortionValues[1] = 0.22f; // Lens distortion constant parameter 1
|
||||
device.LensDistortionValues[2] = 0.24f; // Lens distortion constant parameter 2
|
||||
device.LensDistortionValues[3] = 0.0f; // Lens distortion constant parameter 3
|
||||
device.ChromaAbCorrection[0] = 0.996f; // Chromatic aberration correction parameter 0
|
||||
device.ChromaAbCorrection[1] = -0.004f; // Chromatic aberration correction parameter 1
|
||||
device.ChromaAbCorrection[2] = 1.014f; // Chromatic aberration correction parameter 2
|
||||
device.ChromaAbCorrection[3] = 0.0f; // Chromatic aberration correction parameter 3
|
||||
|
||||
// Load VR stereo config for VR device parameteres (Oculus Rift CV1 parameters)
|
||||
VrStereoConfig config = LoadVrStereoConfig(device);
|
||||
config = LoadVrStereoConfig(device);
|
||||
|
||||
// Distortion shader (uses device lens distortion and chroma)
|
||||
Shader distortion = LoadShader(null, "resources/distortion330.fs");
|
||||
distortion = LoadShader(null, $"resources/shaders/glsl{GlslVersion}/distortion.fs");
|
||||
|
||||
// Update distortion shader with lens and distortion-scale parameters
|
||||
Raylib.SetShaderValue(
|
||||
|
|
@ -100,88 +111,104 @@ public class VrSimulator
|
|||
ShaderUniformDataType.Vec2
|
||||
);
|
||||
|
||||
unsafe
|
||||
{
|
||||
SetShaderValue(
|
||||
distortion,
|
||||
GetShaderLocation(distortion, "deviceWarpParam"),
|
||||
device.LensDistortionValues,
|
||||
ShaderUniformDataType.Vec4
|
||||
);
|
||||
SetShaderValue(
|
||||
distortion,
|
||||
GetShaderLocation(distortion, "chromaAbParam"),
|
||||
device.ChromaAbCorrection,
|
||||
ShaderUniformDataType.Vec4
|
||||
);
|
||||
}
|
||||
SetShaderValue(
|
||||
distortion,
|
||||
GetShaderLocation(distortion, "deviceWarpParam"),
|
||||
device.LensDistortionValues,
|
||||
ShaderUniformDataType.Vec4
|
||||
);
|
||||
SetShaderValue(
|
||||
distortion,
|
||||
GetShaderLocation(distortion, "chromaAbParam"),
|
||||
device.ChromaAbCorrection,
|
||||
ShaderUniformDataType.Vec4
|
||||
);
|
||||
|
||||
// Initialize framebuffer for stereo rendering
|
||||
// NOTE: Screen size should match HMD aspect ratio
|
||||
RenderTexture2D target = LoadRenderTexture(GetScreenWidth(), GetScreenHeight());
|
||||
target = LoadRenderTexture(device.HResolution, device.VResolution);
|
||||
|
||||
// The target's height is flipped (in the source Rectangle), due to OpenGL reasons
|
||||
sourceRec = new(0.0f, 0.0f, (float)target.Texture.Width, -(float)target.Texture.Height);
|
||||
destRec = new(0.0f, 0.0f, (float)GetScreenWidth(), (float)GetScreenHeight());
|
||||
|
||||
// Define the camera to look into our 3d world
|
||||
Camera3D camera;
|
||||
camera.Position = new Vector3(5.0f, 2.0f, 5.0f);
|
||||
camera.Target = new Vector3(0.0f, 2.0f, 0.0f);
|
||||
camera.Up = new Vector3(0.0f, 1.0f, 0.0f);
|
||||
camera.FovY = 60.0f;
|
||||
camera.Projection = CameraProjection.Perspective;
|
||||
camera = new();
|
||||
camera.Position = new Vector3(5.0f, 2.0f, 5.0f); // Camera position
|
||||
camera.Target = new Vector3(0.0f, 2.0f, 0.0f); // Camera looking at point
|
||||
camera.Up = new Vector3(0.0f, 1.0f, 0.0f); // Camera up vector
|
||||
camera.FovY = 60.0f; // Camera field-of-view Y
|
||||
camera.Projection = CameraProjection.Perspective; // Camera projection type
|
||||
|
||||
Vector3 cubePosition = new(0.0f, 0.0f, 0.0f);
|
||||
cubePosition = new(0.0f, 0.0f, 0.0f);
|
||||
}
|
||||
|
||||
SetTargetFPS(90); // Set our game to run at 90 frames-per-second
|
||||
public void Update()
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
UpdateCamera(ref camera, CameraMode.FirstPerson);
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginTextureMode(target);
|
||||
ClearBackground(Color.RayWhite);
|
||||
BeginVrStereoMode(config);
|
||||
BeginMode3D(camera);
|
||||
|
||||
DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, Color.Red);
|
||||
DrawCubeWires(cubePosition, 2.0f, 2.0f, 2.0f, Color.Maroon);
|
||||
DrawGrid(40, 1.0f);
|
||||
|
||||
EndMode3D();
|
||||
EndVrStereoMode();
|
||||
EndTextureMode();
|
||||
|
||||
BeginDrawing();
|
||||
ClearBackground(Color.RayWhite);
|
||||
BeginShaderMode(distortion);
|
||||
DrawTexturePro(target.Texture, sourceRec, destRec, new Vector2(0.0f, 0.0f), 0.0f, Color.White);
|
||||
EndShaderMode();
|
||||
DrawFPS(10, 10);
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
public void Unload()
|
||||
{
|
||||
UnloadVrStereoConfig(config); // Unload stereo config
|
||||
|
||||
UnloadRenderTexture(target); // Unload stereo render fbo
|
||||
UnloadShader(distortion); // Unload distortion shader
|
||||
}
|
||||
|
||||
public static int Main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
// NOTE: screenWidth/screenHeight should match VR device aspect ratio
|
||||
InitWindow(screenWidth, screenHeight, "raylib [core] example - vr simulator");
|
||||
|
||||
DisableCursor(); // Limit cursor to relative movement inside the window
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
var game = new VrSimulator();
|
||||
game.Init();
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
UpdateCamera(ref camera, CameraMode.FirstPerson);
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
ClearBackground(Color.RayWhite);
|
||||
|
||||
BeginTextureMode(target);
|
||||
ClearBackground(Color.RayWhite);
|
||||
|
||||
BeginVrStereoMode(config);
|
||||
BeginMode3D(camera);
|
||||
|
||||
DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, Color.Red);
|
||||
DrawCubeWires(cubePosition, 2.0f, 2.0f, 2.0f, Color.Maroon);
|
||||
DrawGrid(40, 1.0f);
|
||||
|
||||
EndMode3D();
|
||||
EndVrStereoMode();
|
||||
EndTextureMode();
|
||||
|
||||
BeginShaderMode(distortion);
|
||||
DrawTextureRec(
|
||||
target.Texture,
|
||||
new Rectangle(0, 0, (float)target.Texture.Width, (float)-target.Texture.Height),
|
||||
new Vector2(0.0f, 0.0f),
|
||||
Color.White
|
||||
);
|
||||
EndShaderMode();
|
||||
|
||||
DrawFPS(10, 10);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
game.Update();
|
||||
}
|
||||
|
||||
game.Unload();
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadVrStereoConfig(config);
|
||||
UnloadRenderTexture(target);
|
||||
UnloadShader(distortion);
|
||||
|
||||
CloseWindow();
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
|
|
|
|||
Loading…
Reference in a new issue