Babylon.Blazor 1.3.24

There is a newer version of this package available.
See the version list below for details.
dotnet add package Babylon.Blazor --version 1.3.24
NuGet\Install-Package Babylon.Blazor -Version 1.3.24
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Babylon.Blazor" Version="1.3.24" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Babylon.Blazor --version 1.3.24
#r "nuget: Babylon.Blazor, 1.3.24"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Babylon.Blazor as a Cake Addin
#addin nuget:?package=Babylon.Blazor&version=1.3.24

// Install Babylon.Blazor as a Cake Tool
#tool nuget:?package=Babylon.Blazor&version=1.3.24

Babylon.Blazor

Build status Publish Status NuGet Status

This library packages the well-known 3D library Babylon.js into a Razor component that can be used in a C# Blazor project. The library is intended to use for creation of molecules visualization and used limited API of Babylon library.
Demo application - Demo for showing different parts of library Pubchem Viewer - Demo application which used library reference. Show chemical information from pubchem.ncbi.nlm.nih.gov

Getting Started

Prerequisites

To create Blazor Apps, install the latest version of Visual Studio with the ASP.NET and web development workload. For Blazor WebAssembly you need at least Visual Studio 2019 16.6+. Another alternative would be to use Visual Studio code. Click here for more information.

Library requires .NET 5 or higher as I use IJSObjectReference

Installing

After you have created your Blazor project, you need to do the following steps:

Install the latest NuGet Package

Using Package Manager

Install-Package Babylon.Blazor

Using .NET CLI

dotnet add package Babylon.Blazor

Using MS VS Manage NuGet Packages
Search for Babylon.Blazor

Add reference to babylon js library. Add 2 lines into index.html

<body>
    <div id="app">Loading...</div>

    <div id="blazor-error-ui">
        An unhandled error has occurred.
        <a href="" class="reload">Reload</a>
        <a class="dismiss">🗙</a>
    </div>

    <script src="https://preview.babylonjs.com/babylon.js"></script>
    <script src="https://preview.babylonjs.com/loaders/babylonjs.loaders.min.js"></script>

    <script src="_framework/blazor.webassembly.js"></script>
    <script>navigator.serviceWorker.register('service-worker.js');</script>
</body>

Add InstanceCreator to DI

 public class Program
    {
        public static async Task Main(string[] args)
        {
       ...
            
            builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

            builder.Services.AddTransient(sp => new InstanceCreator(sp.GetService<IJSRuntime>()));
            await builder.Build().RunAsync();
        }
    }

Add Razor page and replace context to similar code

@page "/test"
@using Babylon.Blazor.Chemical
<h1>Water</h1>

<p> Chemical formula of water is H<sub>2</sub>O</p>

<div style="height: 600px;">
   <BabylonCanvas CanvasId="Canvas1" SceneData=@PanelData/>
</div>

@code {

    ChemicalData PanelData { get; } = new ChemicalData();

    async Task InitDataAsync()
    {
        // Fake await line
        await Task.FromResult(1);


        PanelData.Atoms.Add(new AtomDescription() { Name = "O", X = 2.5369, Y = -0.1550, Z = 0.0000 });
        PanelData.Atoms.Add(new AtomDescription() { Name = "H", X = 3.0739, Y =  0.1550, Z = 0.0000 });
        PanelData.Atoms.Add(new AtomDescription() { Name = "H", X = 2.0000, Y =  0.1550, Z = 0.0000 });

        PanelData.Bonds.Add(new BondDescription(1, 2,  BondDescription.BondType.Single));
        PanelData.Bonds.Add(new BondDescription(1, 3, BondDescription.BondType.Single));
    }

    protected override async Task OnInitializedAsync()
    {
        await InitDataAsync();
    }

}

Add to _Imports.razor

@using Babylon.Blazor

NOTE: You can skip this step

Demo Application

