Angular 16+ & ThreeJS

@mikegoodstadt the purpose of the ngZone.runOutsideAngular is to avoid triggering the changeDetection for each AnimationFrame loop, because of ngZone will Monkey Patch every object it can reaches, so there will be one way to opt the WegGL stuff out of the ngZone area or just let run specific parts outside of angular.

The problem with removing and adding frames came from that the enging service is injected as root service. Would you remove this and let the service gets destroy with your component then the ngOnDestroy gets triggered, so it depends on your implementation if you want to have only one canvas or multiple ones.

Hey, thanks a lot for this, great time saver and reference.

The template is updated to latest Angular 9.1.0 and ThreeJS 115 compiled without errors.

Have some fun! :slight_smile:

1 Like

Hi there,

I built a small project using your threejs template as seed. And it works really nicely on “ng serve”, but after built, the resulting app doesn’t seem to load models and lights correctly,

like if the build process messed things up with threejs or something.

Did you ever encounter this?

1 Like

Ok, found it, there was a small error in code that locally was just being ignored, but on the built version it seemed to provoke a cascade error that broke the whole thing. A simple wrong path on one of the textures.

1 Like

The template is updated to latest Angular 10.0.x and ThreeJS 118 compiled without errors.

Have some fun! :slight_smile:

1 Like

@JohnnyDevNull This is a really cool project! Unfortunately the size of production main bundle is greater than 700 kB. Is there any way to reduce the bundle size?

Hi dude, I recently had to use three.js, and your viewing your project is what I want to achieve. Can you teach me how to read obj file, I would greatly appreciate it.

The template is updated to latest Angular 11.2.x and ThreeJS 118 compiled without errors.

Have some fun! :slight_smile:

The template is updated to latest Angular 12.0.x and ThreeJS 129 compiled without errors.

Have some fun! :slight_smile:

1 Like

I would love to know if you managed to solve it and how, the models with textures are all burned. Thank you so much

The template is updated to latest Angular 13.1.x and ThreeJS 136 compiled without errors.

Have some fun!

1 Like

Hello, I loaded model using GTLF Loader and its working. I stored the gtlf.scene in a variable.
I created a new method inside the engine.service file. I invoked it into engine.component.ts file.
I am seeing that, the model is undefined even if its loaded into scene.
thanks for reading my post.
engine.component.ts (779 Bytes)
engine.service.ts (5.9 KB)

The template is updated to latest Angular 14.2.x and ThreeJS 143 compiled and run without errors.

Have some fun!

1 Like

@JohnnyDevNull is how you call the render method within “this.ngZone.runOutsideAngular()” still a good practice? I am just asking because your initial post is from 2018.

I am using Angular 14 with ThreeJS

 public animate(): void {
    // We have to run this outside angular zones,
    // because it could trigger heavy changeDetection cycles.
    this.ngZone.runOutsideAngular(() => {
      if (document.readyState !== 'loading') {
        this.render();
      } else {
        window.addEventListener('DOMContentLoaded', () => {
          this.render();
        });
      }

      window.addEventListener('resize', () => {
        this.resize();
      });
    });
  }

@sappytree: If you not opt out the renderer within zonejs this is a important way to not invoke changeDetection with every frame.

So you have two options. The option which is provided in the template, btw this was a PR from someone other who introduced this importend key point or the second way, opt-out zone.js from the requestAnimationFrame. You can do this in the pollyfills.ts file.

Search for __Zone_disable_requestAnimationFrame and un-comment that line.

Then you can directly use the render method without manual opting out with the runOutsideAngular call.

Lovely greetings
Philipp

The template is updated to latest Angular 16.2.x and ThreeJS 155 compiled and run without errors.

Have some fun!