|
| 1 | +/* credit: https://codesandbox.io/p/sandbox/react-postprocessing-dof-blob-forked-7hj8w3?file=/src/App.js:29,15 */ |
| 2 | + |
| 3 | +import { |
| 4 | + ChangeDetectionStrategy, |
| 5 | + Component, |
| 6 | + CUSTOM_ELEMENTS_SCHEMA, |
| 7 | + type ElementRef, |
| 8 | + input, |
| 9 | + viewChild, |
| 10 | + viewChildren, |
| 11 | +} from '@angular/core'; |
| 12 | +import { extend, injectBeforeRender, injectLoader, NgtArgs, NgtCanvas } from 'angular-three'; |
| 13 | +import { NgtpBloom, NgtpDepthOfField, NgtpEffectComposer, NgtpVignette } from 'angular-three-postprocessing'; |
| 14 | +import { injectTexture, NgtsLoader } from 'angular-three-soba/loaders'; |
| 15 | +import { NgtsMeshDistortMaterial } from 'angular-three-soba/materials'; |
| 16 | +import * as THREE from 'three'; |
| 17 | +import { CubeTextureLoader, Material, MathUtils, type Mesh } from 'three'; |
| 18 | + |
| 19 | +extend(THREE); |
| 20 | + |
| 21 | +@Component({ |
| 22 | + selector: 'app-main-sphere', |
| 23 | + standalone: true, |
| 24 | + template: ` |
| 25 | + <ngt-mesh #mesh [material]="material()"> |
| 26 | + <ngt-icosahedron-geometry *args="[1, 4]" /> |
| 27 | + </ngt-mesh> |
| 28 | + `, |
| 29 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 30 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 31 | + imports: [NgtArgs], |
| 32 | +}) |
| 33 | +export class MainSphere { |
| 34 | + material = input.required<Material>(); |
| 35 | + |
| 36 | + meshRef = viewChild.required<ElementRef<Mesh>>('mesh'); |
| 37 | + |
| 38 | + constructor() { |
| 39 | + injectBeforeRender(({ clock, pointer }) => { |
| 40 | + const mesh = this.meshRef().nativeElement; |
| 41 | + mesh.rotation.z = clock.getElapsedTime(); |
| 42 | + mesh.rotation.y = MathUtils.lerp(mesh.rotation.y, pointer.x * Math.PI, 0.1); |
| 43 | + mesh.rotation.x = MathUtils.lerp(mesh.rotation.x, pointer.y * Math.PI, 0.1); |
| 44 | + }); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +@Component({ |
| 49 | + selector: 'app-sphere-instances', |
| 50 | + standalone: true, |
| 51 | + template: ` |
| 52 | + <app-main-sphere [material]="material()" /> |
| 53 | + @for (position of initialPositions; track $index) { |
| 54 | + <ngt-mesh #mesh [material]="material()" [position]="position"> |
| 55 | + <ngt-icosahedron-geometry *args="[1, 4]" /> |
| 56 | + </ngt-mesh> |
| 57 | + } |
| 58 | + `, |
| 59 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 60 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 61 | + imports: [MainSphere, NgtArgs], |
| 62 | +}) |
| 63 | +export class SphereInstances { |
| 64 | + material = input.required<Material>(); |
| 65 | + |
| 66 | + initialPositions = [ |
| 67 | + [-4, 20, -12], |
| 68 | + [-10, 12, -4], |
| 69 | + [-11, -12, -23], |
| 70 | + [-16, -6, -10], |
| 71 | + [12, -2, -3], |
| 72 | + [13, 4, -12], |
| 73 | + [14, -2, -23], |
| 74 | + [8, 10, -20], |
| 75 | + ]; |
| 76 | + |
| 77 | + meshesRef = viewChildren<ElementRef<Mesh>>('mesh'); |
| 78 | + |
| 79 | + constructor() { |
| 80 | + injectBeforeRender(() => { |
| 81 | + const meshes = this.meshesRef(); |
| 82 | + meshes.forEach((mesh) => { |
| 83 | + mesh.nativeElement.position.y += 0.02; |
| 84 | + if (mesh.nativeElement.position.y > 19) mesh.nativeElement.position.y = -18; |
| 85 | + mesh.nativeElement.rotation.x += 0.06; |
| 86 | + mesh.nativeElement.rotation.y += 0.06; |
| 87 | + mesh.nativeElement.rotation.z += 0.02; |
| 88 | + }); |
| 89 | + }); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +@Component({ |
| 94 | + standalone: true, |
| 95 | + template: ` |
| 96 | + <ngt-color attach="background" *args="['#050505']" /> |
| 97 | + <ngt-fog attach="fog" *args="['#161616', 8, 30]" /> |
| 98 | +
|
| 99 | + <!-- we render the material with attach="none" so we can share it between instances --> |
| 100 | + <ngts-mesh-distort-material |
| 101 | + #distortMaterial |
| 102 | + attach="none" |
| 103 | + [options]="{ |
| 104 | + envMap: envMap()?.[0], |
| 105 | + bumpMap: bumpMap(), |
| 106 | + emissive: '#010101', |
| 107 | + emissiveIntensity: 2, |
| 108 | + roughness: 0.1, |
| 109 | + metalness: 1, |
| 110 | + bumpScale: 0.005, |
| 111 | + clearcoat: 1, |
| 112 | + clearcoatRoughness: 1, |
| 113 | + radius: 1, |
| 114 | + distort: 0.4, |
| 115 | + toneMapped: false, |
| 116 | + }" |
| 117 | + /> |
| 118 | +
|
| 119 | + @if (distortMaterial.material) { |
| 120 | + <app-sphere-instances [material]="distortMaterial.material" /> |
| 121 | + } |
| 122 | +
|
| 123 | + <ngtp-effect-composer [options]="{ multisampling: 0, disableNormalPass: true }"> |
| 124 | + <ngtp-depth-of-field [options]="{ focusDistance: 0, focalLength: 0.02, bokehScale: 2, height: 480 }" /> |
| 125 | + <ngtp-bloom [options]="{ kernelSize: 3, luminanceThreshold: 0, luminanceSmoothing: 0.9, intensity: 1.5 }" /> |
| 126 | + <ngtp-vignette [options]="{ eskil: false, offset: 0.1, darkness: 1.1 }" /> |
| 127 | + </ngtp-effect-composer> |
| 128 | + `, |
| 129 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 130 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 131 | + imports: [ |
| 132 | + NgtsMeshDistortMaterial, |
| 133 | + SphereInstances, |
| 134 | + NgtArgs, |
| 135 | + NgtpEffectComposer, |
| 136 | + NgtpDepthOfField, |
| 137 | + NgtpBloom, |
| 138 | + NgtpVignette, |
| 139 | + ], |
| 140 | +}) |
| 141 | +export class SceneGraph { |
| 142 | + envMap = injectLoader( |
| 143 | + // @ts-expect-error - CubeTextureLoader is ok |
| 144 | + () => CubeTextureLoader, |
| 145 | + () => [['px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png']], |
| 146 | + { extensions: (loader) => loader.setPath('/cube/') }, |
| 147 | + ); |
| 148 | + bumpMap = injectTexture(() => '/bump.jpg'); |
| 149 | +} |
| 150 | + |
| 151 | +@Component({ |
| 152 | + standalone: true, |
| 153 | + template: ` |
| 154 | + <ngt-canvas [sceneGraph]="sceneGraph" [camera]="{ position: [0, 0, 3] }" /> |
| 155 | + <ngts-loader /> |
| 156 | + `, |
| 157 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 158 | + host: { class: 'postprocessing-sample' }, |
| 159 | + imports: [NgtCanvas, NgtsLoader], |
| 160 | +}) |
| 161 | +export default class PostprocessingSample { |
| 162 | + sceneGraph = SceneGraph; |
| 163 | +} |
0 commit comments