Viewer API Improvements (#2072)

* Fix some monstrous bugs with index buffer shuffling now that we've changed our approach a bit.~

* Finished with the new material management approach for mesh batches. SelectionExtension now uses this approach and also considers existing material opacities when setting select and hover materials

* Updated LineBatch to work with the new material management approach

* Implemented the required draw range related changes to the point batch

* Text batche now work with the new material management

* SpeckleLineMaterial and SpecklePointsMaterial are now SpeckleMaterial as well. Had to rename two properties of SpeckleMaterial due to some typescript named property clash thing, but nothing really changed

* Removed eslint-disable clauses in materials where they were no longer needed. Removed unused imports and overrides

* Added the RTE define for some materials by default. It can still be overriden if users want to

* Stencil outlines is now an toggle-able option for any SpeckleMaterial. Restricted to meshes

* Implemented setting desired material for all geometry types via RenderMaterial and DisplayStyle data. SpeckleRenderer now has three overloaded setMaterial function. One for a material instance, one for a filer material and one for RenderData&DisplayStyle. Moved material hash related functionality from NodeRendeView into Materials

* Added MaterialOptions which can be used when setting materials based on RenderMaterial/DisplayStyle to toggle various material features like stencilOutlines, pointSize. SelectionExtension now uses data material to set materials, and things are so much more simpler and nicer

* Added public method for setting seletion extension options

* After some profiling, realized three.js was doing a lot of pointless work each frame so now we're caching materials created from RenderMaterial/DisplayStyle to avoid this. Perf is nice and sharp now

* addRenderTreeAsync is now a generator. Handled automatic zooming on viewer loading. Disabled section tool by default

* Centralized RTE and shadow RTE buffer in an extended webglrenderer. This avoids re-computing rte data for each material over and over. Also, rte data is now centralized and available to materials

* SpeckleMeshes now use a cached material clone as their batch material like any other material they use

* Cleaned up Materils. Updated the debug show batches function use the new material manipulation system. So much easier now

* Real time measurement exist now as a separate extension. Existing functionality preserved, besides one or two small additions. Renamed the MEASUREMENTS object layer to OVERLAY for a more generic usage. SelectionExtension can now be enabled/disabled. Added an additional overload to the setCameraView method in ICameraController which takes a box3 as target to focus the camera on

* Removed viewer related events from input and replaced them everywhere with the proper input event types where required

* Fixed two issues with the shadowcatcher. One was a regression introduced after we centralized the RTE data. The other was a super old one one and was essentially causing the shadowcatcher to generate the contact shadows incorrectly because the correct transform textures were not bound (this is three.js being a pain)

* WIP on the filtering extension

* FilteringExtension is done. Kept the same implementation

* Added filter reset function

* Removed uniform texture and batch count binding from each material's OnBeforeRender function. Additionallit our material override function from SpeckleMesh now uses our fast copy instead of three's copy function for materials. This decreased CPU overhead each frame by 20+% and also eliminated the ugly call to SpeckleMesh's function for updating the material with the transform texture and batch object count from each material's render callback which was not supposed to be there

* Update RenderingStats to measure CPU render time per frame.

* Added early and late update functions for extensions. First gets called before the core's update, and the latter afterwards

* All speckle materials now use the centralized RTE data. Updated the fasts copy method to copy userdata defined uniforms where needed

* Added explosion extension. Additional cleanup

* Dirty transforms are marked on a per batch object basis, whenever their transforms gets changed. This automates the transform texture update execution, so we no longer need to manually mark entire batches as dirty

* Added getObjects which returns all batch objects in the scene and getObjects which takes an rv an returns only the batch object associated with that rv. Both of these methods are availale on the renderer

* Added setters for position, rotiation and scale in batch object

* Diff extension is complete. We stuck with creating material instances and using those for coloring since it ensures maximum draw call efficiency. Fixed an issue in MeshBatch where transparent draw groups were not always sorted at the end of the group list leading to incorect opauque object selection during the depth pass

* RV batch materials are obtained via the viewer-core API based on the RV itslef. This removed the need to get all batch materials for the differ

* Small cleanup

* Removed all circular dependencies besides one, like I predicted. The final one will dissapaear on it's own in the near future when we'll gracefull make DataTree obsolete. As a note, the circular dependencies were very shallow, reffering to enums/interfaces/statics declared in specific files. There was no real circular dependency on a class level

* Removed last circular dependency just for the sake of completion

* SpeckleRendere now has a clipping volume which is used internally to reject picks outside of it, and it's also exposed to the outside world to be used however

* Implemented the SpeckleLoader along with it's abstract supertype. Data loading is now done through this loader which handles tree population with raw data as well as render view data.

* Working minimal obj loader

* Added total node counting and displaying

* Viewer's load object now takes a loader of any speckle loader base class and uses that to load, instead of taking urls and tokens. This allows for any kind of loader to work with the vieweer's load function. Moved indexing of obj geometry to the obj geometry convertor. Loaders now take the target world tree instance instead of a viewer instance

* Loaders can now load from string and array buffer data where implemented. ObjLoader can now load from a string payload. Sandbox can now load obj files from the UI using a file picker

* alex/API2.0-core

* Started on #1673. Fixed an issue with walkAsync where the recursive generator would waste too much time idle.

* Solution for #1673. Replaced the old async pausing approach with a better version that has true variable wait time, and does not add additional dead wait time. Render tree building is now several times faster

* Cut down some more on load time by using a lookup table for determined speckle types. For a very large number of objects, getting the actual speckle type was quite slow. For our reference stream with 1kk objects we cut down around 2 seconds of load time

* BoxFromObjects now returns the correctly transformed aabb

* Implemented optimisations for batch building step from the loading process. Reduced the step's time by around 50%

* Implemented a NodeMap which allows us to search for nodes very very fast.

* Added a dynamic pause in the loader which stops the converter from blocking. Paramater object types are not added as nodes anymore. Callback from converter is not without arguments

* Replaced some internal walks with the newer and much faster id finding approach

* Disable object shallow cloning acrss the speckle converter

* Fixed the issues with block instances and revit instances caused by not duplicating speckle objects in the converter

* getObjectPropertis is now async and slightly improved the execution time of the flatten function which it uses

* Set caching to true by default

* Fix for display style hashing

* Implemented legacy viewer as a wrapper around the old viewer API

* Started testing FE1 with API 2.0. Fixed some legacy issues. Also the camera controller extension now exposes it's underlying controls object for the sake of not messing around with unwatend changes in FE1

* Updated FE1 with API2.0 selection changes

* Fixing selection bugs

* The viewer now ignores duplicate id nodes

* Fixes to object properties population, camera zooming and adding subtrees in core

* Fixed some filtering issues. Added UpdateFlags to the viewer's requestRender function

* Fixed an issue where section boxes were incorrectly added to the URL

* Objects with no id are not given nodes into the tree

* Updates to FE2 for API2.0. Also fixed a really really obscure bug in viewer-core where a material an incorrect material was set when reseting the batch to the default material

* We now store separate node maps for each model loaded. Each node now holds it's subtree id (as a small number for memory considerations)

* Render request after updating the visual diff

* Fixed some missing update calls on shadows

* Reverted FE1 changes and pinned the viewer library to a specific (latest) version pre-API2.0

* We're adding a viewer node for loaded models as subtree parent. This is because we're no longer spoofing ids and the model parent id needs to be preserved

* Fixed an issue where clicking on a comment bubble made the pipeline use accumulation improperly, leading to dark blight

* Null check for setMaterial. Fixed another case of dark blight. Hack required by frontend

* Fixed the issue with filtering state not propagating in the FE

* Updated selection event changes

* Fixed an issue where an undefined subtree id would yield an incorrect render tree upon requesting it

* Fixed an issue where undefined nodes were returned as valid when searching of ids

* NodeRenderView now holds it's subtree id along the speckle id. This allows for precise node matching when looking for specific nodes

* Fixed an issue with BlockInstances not instancing underlying meshes from breps

* Some fixes to diffing. Some older than API2.0, some new. Render views now have guid composed of their id and subtreeid.

* Update node render view id with guid where needed

* Unload function now checks for requested resource to unload before trying to unload it

* Check for the existence of a batch before applying draw ranges. Inthe FE, extension can temporarily keep dead rvs in their state when switching between streams leading to errors

* Fixed an issue for filtering where subtree roots would cause incorrect rv additions to the ghosted rv lists

* Unified block instance and revit instance conversion implementation

* Separted instanced from non-instanced rvs. Working on InstancedMeshBatch

* WIP on instanced types

* Implemented visual for box draging

* BoxSelection extension beautified and documented along with some small but welcomed changes to the viewer-core

* Viewer's getExtension now looks in the prototype chain before returning an undefined extension

* Added the extended Selection extension here for possible later use

* First iteration on instancing. General idea works. Still WIP on several fronts

* Fixed issues related to incorrect transform being calculated for instanced geometry. Fixed an issue with incorrect bounds being calculated

* Fixed a few issues with instanced vs non instanced render view gathering

* Disabled box selection extension

* Fixed some linter errors

* Disabled a lint 'error'

* Fixed issues with depth rendering and instanced objects and fixed draw range visibility for instanced batches in a minimalistic way

* Updated the measurements extension with the visiblity option

* Restructed a bit our implementation for the acceleration structure because now the BLAS needs to aggregate an instance of three-mesh-bvh not extend it. So that instanced batch objects can share a single bvh instance -> no redundancy

* WIP

* Revert "WIP"

This reverts commit 20d4bbf6210b0d37b956cc5b41fdb06f29845b4f.

* More WIP on trying to make instanced geometry TAS and BAS work properly

* Both Tas and Bas intersection testing seem to working fine. Now we need to implemented draw grouping for instanced mesh batches

* Added draw group management to InstancedMeshBatch. It works in the same manner as for non instanced batches, but the offset value refers to instances not triangles. I believe it could be simplified, but I'd like to get it up and working first

* Draw groups need to hold the index and count from the instanced buffer attribute

* Added array shuffling to the instanced mesh batch using the same approach we used for the non instanced one.

* Instanced batches now dynamically add InstancedMesh objects based on draw groups and a computed transform buffer

* Applying draw range updates for instanced batches now works. Still some issues to handle. It aint much, but it's honest work

* Disabled some more RTE until setting visibility for draw ranges is finished

* Moved getting opaque, transparent and stencil draw ranges out from Batcher and on to a per-batch basis. Now instanced batches correctly apply draw ranges and get their opaque, transparent and stecil ranges

* Fixed an issue with setting the visibility ranges for instanced mesh batches

* Instanced attributes for instanced meshes now no longer allocate

* Shadow depth material for instances is now set in the speckle renderer

* R-enabled RTE globally. Made instancing work with RTE. Made instancing work with both RTE and shadowmapping.

* Fixed an issue with materials building up in the mesh batch's cache incorrectly

* Implemented gradient indexs attributes for instanced batches. Thismeans, any color ramp based material like the ones we use for filtering now works

* Changed the way compound ids are created for instances, so that less memory is required. i.e chrome is not crashing anymore on particular streams

* Implemented reseting the draw ranges for instanced mesh batches. We no longer double buffer the gradient index buffer. We just create a new one when shuffling, populate it, then copy it over at the end. We're still double buffering the transforms buffer since that one is larger and we might not want to allocate it each time we shuffle

* Removed references of draw groups with ids since we're not doing that anymore. Fixed an issue with mesh batches where the material cache would keep piling materials incorrectly

* Several issues with selective transformations on instanced batches fixed.

* Added default null materials for instanced meshes both with and without vertex colors

* Got rid of the patched InstancedMesh because it was ridiculously slow. Instead we're now computing the scene box using our acceleration structures where available and three.s boundingbox where not available

* Minor, yet big regression fix

* Fixed regression

* Exported some extra types

* WIP on instanced balancing

* Instanced objects under a certain threshold now get batched together as regular mesh batches

* Forgot to update the rvs aabb

* Unified instanced and non instanced batch creation. Instances which do not qualify for instanced batching anre now mixed together with the rest of the non instanced batched objects

* Added some timing information to instanced batches

* Fixed an issue with zooming in on objects not working in the selection extension. Fixed an issue with object picking failing due to fp precision for objects right at the edge of the clipping volume.

* Removed logs

* Update stream moving via UI

* Removed the priority argument from loadObject since it's not needed anymore

* Updated LegacyViewer

* Fixed an issue with API 2.0 where the legacy form of transforms with only an array of values as the matrix would not work

* Updated FE1 viewer package to latest before API 2.0

* Disabled selection when measurement mode is on

* Updated ibl params updating

* Fixed an issue with measurement text not showing up

* Logs

* lockfile

* Made DataTree obsolete. Removed unused 'input' property from viewer

* Fixed circular dependencies

* Removed DebugViewer, other small changes

* Small changes

* Small fixes

* Added id to NodeData

* Removed unused bounds property in rendertree

* Notes to future self + ExtendedExtension is now exported

* Removed an unused function from renderer. Removed the parent argumetn from getRenderViewerForNode... since it was legacy and has no meaning anymore.

* Removed the instance type check in getRenderViewesForNode method. Should not be needed anymore and it was bad to begin with anyway

* Removed some test code

* Small changes

* Removed pointless bounds calculation. Added note

* Added return types

* Ingestigating large group operations

* Nested nodes from TreeNode are now optional. Small update to node render view.

* #1818 Remove the concept of speckle data existing behind a 'data' field

* Removed unneeded property

* made arguments options in transformTRS

* getBoundingBox from acceleration structures have an optional argument now

* Several Batch methods no longer take variable numbe of arguments, but arrays now

* Added note

* Note

* Removed unused things

* Note and made the raycaster protected in intersections

* Added some typings

* Replaceds some functions with accessors. Added typings. Renamed stuff

* Removed some unused properties.

* Typings for measurements

* Small typing changes

* Fixed some more compile errors

* Sources from 'batching' folder are now strict compile compliant

* Sources from 'materials' folder are now strict compile compliant

* Sources from 'extensions' folder are now strict compile compliant

* Sources from 'tree' folder are now strict compile compliant

* Viewer interface, implementation, legacy implementation and the exports now comply with the strict compilation flag. Also added the new tsconfig

* Sources from 'objects' folder now comply to the strict compilation rules

* Sources from 'pipeline' now satisfy the requirements for strict compilation

* Sources from 'loaders' folder are now compiling with strict. That was so much fun

* Sources from 'query' folder are now compiling with strict

* Another round of correction triggered by previous changes

* Update the declaration file in the object loader to contain a member that needs to be public

* SpeckleRenderer along with the rest of the surces from the root 'modules' folder are not compiled with strict

* Completely deprecated DataTree. Updated the dependencies with @types/underscore. Fixed remaining compilation issues

* Fixed a failing build on the CI regarding a timeout id

* Fixed compile errors from sandbox

* Another fix

* All EventEmiter child classes now have mapped event handler argument types, so that when attaching to a specific event, the provided handler has the correct types for it's arguments. Implicitly, got rid of the the unknown types in all event handlers.

* Disabled verbatimModuleSyntax because it was messing things up. Fixed an issue with an improper import

* Fixed frontend-2 linting errors. Also added all the event payload maps to the viewer export

* Some good additions but mostly typescript catering

* Some more typescript catering, but also something useful. The intersect function is now overloaded so that when you specify only the ObjectLayers.STREAM_CONTENT_MESH object layer in it's layers argument it will always return a ExtendedMeshIntersection which guarantees to have a batchObject, face and it's object is of type SpeckleMesh | SpeckleInstancedMesh. Generally you mostly raycast against meshes and getting a three.js intersection object which has all it's fileds optional led to some very useless defined checks. With this we can avoid all of them

* Continued from yesterday, finished with the changes in intersections. Added MeshIntersection which is returned by all bvh intersections. This eliminates the need to check for face, faceIndex or index on intersection results from intersection meshes. Groups from MeshBatch and InstancedMeshBatch are now always DrawRanges(they always were)

* Mostly catering to typescript

* Removed underscore and all unused dependancies. Fixed remaining lint and build errors

* Minor changes

* Added the no-non-null assertion rule to the sandbox

* getExtension never returns null, but rather throws an exception if requested extension does not exist. Added hasExtension for (theoretical) situations where you want to check for extension existence but don't want to go by try/catch with getExtension. Fixed remaining lint/build errors

* getBatches now returns explicit batch types based on the geometry type that you provide

* Adding the lockfile

* undoing unnecessary FE2 changes

* Merged viewer/fe SpeckleObject types

* Fixed two small issues

* Minor linting issues

---------

Co-authored-by: Kristaps Fabians Geikins <fabians@speckle.systems>
This commit is contained in:
Alexandru Popovici
2024-05-07 15:18:38 +03:00
committed by GitHub
parent 3c91af0a28
commit ec78d316bb
109 changed files with 3407 additions and 2528 deletions
@@ -3,16 +3,13 @@ import {
BufferGeometry,
Float32BufferAttribute,
FrontSide,
Intersection,
Material,
Matrix4,
Object3D,
Ray,
Side,
Uint16BufferAttribute,
Uint32BufferAttribute,
Vector3,
Event
Vector3
} from 'three'
import {
CENTER,
@@ -21,6 +18,7 @@ import {
ShapecastIntersection,
SplitStrategy
} from 'three-mesh-bvh'
import { MeshIntersection } from './SpeckleRaycaster'
const SKIP_GENERATION = Symbol('skip tree generation')
@@ -31,7 +29,7 @@ export interface BVHOptions {
verbose: boolean
useSharedArrayBuffer: boolean
setBoundingBox: boolean
onProgress: () => void
onProgress?: () => void
[SKIP_GENERATION]: boolean
}
@@ -42,7 +40,6 @@ export const DefaultBVHOptions = {
verbose: true,
useSharedArrayBuffer: false,
setBoundingBox: false,
onProgress: null,
[SKIP_GENERATION]: false
}
@@ -71,10 +68,10 @@ to get the correct values for Vectors, Rays, Boxes, etc
export class AccelerationStructure {
private static readonly MatBuff: Matrix4 = new Matrix4()
private _bvh: MeshBVH
public inputTransform: Matrix4
public outputTransform: Matrix4
public inputOriginTransform: Matrix4
public outputOriginTransfom: Matrix4
public inputTransform!: Matrix4
public outputTransform!: Matrix4
public inputOriginTransform!: Matrix4
public outputOriginTransfom!: Matrix4
public get geometry() {
return this._bvh.geometry
@@ -85,12 +82,17 @@ export class AccelerationStructure {
}
public static buildBVH(
indices: number[],
position: Float32Array,
indices: number[] | undefined,
position: number[] | undefined,
options: BVHOptions = DefaultBVHOptions,
transform?: Matrix4
): MeshBVH {
let bvhPositions = position
/** There is no unniverse where you can build a BVH without proper indices or positions */
if (!indices || !position) {
throw new Error('Cannot build BVH with undefined indices or position!')
}
let bvhPositions = new Float32Array(position)
if (transform) {
bvhPositions = new Float32Array(position.length)
const vecBuff = new Vector3()
@@ -129,21 +131,23 @@ export class AccelerationStructure {
public raycast(
ray: Ray,
materialOrSide: Side | Material | Material[] = FrontSide
): Intersection<Object3D<Event>>[] {
): MeshIntersection[] {
const res = this._bvh.raycast(this.transformInput<Ray>(ray), materialOrSide)
res.forEach((value) => {
value.point = this.transformOutput(value.point)
})
return res
/** The intersection results from raycasting a bvh will always overlap with MeshIntersection because the bvh uses indexed geometry */
return res as MeshIntersection[]
}
public raycastFirst(
ray: Ray,
materialOrSide: Side | Material | Material[] = FrontSide
): Intersection<Object3D<Event>> {
): MeshIntersection {
const res = this._bvh.raycastFirst(this.transformInput<Ray>(ray), materialOrSide)
res.point = this.transformOutput(res.point)
return res
/** The intersection results from raycasting a bvh will always overlap with MeshIntersection because the bvh uses indexed geometry */
return res as MeshIntersection
}
public shapecast(
@@ -274,9 +278,10 @@ export class AccelerationStructure {
return output.applyMatrix4(AccelerationStructure.MatBuff) as T
}
public getBoundingBox(target) {
this._bvh.getBoundingBox(target)
return this.transformOutput(target)
public getBoundingBox(target?: Box3): Box3 {
const _target: Box3 = target ? target : new Box3()
this._bvh.getBoundingBox(_target)
return this.transformOutput(_target)
}
public getVertexAtIndex(index: number): Vector3 {
@@ -1,7 +1,13 @@
import { Matrix4, NoBlending, PMREMGenerator, ShaderMaterial } from 'three'
import {
Matrix4,
NoBlending,
PMREMGenerator,
ShaderMaterial,
WebGLRenderer
} from 'three'
export class RotatablePMREMGenerator extends PMREMGenerator {
constructor(renderer) {
constructor(renderer: WebGLRenderer) {
super(renderer)
}
@@ -1,10 +1,17 @@
import { Box3, OrthographicCamera, PerspectiveCamera } from 'three'
export enum CameraEvent {
Stationary,
Dynamic,
FrameUpdate,
ProjectionChanged
Stationary = 'stationary',
Dynamic = 'dynamic',
FrameUpdate = 'frame-update',
ProjectionChanged = 'projection-changed'
}
export interface CameraEventPayload {
[CameraEvent.Stationary]: void
[CameraEvent.Dynamic]: void
[CameraEvent.FrameUpdate]: boolean
[CameraEvent.ProjectionChanged]: CameraProjection
}
export interface SpeckleCamera {
@@ -12,8 +19,11 @@ export interface SpeckleCamera {
get fieldOfView(): number
set fieldOfView(value: number)
get aspect(): number
on(type: CameraEvent, handler: (...args) => void)
setCameraPlanes(targetVolume: Box3, offsetScale?: number)
on<T extends CameraEvent>(
eventType: T,
listener: (arg: CameraEventPayload[T]) => void
): void
setCameraPlanes(targetVolume: Box3, offsetScale?: number): void
}
export enum CameraProjection {
PERSPECTIVE,
@@ -1,52 +1,55 @@
import CameraControls from 'camera-controls'
import { MathUtils, PerspectiveCamera, Vector3 } from 'three'
import { MathUtils, OrthographicCamera, PerspectiveCamera, Vector3 } from 'three'
let ACTION
;(function (ACTION) {
ACTION[(ACTION['NONE'] = 0)] = 'NONE'
ACTION[(ACTION['ROTATE'] = 1)] = 'ROTATE'
ACTION[(ACTION['TRUCK'] = 2)] = 'TRUCK'
ACTION[(ACTION['OFFSET'] = 3)] = 'OFFSET'
ACTION[(ACTION['DOLLY'] = 4)] = 'DOLLY'
ACTION[(ACTION['ZOOM'] = 5)] = 'ZOOM'
ACTION[(ACTION['TOUCH_ROTATE'] = 6)] = 'TOUCH_ROTATE'
ACTION[(ACTION['TOUCH_TRUCK'] = 7)] = 'TOUCH_TRUCK'
ACTION[(ACTION['TOUCH_OFFSET'] = 8)] = 'TOUCH_OFFSET'
ACTION[(ACTION['TOUCH_DOLLY'] = 9)] = 'TOUCH_DOLLY'
ACTION[(ACTION['TOUCH_ZOOM'] = 10)] = 'TOUCH_ZOOM'
ACTION[(ACTION['TOUCH_DOLLY_TRUCK'] = 11)] = 'TOUCH_DOLLY_TRUCK'
ACTION[(ACTION['TOUCH_DOLLY_OFFSET'] = 12)] = 'TOUCH_DOLLY_OFFSET'
ACTION[(ACTION['TOUCH_ZOOM_TRUCK'] = 13)] = 'TOUCH_ZOOM_TRUCK'
ACTION[(ACTION['TOUCH_ZOOM_OFFSET'] = 14)] = 'TOUCH_ZOOM_OFFSET'
})(ACTION || (ACTION = {}))
function isPerspectiveCamera(camera) {
return camera.isPerspectiveCamera
enum ACTION {
NONE = 0,
ROTATE = 1,
TRUCK = 2,
OFFSET = 3,
DOLLY = 4,
ZOOM = 5,
TOUCH_ROTATE = 6,
TOUCH_TRUCK = 7,
TOUCH_OFFSET = 8,
TOUCH_DOLLY = 9,
TOUCH_ZOOM = 10,
TOUCH_DOLLY_TRUCK = 11,
TOUCH_DOLLY_OFFSET = 12,
TOUCH_ZOOM_TRUCK = 13,
TOUCH_ZOOM_OFFSET = 14
}
function isOrthographicCamera(camera) {
return camera.isOrthographicCamera
function isPerspectiveCamera(camera: PerspectiveCamera | OrthographicCamera) {
return (camera as PerspectiveCamera).isPerspectiveCamera
}
function isOrthographicCamera(camera: PerspectiveCamera | OrthographicCamera) {
return (camera as OrthographicCamera).isOrthographicCamera
}
const EPSILON = 1e-5
function approxZero(number, error = EPSILON) {
function approxZero(number: number, error = EPSILON) {
return Math.abs(number) < error
}
function approxEquals(a, b, error = EPSILON) {
function approxEquals(a: number, b: number, error = EPSILON) {
return approxZero(a - b, error)
}
let _deltaTarget, _deltaOffset, _v3A, _v3B, _v3C
let _xColumn
let _yColumn
let _zColumn
let _deltaTarget: Vector3,
_deltaOffset: Vector3,
_v3A: Vector3,
_v3B: Vector3,
_v3C: Vector3
let _xColumn: Vector3
let _yColumn: Vector3
let _zColumn: Vector3
export class SpeckleCameraControls extends CameraControls {
private _didDolly = false
private _didDollyLastFrame = false
public _isTrucking = false
private _hasRestedLastFrame = false
private _didZoom = false
private overrideDollyLerpRatio = 0
private overrideZoomLerpRatio = 0
private overrideDollyLerpRatio: number | undefined = 0
private overrideZoomLerpRatio: number | undefined = 0
static install() {
_v3A = new Vector3()
@@ -127,11 +130,7 @@ export class SpeckleCameraControls extends CameraControls {
* @param enableTransition
* @category Methods
*/
zoomTo(
zoom: number,
enableTransition = false,
lerpRatio: number = undefined
): Promise<void> {
zoomTo(zoom: number, enableTransition = false, lerpRatio?: number): Promise<void> {
this._zoomEnd = MathUtils.clamp(zoom, this.minZoom, this.maxZoom)
this._needsUpdate = true
this.overrideZoomLerpRatio = enableTransition ? 0.05 : lerpRatio
@@ -153,7 +152,7 @@ export class SpeckleCameraControls extends CameraControls {
dollyTo(
distance: number,
enableTransition = true,
lerpRatio = undefined
lerpRatio?: number
): Promise<void> {
const lastRadius = this._sphericalEnd.radius
const newRadius = MathUtils.clamp(distance, this.minDistance, this.maxDistance)
@@ -193,8 +192,7 @@ export class SpeckleCameraControls extends CameraControls {
return this._createOnRestPromise(resolveImmediately)
}
update(delta) {
this._hasRestedLastFrame = this._hasRested
update(delta: number) {
const dampingFactor =
this._state === ACTION.NONE ? this.dampingFactor : this.draggingDampingFactor
const lerpRatio = Math.min(dampingFactor * delta * 60, 1)
@@ -1,5 +1,6 @@
import {
BackSide,
BufferAttribute,
BufferGeometry,
DoubleSide,
Group,
@@ -7,23 +8,26 @@ import {
InstancedMesh,
Material,
Matrix4,
Object3D,
Ray,
Raycaster,
SkinnedMesh,
Sphere,
Triangle,
Vector2,
Vector3
Vector3,
type Intersection
} from 'three'
import { BatchObject } from '../batching/BatchObject'
import Materials from '../materials/Materials'
import { TopLevelAccelerationStructure } from './TopLevelAccelerationStructure'
import { ObjectLayers } from '../../IViewer'
import Logger from 'js-logger'
import {
DrawGroup,
type DrawGroup,
INSTANCE_GRADIENT_BUFFER_STRIDE,
INSTANCE_TRANSFORM_BUFFER_STRIDE
} from '../batching/Batch'
import { ObjectLayers } from '../../IViewer'
import Logger from 'js-logger'
const _inverseMatrix = new Matrix4()
const _ray = new Ray()
@@ -54,20 +58,20 @@ const tmpInverseMatrix = /* @__PURE__ */ new Matrix4()
export default class SpeckleInstancedMesh extends Group {
public static MeshBatchNumber = 0
private tas: TopLevelAccelerationStructure = null
private batchMaterial: Material = null
private tas: TopLevelAccelerationStructure
private batchMaterial: Material | null = null
private materialCache: { [id: string]: Material } = {}
private materialStack: Array<Material | Material[]> = []
private materialStack: Array<Array<Material | Material[]>> = []
private materialCacheLUT: { [id: string]: number } = {}
private _batchObjects: BatchObject[]
private _batchObjects!: BatchObject[]
public groups: Array<DrawGroup> = []
public materials: Material[] = []
private instanceGeometry: BufferGeometry = null
private instanceGeometry: BufferGeometry | undefined = undefined
private instances: InstancedMesh[] = []
public get TAS() {
public get TAS(): TopLevelAccelerationStructure {
return this.tas
}
@@ -92,8 +96,7 @@ export default class SpeckleInstancedMesh extends Group {
}
public setOverrideMaterial(material: Material) {
material
const saveMaterials = []
const saveMaterials: Array<Material[] | Material> = []
for (let k = 0; k < this.instances.length; k++) {
saveMaterials.push(this.instances[k].material)
}
@@ -118,7 +121,11 @@ export default class SpeckleInstancedMesh extends Group {
this.materialCacheLUT[clone.id] = material.id
cachedMaterial = clone
this.updateMaterialTransformsUniform(this.materialCache[material.id])
} else if (copy || material['needsCopy'] || cachedMaterial['needsCopy']) {
} else if (
copy ||
(material as never)['needsCopy'] ||
(cachedMaterial as never)['needsCopy']
) {
Materials.fastCopy(material, cachedMaterial)
}
return cachedMaterial
@@ -149,7 +156,8 @@ export default class SpeckleInstancedMesh extends Group {
this.instances.length = 0
for (let k = 0; k < this.groups.length; k++) {
const material = this.materials[this.groups[k].materialIndex]
const materialIndex = this.groups[k].materialIndex
const material = this.materials[materialIndex]
const group = new InstancedMesh(this.instanceGeometry, material, 0)
group.instanceMatrix = new InstancedBufferAttribute(
transformBuffer.subarray(
@@ -231,7 +239,11 @@ export default class SpeckleInstancedMesh extends Group {
// converts the given BVH raycast intersection to align with the three.js raycast
// structure (include object, world space distance and point).
private convertRaycastIntersect(hit, object, raycaster) {
private convertRaycastIntersect(
hit: Intersection | null,
object: Object3D,
raycaster: Raycaster
) {
if (hit === null) {
return null
}
@@ -247,9 +259,9 @@ export default class SpeckleInstancedMesh extends Group {
}
}
raycast(raycaster: Raycaster, intersects) {
raycast(raycaster: Raycaster, intersects: Array<Intersection>) {
if (this.tas) {
if (this.batchMaterial === undefined) return
if (!this.batchMaterial) return
tmpInverseMatrix.copy(this.matrixWorld).invert()
ray.copy(raycaster.ray).applyMatrix4(tmpInverseMatrix)
@@ -278,12 +290,13 @@ export default class SpeckleInstancedMesh extends Group {
const matrixWorld = this.matrixWorld
if (material === undefined) return
if (geometry === undefined) return
// Checking boundingSphere distance to ray
if (geometry.boundingSphere === null) geometry.computeBoundingSphere()
_sphere.copy(geometry.boundingSphere)
_sphere.copy(geometry.boundingSphere || new Sphere())
_sphere.applyMatrix4(matrixWorld)
if (raycaster.ray.intersectsSphere(_sphere) === false) return
@@ -303,13 +316,13 @@ export default class SpeckleInstancedMesh extends Group {
const index = geometry.index
/** Stored high component if RTE is being used. Regular positions otherwise */
const position = geometry.attributes.position
const position = geometry.attributes.position as BufferAttribute
/** Stored low component if RTE is being used. undefined otherwise */
const positionLow = geometry.attributes['position_low']
const morphPosition = geometry.morphAttributes.position
const positionLow = geometry.attributes['position_low'] as BufferAttribute
const morphPosition = geometry.morphAttributes.position as Array<BufferAttribute>
const morphTargetsRelative = geometry.morphTargetsRelative
const uv = geometry.attributes.uv
const uv2 = geometry.attributes.uv2
const uv = geometry.attributes.uv as BufferAttribute
const uv2 = geometry.attributes.uv2 as BufferAttribute
const groups = geometry.groups
const drawRange = geometry.drawRange
@@ -319,6 +332,10 @@ export default class SpeckleInstancedMesh extends Group {
if (Array.isArray(material)) {
for (let i = 0, il = groups.length; i < il; i++) {
const group = groups[i]
if (!group.materialIndex) {
Logger.error(`Group with no material, skipping!`)
continue
}
const groupMaterial = material[group.materialIndex]
const start = Math.max(group.start, drawRange.start)
@@ -350,7 +367,8 @@ export default class SpeckleInstancedMesh extends Group {
if (intersection) {
intersection.faceIndex = Math.floor(j / 3) // triangle number in indexed buffer semantics
intersection.face.materialIndex = group.materialIndex
if (intersection.face)
intersection.face.materialIndex = group.materialIndex as number
intersects.push(intersection)
}
}
@@ -392,6 +410,10 @@ export default class SpeckleInstancedMesh extends Group {
if (Array.isArray(material)) {
for (let i = 0, il = groups.length; i < il; i++) {
const group = groups[i]
if (!group.materialIndex) {
Logger.error(`Group with no material, skipping!`)
continue
}
const groupMaterial = material[group.materialIndex]
const start = Math.max(group.start, drawRange.start)
@@ -423,7 +445,8 @@ export default class SpeckleInstancedMesh extends Group {
if (intersection) {
intersection.faceIndex = Math.floor(j / 3) // triangle number in non-indexed buffer semantics
intersection.face.materialIndex = group.materialIndex
if (intersection.face)
intersection.face.materialIndex = group.materialIndex as number
intersects.push(intersection)
}
}
@@ -464,7 +487,16 @@ export default class SpeckleInstancedMesh extends Group {
}
}
function checkIntersection(object, material, raycaster, ray, pA, pB, pC, point) {
function checkIntersection(
object: Object3D,
material: Material,
raycaster: Raycaster,
ray: Ray,
pA: Vector3,
pB: Vector3,
pC: Vector3,
point: Vector3
): (Intersection & { uv2: Vector2 | undefined }) | null {
let intersect
if (material.side === BackSide) {
@@ -488,7 +520,8 @@ function checkIntersection(object, material, raycaster, ray, pA, pB, pC, point)
object,
uv: undefined,
uv2: undefined,
face: undefined
face: undefined,
faceIndex: undefined
}
}
@@ -496,19 +529,19 @@ function checkIntersection(object, material, raycaster, ray, pA, pB, pC, point)
* hold the default `position` attribute values
*/
function checkBufferGeometryIntersection(
object,
material,
raycaster,
ray,
positionLow,
positionHigh,
morphPosition,
morphTargetsRelative,
uv,
uv2,
a,
b,
c
object: Object3D,
material: Material,
raycaster: Raycaster,
ray: Ray,
positionLow: BufferAttribute,
positionHigh: BufferAttribute,
morphPosition: Array<BufferAttribute>,
morphTargetsRelative: boolean,
uv: BufferAttribute,
uv2: BufferAttribute,
a: number,
b: number,
c: number
) {
_vA.fromBufferAttribute(positionHigh, a)
_vB.fromBufferAttribute(positionHigh, b)
@@ -519,7 +552,7 @@ function checkBufferGeometryIntersection(
_vC.add(_vTemp.fromBufferAttribute(positionLow, c))
}
const morphInfluences = object.morphTargetInfluences
const morphInfluences = (object as SkinnedMesh).morphTargetInfluences
if (morphPosition && morphInfluences) {
_morphA.set(0, 0, 0)
@@ -552,10 +585,10 @@ function checkBufferGeometryIntersection(
_vC.add(_morphC)
}
if (object.isSkinnedMesh) {
object.boneTransform(a, _vA)
object.boneTransform(b, _vB)
object.boneTransform(c, _vC)
if ((object as SkinnedMesh).isSkinnedMesh) {
;(object as SkinnedMesh).boneTransform(a, _vA)
;(object as SkinnedMesh).boneTransform(b, _vB)
;(object as SkinnedMesh).boneTransform(c, _vC)
}
const intersection = checkIntersection(
@@ -1,7 +1,9 @@
import Logger from 'js-logger'
import {
BackSide,
Box3,
Box3Helper,
BufferAttribute,
BufferGeometry,
Color,
DataTexture,
@@ -10,13 +12,16 @@ import {
Material,
Matrix4,
Mesh,
Object3D,
Ray,
Raycaster,
RGBAFormat,
SkinnedMesh,
Sphere,
Triangle,
Vector2,
Vector3
Vector3,
type Intersection
} from 'three'
import { BatchObject } from '../batching/BatchObject'
import Materials from '../materials/Materials'
@@ -57,23 +62,23 @@ export enum TransformStorage {
export default class SpeckleMesh extends Mesh {
public static MeshBatchNumber = 0
private tas: TopLevelAccelerationStructure = null
private batchMaterial: Material = null
private tas: TopLevelAccelerationStructure
private batchMaterial: Material
private materialCache: { [id: string]: Material } = {}
private materialStack: Array<Material | Material[]> = []
private materialCacheLUT: { [id: string]: number } = {}
private _batchObjects: BatchObject[]
private transformsBuffer: Float32Array = null
private transformStorage: TransformStorage
private _batchObjects!: BatchObject[]
private transformsBuffer: Float32Array | undefined = undefined
private transformStorage!: TransformStorage
public transformsTextureUniform: DataTexture = null
public transformsArrayUniforms: Matrix4[] = null
public transformsTextureUniform: DataTexture
public transformsArrayUniforms: Matrix4[] | null = null
private debugBatchBox = false
private boxHelper: Box3Helper
private boxHelper!: Box3Helper
public get TAS() {
public get TAS(): TopLevelAccelerationStructure {
return this.tas
}
@@ -134,17 +139,23 @@ export default class SpeckleMesh extends Mesh {
this.materialCacheLUT[clone.id] = material.id
cachedMaterial = clone
this.updateMaterialTransformsUniform(this.materialCache[material.id])
} else if (copy || material['needsCopy'] || cachedMaterial['needsCopy']) {
} else if (
copy ||
(material as never)['needsCopy'] ||
(cachedMaterial as never)['needsCopy']
) {
Materials.fastCopy(material, cachedMaterial)
}
return cachedMaterial
}
public restoreMaterial() {
if (this.materialStack.length > 0) this.material = this.materialStack.pop()
if (this.materialStack.length > 0)
this.material = this.materialStack.pop() as Material | Material[]
}
public updateMaterialTransformsUniform(material: Material) {
if (!material.defines) material.defines = {}
material.defines['TRANSFORM_STORAGE'] = this.transformStorage
if (this.transformStorage === TransformStorage.VERTEX_TEXTURE) {
@@ -165,6 +176,7 @@ export default class SpeckleMesh extends Mesh {
}
public updateTransformsUniform() {
if (!this.transformsBuffer) return
let needsUpdate = false
if (this.transformStorage === TransformStorage.VERTEX_TEXTURE) {
for (let k = 0; k < this._batchObjects.length; k++) {
@@ -196,6 +208,7 @@ export default class SpeckleMesh extends Mesh {
}
this.transformsTextureUniform.needsUpdate = needsUpdate
} else {
if (!this.transformsArrayUniforms) return
for (let k = 0; k < this._batchObjects.length; k++) {
const batchObject = this._batchObjects[k]
if (!(needsUpdate ||= batchObject.transformDirty)) continue
@@ -223,12 +236,17 @@ export default class SpeckleMesh extends Mesh {
if (this.tas && needsUpdate) {
this.tas.refit()
this.tas.getBoundingBox(this.tas.bounds)
/** Caterint to typescript
* There is no unniverse where the geomery bounding box/sphere is null at this point
*/
if (!this.geometry.boundingBox) this.geometry.boundingBox = new Box3()
this.geometry.boundingBox.copy(this.tas.bounds)
if (!this.geometry.boundingSphere) this.geometry.boundingSphere = new Sphere()
this.geometry.boundingBox.getBoundingSphere(this.geometry.boundingSphere)
if (!this.boxHelper && this.debugBatchBox) {
this.boxHelper = new Box3Helper(this.tas.bounds, new Color(0xff0000))
this.boxHelper.layers.set(ObjectLayers.PROPS)
this.parent.add(this.boxHelper)
if (this.parent) this.parent.add(this.boxHelper)
}
}
}
@@ -258,13 +276,17 @@ export default class SpeckleMesh extends Mesh {
)
return null
}
return this.material[group.materialIndex]
return this.material[group.materialIndex as number]
}
}
// converts the given BVH raycast intersection to align with the three.js raycast
// structure (include object, world space distance and point).
private convertRaycastIntersect(hit, object, raycaster) {
private convertRaycastIntersect(
hit: Intersection | null,
object: Object3D,
raycaster: Raycaster
) {
if (hit === null) {
return null
}
@@ -280,7 +302,7 @@ export default class SpeckleMesh extends Mesh {
}
}
raycast(raycaster: Raycaster, intersects) {
raycast(raycaster: Raycaster, intersects: Array<Intersection>) {
if (this.tas) {
if (this.batchMaterial === undefined) return
@@ -316,7 +338,7 @@ export default class SpeckleMesh extends Mesh {
if (geometry.boundingSphere === null) geometry.computeBoundingSphere()
_sphere.copy(geometry.boundingSphere)
_sphere.copy(geometry.boundingSphere || new Sphere())
_sphere.applyMatrix4(matrixWorld)
if (raycaster.ray.intersectsSphere(_sphere) === false) return
@@ -336,13 +358,13 @@ export default class SpeckleMesh extends Mesh {
const index = geometry.index
/** Stored high component if RTE is being used. Regular positions otherwise */
const position = geometry.attributes.position
const position = geometry.attributes.position as BufferAttribute
/** Stored low component if RTE is being used. undefined otherwise */
const positionLow = geometry.attributes['position_low']
const morphPosition = geometry.morphAttributes.position
const positionLow = geometry.attributes['position_low'] as BufferAttribute
const morphPosition = geometry.morphAttributes.position as Array<BufferAttribute>
const morphTargetsRelative = geometry.morphTargetsRelative
const uv = geometry.attributes.uv
const uv2 = geometry.attributes.uv2
const uv = geometry.attributes.uv as BufferAttribute
const uv2 = geometry.attributes.uv2 as BufferAttribute
const groups = geometry.groups
const drawRange = geometry.drawRange
@@ -352,6 +374,10 @@ export default class SpeckleMesh extends Mesh {
if (Array.isArray(material)) {
for (let i = 0, il = groups.length; i < il; i++) {
const group = groups[i]
if (!group.materialIndex) {
Logger.error(`Group with no material, skipping!`)
continue
}
const groupMaterial = material[group.materialIndex]
const start = Math.max(group.start, drawRange.start)
@@ -383,7 +409,8 @@ export default class SpeckleMesh extends Mesh {
if (intersection) {
intersection.faceIndex = Math.floor(j / 3) // triangle number in indexed buffer semantics
intersection.face.materialIndex = group.materialIndex
if (intersection.face)
intersection.face.materialIndex = group.materialIndex as number
intersects.push(intersection)
}
}
@@ -425,7 +452,7 @@ export default class SpeckleMesh extends Mesh {
if (Array.isArray(material)) {
for (let i = 0, il = groups.length; i < il; i++) {
const group = groups[i]
const groupMaterial = material[group.materialIndex]
const groupMaterial = material[group.materialIndex as number]
const start = Math.max(group.start, drawRange.start)
const end = Math.min(
@@ -456,7 +483,8 @@ export default class SpeckleMesh extends Mesh {
if (intersection) {
intersection.faceIndex = Math.floor(j / 3) // triangle number in non-indexed buffer semantics
intersection.face.materialIndex = group.materialIndex
if (intersection.face)
intersection.face.materialIndex = group.materialIndex as number
intersects.push(intersection)
}
}
@@ -497,7 +525,16 @@ export default class SpeckleMesh extends Mesh {
}
}
function checkIntersection(object, material, raycaster, ray, pA, pB, pC, point) {
function checkIntersection(
object: Object3D,
material: Material,
raycaster: Raycaster,
ray: Ray,
pA: Vector3,
pB: Vector3,
pC: Vector3,
point: Vector3
): (Intersection & { uv2: Vector2 | undefined }) | null {
let intersect
if (material.side === BackSide) {
@@ -529,19 +566,19 @@ function checkIntersection(object, material, raycaster, ray, pA, pB, pC, point)
* hold the default `position` attribute values
*/
function checkBufferGeometryIntersection(
object,
material,
raycaster,
ray,
positionLow,
positionHigh,
morphPosition,
morphTargetsRelative,
uv,
uv2,
a,
b,
c
object: Object3D,
material: Material,
raycaster: Raycaster,
ray: Ray,
positionLow: BufferAttribute,
positionHigh: BufferAttribute,
morphPosition: Array<BufferAttribute>,
morphTargetsRelative: boolean,
uv: BufferAttribute,
uv2: BufferAttribute,
a: number,
b: number,
c: number
) {
_vA.fromBufferAttribute(positionHigh, a)
_vB.fromBufferAttribute(positionHigh, b)
@@ -552,7 +589,7 @@ function checkBufferGeometryIntersection(
_vC.add(_vTemp.fromBufferAttribute(positionLow, c))
}
const morphInfluences = object.morphTargetInfluences
const morphInfluences = (object as SkinnedMesh).morphTargetInfluences
if (morphPosition && morphInfluences) {
_morphA.set(0, 0, 0)
@@ -585,10 +622,10 @@ function checkBufferGeometryIntersection(
_vC.add(_morphC)
}
if (object.isSkinnedMesh) {
object.boneTransform(a, _vA)
object.boneTransform(b, _vB)
object.boneTransform(c, _vC)
if ((object as SkinnedMesh).isSkinnedMesh) {
;(object as SkinnedMesh).boneTransform(a, _vA)
;(object as SkinnedMesh).boneTransform(b, _vB)
;(object as SkinnedMesh).boneTransform(c, _vC)
}
const intersection = checkIntersection(
@@ -1,7 +1,17 @@
import { Box3, Intersection, Material, Object3D, Raycaster } from 'three'
import {
Box3,
type Intersection,
Object3D,
Raycaster,
Vector3,
RaycasterParameters,
Face
} from 'three'
import { ExtendedTriangle, ShapecastIntersection } from 'three-mesh-bvh'
import { BatchObject } from '../batching/BatchObject'
import { ObjectLayers } from '../../IViewer'
import SpeckleMesh from './SpeckleMesh'
import SpeckleInstancedMesh from './SpeckleInstancedMesh'
export type ExtendedShapeCastCallbacks = {
intersectsTAS?: (
@@ -45,13 +55,29 @@ export type ExtendedShapeCastCallbacks = {
export interface ExtendedIntersection extends Intersection {
batchObject?: BatchObject
material?: Material
pointOnLine?: Vector3
// material?: Material
}
export interface MeshIntersection extends Intersection {
face: Face
faceIndex: number
}
export interface ExtendedMeshIntersection extends MeshIntersection {
batchObject: BatchObject
object: SpeckleMesh | SpeckleInstancedMesh
}
export interface ExtendedRaycasterParameters extends RaycasterParameters {
Line2: { threshold: number }
}
export class SpeckleRaycaster extends Raycaster {
public onObjectIntersectionTest: (object: Object3D) => void = null
public onObjectIntersectionTest: ((object: Object3D) => void) | null = null
public params: ExtendedRaycasterParameters
constructor(origin?, direction?, near = 0, far = Infinity) {
constructor(origin?: Vector3, direction?: Vector3, near = 0, far = Infinity) {
super(origin, direction, near, far)
this.layers.disableAll()
this.layers.enable(ObjectLayers.STREAM_CONTENT)
@@ -61,9 +87,10 @@ export class SpeckleRaycaster extends Raycaster {
this.layers.enable(ObjectLayers.STREAM_CONTENT_POINT_CLOUD)
// OFF by default
this.layers.enable(ObjectLayers.STREAM_CONTENT_POINT)
this.params = { Line2: { threshold: 0 } }
}
public intersectObjects(objects, recursive = true, intersects = []) {
public intersectObjects(objects: Array<Object3D>, recursive = true, intersects = []) {
for (let i = 0, l = objects.length; i < l; i++) {
intersectObject(objects[i], this, intersects, recursive)
}
@@ -74,11 +101,16 @@ export class SpeckleRaycaster extends Raycaster {
}
}
function ascSort(a, b) {
function ascSort(a: Intersection, b: Intersection) {
return a.distance - b.distance
}
function intersectObject(object, raycaster, intersects, recursive) {
function intersectObject(
object: Object3D,
raycaster: SpeckleRaycaster,
intersects: Array<Intersection>,
recursive: boolean
) {
if (object.layers.test(raycaster.layers)) {
if (raycaster.onObjectIntersectionTest) {
raycaster.onObjectIntersectionTest(object)
@@ -87,7 +119,9 @@ function intersectObject(object, raycaster, intersects, recursive) {
}
recursive &&=
// eslint-disable-next-line eqeqeq
object.userData.raycastChildren != undefined ? object.raycastChildren : true
object.userData.raycastChildren != undefined
? object.userData.raycastChildren
: true
if (recursive === true) {
const children = object.children
@@ -8,14 +8,17 @@ import {
MeshBasicMaterial,
PlaneGeometry,
Quaternion,
Raycaster,
Vector2,
Vector3,
Vector4
Vector4,
type Intersection
} from 'three'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
import { Text } from 'troika-three-text'
import { SpeckleObject } from '../tree/DataTree'
import SpeckleBasicMaterial from '../materials/SpeckleBasicMaterial'
import { ObjectLayers } from '../../IViewer'
import { ObjectLayers, type SpeckleObject } from '../../IViewer'
export interface SpeckleTextParams {
textValue?: string
@@ -26,7 +29,7 @@ export interface SpeckleTextParams {
}
export interface SpeckleTextStyle {
backgroundColor?: Color
backgroundColor?: Color | null
backgroundCornerRadius?: number
backgroundPixelHeight?: number
textColor?: Color
@@ -44,7 +47,7 @@ const DefaultSpeckleTextStyle: SpeckleTextStyle = {
export class SpeckleText extends Mesh {
private _layer: ObjectLayers = ObjectLayers.NONE
private _text: Text = null
private _background: Mesh = null
private _background: Mesh | null = null
private _backgroundSize: Vector3 = new Vector3()
private _style: SpeckleTextStyle = Object.assign({}, DefaultSpeckleTextStyle)
private _resolution: Vector2 = new Vector2()
@@ -136,9 +139,11 @@ export class SpeckleText extends Mesh {
if (position) {
if (this._style.billboard) {
this.textMesh.material.userData.billboardPos.value.copy(position)
;(
this._background.material as SpeckleBasicMaterial
).userData.billboardPos.value.copy(position)
if (this._background) {
;(
this._background.material as SpeckleBasicMaterial
).userData.billboardPos.value.copy(position)
}
}
this.position.copy(position)
}
@@ -146,7 +151,7 @@ export class SpeckleText extends Mesh {
if (scale) this.scale.copy(scale)
}
public raycast(raycaster, intersects) {
public raycast(raycaster: Raycaster, intersects: Array<Intersection>) {
const { textRenderInfo, curveRadius } = this.textMesh
if (textRenderInfo) {
const bounds = textRenderInfo.blockBounds
@@ -206,7 +211,7 @@ export class SpeckleText extends Mesh {
raycastMesh.matrixWorld = this.textMesh.matrixWorld
}
raycastMesh.material.side = this.textMesh.material.side
const tempArray = []
const tempArray: Array<Intersection> = []
raycastMesh.raycast(raycaster, tempArray)
for (let i = 0; i < tempArray.length; i++) {
tempArray[i].object = this
@@ -221,7 +226,7 @@ export class SpeckleText extends Mesh {
private updateBackground() {
if (!this._style.backgroundColor) {
this.remove(this._background)
if (this._background) this.remove(this._background)
this._background = null
return
}
@@ -251,7 +256,9 @@ export class SpeckleText extends Mesh {
const color = new Color(this._style.backgroundColor).convertSRGBToLinear()
;(this._background.material as SpeckleBasicMaterial).color = color
;(this._background.material as SpeckleBasicMaterial).billboardPixelHeight =
this._style.backgroundPixelHeight * window.devicePixelRatio
(this._style.backgroundPixelHeight !== undefined
? this._style.backgroundPixelHeight
: DefaultSpeckleTextStyle.backgroundPixelHeight || 0) * window.devicePixelRatio
}
/** From https://discourse.threejs.org/t/roundedrectangle-squircle/28645 */
@@ -282,7 +289,7 @@ export class SpeckleText extends Mesh {
return geometry
function contour(j) {
function contour(j: number) {
qu = Math.trunc((4 * j) / n) + 1 // quadrant qu: 1..4
sgx = qu === 1 || qu === 4 ? 1 : -1 // signum left/right
sgy = qu < 3 ? 1 : -1 // signum top / bottom
@@ -1,7 +1,7 @@
import { Camera, Matrix4, Vector3, WebGLRenderer } from 'three'
import { Geometry } from '../converter/Geometry'
export class RTEBuffers {
private _cache: RTEBuffers = null
private _cache: RTEBuffers | undefined
viewer: Vector3 = new Vector3()
viewerLow: Vector3 = new Vector3()
@@ -1,19 +1,22 @@
import {
Box3,
Box3Helper,
BufferAttribute,
Color,
FrontSide,
Intersection,
Material,
Matrix4,
Object3D,
Ray,
Side,
Vector3
} from 'three'
import { ExtendedTriangle } from 'three-mesh-bvh'
import { BatchObject } from '../batching/BatchObject'
import { ExtendedIntersection, ExtendedShapeCastCallbacks } from './SpeckleRaycaster'
import type {
ExtendedMeshIntersection,
ExtendedShapeCastCallbacks,
MeshIntersection
} from './SpeckleRaycaster'
import { ObjectLayers } from '../../IViewer'
import { AccelerationStructure } from './AccelerationStructure'
@@ -55,8 +58,7 @@ export class TopLevelAccelerationStructure {
public bounds: Box3 = new Box3(new Vector3(0, 0, 0), new Vector3(0, 0, 0))
public boxHelpers: Box3Helper[] = []
public accelerationStructure: AccelerationStructure = null
public lastRefitTime = 0
public accelerationStructure: AccelerationStructure
public constructor(batchObjects: BatchObject[]) {
this.batchObjects = batchObjects
@@ -66,7 +68,7 @@ export class TopLevelAccelerationStructure {
private buildBVH() {
const indices = []
const vertices = new Float32Array(
const vertices: number[] = new Array<number>(
TopLevelAccelerationStructure.CUBE_VERTS * 3 * this.batchObjects.length
)
let vertOffset = 0
@@ -99,7 +101,7 @@ export class TopLevelAccelerationStructure {
this.accelerationStructure.outputOriginTransfom = new Matrix4()
}
private updateVertArray(box: Box3, offset: number, outPositions: Float32Array) {
private updateVertArray(box: Box3, offset: number, outPositions: number[]) {
outPositions[offset] = box.min.x
outPositions[offset + 1] = box.min.y
outPositions[offset + 2] = box.max.z
@@ -134,38 +136,39 @@ export class TopLevelAccelerationStructure {
}
public refit() {
const start = performance.now()
const positions = this.accelerationStructure.geometry.attributes.position.array
const positions = this.accelerationStructure.geometry.attributes.position
.array as number[]
const boxBuffer: Box3 = new Box3()
for (let k = 0; k < this.batchObjects.length; k++) {
const start = this.batchObjects[k].tasVertIndexStart
const basBox =
this.batchObjects[k].accelerationStructure.getBoundingBox(boxBuffer)
this.updateVertArray(basBox, start * 3, positions as Float32Array)
this.updateVertArray(basBox, start * 3, positions)
if (TopLevelAccelerationStructure.debugBoxes) this.boxHelpers[k].box.copy(basBox)
}
this.accelerationStructure.bvh.refit()
this.lastRefitTime = performance.now() - start
}
/* Core Cast Functions */
public raycast(
ray: Ray,
materialOrSide: Side | Material | Material[] = FrontSide
): ExtendedIntersection[] {
const res = []
): ExtendedMeshIntersection[] {
const res: ExtendedMeshIntersection[] = []
const rayBuff = new Ray()
rayBuff.copy(ray)
const tasResults: Intersection<Object3D>[] = this.accelerationStructure.raycast(
const tasResults: MeshIntersection[] = this.accelerationStructure.raycast(
rayBuff,
materialOrSide
)
if (!tasResults.length) return res
tasResults.forEach((tasRes: Intersection<Object3D>) => {
const vertIndex =
this.accelerationStructure.geometry.index.array[tasRes.faceIndex * 3]
/** The index buffer for the bvh's geometry will *never* be undefined as it uses indexed geometry */
const indexBufferAttribute: BufferAttribute = this.accelerationStructure.geometry
.index as BufferAttribute
tasResults.forEach((tasRes: MeshIntersection) => {
const vertIndex = indexBufferAttribute.array[tasRes.faceIndex * 3]
const batchObjectIndex = Math.trunc(
vertIndex / TopLevelAccelerationStructure.CUBE_VERTS
)
@@ -175,9 +178,13 @@ export class TopLevelAccelerationStructure {
materialOrSide
)
hits.forEach((hit) => {
;(hit as ExtendedIntersection).batchObject = this.batchObjects[batchObjectIndex]
/** We're promoting the MeshIntersection to ExtendedMeshIntersection because
* now we know it's corresponding batch object
*/
const extendedHit: ExtendedMeshIntersection = hit as ExtendedMeshIntersection
extendedHit.batchObject = this.batchObjects[batchObjectIndex]
res.push(extendedHit)
})
res.push(...hits)
})
return res
@@ -186,30 +193,33 @@ export class TopLevelAccelerationStructure {
public raycastFirst(
ray: Ray,
materialOrSide: Side | Material | Material[] = FrontSide
): ExtendedIntersection {
const res = null
): ExtendedMeshIntersection | null {
const rayBuff = new Ray()
rayBuff.copy(ray)
const tasRes: Intersection<Object3D> = this.accelerationStructure.raycastFirst(
const tasRes: MeshIntersection = this.accelerationStructure.raycastFirst(
rayBuff,
materialOrSide
)
if (!tasRes) return res
if (!tasRes) return null
const vertIndex =
this.accelerationStructure.geometry.index.array[tasRes.faceIndex * 3]
/** The index buffer for the bvh's geometry will *never* be undefined as it uses indexed geometry */
const indexBufferAttribute: BufferAttribute = this.accelerationStructure.geometry
.index as BufferAttribute
const vertIndex = indexBufferAttribute.array[tasRes.faceIndex * 3]
const batchObjectIndex = Math.trunc(
vertIndex / TopLevelAccelerationStructure.CUBE_VERTS
)
rayBuff.copy(ray)
const hits = this.batchObjects[batchObjectIndex].accelerationStructure.raycast(
rayBuff,
materialOrSide
)
hits.forEach((hit) => {
;(hit as ExtendedIntersection).batchObject = this.batchObjects[batchObjectIndex]
})
res.push(...hits)
const hit: MeshIntersection = this.batchObjects[
batchObjectIndex
].accelerationStructure.raycastFirst(rayBuff, materialOrSide)
/** We're promoting the MeshIntersection to ExtendedMeshIntersection because
* now we know it's corresponding batch object
*/
const extendedHit: ExtendedMeshIntersection = hit as ExtendedMeshIntersection
extendedHit.batchObject = this.batchObjects[batchObjectIndex]
return extendedHit
}
public shapecast(callbacks: ExtendedShapeCastCallbacks): boolean {
@@ -254,12 +264,15 @@ export class TopLevelAccelerationStructure {
let ret = false
this.accelerationStructure.shapecast({
intersectsBounds: (box, isLeaf, score, depth, nodeIndex) => {
const res = callbacks.intersectsTAS(box, isLeaf, score, depth, nodeIndex)
return res
if (callbacks.intersectsTAS)
return callbacks.intersectsTAS(box, isLeaf, score, depth, nodeIndex)
return false
},
intersectsRange: (triangleOffset: number) => {
const vertIndex =
this.accelerationStructure.geometry.index.array[triangleOffset * 3]
/** The index buffer for the bvh's geometry will *never* be undefined as it uses indexed geometry */
const indexBufferAttribute: BufferAttribute = this.accelerationStructure
.geometry.index as BufferAttribute
const vertIndex = indexBufferAttribute.array[triangleOffset * 3]
const batchObjectIndex = Math.trunc(
vertIndex / TopLevelAccelerationStructure.CUBE_VERTS
)