Skip to content

Commit

Permalink
Demo update using obj loader
Browse files Browse the repository at this point in the history
  • Loading branch information
LostBeard committed Oct 5, 2023
1 parent a686352 commit 1cb9264
Show file tree
Hide file tree
Showing 23 changed files with 257 additions and 15 deletions.
41 changes: 38 additions & 3 deletions SpawnDev.BlazorJS.BabylonJS6.Demo/Pages/Index.razor.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
using Microsoft.AspNetCore.Components;
using SpawnDev.BlazorJS.JSObjects;
using static SpawnDev.BlazorJS.BabylonJS6.BABYLON;
using File = SpawnDev.BlazorJS.JSObjects.File;

namespace SpawnDev.BlazorJS.BabylonJS6.Demo.Pages
{
// Based on the BabylonJS example at the link below
// https://doc.babylonjs.com/setup/starterHTML
public partial class Index : IDisposable
{
[Inject]
BlazorJSRuntime JS { get; set; }

[Inject]
HttpClient HttpClient { get; set; }

[Inject]
BabylonJS6Loader BabylonJS6Loader { get; set; }

Expand All @@ -23,10 +31,10 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
if (firstRender)
{
Console.WriteLine("FirstRender");
await BabylonJS6Loader.ImportCore();
await BabylonJS6Loader.ImportGUI();
await BabylonJS6Loader.Import();
engine = new BABYLON.Engine(canvas, true);
scene = CreateScene();
//scene = CreateScene();
scene = await CreateMeshScene();
RenderLoopCallback = new ActionCallback(Engine_RenderLoop);
engine.RunRenderLoop(RenderLoopCallback);
window.OnResize += Window_OnResize;
Expand All @@ -43,6 +51,33 @@ void Engine_RenderLoop()
scene?.Render();
}

async Task<BABYLON.Scene> CreateMeshScene()
{
// Creates a basic Babylon Scene object
var scene = new BABYLON.Scene(engine);
// Creates and positions a free camera
var camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(1000, 1000, 1000), scene);
// Targets the camera to scene origin
camera.SetTarget(BABYLON.Vector3.Zero());
// This attaches the camera to the canvas
camera.AttachControl(canvas, true);
// Creates a light, aiming 0,1,0 - to the sky
var light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0), scene);
// Dim the light a small amount - 0 to 1
light.Intensity = 0.7;
// load .obj
var objBytes = await HttpClient.GetByteArrayAsync("assets/filing_cabinet.obj");
var val = await JS.Fetch("/assets/filing_cabinet.obj");
var blob = await val.Blob();
var file = new File([blob], "filing_cabinet.obj");
BABYLON.SceneLoader.ImportMesh("", "", file, scene, Callback.CreateOne<Array<AbstractMesh>, Array<BaseParticleSystem>, Array<Skeleton>, Array<AnimationGroup>, Array<TransformNode>, Array<Geometry>, Array<Light>>((meshes, particaleSystems, skeletons, animationGroups, transformNodes, geometries, lights) => {
// Set the target of the camera to the first imported mesh
camera.Target = meshes[0].Position;
meshes.Map(m => m.Scaling = new Vector3(0.2));
}));
return scene;
}

BABYLON.Scene CreateScene()
{
// Creates a basic Babylon Scene object
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<ImplicitUsings>true</ImplicitUsings>
<ServiceWorkerAssetsManifest>service-worker-assets.js</ServiceWorkerAssetsManifest>
</PropertyGroup>

Expand All @@ -20,4 +20,8 @@
<ServiceWorker Include="wwwroot\service-worker.js" PublishedContent="wwwroot\service-worker.published.js" />
</ItemGroup>

<ItemGroup>
<Folder Include="wwwroot\assets\" />
</ItemGroup>

</Project>
12 changes: 12 additions & 0 deletions SpawnDev.BlazorJS.BabylonJS6/AbstractMesh.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.JSInterop;

namespace SpawnDev.BlazorJS.BabylonJS6
{
public static partial class BABYLON
{
public partial class AbstractMesh : TransformNode
{
public AbstractMesh(IJSInProcessObjectReference _ref) : base(_ref) { }
}
}
}
12 changes: 12 additions & 0 deletions SpawnDev.BlazorJS.BabylonJS6/AnimationGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.JSInterop;

namespace SpawnDev.BlazorJS.BabylonJS6
{
public static partial class BABYLON
{
public class AnimationGroup : JSDisposable
{
public AnimationGroup(IJSInProcessObjectReference _ref) : base(_ref) { }
}
}
}
13 changes: 13 additions & 0 deletions SpawnDev.BlazorJS.BabylonJS6/BABYLON.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpawnDev.BlazorJS.BabylonJS6
{
public static partial class BABYLON
{
internal static BlazorJSRuntime JS => BlazorJSRuntime.JS;
}
}
40 changes: 40 additions & 0 deletions SpawnDev.BlazorJS.BabylonJS6/BabylonJS6Loader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ public BabylonJS6Loader(BlazorJSRuntime js)
{
JS = js;
}
public async Task Import()
{
await ImportCore();
await ImportGUI();
await ImportLoaders();
await ImportInspector();
await ImportMaterials();
await ImportPostProcess();
await ImportProceduralTectures();
await ImportSerializers();
await ImportViewer();
}
public async Task ImportCore()
{
await JS.LoadScript("_content/SpawnDev.BlazorJS.BabylonJS6/babylon.js", "BABYLON");
Expand All @@ -15,5 +27,33 @@ public async Task ImportGUI()
{
await JS.LoadScript("_content/SpawnDev.BlazorJS.BabylonJS6/babylon.gui.min.js", "BABYLON.GUI");
}
public async Task ImportInspector()
{
await JS.LoadScript("_content/SpawnDev.BlazorJS.BabylonJS6/babylon.inspector.bundle.js");
}
public async Task ImportViewer()
{
await JS.LoadScript("_content/SpawnDev.BlazorJS.BabylonJS6/babylon.viewer.js");
}
public async Task ImportLoaders()
{
await JS.LoadScript("_content/SpawnDev.BlazorJS.BabylonJS6/babylonjs.loaders.min.js");
}
public async Task ImportMaterials()
{
await JS.LoadScript("_content/SpawnDev.BlazorJS.BabylonJS6/babylonjs.materials.min.js");
}
public async Task ImportPostProcess()
{
await JS.LoadScript("_content/SpawnDev.BlazorJS.BabylonJS6/babylonjs.postProcess.min.js");
}
public async Task ImportProceduralTectures()
{
await JS.LoadScript("_content/SpawnDev.BlazorJS.BabylonJS6/babylonjs.proceduralTextures.min.js");
}
public async Task ImportSerializers()
{
await JS.LoadScript("_content/SpawnDev.BlazorJS.BabylonJS6/babylonjs.serializers.min.js");
}
}
}
12 changes: 12 additions & 0 deletions SpawnDev.BlazorJS.BabylonJS6/BaseParticleSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.JSInterop;

namespace SpawnDev.BlazorJS.BabylonJS6
{
public static partial class BABYLON
{
public class BaseParticleSystem : JSObject
{
public BaseParticleSystem(IJSInProcessObjectReference _ref) : base(_ref) { }
}
}
}
1 change: 1 addition & 0 deletions SpawnDev.BlazorJS.BabylonJS6/Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public Camera(IJSInProcessObjectReference _ref) : base(_ref) { }
public float MinZ { get => JSRef.Get<float>("minZ"); set => JSRef.Set("minZ", value); }
public void AttachControl(object? ignored, bool noPreventDefault) => JSRef.CallVoid("attachControl", ignored, noPreventDefault);
public void AttachControl(bool noPreventDefault) => JSRef.CallVoid("attachControl", noPreventDefault);
public Vector3 Position { get => JSRef.Get<Vector3>("position"); set => JSRef.Set("position", value); }
}
}
}
3 changes: 0 additions & 3 deletions SpawnDev.BlazorJS.BabylonJS6/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace SpawnDev.BlazorJS.BabylonJS6
{

public static partial class BABYLON
{
public class Engine : ThinEngine
Expand All @@ -19,6 +18,4 @@ public Engine(ElementReference canvasOrContext, bool antialias = false) : base(J
public void Resize() => JSRef.CallVoid("resize");
}
}


}
12 changes: 12 additions & 0 deletions SpawnDev.BlazorJS.BabylonJS6/Geometry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.JSInterop;

