Skip to content

Commit

Permalink
add userdata(#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
ostatni5 committed Nov 24, 2021
1 parent b09585c commit 3ddcaef
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/ThreeEditor/util/AdditionalUserData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,24 @@ export interface AdditionalUserDataType {
userSetRotation?: boolean;
}

export function generateSimulationInfo(geometryMesh: THREE.Mesh<PossibleGeometryType>) {
export const getGeometryParameters = (geometry: PossibleGeometryType) => {
const parameters: { [key: string]: number } = {};

const type = geometryMesh.geometry.type as keyof typeof geometryParameters;

const geometry = geometryMesh.geometry as PossibleGeometryType;
const type = geometry.type as keyof typeof geometryParameters;

geometryParameters[type].forEach(prop => {
parameters[prop] = geometry.parameters[prop as keyof typeof geometry.parameters];
});

return parameters;
}

export function generateSimulationInfo(geometryMesh: THREE.Mesh<PossibleGeometryType>) {

const geometry = geometryMesh.geometry as PossibleGeometryType;

const parameters = getGeometryParameters(geometry);

let userData: AdditionalUserDataType = {
id: geometryMesh.id,
geometryType: geometryMesh.geometry.type,
Expand Down
24 changes: 23 additions & 1 deletion src/ThreeEditor/util/WorldZone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Color, LineBasicMaterial, MeshBasicMaterial, Object3D, Vector3 } from '
import { debounce } from 'throttle-debounce';

import { Editor } from '../js/Editor';
import { getGeometryParameters, PossibleGeometryType } from './AdditionalUserData';
import { SimulationMesh } from './SimulationBase/SimulationMesh';

export interface WorldZoneJSON {
Expand All @@ -14,6 +15,13 @@ export interface WorldZoneJSON {
color: THREE.ColorRepresentation;
marginMultiplier: number;
autoCalculate: boolean;
userData?: {
geometryType: string;
position: THREE.Vector3Tuple;
parameters: {
[key: string]: number;
};
}
}

export const BOUNDING_ZONE_TYPE = ['sphere', 'cylinder', 'box'] as const;
Expand Down Expand Up @@ -218,6 +226,14 @@ export class WorldZone extends SimulationMesh {
}

toJSON() {
const geometry: {
[K in WorldZoneType]: PossibleGeometryType;
} = {
'box': new THREE.BoxGeometry(this.size.x, this.size.y, this.size.z),
'sphere': new THREE.SphereGeometry(this.size.x),
'cylinder': new THREE.CylinderGeometry(this.size.x, this.size.x, this.size.y),
};

const jsonObject: WorldZoneJSON = {
center: this.box.getCenter(new Vector3()),
size: this.box.getSize(new Vector3()),
Expand All @@ -226,7 +242,13 @@ export class WorldZone extends SimulationMesh {
name: this.name,
color: this.material.color.getHex(),
marginMultiplier: this.marginMultiplier,
autoCalculate: this.autoCalculate
autoCalculate: this.autoCalculate,

userData: {
geometryType: geometry[this._geometryType].type,
parameters: getGeometryParameters(geometry[this._geometryType]),
position: this.box.getCenter(new Vector3()).toArray(),
}
};

return jsonObject;
Expand Down

0 comments on commit 3ddcaef

Please sign in to comment.