/
RealityTransitionTool.ts
118 lines (105 loc) · 4.81 KB
/
RealityTransitionTool.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Tools
*/
import { Vector3d } from "@itwin/core-geometry";
import { RenderSchedule } from "@itwin/core-common";
import { IModelApp, Tool } from "@itwin/core-frontend";
enum FadeMode { X, Y, Z, Transparent }
/** This tool applies a transition in X, Y, Z, or transparency.
* @beta
*/
export class RealityTransitionTool extends Tool {
public static override get minArgs() { return 0; }
public static override get maxArgs() { return 1; }
public static override toolId = "RealityTransition";
/** This method runs the tool, applying a transition in X, Y, Z, or transparency.
* @param fadeMode whether to apply the transition in X, Y, Z, or transparency
*/
public override async run(fadeMode: FadeMode = FadeMode.X): Promise<boolean> {
const vp = IModelApp.viewManager.selectedView;
if (undefined === vp)
return true;
const displayStyle = vp.displayStyle;
const view = vp.view;
const timeNow = Date.now(), timeEnd = timeNow + 1000.0 * 60.0 * 60.0;
const range = vp.iModel.projectExtents;
const directions = [Vector3d.create(1, 0, 0), Vector3d.create(0, 1, 0), Vector3d.create(0, 0, 1)];
const modelInTimeline: RenderSchedule.ModelTimelineProps = { modelId: "", elementTimelines: [] };
const modelOutTimeline: RenderSchedule.ModelTimelineProps = { modelId: "", elementTimelines: [] };
switch (fadeMode) {
case FadeMode.Transparent: {
const fadeInTimeline = new Array<RenderSchedule.VisibilityEntryProps>();
fadeInTimeline.push({ time: timeNow, interpolation: 2, value: 100.0 });
fadeInTimeline.push({ time: timeEnd, interpolation: 2, value: 0.0 });
const fadeOutTimeline = new Array<RenderSchedule.VisibilityEntryProps>();
fadeOutTimeline.push({ time: timeNow, interpolation: 2, value: 0.0 });
fadeOutTimeline.push({ time: timeEnd, interpolation: 2, value: 100.0 });
modelInTimeline.visibilityTimeline = fadeInTimeline;
modelOutTimeline.visibilityTimeline = fadeOutTimeline;
break;
}
default: {
const direction = directions[fadeMode - FadeMode.X];
const clipInTimeline = new Array<RenderSchedule.CuttingPlaneEntryProps>();
clipInTimeline.push({ time: timeNow, interpolation: 2, value: { position: [range.low.x, range.low.y, range.low.z], direction: [direction.x, direction.y, direction.z] } });
clipInTimeline.push({ time: timeEnd, interpolation: 2, value: { position: [range.high.x, range.high.y, range.high.z], direction: [direction.x, direction.y, direction.z] } });
const clipOutTimeline = new Array<RenderSchedule.CuttingPlaneEntryProps>();
clipOutTimeline.push({ time: timeNow, interpolation: 2, value: { position: [range.low.x, range.low.y, range.low.z], direction: [-direction.x, -direction.y, -direction.z] } });
clipOutTimeline.push({ time: timeEnd, interpolation: 2, value: { position: [range.high.x, range.high.y, range.high.z], direction: [-direction.x, -direction.y, -direction.z] } });
modelInTimeline.cuttingPlaneTimeline = clipInTimeline;
modelOutTimeline.cuttingPlaneTimeline = clipOutTimeline;
break;
}
}
const scriptProps: RenderSchedule.ScriptProps = [];
view.forEachModel((model) => {
scriptProps.push({
...(model.jsonProperties.tilesetUrl ? modelOutTimeline : modelInTimeline),
modelId: model.id,
});
});
displayStyle.forEachRealityModel((model) => {
const modelId = model.treeRef?.treeOwner.tileTree?.modelId;
if (modelId) {
scriptProps.push({
...modelOutTimeline,
modelId,
realityModelUrl: model.url,
});
}
});
const script = RenderSchedule.Script.fromJSON(scriptProps);
if (script) {
displayStyle.scheduleScript = script;
vp.timePoint = script.duration.low;
}
return true;
}
/** Executes this tool's run method.
* @param args the first entry of this array contains either "x", "y", "z", or "transparent", indicating the type of transition to apply.
* @see [[run]]
*/
public override async parseAndRun(...args: string[]): Promise<boolean> {
const transitionNames = [
"x",
"y",
"z",
"transparent",
];
let fade = FadeMode.X;
if (0 !== args.length) {
const arg = args[0].toLowerCase();
for (let i = 0; i < transitionNames.length; i++) {
if (arg === transitionNames[i]) {
fade = i;
break;
}
}
}
return this.run(fade);
}
}