For demo application I implemented: Water, Benzene and Epinephrine. All descriptions was get from PubChem catalog. As I not found atom size into description - I not set it. The same is for double and triple bonds - the parallel lines rotation vector mostly oriented along the Y axis. Colors selected automatically from color palette. If chemist sees something wrong then tell me, please. My target was to create C# interface to Java script library. Not to draw molecules absolute correctly.

Water H<sub>2</sub>O

--water pic--

Benzene C<sub>6</sub>H<sub>6</sub>

--Benzene pic--

Epinephrine C<sub>9</sub>H<sub>13</sub>NO<sub>3</sub>

--Epinephrine pic--

In addition I draw some tests

Test1

--Test1 pic--

Test2 (commented)
--Test2 pic--

How it works?

With .NET 5.0 it is possible to transfer objects between Java Script and C# very easily. Call functions from JS object under C# is easy too.

You can read more in article Using JS Object References in Blazor WASM to wrap JS libraries

Here is the steps you need to wrap JS library for Blazor Web Assembly application:

  1. Create Razor library with LibraryName.
  2. Create under wwwroot js export file with functions like this:
export function functionName(parameters) {
        ...
        return javaObject;
}
  1. Create library wrapper
IJSInProcessObjectReference libraryWrapper = await _jSInstance.InvokeAsync<IJSInProcessObjectReference>("import",
                                                             "./_content/LibraryName/LibraryJSExport.js");

NOTE: You can get _jSInstance into main application over dependency injection or service provider call

  1. Create C# wrapper
public async Task<CsharpObj> CsFunctionName(int parameter)
{
        var jsObjRef = await _libraryWrapper.InvokeAsync<IJSObjectReference>("jsFunctionName", parameter);
        return new CsharpObj(jsObjRef);
}
  1. Call the wrapped function
var CsharpObj = await LibraryWrapper.CsFunctionName(2);

NOTE: you can use JS object as function parameter. Use jsObjRef for it.

How to create custom scene?

If you don't want to draw molecules then it is possible to create your own component

  1. Create your own creator class
public class MySceneCreator : SceneCreator
{
    public override async Task CreateAsync(BabylonCanvasBase canvas)
    {
    ...
    }
}
  1. Create Data class
public class MyCustomData:IData
{

}
  1. Create new canvas
public class MyCustomCanvas : BabylonCanvasBase
{
       protected virtual async Task InitializeSzene(LibraryWrapper LibraryWrapper, string canvasId)
        {
            MyCustomData panelData;
            if (ChemicalData is MyCustomData)
            {
                panelData = (MyCustomData)SceneData;
	            MySceneCreator creator = new MySceneCreator(LibraryWrapper, canvasId, panelData);
    	        await creator.CreateAsync(this);
            }
        }
}
  1. Create new Rasor component
@inherits MyCustomCanvas
<canvas id=@CanvasId touch-action="none" />

What's New

in Version 1.1

New features:

  • Show errors on 3D canvas
  • Added new component ChemFormulaViewer
  • Expand ChemicalData. Added new properties: ErrorText, MolecularFormula, Name, ShowErrorText

in Version 1.2

  • added single color Box
  • added Torus
  • added sample of custom scene drawing
    --Custom Draw pic--

in Version 1.3

  • added sprite manager
  • added sprite with base attributes
  • added callback function sample JS to .NET
  • added sprite sample
    --Sprite sample pic--

##Developer notes

If you want to change the library then don't use IIS express by debugging because JS files will be not easy to change.

In some cases you can try to refresh the site from developer mode and disabled cache

--Sprite sample pic--

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.4.0 326 11/22/2023
1.3.26 2,521 8/7/2022
1.3.24 385 8/6/2022
1.2.0 356 1/2/2022
1.1.21 241 1/2/2022
1.1.18 300 8/1/2021
1.1.17 307 8/1/2021
1.1.16 319 8/1/2021
1.1.12 313 7/4/2021
1.0.0 317 6/26/2021