Skip to content

Commit

Permalink
Merge branch 'main' into lighting-like-cad
Browse files Browse the repository at this point in the history
  • Loading branch information
hrgdavor committed Jan 26, 2024
2 parents d7221a7 + b81ee49 commit e09501a
Show file tree
Hide file tree
Showing 20 changed files with 193 additions and 57 deletions.
2 changes: 1 addition & 1 deletion apps/engine-test/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import liveServer from 'live-server'
import { buildBundle, buildOne } from './src_build/esbuildUtil.js'

// *************** read parameters **********************
const { dev, port = 5120 } = parseArgs()
const { dev, port = 5121 } = parseArgs()
const watch = dev
const outDir = dev ? 'build_dev' : 'build'

Expand Down
23 changes: 23 additions & 0 deletions apps/jscad-web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,26 @@ Header set Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
```

If you are using github you should be fine, as gists and github pages have those CORS headers.

# using data url

you can use data url to pack the script into the url

- [example](https://jscad.app/#data:application/javascript;base64,bW9kdWxlLmV4cG9ydHM9ZnVuY3Rpb24gbWFpbigpe3JldHVybiByZXF1aXJlKCdAanNjYWQvbW9kZWxpbmcnKS5wcmltaXRpdmVzLnNwaGVyZSh7cmFkaXVzOiA0MH0pfQ==)

*NOTICE: utf8 encoding is assumed when converting bytes to string*

# using data url and gzip

you can also use gzip to minimize the length of the url.

- [example](https://jscad.app/#data:application/gzip;base64,H4sICN1FqGUAA3Rlc3QADcrBDkAwDADQu6/YjV3GxUUi8SuLFRXrpl1FIv6dd34xBT3AwZ0TFxkXpblgIhM9UmMfhqJMhuFUZGjqaZfZhzamAAfSWluXGSMWvECc5A3+9LAPqDKYvnvtW33S8ZutYgAAAA==)

*NOTICE: utf8 encoding is assumed when converting bytes to string*

# using github gists to share scripts

- To reference latest version, no commit hash should be in URL:
- max-age=300 for raw gist cannot be lowered
- publish change, wait for 5 minutes before sharing with ppls that gist was changed

30 changes: 16 additions & 14 deletions apps/jscad-web/examples/balloons.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @authors Z3 Dev, Simon Clark
*/

import * as jscad from '@jscad/modeling'
const jscad = require('@jscad/modeling')
const { subtract, union } = jscad.booleans
const { colorize, hexToRgb } = jscad.colors
const { extrudeFromSlices, extrudeLinear, slice } = jscad.extrusions
Expand All @@ -15,7 +15,7 @@ const { circle, ellipsoid } = jscad.primitives
const { vectorText } = jscad.text
const { translate, scale, rotateX, center } = jscad.transforms

export const getParameterDefinitions = () => [
const getParameterDefinitions = () => [
{ name: 'balloon', type: 'group', caption: 'Balloons' },
{ name: 'isBig', type: 'checkbox', checked: true, initial: '20', caption: 'Big?' },
{ name: 'color', type: 'color', initial: '#ffb431', caption: 'Color?' },
Expand All @@ -26,6 +26,19 @@ export const getParameterDefinitions = () => [
{ name: 'age', type: 'int', initial: 20, min: 1, max: 100, step: 1, caption: 'Age?' }
]

const main = (params) => {
// use the checkbox to determine the size of the sphere
params.bRadius = (params.isBig === true) ? 16 : 10
// use the color chooser to determine the color of the sphere
params.bColor = hexToRgb(params.color)

return [
createBalloons(params),
createSalutation(params.name),
createBirthDate(params.birthdate)
]
}

// Build text by creating the font strokes (2D), then extruding up (3D).
const text = (message, extrusionHeight, characterLineWidth) => {
if (message === undefined || message.length === 0) return []
Expand Down Expand Up @@ -108,15 +121,4 @@ const createBirthDate = (birthDate) => {
return birthDate3D
}

export const main = (params) => {
// use the checkbox to determine the size of the sphere
params.bRadius = (params.isBig === true) ? 16 : 10
// use the color chooser to determine the color of the sphere
params.bColor = hexToRgb(params.color)

return [
createBalloons(params),
createSalutation(params.name),
createBirthDate(params.birthdate)
]
}
module.exports = { main, getParameterDefinitions }
6 changes: 4 additions & 2 deletions apps/jscad-web/examples/extrusions.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
* Demonstrates the types of extrusions, and the variety of objects they can create.
*/

import * as jscad from '@jscad/modeling'
const jscad = require('@jscad/modeling')
const { bezier } = jscad.curves
const { circle, line, polygon, rectangle, roundedRectangle, star } = jscad.primitives
const { extrudeLinear, extrudeRotate, extrudeFromSlices, slice } = jscad.extrusions
const { translate } = jscad.transforms
const { expand } = jscad.expansions
const { mat4 } = jscad.maths

export const main = () => {
function main() {
const shapes = []

// rounded box
Expand Down Expand Up @@ -65,3 +65,5 @@ const extrudeBezier = (height) => {
}
}, squareSlice)
}

module.exports = { main }
8 changes: 5 additions & 3 deletions apps/jscad-web/examples/gear.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @authors Joost Nieuwenhuijse, Simon Clark
*/

import * as jscad from '@jscad/modeling'
const jscad = require('@jscad/modeling')
const { cylinder, polygon } = jscad.primitives
const { rotateZ } = jscad.transforms
const { extrudeLinear } = jscad.extrusions
Expand All @@ -12,7 +12,7 @@ const { vec2 } = jscad.maths
const { degToRad } = jscad.utils

// Here we define the user editable parameters:
export const getParameterDefinitions = () => [
const getParameterDefinitions = () => [
{ name: 'numTeeth', caption: 'Number of teeth:', type: 'int', initial: 10, min: 5, max: 20 },
{ name: 'circularPitch', caption: 'Circular pitch:', type: 'float', initial: 5 },
{ name: 'pressureAngle', caption: 'Pressure angle:', type: 'float', initial: 20 },
Expand All @@ -22,7 +22,7 @@ export const getParameterDefinitions = () => [
]

// Main entry point; here we construct our solid:
export const main = (params) => {
const main = (params) => {
let gear = involuteGear(
params.numTeeth,
params.circularPitch,
Expand Down Expand Up @@ -125,3 +125,5 @@ const involuteGear = (numTeeth, circularPitch, pressureAngle, clearance, thickne

return union(rootcircle, allTeeth)
}

module.exports = { main, getParameterDefinitions }
8 changes: 5 additions & 3 deletions apps/jscad-web/examples/hulls.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
* Demonstrates Hull and Hull Chain operations in 2D and 3D
*/

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

export const main = (params) => {
function main() {
const radius = 1.5
const segments = 16

Expand All @@ -29,3 +29,5 @@ export const main = (params) => {
colorize([0.2, 0.2, 1.0], translate([20, 0, 0], [hull(shapes2d), hull(shapes3d)]))
]
}

module.exports = { main }
6 changes: 4 additions & 2 deletions apps/jscad-web/examples/jscad.example.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as jscad from '@jscad/modeling'
const jscad = require('@jscad/modeling')
const { intersect, subtract } = jscad.booleans
const { colorize } = jscad.colors
const { cube, sphere } = jscad.primitives

export const main = () => {
function main() {
const outer = subtract(
cube({ size: 10 }),
sphere({ radius: 6.8 })
Expand All @@ -17,3 +17,5 @@ export const main = () => {
colorize([0.7, 0.7, 0.1], inner),
]
}

module.exports = { main }
50 changes: 50 additions & 0 deletions apps/jscad-web/examples/multipart.project.template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Demonstrates a multipart project with part select input.
*
* https://github.com/jscad/OpenJSCAD.org/discussions/1141
*/
const jscad = require('@jscad/modeling')
const { sphere, cube } = jscad.primitives
const { translate } = jscad.transforms

// all of the functions that generate parts will see the parameters without declaring them explicitly
const main = ({//@jscad-params
size = 10, // {type: 'slider'}
part,
}, getParams) => {

// UTILITY placeholder for part generator functions
const parts = {}

// CTRL+R in vscode works just fine
parts.Sample_Cube = () => cube({ size })

parts.Sample_Sphere = () => {
return sphere({ radius: size / 2 })
}

// parts can easily be combined
parts.Assembly = () => [
// jump to definition in vscode (ALT+click) works
parts.Sample_Cube(),
translate([size + 5, 0, 0], parts.Sample_Sphere()),
]

/*********************** UTILITY below is just utility code. do not change **************** */

// we were called by getParameterDefinitions so we need to provide list of parts
if(getParams === true){
const values = Object.keys(parts)
return {values, initial: values[0]}
}

// make sure we always call one of the functions
if(!parts[part]) part = Object.keys(parts)[0]
return parts[part]()
}

const getParameterDefinitions = () => [
{ name: 'part', caption: 'Part', type: 'choice', ...main({}, true)}
]

module.exports = {main, getParameterDefinitions}
7 changes: 4 additions & 3 deletions apps/jscad-web/examples/nuts-and-bolts.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
* Demonstrates advanced extrusion using slices to generate screw threads.
*/

import * as jscad from '@jscad/modeling'
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

export const getParameterDefinitions = () => [
const getParameterDefinitions = () => [
{ name: 'hexWidth', type: 'number', initial: 4, min: 0 },
{ name: 'hexHeight', type: 'number', initial: 3, min: 0 },
{ name: 'threadLength', type: 'number', initial: 12, min: 0 },
Expand All @@ -21,7 +21,7 @@ export const getParameterDefinitions = () => [
{ name: 'segments', type: 'int', initial: 16, min: 3 },
]

export const main = (params) => {
const main = (params) => {
return [
colorize([0.9, 0.6, 0.2], bolt(params)),
colorize([0.4, 0.4, 0.4], translate([15, 0, 0], nut(params)))
Expand Down Expand Up @@ -92,3 +92,4 @@ const angleDiff = (angle1, angle2) => {
return diff > Math.PI ? Math.PI * 2 - diff : diff
}

module.exports = { main, getParameterDefinitions }
8 changes: 5 additions & 3 deletions apps/jscad-web/examples/parameters.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
* Demonstrate all available parameter types
*/

import * as jscad from '@jscad/modeling'
const jscad = require('@jscad/modeling')
const { colorize, hexToRgb } = jscad.colors
const { sphere } = jscad.primitives

const values = [3, 4, 5, 6]
const captions = ['three', 'four', 'five', 'six']

export const getParameterDefinitions = () => [
const getParameterDefinitions = () => [
{ name: 'group1', type: 'group', caption: 'Group 1: Text Entry' },
{ name: 'text', type: 'text', initial: '', size: 20, maxLength: 20, caption: 'Plain Text:', placeholder: '20 characters' },
{ name: 'int', type: 'int', initial: 20, min: 1, max: 100, step: 1, caption: 'Integer:' },
Expand All @@ -30,6 +30,8 @@ export const getParameterDefinitions = () => [
{ name: 'checkbox2', type: 'checkbox', checked: false, caption: 'Optional Checkbox:' }
]

export const main = (params) => {
const main = (params) => {
return colorize(hexToRgb(params.color), sphere({ radius: params.slider }))
}

module.exports = { main, getParameterDefinitions }
6 changes: 4 additions & 2 deletions apps/jscad-web/examples/primitives.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
* Demonstrates the basics of a variety of 2D and 3D primitives
*/

import * as jscad from '@jscad/modeling'
const jscad = require('@jscad/modeling')
const { arc, circle, ellipse, line, polygon, rectangle, roundedRectangle, square, star } = jscad.primitives
const { cube, cuboid, cylinder, cylinderElliptic, ellipsoid, geodesicSphere, roundedCuboid, roundedCylinder, sphere, torus } = jscad.primitives
const { translate } = require('@jscad/modeling').transforms

export const main = () => {
function main() {
const shapes = [
arc({ center: [-1, -1], radius: 2, startAngle: 0, endAngle: (Math.PI / 2), makeTangent: false, segments: 32 }),
line([[1, 1], [-1, -1], [1, -1]]),
Expand Down Expand Up @@ -45,3 +45,5 @@ export const main = () => {
// Arrange primitives in a grid
return shapes.map((primitive, index) => translate([(index % 5 - 1) * 5, Math.floor(index / 5 - 1) * 5, 0], primitive))
}

module.exports = { main }
8 changes: 5 additions & 3 deletions apps/jscad-web/examples/slicer.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
* Slice a geometry object into layers
*/

import * as jscad from '@jscad/modeling'
const jscad = require('@jscad/modeling')
const { colorize } = jscad.colors
const { intersect } = jscad.booleans
const { extrudeLinear, project } = jscad.extrusions
const { measureBoundingBox } = jscad.measurements
const { cuboid, sphere } = jscad.primitives
const { translate } = jscad.transforms

export const getParameterDefinitions = () => [
const getParameterDefinitions = () => [
{ name: 'thicness', type: 'slider', initial: 0.75, min: 0.1, max: 2, step: 0.1, caption: 'Layer thickness:' },
]

export const main = ({ thicness }) => {
const main = ({ thicness }) => {
const obj = sphere({ radius: 6 })

// calculate bounding box
Expand Down Expand Up @@ -43,3 +43,5 @@ export const main = ({ thicness }) => {

return colorize([0.7, 0, 0.1], out)
}

module.exports = { main, getParameterDefinitions }
8 changes: 5 additions & 3 deletions apps/jscad-web/examples/text.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
* @authors Simon Clark
*/

import * as jscad from '@jscad/modeling'
const jscad = require('@jscad/modeling')
const { union } = jscad.booleans
const { extrudeLinear } = jscad.extrusions
const { hullChain } = jscad.hulls
const { circle, sphere } = jscad.primitives
const { vectorText } = jscad.text
const { scale, translate } = jscad.transforms

export const getParameterDefinitions = () => [
const getParameterDefinitions = () => [
{ name: 'outline_string', initial: 'Outline', type: 'text', caption: 'Outline Text', size: 30 },
{ name: 'flat_string', initial: 'Flat', type: 'text', caption: 'Flat Text', size: 30 },
{ name: 'round_string', initial: 'Round', type: 'text', caption: 'Round Text', size: 30 }
]

export const main = (params) => {
const main = (params) => {
const outlineText = buildOutlineText(params.outline_string, 2)
const flatText = buildFlatText(params.flat_string, 2, 2)
const roundText = buildRoundText(params.round_string, 2)
Expand Down Expand Up @@ -77,3 +77,5 @@ const buildRoundText = (message, p) => {
const message3D = union(lineSegments)
return translate([0, -35, 0], message3D)
}

module.exports = { main, getParameterDefinitions }
3 changes: 2 additions & 1 deletion apps/jscad-web/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,10 @@ const exportModel = async (format, extension) => {

const worker = new Worker('./build/bundle.worker.js')
const handlers = {
entities: ({ entities }) => {
entities: ({ entities, mainTime, convertTime}) => {
if (!(entities instanceof Array)) entities = [entities]
viewState.setModel((model = entities))
console.log('Main execution:', mainTime?.toFixed(2), ', jscad mesh -> gl:', convertTime?.toFixed(2))
setError(undefined)
},
}
Expand Down
1 change: 1 addition & 0 deletions apps/jscad-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@jscadui/worker": "*",
"codemirror": "^6.0.1",
"gl-matrix": "^3.4.0",
"fflate": "0.8.1",
"three": "^0.147.0"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit e09501a

Please sign in to comment.