Skip to content

Commit ea5f1d7

Browse files
committed
Removed development changed from main branch
1 parent bd60944 commit ea5f1d7

File tree

4 files changed

+22
-179
lines changed

4 files changed

+22
-179
lines changed

Ab4d.SharpEngine.Samples.BlazorWebAssembly/Ab4d.SharpEngine.Samples.BlazorWebAssembly.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@
3939
</PropertyGroup>
4040

4141

42-
<ItemGroup Condition="$(SHARPENGINE_PRIVATE_DIR)==''">
42+
<ItemGroup>
4343
<PackageReference Include="Ab4d.SharpEngine.Web" Version="0.8.9400-beta1" />
4444
</ItemGroup>
45-
<ItemGroup Condition="$(SHARPENGINE_PRIVATE_DIR)!=''">
45+
<!--<ItemGroup Condition="$(SHARPENGINE_PRIVATE_DIR)!=''">
4646
<ProjectReference Include="$(SHARPENGINE_PRIVATE_DIR)\Ab4d.SharpEngine.Web\Ab4d.SharpEngine.Web.csproj" />
47-
</ItemGroup>
47+
</ItemGroup>-->
4848
<ItemGroup>
4949
<PackageReference Include="Silk.NET.OpenGLES" Version="2.22.0" />
5050
</ItemGroup>

Ab4d.SharpEngine.Samples.BlazorWebAssembly/CanvasInterop.cs

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ public partial class CanvasInterop : ICanvasInterop
2727
private static CanvasInterop? _initialInterop;
2828
private static List<CanvasInterop>? _additionalInteropObjects;
2929

30-
private Dictionary<string, Action<int,int,byte[]?>> _imageBytesLoadedCallbacks = new();
31-
3230
/// <summary>
3331
/// Returns true when the <see cref="InitializeInterop"/> method was called and successfully initialized the browser interop.
3432
/// </summary>
@@ -44,11 +42,6 @@ public partial class CanvasInterop : ICanvasInterop
4442
/// Returns true after the <see cref="InitWebGL"/> method was called, the WebGL context was created and connection with the canvas elements was successfully established.
4543
/// </summary>
4644
public bool IsWebGLInitialized { get; private set; }
47-
48-
/// <summary>
49-
/// Returns true when WebGL 2.0 is used. When false, then WebGL 1.0 is used. In this case some features may not be available.
50-
/// </summary>
51-
public bool IsWebGL2 { get; private set; }
5245

5346
/// <summary>
5447
/// Gets the width of the canvas in pixels (defines width of the back buffer that is used for rendering).
@@ -196,10 +189,9 @@ public void InitWebGL(bool useMultisampleAntiAliasing = true)
196189
}
197190

198191
var dataParts = result.Substring(3).Split(';'); // Skip "OK:" and then split
199-
this.IsWebGL2 = dataParts[0] == "v2";
200-
this.Width = int.Parse(dataParts[1]);
201-
this.Height = int.Parse(dataParts[2]);
202-
this.DpiScale = float.Parse(dataParts[3], CultureInfo.InvariantCulture);
192+
this.Width = int.Parse(dataParts[0]);
193+
this.Height = int.Parse(dataParts[1]);
194+
this.DpiScale = float.Parse(dataParts[2], CultureInfo.InvariantCulture);
203195
this.IsUsingMultisampleAntiAliasing = useMultisampleAntiAliasing;
204196