namespace SpawnDev.BlazorJS.BabylonJS6
{
public static partial class BABYLON
{
public class Geometry : JSObject
{
public Geometry(IJSInProcessObjectReference _ref) : base(_ref) { }
}
}
}
10 changes: 10 additions & 0 deletions SpawnDev.BlazorJS.BabylonJS6/IParticleSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace SpawnDev.BlazorJS.BabylonJS6
{
public static partial class BABYLON
{
public interface IParticleSystem : IJSObject
{

}
}
}
1 change: 1 addition & 0 deletions SpawnDev.BlazorJS.BabylonJS6/JSDisposable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SpawnDev.BlazorJS.BabylonJS6
{
// https://doc.babylonjs.com/typedoc/interfaces/BABYLON.IDisposable
public class JSDisposable : JSObject
{
public JSDisposable(IJSInProcessObjectReference _ref) : base(_ref) { }
Expand Down
3 changes: 1 addition & 2 deletions SpawnDev.BlazorJS.BabylonJS6/Mesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ namespace SpawnDev.BlazorJS.BabylonJS6
{
public static partial class BABYLON
{
public class Mesh : JSDisposable
public class Mesh : AbstractMesh
{
public Mesh(IJSInProcessObjectReference _ref) : base(_ref) { }
public Vector4 Position => JSRef.Get<Vector4>("position");
}
}
}
2 changes: 0 additions & 2 deletions SpawnDev.BlazorJS.BabylonJS6/MeshBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ public static partial class BABYLON
{
public static partial class MeshBuilder
{
static BlazorJSRuntime JS => BlazorJSRuntime.JS;

// CreateSphere
public static Mesh CreateSphere(string name) => JS.Call<Mesh>("BABYLON.MeshBuilder.CreateSphere", name);
public static Mesh CreateSphere(string name, CreateSphereOptions options) => JS.Call<Mesh>("BABYLON.MeshBuilder.CreateSphere", name, options);
Expand Down
2 changes: 1 addition & 1 deletion SpawnDev.BlazorJS.BabylonJS6/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace SpawnDev.BlazorJS.BabylonJS6
{
public static partial class BABYLON
{
public partial class Node : JSDisposable
public class Node : JSDisposable
{
public Node(IJSInProcessObjectReference _ref) : base(_ref) { }
}
Expand Down
12 changes: 12 additions & 0 deletions SpawnDev.BlazorJS.BabylonJS6/ParticleSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.JSInterop;

namespace SpawnDev.BlazorJS.BabylonJS6
{
public static partial class BABYLON
{
public class ParticleSystem : BaseParticleSystem, IParticleSystem
{
public ParticleSystem(IJSInProcessObjectReference _ref) : base(_ref) { }
}
}
}
44 changes: 44 additions & 0 deletions SpawnDev.BlazorJS.BabylonJS6/SceneLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Microsoft.JSInterop;
using SpawnDev.BlazorJS.JSObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using File = SpawnDev.BlazorJS.JSObjects.File;

namespace SpawnDev.BlazorJS.BabylonJS6
{
public static partial class BABYLON
{
public static class SceneLoader
{
public class SceneLoaderProgressEvent
{
/// <summary>
/// Defines if data length to load can be evaluated
/// </summary>
public bool LengthComputable { get; }
/// <summary>
/// Defines the loaded data length
/// </summary>
public long Loaded { get; }
/// <summary>
/// Defines the data length to load
/// </summary>
public long Total { get; }
}
public static void ImportMesh(
Union<string, IEnumerable<string>> meshNames,
string rootUrl,
Union<string, TypedArray, File>? sceneFilename = null,
Scene? scene = null,
ActionCallback<Array<AbstractMesh>, Array<BaseParticleSystem>, Array<Skeleton>, Array<AnimationGroup>, Array<TransformNode>, Array<Geometry>, Array<Light>>? onSuccess = null,
ActionCallback<SceneLoaderProgressEvent>? onProgress = null,
ActionCallback<Scene, string, JSObject?>? onError = null,
string? pluginExtension = null
)
=> JS.CallVoid("BABYLON.SceneLoader.ImportMesh", meshNames, rootUrl, sceneFilename, scene, onSuccess, onProgress, onError, pluginExtension);
}
}
}
12 changes: 12 additions & 0 deletions SpawnDev.BlazorJS.BabylonJS6/Skeleton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.JSInterop;

namespace SpawnDev.BlazorJS.BabylonJS6
{
public static partial class BABYLON
{
public partial class Skeleton : JSDisposable
{
public Skeleton(IJSInProcessObjectReference _ref) : base(_ref) { }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand Down Expand Up @@ -30,7 +30,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="SpawnDev.BlazorJS" Version="2.2.11" />
<PackageReference Include="SpawnDev.BlazorJS" Version="2.2.13" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
Expand Down
1 change: 1 addition & 0 deletions SpawnDev.BlazorJS.BabylonJS6/TargetCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public partial class TargetCamera : Camera
{
public TargetCamera(IJSInProcessObjectReference _ref) : base(_ref) { }
public void SetTarget(Vector3 target) => JSRef.CallVoid("setTarget", target);
public Vector3 Target { get => JSRef.Get<Vector3>("target"); set => JSRef.Set("target", value); }
}
}
}
12 changes: 12 additions & 0 deletions SpawnDev.BlazorJS.BabylonJS6/TextureDome.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.JSInterop;

namespace SpawnDev.BlazorJS.BabylonJS6
{
public static partial class BABYLON
{
public partial class TextureDome : TransformNode
{
public TextureDome(IJSInProcessObjectReference _ref) : base(_ref) { }
}
}
}
Loading

0 comments on commit 1cb9264

Please sign in to comment.