Skip to content

Commit

Permalink
feat: add a CircleFacade primitive
Browse files Browse the repository at this point in the history
  • Loading branch information
lojjic committed Feb 21, 2020
1 parent f478a47 commit d73ae87
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
38 changes: 38 additions & 0 deletions packages/troika-3d/src/facade/primitives/CircleFacade.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { utils } from 'troika-core'
import { CircleBufferGeometry } from 'three'
import { MeshFacade } from './MeshFacade.js'

const geometries = Object.create(null, [
['low', 32],
['medium', 64],
['high', 128]
].reduce((descr, [name, segments]) => {
descr[name] = {
get: utils.memoize(() => new CircleBufferGeometry(1, segments))
}
return descr
}, {}))

export function getCircleGeometry(detail) {
return geometries[detail] || geometries.medium
}

/**
* A simple planar circle, laying along the x-z plane, facing the positive y axis, centered on the origin.
* The `radius` property is an alias to uniform `scaleX` and `scaleZ`. Set `scaleX/Y/Z` individually if
* you need non-uniform scaling.
* The `detail` property allows selecting a LOD; its values can be 'low', 'medium', or 'high'.
* To control the material, see {@link MeshFacade}.
*/
export class CircleFacade extends MeshFacade {
get geometry() {
return getCircleGeometry(this.detail)
}

set radius(r) {
this.scaleX = this.scaleZ = r
}
get radius() {
return this.scaleX
}
}
2 changes: 1 addition & 1 deletion packages/troika-3d/src/facade/primitives/PlaneFacade.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const getGeometry = utils.memoize(() => {
})

/**
* A simple rectangular plane that lays along the x-z plane, facing up, centered on the origin.
* A simple rectangular plane, laying along the x-z plane, facing the positive y axis, centered on the origin.
* The `width` property controls x scale and the `depth` property controls z scale.
* To control the material, see {@link MeshFacade}.
*/
Expand Down
23 changes: 9 additions & 14 deletions packages/troika-3d/src/facade/primitives/SphereFacade.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@ import { utils } from 'troika-core'
import { SphereBufferGeometry } from 'three'
import { MeshFacade } from './MeshFacade.js'

function createGeometry(widthSegments, heightSegments) {
return new SphereBufferGeometry(1, widthSegments, heightSegments)
}

const geometries = Object.create(null, {
low: {
get: utils.memoize(() => createGeometry(16, 12))
},
medium: {
get: utils.memoize(() => createGeometry(32, 24))
},
high: {
get: utils.memoize(() => createGeometry(64, 48))
const geometries = Object.create(null, [
['low', 16, 12],
['medium', 32, 24],
['high', 64, 48]
].reduce((descr, [name, wSegs, hSegs]) => {
descr[name] = {
get: utils.memoize(() => new SphereBufferGeometry(1, wSegs, hSegs))
}
})
return descr
}, {}))

export function getSphereGeometry(detail) {
return geometries[detail] || geometries.medium
Expand Down
1 change: 1 addition & 0 deletions packages/troika-3d/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export {default as Instanceable3DFacade} from './facade/instancing/Instanceable3

// Primitives
export {BoxFacade} from './facade/primitives/BoxFacade.js'
export {CircleFacade} from './facade/primitives/CircleFacade.js'
export {CubeFacade} from './facade/primitives/CubeFacade.js'
export {MeshFacade} from './facade/primitives/MeshFacade.js'
export {PlaneFacade} from './facade/primitives/PlaneFacade.js'
Expand Down

0 comments on commit d73ae87

Please sign in to comment.