205197
// Set static instances of CanvasInterop so that the static callback functions from javascript can find the target CanvasInterop
@@ -233,16 +225,6 @@ private void CheckIsInitialized(bool checkIfConnectedToCanvas = true, [CallerMem
233225
throw new SharpEngineException($"Cannot call {methodName} because the Connect method was not called or it failed to connect to the canvas element.");
234226
}
235227

236-
public void LoadImageBytes(string fileName, Action<int,int,byte[]?>? onTextureLoadedAction)
237-
{
238-
CheckIsInitialized();
239-
240-
if (onTextureLoadedAction != null)
241-
_imageBytesLoadedCallbacks.Add(fileName, onTextureLoadedAction);
242-
243-
LoadImageBytesJs(this.CanvasId, fileName);
244-
}
245-
246228
public void SetCursorStyle(string cursorStyle)
247229
{
248230
CheckIsInitialized();
@@ -317,9 +299,7 @@ public void Dispose()
317299
DisconnectWebGLCanvasJs(CanvasId);
318300
ArePointerEventsSubscribed = false;
319301
IsWebGLInitialized = false;
320-
321-
_imageBytesLoadedCallbacks.Clear();
322-
302+
323303
if (_initialInterop == this)
324304
{
325305
_initialInterop = null;
@@ -411,18 +391,6 @@ private static void OnFrameUpdateJsCallback()
411391
canvasInterop.OnBrowserAnimationFrameUpdated();
412392
}
413393
}
414-
415-
[JSExport]
416-
private static void OnImageBytesLoaded(string canvasId, string imageUrl, int width, int height, byte[]? imageBytes)
417-
{
418-
//if (IsLoggingInteropEvents)
419-
// Console.WriteLine($"OnImageBytesLoaded '{imageUrl}': {width} x {height} = {imageBytes?.Length ?? 0:N0} bytes");
420-
421-
var canvasInterop = GetCanvasInterop(canvasId);
422-
423-
if (canvasInterop._imageBytesLoadedCallbacks.Remove(imageUrl, out var callbackAction))
424-
callbackAction(width, height, imageBytes);
425-
}
426394

427395
[JSExport]
428396
private static void OnPointerMovedJsCallback(string? canvasId, float x, float y, int buttons, int keyboardModifiers)
@@ -522,9 +490,6 @@ private static void OnCanvasResizedJsCallback(string? canvasId, float width, flo
522490
[JSImport("initWebGLCanvas", "sharp-engine.js")]
523491
private static partial string InitWebGLCanvasJs(string canvasId, bool useMSAA, bool subscribePointerEvents, bool subscribeRequestAnimationFrame);
524492

525-
[JSImport("loadImageBytes", "sharp-engine.js")]
526-
private static partial void LoadImageBytesJs(string canvasId, string imageUrl);
527-
528493
[JSImport("subscribeBrowserEvents", "sharp-engine.js")]
529494
private static partial void SubscribeBrowserEventsJs(string canvasId, bool subscribePointerEvents, bool subscribeRequestAnimationFrame);
530495

Ab4d.SharpEngine.Samples.BlazorWebAssembly/Pages/QuickStart.razor

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@
135135
_targetPositionCamera = new TargetPositionCamera()
136136
{
137137
Heading = 30,
138-
Attitude = -30,
139-
Distance = 400,
138+
Attitude = -40,
139+
Distance = 300,
140140
TargetPosition = new Vector3(0, 0, 0),
141141
ShowCameraLight = ShowCameraLightType.Auto,
142142
};
@@ -164,12 +164,12 @@
164164
float hashModelBarOffset = 20;
165165

166166
var hashSymbolMesh = MeshFactory.CreateHashSymbolMesh(centerPosition: new Vector3(0, hashModelBarThickness * 0.5f, 0),
167-
shapeYVector: new Vector3(0, 0, 1),
168-
extrudeVector: new Vector3(0, hashModelBarThickness, 0),
169-
size: hashModelSize,
170-
barThickness: hashModelBarThickness,
171-
barOffset: hashModelBarOffset,
172-
name: "HashSymbolMesh");
167+
shapeYVector: new Vector3(0, 0, 1),
168+
extrudeVector: new Vector3(0, hashModelBarThickness, 0),
169+
size: hashModelSize,
170+
barThickness: hashModelBarThickness,
171+
barOffset: hashModelBarOffset,
172+
name: "HashSymbolMesh");
173173

174174
_hashMaterial = new StandardMaterial(diffuseColor: Color3.FromByteRgb(255, 197, 0));
175175
//_hashMaterial = new StandardMaterial(diffuseColor: Colors.Silver);
@@ -208,44 +208,9 @@
208208
scene.RootNode.Add(sphereModelNode1);
209209
}
210210

211-
212-
if (sharpEngineSceneView.IsInitialized)
213-
AddTexturePlane();
214-
else
215-
sharpEngineSceneView.SceneViewInitialized += (sender, args) => AddTexturePlane();
216-
217-
218211
ShowInfoMessage("Initial 3D scene shown");
219212
}
220213

