Angular 10+ & ThreeJS

I’d like to mention a caveat when working with angular.
It uses zone.js as it’s change detection strategy. This means that zone.js hooks onto requestAnimateFrame, and runs angular’s change detection on each tick.

This is very expensive and you will notice a big performance degradation.

What you need to do is to create a new file in the src folder called ngzone-flags.ts
which you import at the top of your polyfills.ts file.

in this file you should have this:

(window as any).__Zone_disable_requestAnimationFrame = true;
(window as any).__zone_symbol__BLACK_LISTED_EVENTS = ['scroll', 'mousemove', 'touchmove'];

This will tell zone.js to not hook these events.

To check if it worked you can use the performance panel in chrome and run in for a while, to then check the callstack for each request animate frame.
There should be no call to zone.js in there.

Hope this helps as it took me a while to realize why my threejs apps where running so slow in angular :slight_smile:

Ps.
It could potentially break other angular libraries that are dependant on these events, so keep that in mind.

5 Likes

Unfortunately I already did try the three-full npm package and I didn’t get the expected results… Could you explain to me: How would you import the LensFlareElement Class from a (working) Module with the three-full package?

Sorry for my dull questions but I am relatively new to angular and just to mention that I don’t have any clue about webpack, systemjs and what they all are called. I don’t even know how relevant that should be for a beginner. As far as my concern goes, managing to import unsupported modules shouldn’t be a worry at all :confused:

Creative Regards

Well i think i found a solution for this: https://github.com/Itee/three-full/issues/12#issuecomment-433426345

this could be a example conifg:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "es2015",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ],
    "paths": {
      "three-full": ["node_modules/@types/three"]
    }
  }
}

then you should be able to import it like this:
import { LensFlareElement } from 'three-full';

Thank your for your reply. I’ve tried your approach and I still get the same is not a constructor error. See the screenshot below:

ErrorOutout

I am also referring from this Topic:
How do I include THREE.LensFlareElement in Angular Project?

As from @Mugen87’s reply:

Fortunately, there will be a node script with the next release R101 that can generate modules from example files: Added script to generate modules out of example/js files

Whereas on the Milestone Page the progressbar indicates 42% complete. So i don’t think that there will be an easy solution in near time for using example based classes, even with third-party libraries or templates.

Anyway I’ll just leave this as a review for the angular template (non-ionic version) and thank you for your time.

Creative Regards

What about integration with Physijs? I’ve tried this module everything works fine except for the ammo.js part where I’m getting “something is not a constructor” error

FYI:

Three 0.111.0 is actually not compatible with TypeScript ~3.5.3 which is the latest Angular Dependency, so if you do a fresh install of my template you have to stay with max version 0.110.0.

Thats the last version i can compile without any errors.

For newer ones you will get the following errors:

We have to wait until Angular update the TypeScript dependency or try it yourself.

…and as a side note: Install @types/webgl2 and add it to the tsconfig.app.json. In my actual template version i’ve done this already. (see also here: Cannot find name 'WebGL2RenderingContext')

Hack / workaround:

  • upgrade typescript to 3.7.4
  • disableTypeScriptVersionCheck=true in tsconfig.json
  • Include into polyfills.ts:
    // @ts-ignore
    window.__importDefault = function(mod) {
      return mod && mod.__esModule ? mod : { default: mod };
    };
    

Sample commit: https://github.com/makimenko/angular-template-for-threejs/commit/d319384ee86a8d893ff26008087e6838d50a9373

Hey, im having angular 8.2.14. I updated typescript to 3.7.4 and included the disableTypeScriptVersionCheck in my tsconfig.json (I dont have a tsconfig.lib.ts?) and added the 2 lines of code to my polyfills. Tho its not working! What am I doing wrong? I really need that to work soon. : (

ERROR in The Angular Compiler requires TypeScript >=3.4.0 and <3.6.0 but 3.7.4 was found instead.

Unfortunately downgrade typescript

See there https://github.com/angular/angular-cli/issues/16071

You could try the 9-rc8 with the options described in the issue or just downgrade your typescript version together with the Three.js Version to the latest working described in my post above.

2 Likes

To solve this issue just add disableTypeScriptVersionCheck under angularCompilerOptions in your tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
"angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictTemplates": true,
    "disableTypeScriptVersionCheck": true,
    "trace": false
  },
  "exclude": ["node_modules", "tmp"]
}

Allows you to use unsupported version of typescripts without errors. I’m using ts version 3.8

Solved here https://github.com/angular/angular-cli/issues/16071

The template is now updated to Angular 9 and working on latest ThreeJs with Typescript 3.7

1 Like

Thanks! Here is it tested on StackBlitz :slight_smile:
https://angular-three-template.stackblitz.io

There is no reply to this but can I assume that this is now dealt with by “this.ngZone.runOutsideAngular” in the animate() method?
This was pushed shortly after your comment, on 29th Jan 2019:

Performance Update

Am I correct in thinking that the ngDestroy needs to be called to ensure disposal of previous scenes (causing accelerated/faster and faster spinning)?
I have added buttons on this StackBlitz to simulate what happens (in my app it gets triggered in other more intricate reloading events): https://angular-three-template.stackblitz.io/
For example, at the start of the animate() function seems a good place to call it…

@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