Skip to content

Commit c57fa52

Browse files
committed
Correct raycaster, make scene background color type also a number
1 parent 3503191 commit c57fa52

File tree

7 files changed

+32
-22
lines changed

7 files changed

+32
-22
lines changed

examples/simple-html/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "web-ifc-viewer-basic-example",
33
"private": true,
44
"type": "module",
5-
"version": "1.0.9",
5+
"version": "1.0.11",
66
"description": "A basic html example for web-ifc-viewer",
77
"main": "index.html",
88
"scripts": {

examples/simple-react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "web-ifc-viewer-simple-react",
3-
"version": "0.1.7",
3+
"version": "1.0.11",
44
"private": true,
55
"dependencies": {
66
"@material-ui/core": "^4.11.4",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "web-ifc-viewer-root",
33
"description": "IFC viewer",
4-
"version": "1.0.9",
4+
"version": "1.0.11",
55
"private": true,
66
"author": "agviegas",
77
"license": "MIT",

viewer/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "web-ifc-viewer",
3-
"version": "1.0.9",
3+
"version": "1.0.11",
44
"description": "IFC viewer",
55
"main": "dist/index.js",
66
"scripts": {

viewer/src/components/raycaster.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,23 @@ export class IFCRaycaster {
44
private camera: THREE.Camera;
55
private ifcModels: THREE.Object3D[];
66
private raycaster: THREE.Raycaster;
7-
private mouse: THREE.Vector2;
7+
private canvas: HTMLCanvasElement;
88

9-
constructor(ifcModels: THREE.Object3D[], camera: THREE.Camera, mouse: THREE.Vector2) {
9+
constructor(ifcModels: THREE.Object3D[], camera: THREE.Camera, renderer: THREE.WebGLRenderer) {
1010
this.camera = camera;
11-
this.mouse = mouse;
11+
this.canvas = renderer.domElement;
1212
this.ifcModels = ifcModels;
1313
this.raycaster = new THREE.Raycaster();
1414
//@ts-ignore
1515
this.raycaster.firstHitOnly = true;
1616
}
1717

1818
castRay(event: any, onHit: (event: any, item: THREE.Intersection) => any ) {
19-
this.raycaster.setFromCamera(this.mouse, this.camera);
19+
const mouse = new THREE.Vector2();
20+
const canvasBounds = this.canvas.getBoundingClientRect();
21+
mouse.x = ((event.clientX - canvasBounds.left) / (canvasBounds.right - canvasBounds.left)) * 2 - 1;
22+
mouse.y = -((event.clientY - canvasBounds.top) / (canvasBounds.bottom - canvasBounds.top)) * 2 + 1;
23+
this.raycaster.setFromCamera(mouse, this.camera);
2024
const result = this.raycaster.intersectObjects(this.ifcModels);
2125
if(result.length > 0) return onHit(event, result[0]);
2226
}

viewer/src/core/IFC/ifc-manager.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ export class IfcManager {
3131
ifc_objects: THREE.Object3D[],
3232
scene: THREE.Scene,
3333
camera: THREE.PerspectiveCamera,
34-
mouse: THREE.Vector2
34+
renderer: THREE.WebGLRenderer
3535
) {
3636
this.loader = new IFCLoader();
3737
this.models = ifc_objects;
3838
this.scene = scene;
39-
this.caster = new IFCRaycaster(this.models, camera, mouse);
39+
this.caster = new IFCRaycaster(this.models, camera, renderer);
4040
this.preselection = new IfcSelection(this.loader, this.scene, this.preselectMat);
4141
this.selection = new IfcSelection(this.loader, this.scene, this.selectMat);
4242
}
@@ -58,30 +58,32 @@ export class IfcManager {
5858
}
5959
}
6060

61-
setWasmPath(path: string){
61+
setWasmPath(path: string) {
6262
this.loader.setWasmPath(path);
6363
}
6464

6565
getSpatialStructure(modelID: number, recursive = false) {
6666
return this.loader.getSpatialStructure(modelID, recursive);
6767
}
6868

69-
getProperties(modelID: number, id: number, indirect = false) {
69+
getProperties(modelID: number, id: number, indirect: boolean, recursive: boolean) {
70+
if(modelID == null || id == null ) return null;
7071
const props = this.loader.getItemProperties(modelID, id);
71-
if(indirect){
72-
props.psets = this.loader.getPropertySets(modelID, id, true);
72+
if (indirect) {
73+
props.psets = this.loader.getPropertySets(modelID, id, recursive);
7374
props.type = this.loader.getTypeProperties(modelID, id);
7475
}
7576
console.log(props);
7677
return props;
7778
}
7879

7980
preselect(event: any) {
80-
const { modelID, id } = this.caster.castRay(event, this.preselection.select);
81+
this.caster.castRay(event, this.preselection.select);
8182
}
8283

83-
select(event: any) {
84-
const { modelID, id } = this.caster.castRay(event, this.selection.select);
85-
return this.getProperties(modelID, id, true);
84+
select(event: any, indirect: boolean, recursive: boolean) {
85+
const result = this.caster.castRay(event, this.selection.select);
86+
if(result == null || result.modelID == null || result.id == null) return null;
87+
return this.getProperties(result.modelID, result.id, indirect, recursive);
8688
}
8789
}

viewer/src/core/viewer.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { VisualizationInfo } from '@parametricos/bcf-js';
66
import { IfcManager } from './IFC/ifc-manager';
77

88
export interface ViewerOptions {
9-
backgroundColor?: THREE.Color
9+
backgroundColor?: THREE.Color | number
1010
}
1111

1212
export class Viewer {
@@ -54,6 +54,10 @@ export class Viewer {
5454
this.controls = controls;
5555

5656
//Scene
57+
if(typeof options?.backgroundColor == 'number') {
58+
const color = options?.backgroundColor as number;
59+
options.backgroundColor = new THREE.Color(color);
60+
}
5761
scene.background = options?.backgroundColor || new THREE.Color(0xa9a9a9);
5862

5963
//Renderer
@@ -102,7 +106,7 @@ export class Viewer {
102106
this.render();
103107

104108
//IFC management
105-
this.ifcManager = new IfcManager(this.ifc_objects, this.scene, this.camera, this.mouse);
109+
this.ifcManager = new IfcManager(this.ifc_objects, this.scene, this.camera, this.renderer);
106110
}
107111

108112
render = () => {
@@ -131,8 +135,8 @@ export class Viewer {
131135
this.ifcManager.preselect(event);
132136
}
133137

134-
select = (event: any) => {
135-
this.ifcManager.select(event);
138+
select = (event: any, indirect = true, recursive = false) => {
139+
this.ifcManager.select(event, indirect, recursive);
136140
}
137141

138142
get ifcObjects() {

0 commit comments

Comments
 (0)