├── .editorconfig ├── .gitignore ├── README.md ├── angular.json ├── browserslist ├── e2e ├── protractor.conf.js ├── src │ ├── app.e2e-spec.ts │ └── app.po.ts └── tsconfig.json ├── karma.conf.js ├── package-lock.json ├── package.json ├── src ├── app │ ├── app-routing.module.ts │ ├── app.component.html │ ├── app.component.ts │ ├── app.module.ts │ ├── components │ │ ├── loading-spinner │ │ │ ├── loading-spinner.component.html │ │ │ ├── loading-spinner.component.scss │ │ │ └── loading-spinner.component.ts │ │ └── preference-pane │ │ │ ├── preference-pane.component.html │ │ │ ├── preference-pane.component.scss │ │ │ └── preference-pane.component.ts │ ├── models.ts │ ├── scenes │ │ ├── 1_unoptimized │ │ │ ├── unoptimized.component.html │ │ │ └── unoptimized.component.ts │ │ ├── 2_optimized │ │ │ ├── mesh-optimized.component.html │ │ │ └── mesh-optimized.component.ts │ │ ├── 3_instance │ │ │ ├── mesh-instances.component.html │ │ │ └── mesh-instances.component.ts │ │ ├── basic-render-service.abstract.ts │ │ └── solar-system.service.ts │ └── services │ │ ├── interaction.service.ts │ │ ├── loading.service.ts │ │ └── preference.service.ts ├── assets │ └── .gitkeep ├── environments │ ├── environment.prod.ts │ └── environment.ts ├── favicon.ico ├── index.html ├── main.ts ├── polyfills.ts ├── styles.scss └── test.ts ├── tsconfig.app.json ├── tsconfig.json ├── tsconfig.spec.json └── tslint.json /.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see https://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 2 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | max_line_length = off 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # compiled output 4 | /dist 5 | /tmp 6 | /out-tsc 7 | # Only exists if Bazel was run 8 | /bazel-out 9 | 10 | # dependencies 11 | /node_modules 12 | 13 | # profiling files 14 | chrome-profiler-events*.json 15 | speed-measure-plugin*.json 16 | 17 | # IDEs and editors 18 | /.idea 19 | .project 20 | .classpath 21 | .c9/ 22 | *.launch 23 | .settings/ 24 | *.sublime-workspace 25 | 26 | # IDE - VSCode 27 | .vscode/* 28 | !.vscode/settings.json 29 | !.vscode/tasks.json 30 | !.vscode/launch.json 31 | !.vscode/extensions.json 32 | .history/* 33 | 34 | # misc 35 | /.sass-cache 36 | /connect.lock 37 | /coverage 38 | /libpeerconnection.log 39 | npm-debug.log 40 | yarn-error.log 41 | testem.log 42 | /typings 43 | 44 | # System Files 45 | .DS_Store 46 | Thumbs.db 47 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NgBabylonJSPerfDemo 2 | 3 | This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 8.3.8. 4 | 5 | ## Development server 6 | 7 | Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files. 8 | 9 | ## Code scaffolding 10 | 11 | Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module`. 12 | 13 | ## Build 14 | 15 | Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `--prod` flag for a production build. 16 | 17 | ## Running unit tests 18 | 19 | Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io). 20 | 21 | ## Running end-to-end tests 22 | 23 | Run `ng e2e` to execute the end-to-end tests via [Protractor](http://www.protractortest.org/). 24 | 25 | ## Further help 26 | 27 | To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md). 28 | -------------------------------------------------------------------------------- /angular.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "./node_modules/@angular/cli/lib/config/schema.json", 3 | "version": 1, 4 | "newProjectRoot": "projects", 5 | "projects": { 6 | "ng-babylonJS-perf-demo": { 7 | "projectType": "application", 8 | "schematics": { 9 | "@schematics/angular:component": { 10 | "style": "scss" 11 | } 12 | }, 13 | "root": "", 14 | "sourceRoot": "src", 15 | "prefix": "app", 16 | "architect": { 17 | "build": { 18 | "builder": "@angular-devkit/build-angular:browser", 19 | "options": { 20 | "outputPath": "dist/ng-babylonJS-perf-demo", 21 | "index": "src/index.html", 22 | "main": "src/main.ts", 23 | "polyfills": "src/polyfills.ts", 24 | "tsConfig": "tsconfig.app.json", 25 | "aot": false, 26 | "assets": [ 27 | "src/favicon.ico", 28 | "src/assets" 29 | ], 30 | "styles": [ 31 | "./node_modules/@angular/material/prebuilt-themes/purple-green.css", 32 | "src/styles.scss" 33 | ], 34 | "scripts": [] 35 | }, 36 | "configurations": { 37 | "production": { 38 | "fileReplacements": [ 39 | { 40 | "replace": "src/environments/environment.ts", 41 | "with": "src/environments/environment.prod.ts" 42 | } 43 | ], 44 | "optimization": true, 45 | "outputHashing": "all", 46 | "sourceMap": false, 47 | "extractCss": true, 48 | "namedChunks": false, 49 | "aot": true, 50 | "extractLicenses": true, 51 | "vendorChunk": false, 52 | "buildOptimizer": true, 53 | "budgets": [ 54 | { 55 | "type": "initial", 56 | "maximumWarning": "2mb", 57 | "maximumError": "5mb" 58 | }, 59 | { 60 | "type": "anyComponentStyle", 61 | "maximumWarning": "6kb", 62 | "maximumError": "10kb" 63 | } 64 | ] 65 | } 66 | } 67 | }, 68 | "serve": { 69 | "builder": "@angular-devkit/build-angular:dev-server", 70 | "options": { 71 | "browserTarget": "ng-babylonJS-perf-demo:build" 72 | }, 73 | "configurations": { 74 | "production": { 75 | "browserTarget": "ng-babylonJS-perf-demo:build:production" 76 | } 77 | } 78 | }, 79 | "extract-i18n": { 80 | "builder": "@angular-devkit/build-angular:extract-i18n", 81 | "options": { 82 | "browserTarget": "ng-babylonJS-perf-demo:build" 83 | } 84 | }, 85 | "test": { 86 | "builder": "@angular-devkit/build-angular:karma", 87 | "options": { 88 | "main": "src/test.ts", 89 | "polyfills": "src/polyfills.ts", 90 | "tsConfig": "tsconfig.spec.json", 91 | "karmaConfig": "karma.conf.js", 92 | "assets": [ 93 | "src/favicon.ico", 94 | "src/assets" 95 | ], 96 | "styles": [ 97 | "./node_modules/@angular/material/prebuilt-themes/purple-green.css", 98 | "src/styles.scss" 99 | ], 100 | "scripts": [] 101 | } 102 | }, 103 | "lint": { 104 | "builder": "@angular-devkit/build-angular:tslint", 105 | "options": { 106 | "tsConfig": [ 107 | "tsconfig.app.json", 108 | "tsconfig.spec.json", 109 | "e2e/tsconfig.json" 110 | ], 111 | "exclude": [ 112 | "**/node_modules/**" 113 | ] 114 | } 115 | }, 116 | "e2e": { 117 | "builder": "@angular-devkit/build-angular:protractor", 118 | "options": { 119 | "protractorConfig": "e2e/protractor.conf.js", 120 | "devServerTarget": "ng-babylonJS-perf-demo:serve" 121 | }, 122 | "configurations": { 123 | "production": { 124 | "devServerTarget": "ng-babylonJS-perf-demo:serve:production" 125 | } 126 | } 127 | } 128 | } 129 | } 130 | }, 131 | "defaultProject": "ng-babylonJS-perf-demo" 132 | } -------------------------------------------------------------------------------- /browserslist: -------------------------------------------------------------------------------- 1 | # This file is used by the build system to adjust CSS and JS output to support the specified browsers below. 2 | # For additional information regarding the format and rule options, please see: 3 | # https://github.com/browserslist/browserslist#queries 4 | 5 | # You can see what browsers were selected by your queries by running: 6 | # npx browserslist 7 | 8 | > 0.5% 9 | last 2 versions 10 | Firefox ESR 11 | not dead 12 | not IE 9-11 # For IE 9-11 support, remove 'not'. -------------------------------------------------------------------------------- /e2e/protractor.conf.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | // Protractor configuration file, see link for more information 3 | // https://github.com/angular/protractor/blob/master/lib/config.ts 4 | 5 | const { SpecReporter } = require('jasmine-spec-reporter'); 6 | 7 | /** 8 | * @type { import("protractor").Config } 9 | */ 10 | exports.config = { 11 | allScriptsTimeout: 11000, 12 | specs: [ 13 | './src/**/*.e2e-spec.ts' 14 | ], 15 | capabilities: { 16 | 'browserName': 'chrome' 17 | }, 18 | directConnect: true, 19 | baseUrl: 'http://localhost:4200/', 20 | framework: 'jasmine', 21 | jasmineNodeOpts: { 22 | showColors: true, 23 | defaultTimeoutInterval: 30000, 24 | print: function() {} 25 | }, 26 | onPrepare() { 27 | require('ts-node').register({ 28 | project: require('path').join(__dirname, './tsconfig.json') 29 | }); 30 | jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } })); 31 | } 32 | }; -------------------------------------------------------------------------------- /e2e/src/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { AppPage } from './app.po'; 2 | import { browser, logging } from 'protractor'; 3 | 4 | describe('workspace-project App', () => { 5 | let page: AppPage; 6 | 7 | beforeEach(() => { 8 | page = new AppPage(); 9 | }); 10 | 11 | it('should display welcome message', () => { 12 | page.navigateTo(); 13 | expect(page.getTitleText()).toEqual('ng-babylonJS-perf-demo app is running!'); 14 | }); 15 | 16 | afterEach(async () => { 17 | // Assert that there are no errors emitted from the browser 18 | const logs = await browser.manage().logs().get(logging.Type.BROWSER); 19 | expect(logs).not.toContain(jasmine.objectContaining({ 20 | level: logging.Level.SEVERE, 21 | } as logging.Entry)); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /e2e/src/app.po.ts: -------------------------------------------------------------------------------- 1 | import { browser, by, element } from 'protractor'; 2 | 3 | export class AppPage { 4 | navigateTo() { 5 | return browser.get(browser.baseUrl) as Promise; 6 | } 7 | 8 | getTitleText() { 9 | return element(by.css('app-root .content span')).getText() as Promise; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /e2e/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/e2e", 5 | "module": "commonjs", 6 | "target": "es5", 7 | "types": [ 8 | "jasmine", 9 | "jasminewd2", 10 | "node" 11 | ] 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /karma.conf.js: -------------------------------------------------------------------------------- 1 | // Karma configuration file, see link for more information 2 | // https://karma-runner.github.io/1.0/config/configuration-file.html 3 | 4 | module.exports = function (config) { 5 | config.set({ 6 | basePath: '', 7 | frameworks: ['jasmine', '@angular-devkit/build-angular'], 8 | plugins: [ 9 | require('karma-jasmine'), 10 | require('karma-chrome-launcher'), 11 | require('karma-jasmine-html-reporter'), 12 | require('karma-coverage-istanbul-reporter'), 13 | require('@angular-devkit/build-angular/plugins/karma') 14 | ], 15 | client: { 16 | clearContext: false // leave Jasmine Spec Runner output visible in browser 17 | }, 18 | coverageIstanbulReporter: { 19 | dir: require('path').join(__dirname, './coverage/ng-babylonJS-perf-demo'), 20 | reports: ['html', 'lcovonly', 'text-summary'], 21 | fixWebpackSourcePaths: true 22 | }, 23 | reporters: ['progress', 'kjhtml'], 24 | port: 9876, 25 | colors: true, 26 | logLevel: config.LOG_INFO, 27 | autoWatch: true, 28 | browsers: ['Chrome'], 29 | singleRun: false, 30 | restartOnFileChange: true 31 | }); 32 | }; 33 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ng-babylon-js-perf-demo", 3 | "version": "0.0.0", 4 | "scripts": { 5 | "ng": "ng", 6 | "start": "ng serve", 7 | "build": "ng build", 8 | "test": "ng test", 9 | "lint": "ng lint", 10 | "e2e": "ng e2e" 11 | }, 12 | "private": true, 13 | "dependencies": { 14 | "@angular/animations": "~8.2.9", 15 | "@angular/cdk": "~8.2.2", 16 | "@angular/common": "~8.2.9", 17 | "@angular/compiler": "~8.2.9", 18 | "@angular/core": "~8.2.9", 19 | "@angular/forms": "~8.2.9", 20 | "@angular/material": "^8.2.2", 21 | "@angular/platform-browser": "~8.2.9", 22 | "@angular/platform-browser-dynamic": "~8.2.9", 23 | "@angular/router": "~8.2.9", 24 | "@babylonjs/core": "^4.0.3", 25 | "@babylonjs/gui": "^4.0.3", 26 | "@babylonjs/inspector": "^4.0.3", 27 | "@babylonjs/materials": "^4.0.3", 28 | "hammerjs": "^2.0.8", 29 | "rxjs": "~6.4.0", 30 | "tslib": "^1.10.0", 31 | "zone.js": "~0.9.1" 32 | }, 33 | "devDependencies": { 34 | "@angular-devkit/build-angular": "~0.803.8", 35 | "@angular/cli": "~8.3.8", 36 | "@angular/compiler-cli": "~8.2.9", 37 | "@angular/language-service": "~8.2.9", 38 | "@types/node": "~8.9.4", 39 | "@types/jasmine": "~3.3.8", 40 | "@types/jasminewd2": "~2.0.3", 41 | "codelyzer": "^5.0.0", 42 | "jasmine-core": "~3.4.0", 43 | "jasmine-spec-reporter": "~4.2.1", 44 | "karma": "~4.1.0", 45 | "karma-chrome-launcher": "~2.2.0", 46 | "karma-coverage-istanbul-reporter": "~2.0.1", 47 | "karma-jasmine": "~2.0.1", 48 | "karma-jasmine-html-reporter": "^1.4.0", 49 | "protractor": "~5.4.0", 50 | "ts-node": "~7.0.0", 51 | "tslint": "~5.15.0", 52 | "typescript": "~3.5.3" 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/app/app-routing.module.ts: -------------------------------------------------------------------------------- 1 | import {NgModule} from '@angular/core'; 2 | import {CommonModule} from '@angular/common'; 3 | import {RouterModule, Routes} from '@angular/router'; 4 | import {UnoptimizedComponent} from './scenes/1_unoptimized/unoptimized.component'; 5 | import {MeshOptimizedComponent} from './scenes/2_optimized/mesh-optimized.component'; 6 | import {MeshInstancesComponent} from './scenes/3_instance/mesh-instances.component'; 7 | 8 | 9 | export const routes: Routes = [ 10 | {path: 'naive', component: UnoptimizedComponent}, 11 | {path: 'meshOpti', component: MeshOptimizedComponent}, 12 | {path: 'instanceOpti', component: MeshInstancesComponent} 13 | ]; 14 | 15 | @NgModule({ 16 | declarations: [], 17 | imports: [ 18 | CommonModule, 19 | RouterModule.forRoot(routes) 20 | ], 21 | exports: [RouterModule] 22 | }) 23 | export class AppRoutingModule { 24 | } 25 | -------------------------------------------------------------------------------- /src/app/app.component.html: -------------------------------------------------------------------------------- 1 | 2 |

