Advanced JSCAD Features

Master advanced JSCAD techniques for complex 3D modeling

Boolean Operations

Boolean operations allow you to combine, subtract, and intersect geometries to create complex shapes.

Union (Combine)

Combines multiple geometries into a single object.

const { union } = jscad.booleans;
const { cube, sphere } = jscad.primitives;

const combined = union(
  cube({ size: 10 }),
  sphere({ radius: 5, center: [5, 0, 0] })
);

Subtract (Difference)

Removes one geometry from another.

const { subtract } = jscad.booleans;
const { cube, sphere } = jscad.primitives;

const subtracted = subtract(
  cube({ size: 10 }),
  sphere({ radius: 3, center: [0, 0, 0] })
);

Intersect

Keeps only the overlapping parts of geometries.

const { intersect } = jscad.booleans;
const { cube, sphere } = jscad.primitives;

const intersected = intersect(
  cube({ size: 8 }),
  sphere({ radius: 6, center: [0, 0, 0] })
);

💡 Pro Tip

Boolean operations work best with closed, watertight geometries. Ensure your shapes are properly constructed before applying operations.

Scene Management

Organize complex scenes with multiple objects, transformations, and hierarchical structures.

Multiple Objects

Return arrays of objects for complex scenes.

function main() {
  const { cube, sphere, cylinder } = jscad.primitives;
  const { translate, rotate } = jscad.transforms;
  
  const base = cube({ size: 20 });
  const top = translate([0, 0, 10], sphere({ radius: 5 }));
  const handle = translate([0, 0, 15], cylinder({ radius: 1, height: 10 }));
  
  return [base, top, handle];
}

Transformations

Apply complex transformations to create dynamic scenes.

const { translate, rotate, scale, mirror } = jscad.transforms;

// Create a pattern
const pattern = [];
for (let i = 0; i < 5; i++) {
  const rotated = rotate([0, 0, i * 72], cube({ size: 2 }));
  const translated = translate([i * 3, 0, 0], rotated);
  pattern.push(translated);
}

return pattern;

Color and Materials

Add visual properties to your objects.

function main() {
  const { cube, sphere } = jscad.primitives;
  const { translate } = jscad.transforms;
  
  const redCube = {
    geometry: cube({ size: 5 }),
    color: [1, 0, 0, 1] // Red with full opacity
  };
  
  const blueSphere = {
    geometry: translate([8, 0, 0], sphere({ radius: 3 })),
    color: [0, 0, 1, 0.8] // Blue with 80% opacity
  };
  
  return [redCube, blueSphere];
}

AI Integration

Leverage AI-powered features to enhance your JSCAD workflow.

AI Code Generation

Use natural language to generate JSCAD code through the integrated AI chat.

// Example AI prompt:
"Create a parametric gear with 20 teeth, 5mm thickness, 
and 10mm pitch diameter using JSCAD"

// AI will generate code like:
function main(params) {
  const { cylinder } = jscad.primitives;
  const { rotate, translate } = jscad.transforms;
  
  const teeth = params.teeth || 20;
  const thickness = params.thickness || 5;
  const pitchDiameter = params.pitchDiameter || 10;
  
  // Generated gear logic...
}

Smart Parameter Suggestions

AI analyzes your code and suggests optimal parameter ranges and types.

// AI-enhanced parameter definitions
function getParameterDefinitions() {
  return [
    {
      name: 'radius',
      type: 'float',
      initial: 10,
      min: 1,    // AI-suggested minimum
      max: 50,   // AI-suggested maximum
      step: 0.5, // AI-suggested step size
      caption: 'Radius of the object'
    }
  ];
}

Code Optimization

AI can analyze and optimize your JSCAD code for better performance.

// Before optimization (inefficient)
function main() {
  const objects = [];
  for (let i = 0; i < 1000; i++) {
    objects.push(cube({ size: 1 }));
  }
  return objects;
}

// After AI optimization
function main() {
  const { union } = jscad.booleans;
  const { cube } = jscad.primitives;
  const { translate } = jscad.transforms;
  
  // Use instancing for better performance
  const baseCube = cube({ size: 1 });
  const instances = [];
  
  for (let i = 0; i < 1000; i++) {
    instances.push(translate([i, 0, 0], baseCube));
  }
  
  return union(instances);
}

Performance Optimization

Optimize your JSCAD code for better rendering performance and faster compilation.

Geometry Optimization

  • • Use appropriate primitive resolution
  • • Combine similar objects with union operations
  • • Avoid unnecessary transformations
  • • Cache frequently used geometries

Memory Management

// Good: Reuse geometries
function main() {
  const baseGeometry = cube({ size: 1 });
  const instances = [];
  
  for (let i = 0; i < 100; i++) {
    instances.push(translate([i, 0, 0], baseGeometry));
  }
  
  return union(instances);
}

// Avoid: Creating new geometries in loops
function main() {
  const instances = [];
  
  for (let i = 0; i < 100; i++) {
    instances.push(cube({ size: 1 })); // Creates new geometry each time
  }
  
  return union(instances);
}

Lazy Evaluation

Use conditional logic to avoid unnecessary computations.

function main(params) {
  const { cube, sphere } = jscad.primitives;
  
  // Only create complex geometry when needed
  if (params.showComplex) {
    return complexGeometry(params);
  } else {
    return simpleGeometry(params);
  }
}