Skip to content

Instantly share code, notes, and snippets.

@KeanW
Created September 11, 2018 10:09
Show Gist options
  • Save KeanW/11020753c084c55cb0e6e0d6a2b5538c to your computer and use it in GitHub Desktop.
Save KeanW/11020753c084c55cb0e6e0d6a2b5538c to your computer and use it in GitHub Desktop.
Forge viewer extension that creates Streamline objects
/// <reference path='../../types/three.d.ts' />
import { Logger } from '../../core/Logger';
let logger = Logger.getInstance();
import { ShowableExtension } from '../../core/ShowableExtension';
import { DasherModel } from '../../core/DasherModel';
import { Streamline } from './Streamline';
export class StreamlineExtension extends ShowableExtension {
private _overlayScene = "DasherStreamlinesOverlay";
private _bounds: THREE.Box3;
private _lastLineCreated: Date;
constructor(viewer: Autodesk.Viewing.Private.GuiViewer3D, options: any) {
super(viewer, options);
this._lastLineCreated = new Date();
}
public load(): boolean {
this.createUI(
'toolbar-sensor-streamline',
'Streamlines',
'DasherControlSensors',
'flash'
);
this._dataModel.on(
DasherModel.eventTypes.CurrentHistoricTimeChanged,
this.onCurrentHistoricTimeChanged
);
this._bounds = this.viewer.utilities.getBoundingBox();
logger.log('StreamlineExtension loaded');
return true;
}
public unload(): boolean {
this.destroyUI('DasherControlSensors');
this._dataModel.off(
DasherModel.eventTypes.CurrentHistoricTimeChanged,
this.onCurrentHistoricTimeChanged
);
logger.log('StreamlineExtension unloaded');
return true;
}
public show(): boolean {
this.viewer.impl.createOverlayScene(this._overlayScene);
return true;
}
public hide(): boolean {
this.viewer.impl.removeOverlayScene(this._overlayScene);
return true;
}
private addToScene(obj: THREE.Object3D) {
this.viewer.impl.addOverlay(this._overlayScene, obj);
this.viewer.impl.invalidate(false, false, true);
}
private removeFromScene(obj: THREE.Object3D) {
this.viewer.impl.removeOverlay(this._overlayScene, obj);
this.viewer.impl.invalidate(false, false, true);
}
private onCurrentHistoricTimeChanged = (time: Date): void => {
let now: any = new Date();
let elapsed = now - <any>this._lastLineCreated;
if (elapsed > 100) {
this._lastLineCreated = now;
this.createStreamline(10, 3, 0.8, 20, 100, this._bounds);
}
}
private createStreamline(
vertCount: number,
width: number,
opacity: number,
grow: number,
displayTime: number,
bounds: THREE.Box3
): void {
let dims = this.viewer.getDimensions();
let verts: THREE.Vector3[] = [];
// Create random points...
// X: at fixed intervals across bounds
// Y: 40% of bounds plus 20% variability
// Z: 45% of bounds plus 10% variability
for (let i = 0; i < vertCount; i++) {
verts.push(
new THREE.Vector3(
bounds.min.x + (i * (1 / (vertCount - 1))) * (bounds.max.x - bounds.min.x),
bounds.min.y + (0.4 + (0.2 * Math.random())) * (bounds.max.y - bounds.min.y),
bounds.min.z + (0.45 + (0.1 * Math.random())) * (bounds.max.z - bounds.min.z)
)
);
}
this.addToScene(
new Streamline(
verts,
new THREE.Color(Math.random() < 0.1 ? 0xff00ff : 0xffff00),
width,
opacity,
grow,
displayTime,
new THREE.Vector2(dims.width, dims.height),
(s: Streamline) => { this.removeFromScene(s); }
)
);
}
}
@pangzx1
Copy link

pangzx1 commented Aug 27, 2020

Thank you very much for answering me. I am still in school and I am more interested in this. But I just got in touch and will encounter many problems. I hope I can leave you a message here in the future, okay?

@KeanW
Copy link
Author

KeanW commented Aug 27, 2020

I suggest posting to StackOverflow: the Forge team monitors it and has much more experience with supporting development with the viewer. Feel free to post specific questions related to blog posts via keanw.com, though (but via the blog, rather than GitHub). Again, I really don't have time to provide more general support.

@pangzx1
Copy link

pangzx1 commented Aug 27, 2020

Okay, thank you very much for your suggestions, and once again thank you for your answers. I wish you a happy life!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment