-
Notifications
You must be signed in to change notification settings - Fork 1
/
scene-viewer-container.component.ts
222 lines (195 loc) · 7.65 KB
/
scene-viewer-container.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import { AfterViewInit, ChangeDetectionStrategy, Component, ViewChild } from '@angular/core';
// import '@babylonjs/core/Rendering/edgesRenderer';
// import '@babylonjs/core/Rendering/outlineRenderer';
import { select, Store } from '@ngrx/store';
import { rgbaToInt, Tool } from '@talus/model';
import {
UiFullscreenOverlayService,
UiPointerButton,
UiPointerPickInfo,
UiSceneViewerComponent,
UiSceneViewerService,
} from '@talus/ui';
import { areEqual, Coord, createMaxCoord, removeFraction } from '@talus/vdb';
import { combineLatest, Observable } from 'rxjs';
import * as fromApp from '../app.reducer';
import { LoadFileContainerComponent } from './load-file-container/load-file-container.component';
import {
paintVoxel,
removeVoxel,
setLineCoord,
setVoxel,
setVoxels,
voxelUnderCursorChange,
} from './scene-viewer-container.actions';
@Component({
selector: 'fe-scene-viewer-container',
template: `
<ui-scene-viewer
*ngIf="selectedToolIdAndColor$ | async as selected"
(pointerPick)="onPointerPick($event, selected[0], selected[1])"
(pointUnderPointer)="onPointUnderPointer($event, selected[0], selected[1])"
(dropFiles)="onDropFiles($event)"
></ui-scene-viewer>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class SceneViewerContainerComponent implements AfterViewInit {
@ViewChild(UiSceneViewerComponent)
private sceneViewerComponent: UiSceneViewerComponent;
private selectedToolId$: Observable<Tool> = this.store.pipe(select(fromApp.selectSelectedToolId));
private selectedColor$: Observable<number> = this.store.pipe(
select(fromApp.selectSelectedIntColor),
);
selectedToolIdAndColor$ = combineLatest([this.selectedToolId$, this.selectedColor$]);
private lastUnderPointerPosition: Coord = createMaxCoord();
constructor(
private readonly fullscreenOverlayService: UiFullscreenOverlayService,
private readonly sceneViewerService: UiSceneViewerService,
private readonly store: Store<fromApp.State>,
) {}
ngAfterViewInit(): void {
// Due to the status-bar being on the bottom of the screen a resizing is needed.
this.sceneViewerService.resizeView();
this.store.dispatch(
setVoxel({ xyz: [0, 0, 0], newValue: rgbaToInt({ r: 0, g: 255, b: 0, a: 255 }) }),
);
}
onPointerPick(pickInfo: UiPointerPickInfo, selectedToolId: Tool, selectedColor: number): void {
this.dispatchPickAction(pickInfo, selectedToolId, selectedColor);
}
onPointUnderPointer(
pickInfo: UiPointerPickInfo,
selectedToolId: Tool,
selectedColor: number,
): void {
if (selectedToolId === Tool.SelectLinePoint) {
this.dispatchVoxelUnderCursorChange(pickInfo, selectedColor);
}
}
onDropFiles(files: File[]): void {
if (files.length < 1) {
return;
}
// Can't dispatch action with `File` since it would be an unserializable action.
// Therefore, opening the dialog here and not in an effect.
this.fullscreenOverlayService.open(LoadFileContainerComponent, files[0]);
}
private dispatchVoxelUnderCursorChange(pickInfo: UiPointerPickInfo, selectedColor: number): void {
const toAddPosition = this.calcVoxelToAddPosition(pickInfo);
const underPointerPosition = this.calcVoxelUnderPointerPosition(pickInfo);
if (areEqual(this.lastUnderPointerPosition, underPointerPosition)) {
return;
}
this.lastUnderPointerPosition = underPointerPosition;
this.store.dispatch(
voxelUnderCursorChange({
toAddPosition,
underPointerPosition,
color: selectedColor,
needsSync: true,
}),
);
}
private initializeChessboard(position: Coord): void {
const white = rgbaToInt({ r: 255, g: 255, b: 255, a: 255 });
const grey = rgbaToInt({ r: 200, g: 200, b: 200, a: 255 });
const coords: Coord[] = [];
const newValues: number[] = [];
const diameter = 8;
for (let x = -diameter; x < diameter; x++) {
for (let z = -diameter; z < diameter; z++) {
coords.push([position[0] + x, position[1], position[2] + z]);
newValues.push((x + z) % 2 === 0 ? white : grey);
}
}
this.store.dispatch(setVoxels({ coords, newValues, needsSync: true }));
}
private dispatchPickAction(
pickInfo: UiPointerPickInfo,
selectedToolId: Tool,
newValue: number,
): void {
if (pickInfo.pointerButton !== UiPointerButton.Main) {
return;
}
switch (selectedToolId) {
case Tool.SelectLinePoint:
this.store.dispatch(
setLineCoord({ xyz: this.calcVoxelToAddPosition(pickInfo), newValue, needsSync: true }),
);
break;
case Tool.SetVoxel:
this.store.dispatch(
setVoxel({ xyz: this.calcVoxelToAddPosition(pickInfo), newValue, needsSync: true }),
);
break;
case Tool.RemoveVoxel:
this.store.dispatch(
removeVoxel({ xyz: this.calcVoxelUnderPointerPosition(pickInfo), needsSync: true }),
);
break;
case Tool.PaintVoxel:
this.store.dispatch(
paintVoxel({
xyz: this.calcVoxelUnderPointerPosition(pickInfo),
newValue,
needsSync: true,
}),
);
break;
}
}
private calcVoxelToAddPosition(pickInfo: UiPointerPickInfo): Coord {
const pickedIntegerPoint = this.roundDimensionAlongNormal(pickInfo);
// VDB removes fractional-part of the coordinate, i.e. 0.54 -> 0.
// Therefore, positive numbers are getting rounded down (1.9 -> 1) and
// negative numbers are getting rounded up (-0.2 -> 0).
// Hence, the subtraction of 1 is needed.
const newPoint: Coord = [
pickInfo.normal[0] < 0 || (pickInfo.normal[0] === 0 && pickedIntegerPoint[0] < 0)
? pickedIntegerPoint[0] - 1
: pickedIntegerPoint[0],
pickInfo.normal[1] < 0 || (pickInfo.normal[1] === 0 && pickedIntegerPoint[1] < 0)
? pickedIntegerPoint[1] - 1
: pickedIntegerPoint[1],
pickInfo.normal[2] < 0 || (pickInfo.normal[2] === 0 && pickedIntegerPoint[2] < 0)
? pickedIntegerPoint[2] - 1
: pickedIntegerPoint[2],
];
removeFraction(newPoint);
return newPoint;
}
private calcVoxelUnderPointerPosition(pickInfo: UiPointerPickInfo): Coord {
const pickedIntegerPoint = this.roundDimensionAlongNormal(pickInfo);
const newPoint: Coord = [
pickInfo.normal[0] > 0 || (pickInfo.normal[0] === 0 && pickedIntegerPoint[0] < 0)
? pickedIntegerPoint[0] - 1
: pickedIntegerPoint[0],
pickInfo.normal[1] > 0 || (pickInfo.normal[1] === 0 && pickedIntegerPoint[1] < 0)
? pickedIntegerPoint[1] - 1
: pickedIntegerPoint[1],
pickInfo.normal[2] > 0 || (pickInfo.normal[2] === 0 && pickedIntegerPoint[2] < 0)
? pickedIntegerPoint[2] - 1
: pickedIntegerPoint[2],
];
removeFraction(newPoint);
return newPoint;
}
/**
* Babylon.js returns sometimes a floating instead of integer number for the dimension of
* the normal vector of the picked point.
* E.g. Click on [1, 0.5, 0.6] on voxel at [0, 0, 0] on its site which has the x-axis as its
* normal vector [1, 0, 0], could result in a picking point of something like [0.99999, 0.5, 0.6].
* Where in fact it should be [1, 0.5, 0.6].
* Since all the voxels are placed on integer positions the dimension of the picked point
* needs to be rounded.
*/
private roundDimensionAlongNormal(pickInfo: UiPointerPickInfo): Coord {
return [
pickInfo.normal[0] !== 0 ? Math.round(pickInfo.pickedPoint[0]) : pickInfo.pickedPoint[0],
pickInfo.normal[1] !== 0 ? Math.round(pickInfo.pickedPoint[1]) : pickInfo.pickedPoint[1],
pickInfo.normal[2] !== 0 ? Math.round(pickInfo.pickedPoint[2]) : pickInfo.pickedPoint[2],
];
}
}