|
| 1 | +import { NgIf } from '@angular/common'; |
| 2 | +import { CUSTOM_ELEMENTS_SCHEMA, Component, ElementRef, ViewChild, effect } from '@angular/core'; |
| 3 | +import { |
| 4 | + NgtArgs, |
| 5 | + createAttachFunction, |
| 6 | + extend, |
| 7 | + injectBeforeRender, |
| 8 | + injectNgtRef, |
| 9 | + injectNgtStore, |
| 10 | + type NgtNode, |
| 11 | +} from 'angular-three'; |
| 12 | +import { NgtsOrbitControls } from 'angular-three-soba/controls'; |
| 13 | +import { injectNgtsTextureLoader } from 'angular-three-soba/loaders'; |
| 14 | +import { Mesh, PlaneGeometry } from 'three'; |
| 15 | +import { |
| 16 | + EffectComposer, |
| 17 | + GammaCorrectionShader, |
| 18 | + RGBShiftShader, |
| 19 | + RenderPass, |
| 20 | + ShaderPass, |
| 21 | + UnrealBloomPass, |
| 22 | + type Pass, |
| 23 | +} from 'three-stdlib'; |
| 24 | + |
| 25 | +// Using "extend" to extend the THREE catalogue of Angular Three custom renderer |
| 26 | +extend({ EffectComposer, RenderPass, ShaderPass, UnrealBloomPass }); |
| 27 | + |
| 28 | +declare global { |
| 29 | + interface HTMLElementTagNameMap { |
| 30 | + 'ngt-shader-pass': NgtNode<ShaderPass, typeof ShaderPass>; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Vaporware Scene is ported from Maxime's article: https://blog.maximeheckel.com/posts/vaporwave-3d-scene-with-threejs/ |
| 36 | + */ |
| 37 | +@Component({ |
| 38 | + standalone: true, |
| 39 | + template: ` |
| 40 | + <!-- Top-level elements with "attach" will get attached to the root THREE.Scene --> |
| 41 | + <!-- Here, the Fog will get attached to scene.fog --> |
| 42 | + <ngt-fog attach="fog" *args="['#000', 1, 2.5]" /> |
| 43 | +
|
| 44 | + <ngt-ambient-light [intensity]="Math.PI" /> |
| 45 | +
|
| 46 | + <ngt-spot-light |
| 47 | + color="#d53c3d" |
| 48 | + [position]="[0.5, 0.75, 2.2]" |
| 49 | + [intensity]="20" |
| 50 | + [distance]="25" |
| 51 | + [angle]="Math.PI * 0.1" |
| 52 | + [penumbra]="0.25" |
| 53 | + [decay]="10" |
| 54 | + > |
| 55 | + <ngt-vector3 *args="[-0.25, 0.25, 0.25]" attach="target.position" /> |
| 56 | + </ngt-spot-light> |
| 57 | +
|
| 58 | + <ngt-spot-light |
| 59 | + color="#d53c3d" |
| 60 | + [position]="[-0.5, 0.75, 2.2]" |
| 61 | + [intensity]="20" |
| 62 | + [distance]="25" |
| 63 | + [angle]="Math.PI * 0.1" |
| 64 | + [penumbra]="0.25" |
| 65 | + [decay]="10" |
| 66 | + > |
| 67 | + <ngt-vector3 *args="[0.25, 0.25, 0.25]" attach="target.position" /> |
| 68 | + </ngt-spot-light> |
| 69 | +
|
| 70 | + <!-- Good ol' ngIf-as trick to delay rendering of the whole block. --> |
| 71 | + <!-- We don't want to render anything until the textures are loaded --> |
| 72 | + <ng-container *ngIf="textures() as textures"> |
| 73 | + <!-- We use geometryRef for geometry because *args structural directive wraps ngt-plane-geometry --> |
| 74 | + <!-- A simple Template Variable "#geometry" won't work. --> |
| 75 | + <ngt-plane-geometry *args="[1, 2, 24, 24]" [ref]="geometryRef" /> |
| 76 | +
|
| 77 | + <!-- For material (or any element that doesn't have a structural directive attached to them) --> |
| 78 | + <!-- we can use Template Variable to get a hold of that element instance --> |
| 79 | + <ngt-mesh-standard-material |
| 80 | + #material |
| 81 | + [map]="textures.grid" |
| 82 | + [displacementMap]="textures.displacement" |
| 83 | + [metalnessMap]="textures.metalness" |
| 84 | + [displacementScale]="0.4" |
| 85 | + [metalness]="0.96" |
| 86 | + [roughness]="0.5" |
| 87 | + /> |
| 88 | +
|
| 89 | + <!-- With access to geometry and material, we can reuse them --> |
| 90 | + <ngt-mesh |
| 91 | + #frontPlane |
| 92 | + [rotation]="[-Math.PI / 2, 0, 0]" |
| 93 | + [position]="[0, 0, 0.15]" |
| 94 | + [geometry]="geometryRef.nativeElement" |
| 95 | + [material]="material" |
| 96 | + /> |
| 97 | +
|
| 98 | + <ngt-mesh |
| 99 | + #backPlane |
| 100 | + [rotation]="[-Math.PI / 2, 0, 0]" |
| 101 | + [position]="[0, 0, -1.85]" |
| 102 | + [geometry]="geometryRef.nativeElement" |
| 103 | + [material]="material" |
| 104 | + /> |
| 105 | + </ng-container> |
| 106 | +
|
| 107 | + <ngt-effect-composer *args="[gl()]" [ref]="composerRef"> |
| 108 | + <ngt-render-pass *args="[scene(), camera()]" [attach]="passAttach" /> |
| 109 | + <ngt-shader-pass |
| 110 | + *args="[RGBShiftShader]" |
| 111 | + [attach]="passAttach" |
| 112 | + (afterAttach)="$event.node.uniforms['amount'].value = 0.0015" |
| 113 | + /> |
| 114 | + <ngt-shader-pass *args="[GammaCorrectionShader]" [attach]="passAttach" /> |
| 115 | + <ngt-unreal-bloom-pass *args="[size().width / size().height, 0.2, 0.8, 0]" [attach]="passAttach" /> |
| 116 | + </ngt-effect-composer> |
| 117 | +
|
| 118 | + <ngts-orbit-controls |
| 119 | + [maxPolarAngle]="Math.PI / 2.1" |
| 120 | + [minPolarAngle]="Math.PI / 3" |
| 121 | + [minAzimuthAngle]="-Math.PI / 2" |
| 122 | + [maxAzimuthAngle]="Math.PI / 2" |
| 123 | + /> |
| 124 | + `, |
| 125 | + imports: [NgtArgs, NgtsOrbitControls, NgIf], |
| 126 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 127 | +}) |
| 128 | +export class VaporwareScene { |
| 129 | + Math = Math; |
| 130 | + RGBShiftShader = RGBShiftShader; |
| 131 | + GammaCorrectionShader = GammaCorrectionShader; |
| 132 | + |
| 133 | + // injectNgtStore returns the Canvas Store that keeps all the data to the current Scene Graph under ngt-canvas |
| 134 | + private store = injectNgtStore(); |
| 135 | + gl = this.store.select('gl'); |
| 136 | + scene = this.store.select('scene'); |
| 137 | + camera = this.store.select('camera'); |
| 138 | + size = this.store.select('size'); |
| 139 | + |
| 140 | + // injectNgtRef returns a NgtInjectedRef, which is similar to ElementRef but is enhanced with Signal to track some data changes. |
| 141 | + composerRef = injectNgtRef<EffectComposer>(); |
| 142 | + geometryRef = injectNgtRef<PlaneGeometry>(); |
| 143 | + |
| 144 | + // Angular Three supports using ViewChild to get the instaces of the THREE objects via ElementRef |
| 145 | + @ViewChild('frontPlane') frontPlane?: ElementRef<Mesh>; |
| 146 | + @ViewChild('backPlane') backPlane?: ElementRef<Mesh>; |
| 147 | + |
| 148 | + // most Angular Three's custom inject fns accept Functions as arguments so that consumers can pass in Signals |
| 149 | + // and the internal will react to changes. |
| 150 | + textures = injectNgtsTextureLoader(() => ({ |
| 151 | + grid: 'assets/grid.png', |
| 152 | + displacement: 'assets/displacement.png', |
| 153 | + metalness: 'assets/metalness.png', |
| 154 | + })); |
| 155 | + |
| 156 | + // [attach] can accept an AttachFunction. |
| 157 | + passAttach = createAttachFunction<EffectComposer, Pass>(({ parent, child }) => { |
| 158 | + parent.addPass(child); |
| 159 | + // optionally returns a clean up function that will get called when the child is destroyed |
| 160 | + return () => parent.removePass(child); |
| 161 | + }); |
| 162 | + |
| 163 | + constructor() { |
| 164 | + effect(() => { |
| 165 | + const [size, composer] = [this.size(), this.composerRef.nativeElement]; |
| 166 | + if (!composer) return; |
| 167 | + composer.setSize(size.width, size.height); |
| 168 | + }); |
| 169 | + |
| 170 | + // injectBeforeRender allows consumers to hook into Animation Loop. This runs outside of Zone |
| 171 | + injectBeforeRender(({ clock }) => { |
| 172 | + const [frontPlane, backPlane] = [this.frontPlane?.nativeElement, this.backPlane?.nativeElement]; |
| 173 | + if (frontPlane && backPlane) { |
| 174 | + const elapsedTime = clock.getElapsedTime(); |
| 175 | + frontPlane.position.z = (elapsedTime * 0.15) % 2; |
| 176 | + backPlane.position.z = ((elapsedTime * 0.15) % 2) - 2; |
| 177 | + } |
| 178 | + }); |
| 179 | + |
| 180 | + // injectBeforeRender can also accept a priority option. When priority differs between different |
| 181 | + // beforeRender callbacks, Angular Three opts out off automatic rendering. In this case, |
| 182 | + // the EffectComposer is responsible for rendering our scene with composer.render() |
| 183 | + injectBeforeRender( |
| 184 | + ({ delta }) => { |
| 185 | + const composer = this.composerRef.nativeElement; |
| 186 | + if (!composer) return; |
| 187 | + composer.render(delta); |
| 188 | + }, |
| 189 | + { priority: 1 }, |
| 190 | + ); |
| 191 | + } |
| 192 | +} |
0 commit comments