221-
private void AddTexturePlane()
222-
{
223-
var gpuDevice = sharpEngineSceneView.Scene.GpuDevice;
224-
if (gpuDevice == null)
225-
return;
226-
227-
Ab4d.SharpEngine.Utilities.TextureLoader.CreateTexture("ab4d-logo-256.png", gpuDevice, gpuImage =>
228-
{
229-
var textureMaterial = new StandardMaterial(gpuImage)
230-
{
231-
HasTransparency = true,
232-
};
233-
234-
var planeModelNode = new PlaneModelNode(centerPosition: new Vector3(0, 30, -150),
235-
size: new Vector2(60, 60),
236-
normal: new Vector3(0, 0, 1),
237-
heightDirection: new Vector3(0, 1, 0),
238-
name: "TexturePlane")
239-
{
240-
IsHitTestVisible = false,
241-
Material = textureMaterial,
242-
BackMaterial = textureMaterial,
243-
};
244-
245-
sharpEngineSceneView.Scene.RootNode.Add(planeModelNode);
246-
});
247-
}
248-
249214
private void UpdateRenderTime()
250215
{
251216
var statistics = sharpEngineSceneView.SceneView.Statistics;
@@ -273,8 +238,7 @@
273238
modelNode.Material = StandardMaterials.Red;
274239
}
275240
}
276-
277-
241+
278242
private void AddManyObjects()
279243
{
280244
int objectsCount = 1000;

Ab4d.SharpEngine.Samples.BlazorWebAssembly/wwwroot/sharp-engine.js

Lines changed: 6 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,14 @@ export function initWebGLCanvas(canvasId, useMSAA, subscribeMouseEvents, subscri
3030

3131
if (canvas)
3232
{
33-
let webglVersion;
34-
3533
var context = canvas.getContext('webgl2', { antialias: useMSAA });
3634

37-
if (context) {
38-
webglVersion = "2";
39-
}
40-
else {
41-
context = canvas.getContext('webgl', { antialias: useMSAA });
42-
43-
if (context) {
44-
console.warn("WebGL 2.0 is not supported. Using WebGL 1.0 but some features may not work.")
45-
webglVersion = "1";
46-
}
47-
else {
48-
var errorMessage = "WebGL 1.0 is not supported";
49-
console.error(errorMessage);
50-
return errorMessage;
51-
}
35+
if (!context) {
36+
var errorMessage = "WebGL is not supported";
37+
console.error(errorMessage);
38+
return errorMessage;
5239
}
53-
40+
5441
if (!initialCanvas)
5542
initialCanvas = canvas;
5643

@@ -82,7 +69,7 @@ export function initWebGLCanvas(canvasId, useMSAA, subscribeMouseEvents, subscri
8269
// It is not possible (at least in .Net 9) to pass an objects for JS to .Net
8370
// It was possible to encode width and height into an int, but we also need dpiScale,
8471
// so we need to pass it as a string
85-
return "OK:v" + webglVersion + ";" + displayWidth + ";" + displayHeight + ";" + dpi;
72+
return "OK:" + displayWidth + ";" + displayHeight + ";" + dpi;
8673
}
8774
else
8875
{
@@ -92,79 +79,6 @@ export function initWebGLCanvas(canvasId, useMSAA, subscribeMouseEvents, subscri
9279
}
9380
}
9481

95-
export async function loadImageBytes(canvasId, url) {
96-
log("js: loadImageBytes: start loading " + url);
97-
98-
if (true || !createImageBitmap || typeof OffscreenCanvas === "undefined") {
99-
// Before Safari 16.4 (2024-03-27)
100-
await loadImageBytesOldWay(canvasId, url);
101-
return;
102-
}
103-
104-
const response = await fetch(url, { mode: 'cors' });
105-
if (!response.ok)
106-
console.error('Image could not be fetched, url: ' + url);
107-
108-
log("js: image loaded: " + url);
109-
110-
const blob = await response.blob();
111-
112-
const bitmap = await createImageBitmap(blob, { premultiplyAlpha: 'none' });
113-
114-
const canvas = new OffscreenCanvas(bitmap.width, bitmap.height);
115-
const ctx = canvas.getContext('2d');
116-
ctx.drawImage(bitmap, 0, 0);
117-
const imageData = ctx.getImageData(0, 0, bitmap.width, bitmap.height);
118-
119-
// For large arrays prefer using transferable, otherwise fallback as array
120-
const data = Array.from(imageData.data);
121-
122-
log("js: image byte array retrieved: " + url);
123-
124-
if (interop)
125-
interop.OnImageBytesLoaded(canvasId, url, bitmap.width, bitmap.height, data);
126-
}
127-
128-
async function loadImageBytesOldWay(canvasId, url) {
129-
130-
const image = new Image();
131-
image.src = url;
132-
133-
let data;
134-
let width, height;
135-
136-
try {
137-
await image.decode();
138-
139-
log("js: image loaded by using Image: " + url);
140-
141-
const canvas = document.createElement('canvas');
142-
const ctx = canvas.getContext('2d');
143-
144-
width = image.width;
145-
height = image.height;
146-
147-
canvas.width = width;
148-
canvas.height = height;
149-
ctx.drawImage(image, 0, 0);
150-
// Get all pixels
151-
const imageData = ctx.getImageData(0, 0, width, height);
152-
data = Array.from(imageData.data); // Length = width * height * 4
153-
154-
log("js: image byte array retrieved: " + url);
155-
}
156-
catch (ex)
157-
{
158-
data = null;
159-
width = 0;
160-
height = 0;
161-
console.error("js: Texture image '" + url + "' failed to load.", ex);
162-
}
163-
164-
if (interop)
165-
interop.OnImageBytesLoaded(canvasId, url, width, height, data);
166-
}
167-
16882
// NOTE:
16983
// We cannot subscribe to mouse events in SetWebGLCanvas because this requires async method
17084
// because we need to call "await getAssemblyExports" (without awit the exports are empyt).

0 commit comments

Comments
 (0)