Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions packages/examples/core/booleans/basicBooleans.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@
* @licence MIT License
*/

const jscad = require('@jscad/modeling')
const { cube, sphere } = jscad.primitives
const { translate } = jscad.transforms
const { colorize } = jscad.colors
const { union, subtract, intersect } = jscad.booleans
import { cube, sphere, translate, colorize, union, subtract, intersect } from '@jscad/modeling'

const main = () => {
export const main = () => {
const aCube = colorize([1, 0, 0], translate([-4.5, 0, 0], cube()))
const aSphere = colorize([0, 1, 0], translate([-5.2, -0.8, 0.8], sphere({ segments: 32 })))

Expand All @@ -29,5 +25,3 @@ const main = () => {
translate([9, 0, 0], aIntersection)
]
}

module.exports = { main }
9 changes: 2 additions & 7 deletions packages/examples/core/colors/basicColors.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
* @licence MIT License
*/

const jscad = require('@jscad/modeling')
const { colorize, hslToRgb, colorNameToRgb, hexToRgb, hsvToRgb } = jscad.colors
const { cuboid, sphere } = jscad.primitives
const { translate } = jscad.transforms
import { colorize, hslToRgb, colorNameToRgb, hexToRgb, hsvToRgb, cuboid, sphere, translate } from '@jscad/modeling'

const main = () => {
export const main = () => {
// the color() function applies a color (rgb, or rgba) to the given object
const simple = colorize([0, 1, 0, 0.8], cuboid())

Expand Down Expand Up @@ -51,5 +48,3 @@ const main = () => {
fromHsv
]
}

module.exports = { main }
11 changes: 3 additions & 8 deletions packages/examples/core/colors/colorCube.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
* @licence MIT License
*/

const jscad = require('@jscad/modeling')
const { colorize, hslToRgb, hsvToRgb } = jscad.colors
const { cuboid } = jscad.primitives
const { translate } = jscad.transforms
import { colorize, hslToRgb, hsvToRgb, cuboid, translate } from '@jscad/modeling'

const getTranslation = (x, y, z, steps) => {
const spacing = 4
Expand All @@ -33,7 +30,7 @@ const getColor = (a, b, c, method) => {
* @param {String} params.method - The spectrum function to use: 'rgb'|'hsv'|'hsl'
* @returns {[geometry]}
*/
const main = (params) => {
export const main = (params) => {
const o = []
const rows = 8
for (let ix = 0; ix <= rows; ix++) {
Expand All @@ -48,7 +45,7 @@ const main = (params) => {
return o
}

const getParameterDefinitions = () => [
export const getParameterDefinitions = () => [
{
name: 'method',
type: 'choice',
Expand All @@ -58,5 +55,3 @@ const getParameterDefinitions = () => [
initial: 'hsl'
}
]

module.exports = { main, getParameterDefinitions }
8 changes: 2 additions & 6 deletions packages/examples/core/colors/transparency.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
* @licence MIT License
*/

const { colorize, hslToRgb, colorNameToRgb } = require('@jscad/modeling').colors
const { cuboid, cylinder } = require('@jscad/modeling').primitives
const { translate } = require('@jscad/modeling').transforms
import { colorize, hslToRgb, colorNameToRgb, cuboid, cylinder, translate } from '@jscad/modeling'

const main = () => {
export const main = () => {
const shapes = []
for (let i = 7; i >= 0; i--) {
// reverse order for seeing through all cylinders (see http://www.opengl.org/wiki/Transparency_Sorting)
Expand All @@ -29,5 +27,3 @@ const main = () => {
)
return shapes
}

module.exports = { main }
41 changes: 17 additions & 24 deletions packages/examples/core/curves/bezier/extrudeAlongPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,10 @@
* @licence MIT License
*/

const jscad = require('@jscad/modeling')
const { colors, geometries, maths, extrusions } = jscad
import { colorize, geom2, vec3, mat4, extrudeFromSlices, slice } from '@jscad/modeling'
import { cuboid, circle, bezier, translate } from '@jscad/modeling'

const { cuboid, circle } = jscad.primitives
const { translate } = jscad.transforms
const { slice } = jscad.extrusions
const { bezier } = jscad.curves

const main = () => [
export const main = () => [
box4x4([-8, -4, 2], [1, 0, 0]),
box4x4([8, 4, 12], [0, 1, 0]),
tube([
Expand All @@ -38,45 +33,45 @@ const tube = (bezierControlPoints) => {
// Create the initial slice
const circ = circle({ radius: 1, segments: 32 })
const l = bezierControlPoints.length - 1
const circPoints = geometries.geom2.toPoints(circ)
let tubeSlice = slice.fromPoints(circPoints)
const circPoints = geom2.toPoints(circ)
let tubeSlice = slice.fromVertices(circPoints)

// Rotate it close to the direction we are going in. Rotation gets funky around 180˚
const bezierDelta = maths.vec3.clone([
const bezierDelta = vec3.clone([
bezierControlPoints[l][0] - bezierControlPoints[0][0],
bezierControlPoints[l][1] - bezierControlPoints[0][1],
bezierControlPoints[l][2] - bezierControlPoints[0][2]
])
tubeSlice = slice.transform(rotationMatrixFromVectors(maths.vec3.clone([0, 0, 1]), bezierDelta), tubeSlice)
tubeSlice = slice.transform(rotationMatrixFromVectors(vec3.clone([0, 0, 1]), bezierDelta), tubeSlice)

// Create the bezier function
const tubeCurve = bezier.create(bezierControlPoints)

// ...and extrude.
return extrusions.extrudeFromSlices({
return extrudeFromSlices({
numberOfSlices: 60,
capStart: true,
capEnd: true,
callback: function (progress, count, base) {
const positionArray = bezier.valueAt(progress, tubeCurve)
const tangentArray = bezier.tangentAt(progress, tubeCurve)
const rotationMatrix = rotationMatrixFromVectors(bezierDelta, maths.vec3.clone(tangentArray))
const translationMatrix = maths.mat4.fromTranslation(maths.mat4.create(), positionArray)
return slice.transform(maths.mat4.multiply(translationMatrix, translationMatrix, rotationMatrix), base)
const rotationMatrix = rotationMatrixFromVectors(bezierDelta, vec3.clone(tangentArray))
const translationMatrix = mat4.fromTranslation(mat4.create(), positionArray)
return slice.transform(mat4.multiply(translationMatrix, translationMatrix, rotationMatrix), base)
}
}, tubeSlice)
}

const rotationMatrixFromVectors = (srcVector, targetVector) => {
// From https://gist.github.com/kevinmoran/b45980723e53edeb8a5a43c49f134724
srcVector = maths.vec3.normalize(maths.vec3.create(), srcVector)
targetVector = maths.vec3.normalize(maths.vec3.create(), targetVector)
srcVector = vec3.normalize(vec3.create(), srcVector)
targetVector = vec3.normalize(vec3.create(), targetVector)

const axis = maths.vec3.cross(maths.vec3.create(), targetVector, srcVector)
const cosA = maths.vec3.dot(targetVector, srcVector)
const axis = vec3.cross(vec3.create(), targetVector, srcVector)
const cosA = vec3.dot(targetVector, srcVector)
const k = 1 / (1 + cosA)

return maths.mat4.fromValues(
return mat4.fromValues(
(axis[0] * axis[0] * k) + cosA, (axis[1] * axis[0] * k) - axis[2], (axis[2] * axis[0] * k) + axis[1], 0,
(axis[0] * axis[1] * k) + axis[2], (axis[1] * axis[1] * k) + cosA, (axis[2] * axis[1] * k) - axis[0], 0,
(axis[0] * axis[2] * k) - axis[1], (axis[1] * axis[2] * k) + axis[0], (axis[2] * axis[2] * k) + cosA, 0,
Expand All @@ -86,7 +81,5 @@ const rotationMatrixFromVectors = (srcVector, targetVector) => {

const box4x4 = (translation, color) => {
const b = cuboid({ size: [4, 4, 4] })
return colors.colorize(color, translate(translation, b))
return colorize(color, translate(translation, b))
}

module.exports = { main }
17 changes: 6 additions & 11 deletions packages/examples/core/curves/bezier/simpleExtrude.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,25 @@
* @licence MIT License
*/

const jscad = require('@jscad/modeling')
const { maths, extrusions } = jscad
const { bezier } = jscad.curves
const { slice } = extrusions
import { mat4, extrudeFromSlices, slice, bezier } from '@jscad/modeling'

const main = () => [
export const main = () => [
extrudeWobble(30)
]

const extrudeWobble = (height) => {
const squareSlice = slice.fromPoints([[10, 10], [-10, 10], [-10, -10], [10, -10]])
const squareSlice = slice.fromVertices([[10, 10], [-10, 10], [-10, -10], [10, -10]])

const xCurve = bezier.create([1, 2, 0.4, 1])
const yCurve = bezier.create([1, 2, 0.5])

return extrusions.extrudeFromSlices({
return extrudeFromSlices({
numberOfSlices: 20,
capStart: true,
capEnd: true,
callback: function (progress, count, base) {
let newslice = slice.transform(maths.mat4.fromTranslation(maths.mat4.create(), [0, 0, height * progress]), base)
newslice = slice.transform(maths.mat4.fromScaling(maths.mat4.create(), [
let newslice = slice.transform(mat4.fromTranslation(mat4.create(), [0, 0, height * progress]), base)
newslice = slice.transform(mat4.fromScaling(mat4.create(), [
bezier.valueAt(progress, xCurve),
bezier.valueAt(progress, yCurve),
1
Expand All @@ -38,5 +35,3 @@ const extrudeWobble = (height) => {
}
}, squareSlice)
}

module.exports = { main }
17 changes: 4 additions & 13 deletions packages/examples/core/extrusions/basicExtrusions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,23 @@
* @licence MIT License
*/

const jscad = require('@jscad/modeling')
const { line, polygon, star } = jscad.primitives
const { extrudeRectangular, extrudeLinear, extrudeRotate } = jscad.extrusions
const { translate } = jscad.transforms
const { offset } = jscad.offsets
import { line, polygon, star, extrudeLinear, extrudeRotate, translate, offset, TAU } from '@jscad/modeling'

const main = () => {
export const main = () => {
const shapes = []
const aLine = line([[0, 0], [0, 5], [2, 8], [5, 9]])
shapes.push(translate([-17, 0, 0], aLine))

const aRectangularExtrude = extrudeRectangular({ size: 1, height: 1 }, aLine)
shapes.push(translate([-12, 0, 0], aRectangularExtrude))

const anExpandedExtrude = extrudeLinear({ height: 1 }, offset({ delta: 1, corners: 'round', segments: 32 }, aLine))
shapes.push(translate([-7, 0, 0], anExpandedExtrude))

const poly = polygon({ points: [[-1, -1], [3, -1], [3.5, 2], [2, 1], [1, 2], [0, 1], [-1, 2]] })
const extrudedPoly = extrudeLinear({ height: 5, twistAngle: Math.PI / 4, twistSteps: 10 }, poly)
const extrudedPoly = extrudeLinear({ height: 5, twistAngle: TAU / 8, twistSteps: 10 }, poly)
shapes.push(translate([-1, 0, 0], extrudedPoly))

const starPoly = translate([3, 0, 0], star())
const extrudedStar = extrudeRotate({ segments: 32, startAngle: 0, angle: (Math.PI * 0.75), overflow: 'cap' }, starPoly)
const extrudedStar = extrudeRotate({ segments: 32, startAngle: 0, angle: (TAU / 2 * 0.75), overflow: 'cap' }, starPoly)
shapes.push(translate([9, 0, 0], extrudedStar))

return shapes
}

module.exports = { main }
15 changes: 5 additions & 10 deletions packages/examples/core/extrusions/extrudeFromSlices.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
* @licence MIT License
*/

const { circle } = require('@jscad/modeling').primitives
const { geom2 } = require('@jscad/modeling').geometries
const { extrudeFromSlices, slice } = require('@jscad/modeling').extrusions
const { mat4 } = require('@jscad/modeling').maths
import { circle, geom2, extrudeFromSlices, slice, mat4, TAU } from '@jscad/modeling'

const main = () => {
export const main = () => {
// demonstrates manipulating the original base through translation and scale to build a 3D geometry
const jigglySquare = (height) => {
let squareWithHole = geom2.create(
Expand All @@ -28,11 +25,11 @@ const main = () => {
[[-8.0, 8.0], [8.0, 8.0]]
]
)
squareWithHole = slice.fromSides(geom2.toSides(squareWithHole))
squareWithHole = slice.fromGeom2(squareWithHole)
return extrudeFromSlices({
numberOfSlices: 32,
callback: (progress, count, base) => {
const scaleFactor = 1 + (0.03 * Math.cos(3 * Math.PI * progress))
const scaleFactor = 1 + (0.03 * Math.cos(3 * TAU / 2 * progress))
const scaleMatrix = mat4.fromScaling(mat4.create(), [scaleFactor, 2 - scaleFactor, 1])
const transformMatrix = mat4.fromTranslation(mat4.create(), [0, 0, progress * height])
return slice.transform(mat4.multiply(mat4.create(), scaleMatrix, transformMatrix), base)
Expand All @@ -50,7 +47,7 @@ const main = () => {
numberOfSlices: 6,
callback: (progress, count, base) => {
const newPolygon = circle({ radius: 2 + (5 * progress), segments: 4 + (count * count) })
let newSlice = slice.fromSides(geom2.toSides(newPolygon))
let newSlice = slice.fromGeom2(newPolygon)
newSlice = slice.transform(mat4.fromTranslation(mat4.create(), [0, 0, progress * height]), newSlice)
return newSlice
}
Expand All @@ -63,5 +60,3 @@ const main = () => {
squareToCircleExtrusion(10)
]
}

module.exports = { main }
24 changes: 9 additions & 15 deletions packages/examples/core/extrusions/nutsAndBolts.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@
* @licence MIT License
*/

const jscad = require('@jscad/modeling')
const { cylinder } = jscad.primitives
const { subtract, union } = jscad.booleans
const { colorize } = jscad.colors
const { extrudeFromSlices, slice } = jscad.extrusions
const { translate } = jscad.transforms
import { cylinder, extrudeFromSlices, slice, TAU } from '@jscad/modeling'
import { subtract, union, colorize, translate } from '@jscad/modeling'

const options = {
hexWidth: 10,
Expand All @@ -26,7 +22,7 @@ const options = {
segments: 32
}

const main = () => [
export const main = () => [
colorize([0.9, 0.6, 0.2], bolt(options)),
colorize([0.4, 0.4, 0.4], translate([30, 0, 0], nut(options)))
]
Expand Down Expand Up @@ -61,18 +57,18 @@ const threads = (options) => {
// generate each slice manually
const points = []
for (let i = 0; i < segments; i++) {
const pointAngle = Math.PI * 2 * i / segments
const threadAngle = (2 * Math.PI * revolutions * progress) % (Math.PI * 2)
const pointAngle = TAU * i / segments
const threadAngle = (TAU * revolutions * progress) % TAU

// define the shape of the threads
const phase = angleDiff(threadAngle, pointAngle) / Math.PI
const phase = angleDiff(threadAngle, pointAngle) / TAU / 2
const radius = lerp(innerRadius, outerRadius, 1.4 * phase - 0.2)

const x = radius * Math.cos(pointAngle)
const y = radius * Math.sin(pointAngle)
points.push([x, y, threadLength * progress])
}
return slice.fromPoints(points)
return slice.fromVertices(points)
}
}, {})
}
Expand All @@ -81,8 +77,6 @@ const threads = (options) => {
const lerp = (a, b, t) => Math.max(a, Math.min(b, a + (b - a) * t))

const angleDiff = (angle1, angle2) => {
const diff = Math.abs((angle1 - angle2) % (Math.PI * 2))
return diff > Math.PI ? Math.PI * 2 - diff : diff
const diff = Math.abs((angle1 - angle2) % TAU)
return diff > (TAU / 2) ? TAU - diff : diff
}

module.exports = { main }
11 changes: 3 additions & 8 deletions packages/examples/core/hulls/hull2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@
* @licence MIT License
*/

const jscad = require('@jscad/modeling')
const { circle, rectangle } = jscad.primitives
const { translate } = jscad.transforms
const { hull, hullChain } = jscad.hulls
import { circle, rectangle, translate, hull, hullChain } from '@jscad/modeling'

const getParameterDefinitions = () => [
export const getParameterDefinitions = () => [
{ name: 'doHull', type: 'radio', caption: 'Show:', values: ['shapes', 'hull', 'chain'], captions: ['Original Shapes', 'Hull', 'Hull Chain'], initial: 'shapes' }
]

const main = (params) => {
export const main = (params) => {
const shapes = [
translate([15, 0, 0], circle({ radius: 2, segments: 16 })),
translate([8, 6, 0], circle({ radius: 3.5, segments: 16 })),
Expand All @@ -32,5 +29,3 @@ const main = (params) => {
return shapes
}
}

module.exports = { main, getParameterDefinitions }
Loading