1 // Copyright (c) 2019 OPEN CASCADE SAS
3 // This file is part of the examples of the Open CASCADE Technology software library.
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
22 #include "WasmOcctView.h"
24 #include <AIS_Shape.hxx>
25 #include <AIS_ViewCube.hxx>
26 #include <Aspect_Handle.hxx>
27 #include <Aspect_DisplayConnection.hxx>
28 #include <Image_AlienPixMap.hxx>
29 #include <Message.hxx>
30 #include <Message_Messenger.hxx>
31 #include <Graphic3d_CubeMapPacked.hxx>
32 #include <OpenGl_GraphicDriver.hxx>
33 #include <Prs3d_DatumAspect.hxx>
34 #include <Prs3d_ToolCylinder.hxx>
35 #include <Prs3d_ToolDisk.hxx>
36 #include <Wasm_Window.hxx>
38 #include <BRep_Builder.hxx>
39 #include <BRepBndLib.hxx>
40 #include <BRepTools.hxx>
41 #include <Standard_ArrayStreamBuffer.hxx>
43 #include <emscripten/bind.h>
47 #define THE_CANVAS_ID "canvas"
51 //! Auxiliary wrapper for loading model.
52 struct ModelAsyncLoader
57 ModelAsyncLoader (const char* theName, const char* thePath)
58 : Name (theName), Path (thePath) {}
60 //! File data read event.
61 static void onDataRead (void* theOpaque, void* theBuffer, int theDataLen)
63 const ModelAsyncLoader* aTask = (ModelAsyncLoader* )theOpaque;
64 WasmOcctView::openFromMemory (aTask->Name, reinterpret_cast<uintptr_t>(theBuffer), theDataLen, false);
68 //! File read error event.
69 static void onReadFailed (void* theOpaque)
71 const ModelAsyncLoader* aTask = (ModelAsyncLoader* )theOpaque;
72 Message::DefaultMessenger()->Send (TCollection_AsciiString("Error: unable to load file ") + aTask->Path.c_str(), Message_Fail);
77 //! Auxiliary wrapper for loading cubemap.
78 struct CubemapAsyncLoader
80 //! Image file read event.
81 static void onImageRead (const char* theFilePath)
83 Handle(Graphic3d_CubeMapPacked) aCubemap;
84 Handle(Image_AlienPixMap) anImage = new Image_AlienPixMap();
85 if (anImage->Load (theFilePath))
87 aCubemap = new Graphic3d_CubeMapPacked (anImage);
89 WasmOcctView::Instance().View()->SetBackgroundCubeMap (aCubemap, true, false);
90 WasmOcctView::Instance().UpdateView();
93 //! Image file failed read event.
94 static void onImageFailed (const char* theFilePath)
96 Message::DefaultMessenger()->Send (TCollection_AsciiString("Error: unable to load image ") + theFilePath, Message_Fail);
101 // ================================================================
102 // Function : Instance
104 // ================================================================
105 WasmOcctView& WasmOcctView::Instance()
107 static WasmOcctView aViewer;
111 // ================================================================
112 // Function : WasmOcctView
114 // ================================================================
115 WasmOcctView::WasmOcctView()
116 : myDevicePixelRatio (1.0f),
119 addActionHotKeys (Aspect_VKey_NavForward, Aspect_VKey_W, Aspect_VKey_W | Aspect_VKeyFlags_SHIFT);
120 addActionHotKeys (Aspect_VKey_NavBackward , Aspect_VKey_S, Aspect_VKey_S | Aspect_VKeyFlags_SHIFT);
121 addActionHotKeys (Aspect_VKey_NavSlideLeft, Aspect_VKey_A, Aspect_VKey_A | Aspect_VKeyFlags_SHIFT);
122 addActionHotKeys (Aspect_VKey_NavSlideRight, Aspect_VKey_D, Aspect_VKey_D | Aspect_VKeyFlags_SHIFT);
123 addActionHotKeys (Aspect_VKey_NavRollCCW, Aspect_VKey_Q, Aspect_VKey_Q | Aspect_VKeyFlags_SHIFT);
124 addActionHotKeys (Aspect_VKey_NavRollCW, Aspect_VKey_E, Aspect_VKey_E | Aspect_VKeyFlags_SHIFT);
126 addActionHotKeys (Aspect_VKey_NavSpeedIncrease, Aspect_VKey_Plus, Aspect_VKey_Plus | Aspect_VKeyFlags_SHIFT,
128 Aspect_VKey_NumpadAdd, Aspect_VKey_NumpadAdd | Aspect_VKeyFlags_SHIFT);
129 addActionHotKeys (Aspect_VKey_NavSpeedDecrease, Aspect_VKey_Minus, Aspect_VKey_Minus | Aspect_VKeyFlags_SHIFT,
130 Aspect_VKey_NumpadSubtract, Aspect_VKey_NumpadSubtract | Aspect_VKeyFlags_SHIFT);
132 // arrow keys conflict with browser page scrolling, so better be avoided in non-fullscreen mode
133 addActionHotKeys (Aspect_VKey_NavLookUp, Aspect_VKey_Numpad8); // Aspect_VKey_Up
134 addActionHotKeys (Aspect_VKey_NavLookDown, Aspect_VKey_Numpad2); // Aspect_VKey_Down
135 addActionHotKeys (Aspect_VKey_NavLookLeft, Aspect_VKey_Numpad4); // Aspect_VKey_Left
136 addActionHotKeys (Aspect_VKey_NavLookRight, Aspect_VKey_Numpad6); // Aspect_VKey_Right
137 addActionHotKeys (Aspect_VKey_NavSlideLeft, Aspect_VKey_Numpad1); // Aspect_VKey_Left |Aspect_VKeyFlags_SHIFT
138 addActionHotKeys (Aspect_VKey_NavSlideRight, Aspect_VKey_Numpad3); // Aspect_VKey_Right|Aspect_VKeyFlags_SHIFT
139 addActionHotKeys (Aspect_VKey_NavSlideUp, Aspect_VKey_Numpad9); // Aspect_VKey_Up |Aspect_VKeyFlags_SHIFT
140 addActionHotKeys (Aspect_VKey_NavSlideDown, Aspect_VKey_Numpad7); // Aspect_VKey_Down |Aspect_VKeyFlags_SHIFT
143 // ================================================================
144 // Function : ~WasmOcctView
146 // ================================================================
147 WasmOcctView::~WasmOcctView()
151 // ================================================================
154 // ================================================================
155 void WasmOcctView::run()
165 myView->MustBeResized();
168 // There is no infinite message loop, main() will return from here immediately.
169 // Tell that our Module should be left loaded and handle events through callbacks.
170 //emscripten_set_main_loop (redrawView, 60, 1);
171 //emscripten_set_main_loop (redrawView, -1, 1);
172 EM_ASM(Module['noExitRuntime'] = true);
175 // ================================================================
176 // Function : initWindow
178 // ================================================================
179 void WasmOcctView::initWindow()
181 myDevicePixelRatio = emscripten_get_device_pixel_ratio();
182 myCanvasId = THE_CANVAS_ID;
183 const char* aTargetId = !myCanvasId.IsEmpty() ? myCanvasId.ToCString() : EMSCRIPTEN_EVENT_TARGET_WINDOW;
184 const EM_BOOL toUseCapture = EM_TRUE;
185 emscripten_set_resize_callback (EMSCRIPTEN_EVENT_TARGET_WINDOW, this, toUseCapture, onResizeCallback);
187 emscripten_set_mousedown_callback (aTargetId, this, toUseCapture, onMouseCallback);
188 emscripten_set_mouseup_callback (aTargetId, this, toUseCapture, onMouseCallback);
189 emscripten_set_mousemove_callback (aTargetId, this, toUseCapture, onMouseCallback);
190 emscripten_set_dblclick_callback (aTargetId, this, toUseCapture, onMouseCallback);
191 emscripten_set_click_callback (aTargetId, this, toUseCapture, onMouseCallback);
192 emscripten_set_mouseenter_callback (aTargetId, this, toUseCapture, onMouseCallback);
193 emscripten_set_mouseleave_callback (aTargetId, this, toUseCapture, onMouseCallback);
194 emscripten_set_wheel_callback (aTargetId, this, toUseCapture, onWheelCallback);
196 emscripten_set_touchstart_callback (aTargetId, this, toUseCapture, onTouchCallback);
197 emscripten_set_touchend_callback (aTargetId, this, toUseCapture, onTouchCallback);
198 emscripten_set_touchmove_callback (aTargetId, this, toUseCapture, onTouchCallback);
199 emscripten_set_touchcancel_callback(aTargetId, this, toUseCapture, onTouchCallback);
201 //emscripten_set_keypress_callback (EMSCRIPTEN_EVENT_TARGET_WINDOW, this, toUseCapture, onKeyCallback);
202 emscripten_set_keydown_callback (EMSCRIPTEN_EVENT_TARGET_WINDOW, this, toUseCapture, onKeyDownCallback);
203 emscripten_set_keyup_callback (EMSCRIPTEN_EVENT_TARGET_WINDOW, this, toUseCapture, onKeyUpCallback);
206 // ================================================================
207 // Function : dumpGlInfo
209 // ================================================================
210 void WasmOcctView::dumpGlInfo (bool theIsBasic)
212 TColStd_IndexedDataMapOfStringString aGlCapsDict;
213 myView->DiagnosticInformation (aGlCapsDict, theIsBasic ? Graphic3d_DiagnosticInfo_Basic : Graphic3d_DiagnosticInfo_Complete);
216 TCollection_AsciiString aViewport;
217 aGlCapsDict.FindFromKey ("Viewport", aViewport);
219 aGlCapsDict.Add ("Viewport", aViewport);
221 aGlCapsDict.Add ("Display scale", TCollection_AsciiString(myDevicePixelRatio));
225 TCollection_AsciiString* aGlVer = aGlCapsDict.ChangeSeek ("GLversion");
226 TCollection_AsciiString* aGlslVer = aGlCapsDict.ChangeSeek ("GLSLversion");
230 *aGlVer = *aGlVer + " [GLSL: " + *aGlslVer + "]";
235 TCollection_AsciiString anInfo;
236 for (TColStd_IndexedDataMapOfStringString::Iterator aValueIter (aGlCapsDict); aValueIter.More(); aValueIter.Next())
238 if (!aValueIter.Value().IsEmpty())
240 if (!anInfo.IsEmpty())
244 anInfo += aValueIter.Key() + ": " + aValueIter.Value();
248 ::Message::DefaultMessenger()->Send (anInfo, Message_Warning);
251 // ================================================================
252 // Function : initPixelScaleRatio
254 // ================================================================
255 void WasmOcctView::initPixelScaleRatio()
257 SetTouchToleranceScale (myDevicePixelRatio);
258 if (!myView.IsNull())
260 myView->ChangeRenderingParams().Resolution = (unsigned int )(96.0 * myDevicePixelRatio + 0.5);
262 if (!myContext.IsNull())
264 myContext->SetPixelTolerance (int(myDevicePixelRatio * 6.0));
265 if (!myViewCube.IsNull())
267 static const double THE_CUBE_SIZE = 60.0;
268 myViewCube->SetSize (myDevicePixelRatio * THE_CUBE_SIZE, false);
269 myViewCube->SetBoxFacetExtension (myViewCube->Size() * 0.15);
270 myViewCube->SetAxesPadding (myViewCube->Size() * 0.10);
271 myViewCube->SetFontHeight (THE_CUBE_SIZE * 0.16);
272 if (myViewCube->HasInteractiveContext())
274 myContext->Redisplay (myViewCube, false);
280 // ================================================================
281 // Function : initViewer
283 // ================================================================
284 bool WasmOcctView::initViewer()
286 // Build with "--preload-file MyFontFile.ttf" option
287 // and register font in Font Manager to use custom font(s).
288 /*const char* aFontPath = "MyFontFile.ttf";
289 if (Handle(Font_SystemFont) aFont = Font_FontMgr::GetInstance()->CheckFont (aFontPath))
291 Font_FontMgr::GetInstance()->RegisterFont (aFont, true);
295 Message::DefaultMessenger()->Send (TCollection_AsciiString ("Error: font '") + aFontPath + "' is not found", Message_Fail);
298 Handle(Aspect_DisplayConnection) aDisp;
299 Handle(OpenGl_GraphicDriver) aDriver = new OpenGl_GraphicDriver (aDisp, false);
300 aDriver->ChangeOptions().buffersNoSwap = true; // swap has no effect in WebGL
301 aDriver->ChangeOptions().buffersOpaqueAlpha = true; // avoid unexpected blending of canvas with page background
302 if (!aDriver->InitContext())
304 Message::DefaultMessenger()->Send (TCollection_AsciiString ("Error: EGL initialization failed"), Message_Fail);
308 Handle(V3d_Viewer) aViewer = new V3d_Viewer (aDriver);
309 aViewer->SetComputedMode (false);
310 aViewer->SetDefaultShadingModel (Graphic3d_TypeOfShadingModel_Phong);
311 aViewer->SetDefaultLights();
312 aViewer->SetLightOn();
313 for (V3d_ListOfLight::Iterator aLightIter (aViewer->ActiveLights()); aLightIter.More(); aLightIter.Next())
315 const Handle(V3d_Light)& aLight = aLightIter.Value();
316 if (aLight->Type() == Graphic3d_TypeOfLightSource_Directional)
318 aLight->SetCastShadows (true);
322 Handle(Wasm_Window) aWindow = new Wasm_Window (THE_CANVAS_ID);
323 aWindow->Size (myWinSizeOld.x(), myWinSizeOld.y());
325 myTextStyle = new Prs3d_TextAspect();
326 myTextStyle->SetFont (Font_NOF_ASCII_MONO);
327 myTextStyle->SetHeight (12);
328 myTextStyle->Aspect()->SetColor (Quantity_NOC_GRAY95);
329 myTextStyle->Aspect()->SetColorSubTitle (Quantity_NOC_BLACK);
330 myTextStyle->Aspect()->SetDisplayType (Aspect_TODT_SHADOW);
331 myTextStyle->Aspect()->SetTextFontAspect (Font_FA_Bold);
332 myTextStyle->Aspect()->SetTextZoomable (false);
333 myTextStyle->SetHorizontalJustification (Graphic3d_HTA_LEFT);
334 myTextStyle->SetVerticalJustification (Graphic3d_VTA_BOTTOM);
336 myView = new V3d_View (aViewer);
337 myView->Camera()->SetProjectionType (Graphic3d_Camera::Projection_Perspective);
338 myView->SetImmediateUpdate (false);
339 myView->ChangeRenderingParams().IsShadowEnabled = false;
340 myView->ChangeRenderingParams().Resolution = (unsigned int )(96.0 * myDevicePixelRatio + 0.5);
341 myView->ChangeRenderingParams().ToShowStats = true;
342 myView->ChangeRenderingParams().StatsTextAspect = myTextStyle->Aspect();
343 myView->ChangeRenderingParams().StatsTextHeight = (int )myTextStyle->Height();
344 myView->SetWindow (aWindow);
347 myContext = new AIS_InteractiveContext (aViewer);
348 initPixelScaleRatio();
352 // ================================================================
353 // Function : initDemoScene
355 // ================================================================
356 void WasmOcctView::initDemoScene()
358 if (myContext.IsNull())
363 //myView->TriedronDisplay (Aspect_TOTP_LEFT_LOWER, Quantity_NOC_GOLD, 0.08, V3d_WIREFRAME);
365 myViewCube = new AIS_ViewCube();
366 // presentation parameters
367 initPixelScaleRatio();
368 myViewCube->SetTransformPersistence (new Graphic3d_TransformPers (Graphic3d_TMF_TriedronPers, Aspect_TOTP_RIGHT_LOWER, Graphic3d_Vec2i (100, 100)));
369 myViewCube->Attributes()->SetDatumAspect (new Prs3d_DatumAspect());
370 myViewCube->Attributes()->DatumAspect()->SetTextAspect (myTextStyle);
371 // animation parameters
372 myViewCube->SetViewAnimation (myViewAnimation);
373 myViewCube->SetFixedAnimationLoop (false);
374 myViewCube->SetAutoStartAnimation (true);
375 myContext->Display (myViewCube, false);
377 // Build with "--preload-file MySampleFile.brep" option to load some shapes here.
380 // ================================================================
381 // Function : ProcessInput
383 // ================================================================
384 void WasmOcctView::ProcessInput()
386 if (!myView.IsNull())
388 // Queue onRedrawView()/redrawView callback to redraw canvas after all user input is flushed by browser.
389 // Redrawing viewer on every single message would be a pointless waste of resources,
390 // as user will see only the last drawn frame due to WebGL implementation details.
391 if (++myUpdateRequests == 1)
393 emscripten_async_call (onRedrawView, this, 0);
398 // ================================================================
399 // Function : UpdateView
401 // ================================================================
402 void WasmOcctView::UpdateView()
404 if (!myView.IsNull())
406 myView->Invalidate();
407 // queue next onRedrawView()/redrawView()
412 // ================================================================
413 // Function : redrawView
415 // ================================================================
416 void WasmOcctView::redrawView()
418 if (!myView.IsNull())
420 FlushViewEvents (myContext, myView, true);
424 // ================================================================
425 // Function : handleViewRedraw
427 // ================================================================
428 void WasmOcctView::handleViewRedraw (const Handle(AIS_InteractiveContext)& theCtx,
429 const Handle(V3d_View)& theView)
431 myUpdateRequests = 0;
432 AIS_ViewController::handleViewRedraw (theCtx, theView);
433 if (myToAskNextFrame)
437 emscripten_async_call (onRedrawView, this, 0);
441 // ================================================================
442 // Function : onResizeEvent
444 // ================================================================
445 EM_BOOL WasmOcctView::onResizeEvent (int theEventType, const EmscriptenUiEvent* theEvent)
447 (void )theEventType; // EMSCRIPTEN_EVENT_RESIZE or EMSCRIPTEN_EVENT_CANVASRESIZED
454 Handle(Wasm_Window) aWindow = Handle(Wasm_Window)::DownCast (myView->Window());
455 Graphic3d_Vec2i aWinSizeNew;
457 aWindow->Size (aWinSizeNew.x(), aWinSizeNew.y());
458 const float aPixelRatio = emscripten_get_device_pixel_ratio();
459 if (aWinSizeNew != myWinSizeOld
460 || aPixelRatio != myDevicePixelRatio)
462 myWinSizeOld = aWinSizeNew;
463 if (myDevicePixelRatio != aPixelRatio)
465 myDevicePixelRatio = aPixelRatio;
466 initPixelScaleRatio();
469 myView->MustBeResized();
470 myView->Invalidate();
477 // ================================================================
478 // Function : onMouseEvent
480 // ================================================================
481 EM_BOOL WasmOcctView::onMouseEvent (int theEventType, const EmscriptenMouseEvent* theEvent)
488 Handle(Wasm_Window) aWindow = Handle(Wasm_Window)::DownCast (myView->Window());
489 return aWindow->ProcessMouseEvent (*this, theEventType, theEvent) ? EM_TRUE : EM_FALSE;
492 // ================================================================
493 // Function : onWheelEvent
495 // ================================================================
496 EM_BOOL WasmOcctView::onWheelEvent (int theEventType, const EmscriptenWheelEvent* theEvent)
499 || theEventType != EMSCRIPTEN_EVENT_WHEEL)
504 Handle(Wasm_Window) aWindow = Handle(Wasm_Window)::DownCast (myView->Window());
505 return aWindow->ProcessWheelEvent (*this, theEventType, theEvent) ? EM_TRUE : EM_FALSE;
508 // ================================================================
509 // Function : onTouchEvent
511 // ================================================================
512 EM_BOOL WasmOcctView::onTouchEvent (int theEventType, const EmscriptenTouchEvent* theEvent)
519 Handle(Wasm_Window) aWindow = Handle(Wasm_Window)::DownCast (myView->Window());
520 return aWindow->ProcessTouchEvent (*this, theEventType, theEvent) ? EM_TRUE : EM_FALSE;
523 // ================================================================
524 // Function : navigationKeyModifierSwitch
526 // ================================================================
527 bool WasmOcctView::navigationKeyModifierSwitch (unsigned int theModifOld,
528 unsigned int theModifNew,
531 bool hasActions = false;
532 for (unsigned int aKeyIter = 0; aKeyIter < Aspect_VKey_ModifiersLower; ++aKeyIter)
534 if (!myKeys.IsKeyDown (aKeyIter))
539 Aspect_VKey anActionOld = Aspect_VKey_UNKNOWN, anActionNew = Aspect_VKey_UNKNOWN;
540 myNavKeyMap.Find (aKeyIter | theModifOld, anActionOld);
541 myNavKeyMap.Find (aKeyIter | theModifNew, anActionNew);
542 if (anActionOld == anActionNew)
547 if (anActionOld != Aspect_VKey_UNKNOWN)
549 myKeys.KeyUp (anActionOld, theTimeStamp);
551 if (anActionNew != Aspect_VKey_UNKNOWN)
554 myKeys.KeyDown (anActionNew, theTimeStamp);
560 // ================================================================
561 // Function : onKeyDownEvent
563 // ================================================================
564 EM_BOOL WasmOcctView::onKeyDownEvent (int theEventType, const EmscriptenKeyboardEvent* theEvent)
567 || theEventType != EMSCRIPTEN_EVENT_KEYDOWN) // EMSCRIPTEN_EVENT_KEYPRESS
572 Handle(Wasm_Window) aWindow = Handle(Wasm_Window)::DownCast (myView->Window());
573 return aWindow->ProcessKeyEvent (*this, theEventType, theEvent) ? EM_TRUE : EM_FALSE;
576 //=======================================================================
579 //=======================================================================
580 void WasmOcctView::KeyDown (Aspect_VKey theKey,
584 const unsigned int aModifOld = myKeys.Modifiers();
585 AIS_ViewController::KeyDown (theKey, theTime, thePressure);
587 const unsigned int aModifNew = myKeys.Modifiers();
588 if (aModifNew != aModifOld
589 && navigationKeyModifierSwitch (aModifOld, aModifNew, theTime))
591 // modifier key just pressed
594 Aspect_VKey anAction = Aspect_VKey_UNKNOWN;
595 if (myNavKeyMap.Find (theKey | myKeys.Modifiers(), anAction)
596 && anAction != Aspect_VKey_UNKNOWN)
598 AIS_ViewController::KeyDown (anAction, theTime, thePressure);
602 // ================================================================
603 // Function : onKeyUpEvent
605 // ================================================================
606 EM_BOOL WasmOcctView::onKeyUpEvent (int theEventType, const EmscriptenKeyboardEvent* theEvent)
609 || theEventType != EMSCRIPTEN_EVENT_KEYUP)
614 Handle(Wasm_Window) aWindow = Handle(Wasm_Window)::DownCast (myView->Window());
615 return aWindow->ProcessKeyEvent (*this, theEventType, theEvent) ? EM_TRUE : EM_FALSE;
618 //=======================================================================
621 //=======================================================================
622 void WasmOcctView::KeyUp (Aspect_VKey theKey,
625 const unsigned int aModifOld = myKeys.Modifiers();
626 AIS_ViewController::KeyUp (theKey, theTime);
628 Aspect_VKey anAction = Aspect_VKey_UNKNOWN;
629 if (myNavKeyMap.Find (theKey | myKeys.Modifiers(), anAction)
630 && anAction != Aspect_VKey_UNKNOWN)
632 AIS_ViewController::KeyUp (anAction, theTime);
633 processKeyPress (anAction);
636 const unsigned int aModifNew = myKeys.Modifiers();
637 if (aModifNew != aModifOld
638 && navigationKeyModifierSwitch (aModifOld, aModifNew, theTime))
640 // modifier key released
643 processKeyPress (theKey | aModifNew);
646 //==============================================================================
647 //function : processKeyPress
649 //==============================================================================
650 bool WasmOcctView::processKeyPress (Aspect_VKey theKey)
656 myView->FitAll (0.01, false);
664 // ================================================================
665 // Function : setCubemapBackground
667 // ================================================================
668 void WasmOcctView::setCubemapBackground (const std::string& theImagePath)
670 if (!theImagePath.empty())
672 emscripten_async_wget (theImagePath.c_str(), "/emulated/cubemap.jpg", CubemapAsyncLoader::onImageRead, CubemapAsyncLoader::onImageFailed);
676 WasmOcctView::Instance().View()->SetBackgroundCubeMap (Handle(Graphic3d_CubeMapPacked)(), true, false);
677 WasmOcctView::Instance().UpdateView();
681 // ================================================================
682 // Function : fitAllObjects
684 // ================================================================
685 void WasmOcctView::fitAllObjects (bool theAuto)
687 WasmOcctView& aViewer = Instance();
690 aViewer.FitAllAuto (aViewer.Context(), aViewer.View());
694 aViewer.View()->FitAll (0.01, false);
696 aViewer.UpdateView();
699 // ================================================================
700 // Function : removeAllObjects
702 // ================================================================
703 void WasmOcctView::removeAllObjects()
705 WasmOcctView& aViewer = Instance();
706 for (NCollection_IndexedDataMap<TCollection_AsciiString, Handle(AIS_InteractiveObject)>::Iterator anObjIter (aViewer.myObjects);
707 anObjIter.More(); anObjIter.Next())
709 aViewer.Context()->Remove (anObjIter.Value(), false);
711 aViewer.myObjects.Clear();
712 aViewer.UpdateView();
715 // ================================================================
716 // Function : removeObject
718 // ================================================================
719 bool WasmOcctView::removeObject (const std::string& theName)
721 WasmOcctView& aViewer = Instance();
722 Handle(AIS_InteractiveObject) anObj;
724 && !aViewer.myObjects.FindFromKey (theName.c_str(), anObj))
729 aViewer.Context()->Remove (anObj, false);
730 aViewer.myObjects.RemoveKey (theName.c_str());
731 aViewer.UpdateView();
735 // ================================================================
736 // Function : eraseObject
738 // ================================================================
739 bool WasmOcctView::eraseObject (const std::string& theName)
741 WasmOcctView& aViewer = Instance();
742 Handle(AIS_InteractiveObject) anObj;
744 && !aViewer.myObjects.FindFromKey (theName.c_str(), anObj))
749 aViewer.Context()->Erase (anObj, false);
750 aViewer.UpdateView();
754 // ================================================================
755 // Function : displayObject
757 // ================================================================
758 bool WasmOcctView::displayObject (const std::string& theName)
760 WasmOcctView& aViewer = Instance();
761 Handle(AIS_InteractiveObject) anObj;
763 && !aViewer.myObjects.FindFromKey (theName.c_str(), anObj))
768 aViewer.Context()->Display (anObj, false);
769 aViewer.UpdateView();
773 // ================================================================
774 // Function : openFromUrl
776 // ================================================================
777 void WasmOcctView::openFromUrl (const std::string& theName,
778 const std::string& theModelPath)
780 ModelAsyncLoader* aTask = new ModelAsyncLoader (theName.c_str(), theModelPath.c_str());
781 emscripten_async_wget_data (theModelPath.c_str(), (void* )aTask, ModelAsyncLoader::onDataRead, ModelAsyncLoader::onReadFailed);
784 // ================================================================
785 // Function : openFromMemory
787 // ================================================================
788 bool WasmOcctView::openFromMemory (const std::string& theName,
789 uintptr_t theBuffer, int theDataLen,
792 removeObject (theName);
793 char* aBytes = reinterpret_cast<char*>(theBuffer);
794 if (aBytes == nullptr
800 // Function to check if specified data stream starts with specified header.
801 #define dataStartsWithHeader(theData, theHeader) (::strncmp(theData, theHeader, sizeof(theHeader) - 1) == 0)
803 if (dataStartsWithHeader(aBytes, "DBRep_DrawableShape"))
805 return openBRepFromMemory (theName, theBuffer, theDataLen, theToFree);
807 else if (dataStartsWithHeader(aBytes, "glTF"))
809 //return openGltfFromMemory (theName, theBuffer, theDataLen, theToFree);
816 Message::SendFail() << "Error: file '" << theName.c_str() << "' has unsupported format";
820 // ================================================================
821 // Function : openBRepFromMemory
823 // ================================================================
824 bool WasmOcctView::openBRepFromMemory (const std::string& theName,
825 uintptr_t theBuffer, int theDataLen,
828 removeObject (theName);
830 WasmOcctView& aViewer = Instance();
832 BRep_Builder aBuilder;
833 bool isLoaded = false;
835 char* aRawData = reinterpret_cast<char*>(theBuffer);
836 Standard_ArrayStreamBuffer aStreamBuffer (aRawData, theDataLen);
837 std::istream aStream (&aStreamBuffer);
838 BRepTools::Read (aShape, aStream, aBuilder);
850 Handle(AIS_Shape) aShapePrs = new AIS_Shape (aShape);
851 if (!theName.empty())
853 aViewer.myObjects.Add (theName.c_str(), aShapePrs);
855 aShapePrs->SetMaterial (Graphic3d_NameOfMaterial_Silver);
856 aViewer.Context()->Display (aShapePrs, AIS_Shaded, 0, false);
857 aViewer.View()->FitAll (0.01, false);
858 aViewer.UpdateView();
860 Message::DefaultMessenger()->Send (TCollection_AsciiString("Loaded file ") + theName.c_str(), Message_Info);
861 Message::DefaultMessenger()->Send (OSD_MemInfo::PrintInfo(), Message_Trace);
865 // ================================================================
866 // Function : displayGround
868 // ================================================================
869 void WasmOcctView::displayGround (bool theToShow)
871 static Handle(AIS_Shape) aGroundPrs = new AIS_Shape (TopoDS_Shape());
873 WasmOcctView& aViewer = Instance();
877 aViewer.Context()->Remove (aGroundPrs, false);
878 aBox = aViewer.View()->View()->MinMaxValues();
881 || aBox.IsZThin (Precision::Confusion()))
883 if (!aGroundPrs.IsNull()
884 && aGroundPrs->HasInteractiveContext())
886 aViewer.Context()->Remove (aGroundPrs, false);
887 aViewer.UpdateView();
892 const gp_XYZ aSize = aBox.CornerMax().XYZ() - aBox.CornerMin().XYZ();
893 const double aRadius = Max (aSize.X(), aSize.Y());
894 const double aZValue = aBox.CornerMin().Z() - Min (10.0, aSize.Z() * 0.01);
895 const double aZSize = aRadius * 0.01;
896 gp_XYZ aGroundCenter ((aBox.CornerMin().X() + aBox.CornerMax().X()) * 0.5,
897 (aBox.CornerMin().Y() + aBox.CornerMax().Y()) * 0.5,
900 TopoDS_Compound aGround;
901 gp_Trsf aTrsf1, aTrsf2;
902 aTrsf1.SetTranslation (gp_Vec (aGroundCenter - gp_XYZ(0.0, 0.0, aZSize)));
903 aTrsf2.SetTranslation (gp_Vec (aGroundCenter));
904 Prs3d_ToolCylinder aCylTool (aRadius, aRadius, aZSize, 50, 1);
905 Prs3d_ToolDisk aDiskTool (0.0, aRadius, 50, 1);
906 TopoDS_Face aCylFace, aDiskFace1, aDiskFace2;
907 BRep_Builder().MakeFace (aCylFace, aCylTool .CreatePolyTriangulation (aTrsf1));
908 BRep_Builder().MakeFace (aDiskFace1, aDiskTool.CreatePolyTriangulation (aTrsf1));
909 BRep_Builder().MakeFace (aDiskFace2, aDiskTool.CreatePolyTriangulation (aTrsf2));
911 BRep_Builder().MakeCompound (aGround);
912 BRep_Builder().Add (aGround, aCylFace);
913 BRep_Builder().Add (aGround, aDiskFace1);
914 BRep_Builder().Add (aGround, aDiskFace2);
916 aGroundPrs->SetShape (aGround);
917 aGroundPrs->SetToUpdate();
918 aGroundPrs->SetMaterial (Graphic3d_NameOfMaterial_Stone);
919 aGroundPrs->SetInfiniteState (false);
920 aViewer.Context()->Display (aGroundPrs, AIS_Shaded, -1, false);
921 aGroundPrs->SetInfiniteState (true);
922 aViewer.UpdateView();
926 EMSCRIPTEN_BINDINGS(OccViewerModule) {
927 emscripten::function("setCubemapBackground", &WasmOcctView::setCubemapBackground);
928 emscripten::function("fitAllObjects", &WasmOcctView::fitAllObjects);
929 emscripten::function("removeAllObjects", &WasmOcctView::removeAllObjects);
930 emscripten::function("removeObject", &WasmOcctView::removeObject);
931 emscripten::function("eraseObject", &WasmOcctView::eraseObject);
932 emscripten::function("displayObject", &WasmOcctView::displayObject);
933 emscripten::function("displayGround", &WasmOcctView::displayGround);
934 emscripten::function("openFromUrl", &WasmOcctView::openFromUrl);
935 emscripten::function("openFromMemory", &WasmOcctView::openFromMemory, emscripten::allow_raw_pointers());
936 emscripten::function("openBRepFromMemory", &WasmOcctView::openBRepFromMemory, emscripten::allow_raw_pointers());