3 | Jupyter Clicked: {{jClicked}} 4 |
5 | 6 | 7 | Unoptimized 8 | Optimized 9 | Instances 10 | 11 | 13 | 14 | 15 |
16 | 17 |
18 |
19 |
20 | 21 |
22 | -------------------------------------------------------------------------------- /src/app/app.component.ts: -------------------------------------------------------------------------------- 1 | import {Component, OnDestroy, OnInit} from '@angular/core'; 2 | import {InteractionService} from './services/interaction.service'; 3 | import {Subscription} from 'rxjs'; 4 | 5 | @Component({ 6 | selector: 'app-root', 7 | templateUrl: './app.component.html', 8 | }) 9 | export class AppComponent implements OnInit, OnDestroy { 10 | jClicked = 0; 11 | showPrefPane = false; 12 | subscription: Subscription; 13 | 14 | constructor(readonly interaction: InteractionService) { 15 | } 16 | 17 | ngOnInit() { 18 | this.subscription = this.interaction.onJupyterClick.subscribe(() => this.jClicked++); 19 | } 20 | 21 | ngOnDestroy(): void { 22 | this.subscription.unsubscribe(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/app/app.module.ts: -------------------------------------------------------------------------------- 1 | import {BrowserModule} from '@angular/platform-browser'; 2 | import {NgModule} from '@angular/core'; 3 | 4 | import {AppComponent} from './app.component'; 5 | import {AppRoutingModule} from './app-routing.module'; 6 | import {UnoptimizedComponent} from './scenes/1_unoptimized/unoptimized.component'; 7 | import {MeshOptimizedComponent} from './scenes/2_optimized/mesh-optimized.component'; 8 | import {MeshInstancesComponent} from './scenes/3_instance/mesh-instances.component'; 9 | import {PreferencePaneComponent} from './components/preference-pane/preference-pane.component'; 10 | import {BrowserAnimationsModule} from '@angular/platform-browser/animations'; 11 | import { 12 | MatButtonModule, 13 | MatCardModule, 14 | MatInputModule, 15 | MatMenuModule, MatProgressSpinnerModule, 16 | MatSliderModule, 17 | MatSlideToggleModule, 18 | MatToolbarModule 19 | } from '@angular/material'; 20 | import {ReactiveFormsModule} from '@angular/forms'; 21 | import { LoadingSpinnerComponent } from './components/loading-spinner/loading-spinner.component'; 22 | 23 | @NgModule({ 24 | declarations: [ 25 | AppComponent, 26 | UnoptimizedComponent, 27 | MeshOptimizedComponent, 28 | MeshInstancesComponent, 29 | PreferencePaneComponent, 30 | LoadingSpinnerComponent, 31 | ], 32 | imports: [ 33 | BrowserModule, 34 | AppRoutingModule, 35 | BrowserAnimationsModule, 36 | MatSlideToggleModule, 37 | MatSliderModule, 38 | MatInputModule, 39 | MatCardModule, 40 | MatToolbarModule, 41 | MatMenuModule, 42 | MatButtonModule, 43 | ReactiveFormsModule, 44 | MatProgressSpinnerModule, 45 | ], 46 | providers: [], 47 | bootstrap: [AppComponent] 48 | }) 49 | export class AppModule { 50 | } 51 | -------------------------------------------------------------------------------- /src/app/components/loading-spinner/loading-spinner.component.html: -------------------------------------------------------------------------------- 1 |
2 | 3 |

{{message}}

4 |
5 | -------------------------------------------------------------------------------- /src/app/components/loading-spinner/loading-spinner.component.scss: -------------------------------------------------------------------------------- 1 | :host { 2 | position: fixed; 3 | top: 0; 4 | left: 0; 5 | z-index: 1000; 6 | } 7 | 8 | .backdrop { 9 | height: 100vh; 10 | width: 100vw; 11 | background-color: rgba(33, 33, 33, 0.6); 12 | display: flex; 13 | justify-content: center; 14 | align-items: center; 15 | flex-direction: column; 16 | 17 | p { 18 | color: white; 19 | font-weight: 700; 20 | width: 100%; 21 | text-align: center; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/app/components/loading-spinner/loading-spinner.component.ts: -------------------------------------------------------------------------------- 1 | import {Component, OnInit} from '@angular/core'; 2 | import {LoadingService} from '../../services/loading.service'; 3 | import {animate, state, style, transition, trigger} from '@angular/animations'; 4 | 5 | @Component({ 6 | selector: 'app-loading-spinner', 7 | templateUrl: './loading-spinner.component.html', 8 | styleUrls: ['./loading-spinner.component.scss'], 9 | animations: [ // the fade-in/fade-out animation. 10 | trigger('fade', [ 11 | state('in', style({opacity: 1})), 12 | transition(':enter', [ 13 | style({opacity: 0}), 14 | animate(250) 15 | ]), 16 | transition(':leave', 17 | animate(250, style({opacity: 0}))) 18 | ])] 19 | }) 20 | 21 | export class LoadingSpinnerComponent implements OnInit { 22 | 23 | constructor(readonly loading: LoadingService) { 24 | } 25 | 26 | ngOnInit() { 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/app/components/preference-pane/preference-pane.component.html: -------------------------------------------------------------------------------- 1 |

Optimizations

2 | 3 |

{{asteroidForm.value.amount | number:'4.0' }} Asteroids

4 | 5 | 6 | 7 | 12 | Divisions o.e. Asteroid 13 | 18 |
19 | 20 |

Mesh

21 | Freeze 22 | Merge into Groups 23 | Unindexed Meshes 24 | Flat Shaded Meshes 25 | Freeze Normals 26 | Disable Edge Rendering 27 | Sync Boundings 28 | Batch Create 29 |
30 | 31 | 32 |

Material

33 | Freeze 34 |
35 | 36 | 37 |

NgZone

38 | Run in NgZone 39 |
40 | -------------------------------------------------------------------------------- /src/app/components/preference-pane/preference-pane.component.scss: -------------------------------------------------------------------------------- 1 | :host { 2 | padding: 0.5rem; 3 | display: flex; 4 | flex-direction: column; 5 | 6 | mat-card { 7 | display: flex; 8 | flex-direction: column; 9 | margin: 0.5rem 0; 10 | * { 11 | margin: 0.3rem 0; 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/app/components/preference-pane/preference-pane.component.ts: -------------------------------------------------------------------------------- 1 | import {Component, OnDestroy, OnInit} from '@angular/core'; 2 | import {FormBuilder, FormControl, FormGroup} from '@angular/forms'; 3 | import {PreferenceService} from '../../services/preference.service'; 4 | import {debounceTime, distinctUntilChanged, take, takeUntil} from 'rxjs/operators'; 5 | import {Subject} from 'rxjs'; 6 | import {AsteroidConfiguration, MeshConfiguration} from '../../models'; 7 | 8 | @Component({ 9 | selector: 'app-preference-pane', 10 | templateUrl: './preference-pane.component.html', 11 | styleUrls: ['./preference-pane.component.scss'] 12 | }) 13 | export class PreferencePaneComponent implements OnInit, OnDestroy { 14 | 15 | meshForm: FormGroup; 16 | asteroidForm: FormGroup; 17 | zoneControl: FormControl = new FormControl(true); 18 | materialControl: FormControl = new FormControl(true); 19 | 20 | private readonly destroy = new Subject(); 21 | 22 | constructor(private readonly fb: FormBuilder, private readonly preference: PreferenceService) { 23 | } 24 | 25 | ngOnInit() { 26 | this.meshForm = this.fb.group({ 27 | freeze: null, 28 | merge: null, 29 | index: null, 30 | flat: null, 31 | normals: null, 32 | edge: null, 33 | boundings: null, 34 | batch: null, 35 | }); 36 | 37 | this.asteroidForm = this.fb.group({ 38 | amount: null, 39 | segments: null, 40 | }); 41 | 42 | this.preference.meshConfig.pipe(take(1)).subscribe(x => this.meshForm.patchValue(x)); 43 | this.preference.asteroidConfig.pipe(take(1)).subscribe(x => this.asteroidForm.patchValue(x)); 44 | this.preference.useNgZone.pipe(take(1)).subscribe(x => this.zoneControl.patchValue(x)); 45 | this.preference.materialConfig.pipe(take(1)).subscribe(x => this.materialControl.patchValue(x)); 46 | 47 | this.meshForm.valueChanges.pipe(takeUntil(this.destroy), debounceTime(500), distinctUntilChanged()) 48 | .subscribe((x: MeshConfiguration) => this.preference.meshConfig.next(x)); 49 | this.asteroidForm.valueChanges.pipe(takeUntil(this.destroy), debounceTime(500), distinctUntilChanged()) 50 | .subscribe((x: AsteroidConfiguration) => this.preference.asteroidConfig.next(x)); 51 | this.zoneControl.valueChanges.pipe(takeUntil(this.destroy), debounceTime(500), distinctUntilChanged()) 52 | .subscribe((x: boolean) => this.preference.useNgZone.next(x)); 53 | this.materialControl.valueChanges.pipe(takeUntil(this.destroy), debounceTime(500), distinctUntilChanged()) 54 | .subscribe((freeze: boolean) => this.preference.materialConfig.next({freeze})); 55 | } 56 | 57 | ngOnDestroy(): void { 58 | this.destroy.next(true); 59 | } 60 | 61 | formatLabel(value: number) { 62 | if (value >= 1000) { 63 | return '~' + (value / 1000).toFixed(1) + 'k'; 64 | } 65 | 66 | return value; 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/app/models.ts: -------------------------------------------------------------------------------- 1 | export interface MeshConfiguration { 2 | freeze: boolean; 3 | merge: boolean; 4 | index: boolean; 5 | flat: boolean; 6 | normals: boolean; 7 | edge: boolean; 8 | boundings: boolean; 9 | batch: boolean; 10 | } 11 | 12 | export interface AsteroidConfiguration { 13 | amount: number; 14 | segments: number; 15 | } 16 | 17 | export interface MaterialConfiguration { 18 | freeze: boolean; 19 | } 20 | -------------------------------------------------------------------------------- /src/app/scenes/1_unoptimized/unoptimized.component.html: -------------------------------------------------------------------------------- 1 |
2 | 3 |
4 | -------------------------------------------------------------------------------- /src/app/scenes/1_unoptimized/unoptimized.component.ts: -------------------------------------------------------------------------------- 1 | import {AfterViewInit, Component, ElementRef, OnDestroy, OnInit, ViewChild} from '@angular/core'; 2 | import {SolarSystem} from '../solar-system.service'; 3 | import {AbstractMesh, ActionManager, ExecuteCodeAction, MeshBuilder, Scene} from '@babylonjs/core'; 4 | import {PreferenceService} from '../../services/preference.service'; 5 | import {combineLatest, Subject} from 'rxjs'; 6 | import {debounceTime, takeUntil} from 'rxjs/operators'; 7 | import {AsteroidConfiguration, MeshConfiguration} from '../../models'; 8 | import {LoadingService} from '../../services/loading.service'; 9 | import {InteractionService} from '../../services/interaction.service'; 10 | 11 | const FPS = 60; 12 | 13 | @Component({ 14 | selector: 'app-unoptimized', 15 | templateUrl: './unoptimized.component.html', 16 | }) 17 | export class UnoptimizedComponent implements AfterViewInit, OnDestroy, OnInit { 18 | @ViewChild('rCanvas', {static: true}) 19 | canvasRef: ElementRef; 20 | 21 | // cleanup our subscriptions 22 | protected readonly destroy = new Subject(); 23 | // store configurations for easy access 24 | protected asteroidConfig: AsteroidConfiguration; 25 | protected meshConfig: MeshConfiguration; 26 | // store asteroids to clean up on changes 27 | protected readonly asteroids: AbstractMesh[] = []; 28 | 29 | constructor( 30 | protected readonly solarSystem: SolarSystem, 31 | protected readonly preferences: PreferenceService, 32 | protected readonly loading: LoadingService, 33 | protected readonly interaction: InteractionService, 34 | ) { 35 | } 36 | 37 | ngOnInit(): void { 38 | this.loading.message$.next('Initialising Scene ...'); 39 | this.initScene(); 40 | 41 | // subscribe to the preferences and handle them accordingly 42 | // we don't have a need to distinguish what event fires because the path afterwards is the same 43 | combineLatest(this.preferences.asteroidConfig, this.preferences.meshConfig).pipe(takeUntil(this.destroy), debounceTime(400)) 44 | .subscribe(([asteroidConfig, meshConfig]) => { 45 | // update config 46 | this.asteroidConfig = asteroidConfig; 47 | this.meshConfig = meshConfig; 48 | // change / update asteroids 49 | this.manageAsteroids(); 50 | }); 51 | 52 | this.preferences.materialConfig.pipe(takeUntil(this.destroy)).subscribe(conf => conf.freeze 53 | ? this.solarSystem.scene.freezeMaterials() 54 | : this.solarSystem.scene.unfreezeMaterials()); 55 | } 56 | 57 | initScene() { 58 | // get the scene object 59 | const scene = this.solarSystem.createScene(this.canvasRef); 60 | // by setting blockfreeActiveMeshesAndRenderingGroups we tell the engine to 61 | // insert all meshes without indexing and checking them 62 | scene.blockfreeActiveMeshesAndRenderingGroups = true; 63 | this.addPlanets(scene); 64 | // we have to set it back to its original state 65 | scene.blockfreeActiveMeshesAndRenderingGroups = false; 66 | 67 | } 68 | 69 | ngAfterViewInit(): void { 70 | // start the engine 71 | // be aware that we have to setup the scene before 72 | this.solarSystem.start(this.preferences.useNgZone.getValue()); 73 | } 74 | 75 | ngOnDestroy(): void { 76 | // stop the engine and clean up 77 | this.solarSystem.stop(); 78 | this.destroy.next(true); 79 | } 80 | 81 | manageAsteroids() { 82 | this.loading.message$.next('Managing Asteroids ...'); 83 | // unfreeze, our changes shouold be mirrored to the engine. 84 | this.solarSystem.scene.unfreezeActiveMeshes(); 85 | this.solarSystem.scene.unfreezeMaterials(); 86 | // as above, by setting blockfreeActiveMeshesAndRenderingGroups we tell the engine to 87 | // insert all meshes without indexing and checking them 88 | this.solarSystem.scene.blockfreeActiveMeshesAndRenderingGroups = this.meshConfig.batch; 89 | // clean the "old" asteroids, it is easier for the demo to recreate them with the 90 | // desired configuration than to patch every single one 91 | this.clearAsteroids(); 92 | this.loading.message$.next('Adding Asteroids ...'); 93 | // due to the possible blocking calculation a timeout is needed to display the message 94 | setTimeout(() => { 95 | this.addAsteroids(this.solarSystem.scene, this.asteroidConfig.amount); 96 | 97 | // by freezing the meshes and materials we can skip a lot of change observations 98 | // basically we tell the engine those things won't change 99 | if (this.preferences.materialConfig.getValue().freeze) { 100 | this.loading.message$.next('Freezing Materials ...'); 101 | this.solarSystem.scene.freezeMaterials(); 102 | } 103 | if (this.meshConfig.freeze) { 104 | this.loading.message$.next('Freezing Meshes ...'); 105 | this.solarSystem.scene.freezeActiveMeshes(); // 5-10 fps 106 | } 107 | 108 | this.solarSystem.scene.blockfreeActiveMeshesAndRenderingGroups = false; 109 | this.loading.message$.next(null); 110 | }, 30); 111 | 112 | } 113 | 114 | clearAsteroids() { 115 | this.loading.message$.next('Removing Asteroids ...'); 116 | // instruct the engine to remove this object and remove our reference too 117 | this.asteroids.slice().forEach((asteroid) => { 118 | asteroid.dispose(); 119 | this.asteroids.pop(); 120 | }); 121 | } 122 | 123 | addAsteroids(scene: Scene, amount: number) { 124 | for (let i = 0; i < amount; i++) { 125 | const s = MeshBuilder.CreateSphere(`sphere${i}`, {segments: this.asteroidConfig.segments, diameter: 1}, scene); 126 | this.solarSystem.addRandomMaterial(s); 127 | this.solarSystem.makeAsteroid(s, i); 128 | this.asteroids.push(s); 129 | s.isVisible = true; 130 | } 131 | } 132 | 133 | addPlanets(scene: Scene) { 134 | scene.beginAnimation(this.solarSystem.createPlanetInSystem('mercury', .3, 4, [.5, .5, .5]), 0, FPS, true, 0.25); 135 | scene.beginAnimation(this.solarSystem.createPlanetInSystem('venus', .4, 5, [.9, .9, 0]), 0, FPS, true, 0.2); 136 | scene.beginAnimation(this.solarSystem.createPlanetInSystem('earth', .6, 6.1, [0, 0, 1]), 0, FPS, true, 0.12); 137 | scene.beginAnimation(this.solarSystem.createPlanetInSystem('mars', .5, 7.3, [1, 0, 0]), 0, FPS, true, 0.1); 138 | 139 | const jupyter = this.solarSystem.createPlanetInSystem('jupyter', 1.3, 10.5, [.95, .95, .85]); 140 | jupyter.actionManager = new ActionManager(this.solarSystem.scene); 141 | jupyter.actionManager.registerAction(new ExecuteCodeAction(ActionManager.OnPickUpTrigger, 142 | () => this.interaction.onJupyterClick.next()) 143 | ); 144 | scene.beginAnimation(jupyter, 0, FPS, true, 0.05); 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /src/app/scenes/2_optimized/mesh-optimized.component.html: -------------------------------------------------------------------------------- 1 |
2 | 3 |
4 | -------------------------------------------------------------------------------- /src/app/scenes/2_optimized/mesh-optimized.component.ts: -------------------------------------------------------------------------------- 1 | import {Component} from '@angular/core'; 2 | import {AbstractMesh, Mesh, MeshBuilder, Scene} from '@babylonjs/core'; 3 | import {UnoptimizedComponent} from '../1_unoptimized/unoptimized.component'; 4 | 5 | @Component({ 6 | selector: 'app-mesh-optimized', 7 | templateUrl: './mesh-optimized.component.html', 8 | }) 9 | export class MeshOptimizedComponent extends UnoptimizedComponent { 10 | addAsteroids(scene: Scene, amount: number) { 11 | const baseSphere = this.getBaseSphere(); 12 | 13 | this.loading.message$.next('Adding Asteroids ...'); 14 | for (let i = 0; i < amount; i++) { 15 | const asteroid = baseSphere.clone('instance' + i); 16 | this.asteroids.push(asteroid); 17 | this.solarSystem.makeAsteroid(asteroid, i); 18 | asteroid.isVisible = true; 19 | } 20 | 21 | if (!this.meshConfig.merge) { 22 | return; 23 | } 24 | 25 | this.loading.message$.next('Grouping Asteroids ...'); 26 | const groupSize = 300; 27 | const merged = []; 28 | for (let i = 0; i < amount; i += groupSize) { 29 | const upper = i + groupSize > this.asteroids.length ? this.asteroids.length : i + groupSize; 30 | const mergedMesh = Mesh.MergeMeshes(this.asteroids.slice(i, upper) as Mesh[], true); 31 | if (mergedMesh) { 32 | mergedMesh.parent = this.solarSystem.sun; 33 | this.solarSystem.addRandomMaterial(mergedMesh); 34 | merged.push(mergedMesh); 35 | } 36 | } 37 | this.loading.message$.next('Clearing "single" asteroids ...'); 38 | this.clearAsteroids(); 39 | this.loading.message$.next('Adding "merged" asteroids ...'); 40 | this.asteroids.push(...merged); 41 | } 42 | 43 | getBaseSphere(suffix = ''): Mesh { 44 | const baseSphere = MeshBuilder.CreateSphere('BaseSphere' + suffix, { 45 | segments: this.asteroidConfig.segments, 46 | diameter: 1 47 | }, this.solarSystem.scene); 48 | if (this.meshConfig.index) { 49 | baseSphere.convertToUnIndexedMesh(); 50 | } // TEUER BEI VIELEN MESHES - 1-3fps 51 | if (this.meshConfig.flat) { 52 | baseSphere.convertToFlatShadedMesh(); 53 | } // TEUER BEI VIELEN MESHES - 1-3fps 54 | baseSphere.cullingStrategy = AbstractMesh.CULLINGSTRATEGY_OPTIMISTIC_INCLUSION_THEN_BSPHERE_ONLY; 55 | baseSphere.isVisible = false; 56 | if (this.meshConfig.normals) { 57 | baseSphere.freezeNormals(); 58 | } 59 | if (this.meshConfig.edge) { 60 | baseSphere.disableEdgesRendering(); 61 | } 62 | if (this.meshConfig.boundings) { 63 | baseSphere.doNotSyncBoundingInfo = true; 64 | } // 5-10 fps; 65 | this.solarSystem.addRandomMaterial(baseSphere); 66 | return baseSphere; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/app/scenes/3_instance/mesh-instances.component.html: -------------------------------------------------------------------------------- 1 |
2 | 3 |
4 | -------------------------------------------------------------------------------- /src/app/scenes/3_instance/mesh-instances.component.ts: -------------------------------------------------------------------------------- 1 | import {Component} from '@angular/core'; 2 | import {MeshOptimizedComponent} from '../2_optimized/mesh-optimized.component'; 3 | import {Mesh, Scene} from '@babylonjs/core'; 4 | 5 | @Component({ 6 | selector: 'app-mesh-optimized', 7 | templateUrl: './mesh-instances.component.html' 8 | }) 9 | export class MeshInstancesComponent extends MeshOptimizedComponent { 10 | addAsteroids(scene: Scene, amount: number) { 11 | const bases: Mesh[] = []; 12 | // create 4 different meshes 13 | for (let i = 0; i < 6; i++) { 14 | const base = this.getBaseSphere(`${i}`); 15 | bases.push(base); 16 | } 17 | 18 | for (let i = 0; i < amount; i++) { 19 | // create an mesh instance, this shares everything with the base mesh, 20 | // except its rotation, scale and position 21 | const asteroid = bases[i % 6].createInstance('instance' + i); 22 | this.asteroids.push(asteroid); 23 | this.solarSystem.makeAsteroid(asteroid, i); 24 | } 25 | this.asteroids.push(...bases); 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /src/app/scenes/basic-render-service.abstract.ts: -------------------------------------------------------------------------------- 1 | import {ElementRef, NgZone} from '@angular/core'; 2 | import { 3 | ArcRotateCamera, 4 | Color3, 5 | Color4, 6 | DynamicTexture, 7 | Engine, 8 | FreeCamera, 9 | HemisphericLight, 10 | Light, 11 | Mesh, 12 | MeshBuilder, 13 | Scene, 14 | StandardMaterial, 15 | Vector3 16 | } from '@babylonjs/core'; 17 | import '@babylonjs/inspector'; 18 | 19 | 20 | export class BasicRenderServiceAbstract { 21 | protected engine: Engine; 22 | protected canvas: HTMLCanvasElement; 23 | protected camera: FreeCamera | ArcRotateCamera; 24 | protected light: Light; 25 | 26 | rootMesh: Mesh; 27 | scene: Scene; 28 | 29 | public constructor(private readonly ngZone: NgZone, private document: Document) { 30 | } 31 | 32 | createScene(canvas: ElementRef): void { 33 | this.canvas = canvas.nativeElement; 34 | this.canvas.style.height = '100%'; 35 | this.canvas.style.width = '100%'; 36 | this.engine = new Engine(this.canvas, true); 37 | 38 | this.scene = new Scene(this.engine); 39 | this.scene.clearColor = new Color4(.1, .1, .1, 1); 40 | this.rootMesh = MeshBuilder.CreateDisc('root', {radius: .01}, this.scene); 41 | 42 | this.light = new HemisphericLight('light1', new Vector3(0, 1, 0), this.scene); 43 | 44 | // generates the world x-y-z axis for better understanding 45 | this.showWorldAxis(8); 46 | 47 | // rotate scene by mouse-move 48 | // https://www.babylonjs-playground.com/#CGXLT#5 49 | 50 | let clicked = false; 51 | const currentPosition = {x: 0, y: 0}; 52 | const currentRotation = {x: 0, y: 0}; 53 | let mousemov = false; 54 | let framecount = 0; 55 | const mxframecount = 120; //4 secs at 60 fps 56 | const lastAngleDiff = {x: 0, y: 0}; 57 | const oldAngle = {x: 0, y: 0}; 58 | const newAngle = {x: 0, y: 0}; 59 | 60 | 61 | this.scene.beforeRender = () => { 62 | mousemov = false; 63 | }; 64 | 65 | this.scene.afterRender = () => { 66 | if (!mousemov && framecount < mxframecount) { 67 | lastAngleDiff.x = lastAngleDiff.x / 1.1; 68 | lastAngleDiff.y = lastAngleDiff.y / 1.1; 69 | this.rootMesh.rotation.x += lastAngleDiff.x; 70 | this.rootMesh.rotation.y += lastAngleDiff.y; 71 | framecount++; 72 | currentRotation.x = this.rootMesh.rotation.x; 73 | currentRotation.y = this.rootMesh.rotation.y; 74 | } else if (framecount >= mxframecount) { 75 | framecount = 0; 76 | } 77 | }; 78 | 79 | this.camera = new ArcRotateCamera('Camera', 0, 0.8, 35, Vector3.Zero(), this.scene); 80 | this.camera.setTarget(this.rootMesh); 81 | 82 | 83 | canvas.nativeElement.addEventListener('pointerdown', (evt) => { 84 | currentPosition.x = evt.clientX; 85 | currentPosition.y = evt.clientY; 86 | currentRotation.x = this.rootMesh.rotation.x; 87 | currentRotation.y = this.rootMesh.rotation.y; 88 | clicked = true; 89 | }); 90 | 91 | canvas.nativeElement.addEventListener('pointermove', (evt) => { 92 | 93 | if (clicked) { 94 | mousemov = true; 95 | } 96 | if (!clicked) { 97 | return; 98 | } 99 | oldAngle.x = this.rootMesh.rotation.x; 100 | oldAngle.y = this.rootMesh.rotation.y; 101 | this.rootMesh.rotation.y -= (evt.clientX - currentPosition.x) / 300.0; 102 | this.rootMesh.rotation.x -= (evt.clientY - currentPosition.y) / 300.0; 103 | newAngle.x = this.rootMesh.rotation.x; 104 | newAngle.y = this.rootMesh.rotation.y; 105 | lastAngleDiff.x = newAngle.x - oldAngle.x; 106 | lastAngleDiff.y = newAngle.y - oldAngle.y; 107 | currentPosition.x = evt.clientX; 108 | currentPosition.y = evt.clientY; 109 | }); 110 | 111 | canvas.nativeElement.addEventListener('pointerup', () => { 112 | clicked = false; 113 | }); 114 | } 115 | 116 | start(inZone = true): void { 117 | 118 | if (inZone) { 119 | this.ngZone.runOutsideAngular(() => { 120 | this.startTheEngine(); 121 | }); 122 | } else { 123 | this.startTheEngine(); 124 | } 125 | } 126 | 127 | stop(): void { 128 | this.scene.dispose(); 129 | this.engine.stopRenderLoop(); 130 | this.engine.dispose(); 131 | this.camera.dispose(); 132 | window.removeEventListener('resize', () => { 133 | }); 134 | } 135 | 136 | private startTheEngine() { 137 | let freshRender = true; 138 | const element = this.document.getElementById('fpsLabel'); 139 | 140 | this.engine.runRenderLoop(() => { 141 | this.scene.render(); 142 | if (element) { 143 | element.innerHTML = this.engine.getFps().toFixed() + ' fps'; 144 | } 145 | if (freshRender) { 146 | this.engine.resize(); 147 | freshRender = false; 148 | } 149 | }); 150 | window.addEventListener('resize', () => this.engine.resize()); 151 | } 152 | 153 | 154 | /** 155 | * Source: https://doc.babylonjs.com/snippets/world_axes 156 | * @param size number 157 | */ 158 | showWorldAxis(size: number) { 159 | 160 | 161 | const axisX = Mesh.CreateLines( 162 | 'axisX', 163 | [ 164 | Vector3.Zero(), 165 | new Vector3(size, 0, 0), new Vector3(size * 0.95, 0.05 * size, 0), 166 | new Vector3(size, 0, 0), new Vector3(size * 0.95, -0.05 * size, 0) 167 | ], 168 | this.scene 169 | ); 170 | 171 | axisX.color = new Color3(1, 0, 0); 172 | const xChar = this.makeTextPlane('X', 'red', size / 10); 173 | xChar.position = new Vector3(0.9 * size, -0.05 * size, 0); 174 | 175 | const axisY = Mesh.CreateLines( 176 | 'axisY', 177 | [ 178 | Vector3.Zero(), new Vector3(0, size, 0), new Vector3(-0.05 * size, size * 0.95, 0), 179 | new Vector3(0, size, 0), new Vector3(0.05 * size, size * 0.95, 0) 180 | ], 181 | this.scene 182 | ); 183 | 184 | axisY.color = new Color3(0, 1, 0); 185 | const yChar = this.makeTextPlane('Y', 'green', size / 10); 186 | yChar.position = new Vector3(0, 0.9 * size, -0.05 * size); 187 | 188 | const axisZ = Mesh.CreateLines( 189 | 'axisZ', 190 | [ 191 | Vector3.Zero(), new Vector3(0, 0, size), new Vector3(0, -0.05 * size, size * 0.95), 192 | new Vector3(0, 0, size), new Vector3(0, 0.05 * size, size * 0.95) 193 | ], 194 | this.scene 195 | ); 196 | 197 | axisZ.color = new Color3(0, 0, 1); 198 | const zChar = this.makeTextPlane('Z', 'blue', size / 10); 199 | zChar.position = new Vector3(0, 0.05 * size, 0.9 * size); 200 | } 201 | 202 | makeTextPlane(text: string, color: string, textSize: number) { 203 | const dynamicTexture = new DynamicTexture('DynamicTexture', 50, this.scene, true); 204 | dynamicTexture.hasAlpha = true; 205 | dynamicTexture.drawText(text, 5, 40, 'bold 36px Arial', color, 'transparent', true); 206 | const plane = Mesh.CreatePlane('TextPlane', textSize, this.scene, true); 207 | const material = new StandardMaterial('TextPlaneMaterial', this.scene); 208 | material.backFaceCulling = false; 209 | material.specularColor = new Color3(0, 0, 0); 210 | material.diffuseTexture = dynamicTexture; 211 | plane.material = material; 212 | 213 | return plane; 214 | } 215 | 216 | } 217 | -------------------------------------------------------------------------------- /src/app/scenes/solar-system.service.ts: -------------------------------------------------------------------------------- 1 | import {ElementRef, Inject, Injectable, NgZone} from '@angular/core'; 2 | import {BasicRenderServiceAbstract} from './basic-render-service.abstract'; 3 | import { 4 | Animation, 5 | Axis, 6 | Color3, 7 | InstancedMesh, 8 | Matrix, 9 | Mesh, 10 | MeshBuilder, 11 | PointLight, 12 | Scene, 13 | Space, 14 | StandardMaterial, 15 | Vector3 16 | } from '@babylonjs/core'; 17 | import {DOCUMENT} from '@angular/common'; 18 | import {PreferenceService} from '../services/preference.service'; 19 | 20 | const FPS = 60; 21 | 22 | @Injectable({ 23 | providedIn: 'root' 24 | }) 25 | export class SolarSystem extends BasicRenderServiceAbstract { 26 | 27 | sun: Mesh; 28 | 29 | readonly rotationAnim = new Animation('rotate', 'rotation.y', FPS, Animation.ANIMATIONTYPE_FLOAT, Animation.ANIMATIONLOOPMODE_CYCLE); 30 | readonly wobbleAnim = new Animation('wobble', 'position.y', FPS, Animation.ANIMATIONTYPE_FLOAT, Animation.ANIMATIONLOOPMODE_RELATIVE); 31 | readonly rotationKeys = [ 32 | {frame: 0, value: 0}, 33 | {frame: FPS / 2, value: Math.PI}, 34 | {frame: FPS, value: Math.PI * 2}, 35 | ]; 36 | readonly wobbleKeys = [ 37 | {frame: 0, value: -1}, 38 | {frame: FPS * .5, value: 1}, 39 | {frame: FPS, value: -1}, 40 | ]; 41 | 42 | constructor(readonly zone: NgZone, @Inject(DOCUMENT) readonly doc: Document) { 43 | super(zone, document); 44 | this.rotationAnim.setKeys(this.rotationKeys); 45 | this.wobbleAnim.setKeys(this.wobbleKeys); 46 | } 47 | 48 | createScene(canvas: ElementRef): Scene { 49 | 50 | if (this.scene) { 51 | this.scene.dispose(); 52 | } 53 | 54 | super.createScene(canvas); 55 | 56 | // this.light.dispose(); 57 | this.light = new PointLight('sun', new Vector3(0, 0, 0), this.scene); 58 | this.sun = MeshBuilder.CreateSphere('s1', {segments: 32, diameter: 2}); 59 | const sphereMaterial = new StandardMaterial('sun_surface', this.scene); 60 | sphereMaterial.emissiveColor = new Color3(1, 0.8, 0); 61 | this.sun.material = sphereMaterial; 62 | this.sun.parent = this.rootMesh; 63 | 64 | this.scene.registerAfterRender(() => { 65 | this.sun.rotate( 66 | new Vector3(0, 1, 0), 67 | 0.0004, 68 | Space.LOCAL 69 | ); 70 | }); 71 | 72 | return this.scene; 73 | } 74 | 75 | createPlanetInSystem(name, diameter, distance, color: [number, number, number]) { 76 | const offY = -1 + Math.random(); 77 | const mesh = MeshBuilder.CreateSphere(name, {diameter, segments: 16}, this.scene); 78 | mesh.parent = this.sun; 79 | mesh.setPivotMatrix(Matrix.Translation(distance, -offY, 0), false); 80 | mesh.rotation.y = Math.PI * Math.random(); 81 | mesh.animations = []; 82 | mesh.animations.push(this.rotationAnim); 83 | mesh.animations.push(this.wobbleAnim); 84 | if (color) { 85 | const sphereMaterial = new StandardMaterial(name, this.scene); 86 | sphereMaterial.diffuseColor = new Color3(color[0], color[1], color[2]); 87 | mesh.material = sphereMaterial; 88 | } 89 | return mesh; 90 | } 91 | 92 | addRandomMaterial(mesh: Mesh) { 93 | const sphereMaterial = new StandardMaterial('ranMat' + Math.random(), this.scene); 94 | sphereMaterial.diffuseColor = new Color3(Math.random(), Math.random(), Math.random()).scale(.5); 95 | mesh.material = sphereMaterial; 96 | } 97 | 98 | makeAsteroid(mesh: Mesh | InstancedMesh, nth: number) { 99 | mesh.scaling = new Vector3(0.1, 0.1, 0.1); 100 | mesh.translate(Axis.Y, Math.random() * 1.3 - 1, Space.WORLD); 101 | mesh.translate(Axis.X, Math.random(), Space.WORLD); 102 | mesh.parent = this.sun; 103 | mesh.setPivotMatrix(Matrix.Translation(-90, 0, 0), false); 104 | mesh.rotate(Axis.Y, Math.PI / 2 * nth * Math.random()); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/app/services/interaction.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | import {Subject} from 'rxjs'; 3 | 4 | @Injectable({ 5 | providedIn: 'root' 6 | }) 7 | export class InteractionService { 8 | onJupyterClick = new Subject(); 9 | } 10 | -------------------------------------------------------------------------------- /src/app/services/loading.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | import {ReplaySubject} from 'rxjs'; 3 | 4 | @Injectable({ 5 | providedIn: 'root' 6 | }) 7 | export class LoadingService { 8 | message$ = new ReplaySubject(1); 9 | } 10 | -------------------------------------------------------------------------------- /src/app/services/preference.service.ts: -------------------------------------------------------------------------------- 1 | import {Injectable} from '@angular/core'; 2 | import {AsteroidConfiguration, MaterialConfiguration, MeshConfiguration} from '../models'; 3 | import {BehaviorSubject} from 'rxjs'; 4 | 5 | @Injectable({ 6 | providedIn: 'root' 7 | }) 8 | export class PreferenceService { 9 | readonly meshConfig = new BehaviorSubject({ 10 | freeze: true, 11 | merge: true, 12 | index: true, 13 | flat: true, 14 | normals: true, 15 | edge: true, 16 | boundings: true, 17 | batch: true, 18 | }); 19 | readonly asteroidConfig = new BehaviorSubject({amount: 4000, segments: 2}); 20 | readonly useNgZone = new BehaviorSubject(true); 21 | readonly materialConfig = new BehaviorSubject({freeze: true}); 22 | } 23 | -------------------------------------------------------------------------------- /src/assets/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thinktecture/ng-babylonJS-perf-demo/ad64c6e6c10b5e7a0504ea400bfa6a3dd250d397/src/assets/.gitkeep -------------------------------------------------------------------------------- /src/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true 3 | }; 4 | -------------------------------------------------------------------------------- /src/environments/environment.ts: -------------------------------------------------------------------------------- 1 | // This file can be replaced during build by using the `fileReplacements` array. 2 | // `ng build --prod` replaces `environment.ts` with `environment.prod.ts`. 3 | // The list of file replacements can be found in `angular.json`. 4 | 5 | export const environment = { 6 | production: false 7 | }; 8 | 9 | /* 10 | * For easier debugging in development mode, you can import the following file 11 | * to ignore zone related error stack frames such as `zone.run`, `zoneDelegate.invokeTask`. 12 | * 13 | * This import should be commented out in production mode because it will have a negative impact 14 | * on performance if an error is thrown. 15 | */ 16 | // import 'zone.js/dist/zone-error'; // Included with Angular CLI. 17 | -------------------------------------------------------------------------------- /src/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thinktecture/ng-babylonJS-perf-demo/ad64c6e6c10b5e7a0504ea400bfa6a3dd250d397/src/favicon.ico -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | NgBabylonJSPerfDemo 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import {enableProdMode} from '@angular/core'; 2 | import {platformBrowserDynamic} from '@angular/platform-browser-dynamic'; 3 | 4 | import {AppModule} from './app/app.module'; 5 | import {environment} from './environments/environment'; 6 | 7 | import 'hammerjs'; 8 | 9 | if (environment.production) { 10 | enableProdMode(); 11 | } 12 | 13 | platformBrowserDynamic().bootstrapModule(AppModule) 14 | .catch(err => console.error(err)); 15 | -------------------------------------------------------------------------------- /src/polyfills.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * This file includes polyfills needed by Angular and is loaded before the app. 3 | * You can add your own extra polyfills to this file. 4 | * 5 | * This file is divided into 2 sections: 6 | * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. 7 | * 2. Application imports. Files imported after ZoneJS that should be loaded before your main 8 | * file. 9 | * 10 | * The current setup is for so-called "evergreen" browsers; the last versions of browsers that 11 | * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera), 12 | * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile. 13 | * 14 | * Learn more in https://angular.io/guide/browser-support 15 | */ 16 | 17 | /*************************************************************************************************** 18 | * BROWSER POLYFILLS 19 | */ 20 | 21 | /** IE10 and IE11 requires the following for NgClass support on SVG elements */ 22 | // import 'classlist.js'; // Run `npm install --save classlist.js`. 23 | 24 | /** 25 | * Web Animations `@angular/platform-browser/animations` 26 | * Only required if AnimationBuilder is used within the application and using IE/Edge or Safari. 27 | * Standard animation support in Angular DOES NOT require any polyfills (as of Angular 6.0). 28 | */ 29 | // import 'web-animations-js'; // Run `npm install --save web-animations-js`. 30 | 31 | /** 32 | * By default, zone.js will patch all possible macroTask and DomEvents 33 | * user can disable parts of macroTask/DomEvents patch by setting following flags 34 | * because those flags need to be set before `zone.js` being loaded, and webpack 35 | * will put import in the top of bundle, so user need to create a separate file 36 | * in this directory (for example: zone-flags.ts), and put the following flags 37 | * into that file, and then add the following code before importing zone.js. 38 | * import './zone-flags.ts'; 39 | * 40 | * The flags allowed in zone-flags.ts are listed here. 41 | * 42 | * The following flags will work for all browsers. 43 | * 44 | * (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame 45 | * (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick 46 | * (window as any).__zone_symbol__UNPATCHED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames 47 | * 48 | * in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js 49 | * with the following flag, it will bypass `zone.js` patch for IE/Edge 50 | * 51 | * (window as any).__Zone_enable_cross_context_check = true; 52 | * 53 | */ 54 | 55 | /*************************************************************************************************** 56 | * Zone JS is required by default for Angular itself. 57 | */ 58 | import 'zone.js/dist/zone'; // Included with Angular CLI. 59 | 60 | 61 | /*************************************************************************************************** 62 | * APPLICATION IMPORTS 63 | */ 64 | -------------------------------------------------------------------------------- /src/styles.scss: -------------------------------------------------------------------------------- 1 | 2 | body { 3 | height: 100vh; 4 | padding: 0; 5 | margin: 0; 6 | font-family: Roboto, "Helvetica Neue", sans-serif; 7 | } 8 | 9 | 10 | .container { 11 | height: 100%; 12 | width: 100%; 13 | display: grid; 14 | grid-template-rows: 4rem; 15 | grid-template-columns: auto; 16 | 17 | .content { 18 | grid-column: 1; 19 | grid-row: 2; 20 | } 21 | 22 | .nav { 23 | grid-column-start: 1; 24 | grid-row: 1; 25 | 26 | a { 27 | margin: 0 .2rem; 28 | } 29 | } 30 | } 31 | 32 | .prefPane { 33 | top: 0; 34 | right: 0; 35 | position: fixed; 36 | height: 100vh; 37 | width: 20rem; 38 | overflow: scroll; 39 | transition: .25s; 40 | transform: translateX(100%); 41 | background-color: white; 42 | 43 | &.hidden { 44 | transform: translateX(0); 45 | transition: .25s; 46 | } 47 | } 48 | 49 | .activeRoute { 50 | background-color: #69f0ae; 51 | color: black; 52 | transition: .25s; 53 | } 54 | 55 | .wrapper { 56 | height: 100%; 57 | width: 100%; 58 | } 59 | 60 | #fpsLabel { 61 | position: absolute; 62 | left: 1.5rem; 63 | top: 4rem; 64 | color: #7c7c7c; 65 | cursor: default; 66 | } 67 | 68 | #jupyterLabel { 69 | position: absolute; 70 | left: 1.5rem; 71 | bottom: 2rem; 72 | } 73 | -------------------------------------------------------------------------------- /src/test.ts: -------------------------------------------------------------------------------- 1 | // This file is required by karma.conf.js and loads recursively all the .spec and framework files 2 | 3 | import 'zone.js/dist/zone-testing'; 4 | import { getTestBed } from '@angular/core/testing'; 5 | import { 6 | BrowserDynamicTestingModule, 7 | platformBrowserDynamicTesting 8 | } from '@angular/platform-browser-dynamic/testing'; 9 | 10 | declare const require: any; 11 | 12 | // First, initialize the Angular testing environment. 13 | getTestBed().initTestEnvironment( 14 | BrowserDynamicTestingModule, 15 | platformBrowserDynamicTesting() 16 | ); 17 | // Then we find all the tests. 18 | const context = require.context('./', true, /\.spec\.ts$/); 19 | // And load the modules. 20 | context.keys().map(context); 21 | -------------------------------------------------------------------------------- /tsconfig.app.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "./out-tsc/app", 5 | "types": [] 6 | }, 7 | "files": [ 8 | "src/main.ts", 9 | "src/polyfills.ts" 10 | ], 11 | "include": [ 12 | "src/**/*.ts" 13 | ], 14 | "exclude": [ 15 | "src/test.ts", 16 | "src/**/*.spec.ts" 17 | ] 18 | } 19 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compileOnSave": false, 3 | "compilerOptions": { 4 | "baseUrl": "./", 5 | "outDir": "./dist/out-tsc", 6 | "sourceMap": true, 7 | "declaration": false, 8 | "downlevelIteration": true, 9 | "experimentalDecorators": true, 10 | "module": "esnext", 11 | "moduleResolution": "node", 12 | "importHelpers": true, 13 | "target": "es2015", 14 | "typeRoots": [ 15 | "node_modules/@types" 16 | ], 17 | "lib": [ 18 | "es2018", 19 | "dom" 20 | ] 21 | }, 22 | "angularCompilerOptions": { 23 | "fullTemplateTypeCheck": true, 24 | "strictInjectionParameters": true 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /tsconfig.spec.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "./out-tsc/spec", 5 | "types": [ 6 | "jasmine", 7 | "node" 8 | ] 9 | }, 10 | "files": [ 11 | "src/test.ts", 12 | "src/polyfills.ts" 13 | ], 14 | "include": [ 15 | "src/**/*.spec.ts", 16 | "src/**/*.d.ts" 17 | ] 18 | } 19 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tslint:recommended", 3 | "rules": { 4 | "array-type": false, 5 | "arrow-parens": false, 6 | "deprecation": { 7 | "severity": "warning" 8 | }, 9 | "component-class-suffix": true, 10 | "contextual-lifecycle": true, 11 | "directive-class-suffix": true, 12 | "directive-selector": [ 13 | true, 14 | "attribute", 15 | "app", 16 | "camelCase" 17 | ], 18 | "component-selector": [ 19 | true, 20 | "element", 21 | "app", 22 | "kebab-case" 23 | ], 24 | "import-blacklist": [ 25 | true, 26 | "rxjs/Rx" 27 | ], 28 | "interface-name": false, 29 | "max-classes-per-file": false, 30 | "max-line-length": [ 31 | true, 32 | 140 33 | ], 34 | "member-access": false, 35 | "member-ordering": [ 36 | true, 37 | { 38 | "order": [ 39 | "static-field", 40 | "instance-field", 41 | "static-method", 42 | "instance-method" 43 | ] 44 | } 45 | ], 46 | "no-consecutive-blank-lines": false, 47 | "no-console": [ 48 | true, 49 | "debug", 50 | "info", 51 | "time", 52 | "timeEnd", 53 | "trace" 54 | ], 55 | "no-empty": false, 56 | "no-inferrable-types": [ 57 | true, 58 | "ignore-params" 59 | ], 60 | "no-non-null-assertion": true, 61 | "no-redundant-jsdoc": true, 62 | "no-switch-case-fall-through": true, 63 | "no-var-requires": false, 64 | "object-literal-key-quotes": [ 65 | true, 66 | "as-needed" 67 | ], 68 | "object-literal-sort-keys": false, 69 | "ordered-imports": false, 70 | "quotemark": [ 71 | true, 72 | "single" 73 | ], 74 | "trailing-comma": false, 75 | "no-conflicting-lifecycle": true, 76 | "no-host-metadata-property": true, 77 | "no-input-rename": true, 78 | "no-inputs-metadata-property": true, 79 | "no-output-native": true, 80 | "no-output-on-prefix": true, 81 | "no-output-rename": true, 82 | "no-outputs-metadata-property": true, 83 | "template-banana-in-box": true, 84 | "template-no-negated-async": true, 85 | "use-lifecycle-interface": true, 86 | "use-pipe-transform-interface": true 87 | }, 88 | "rulesDirectory": [ 89 | "codelyzer" 90 | ] 91 | } --------------------------------------------------------------------------------