Skip to content

Commit 842680e

Browse files
committed
refactoring functional rendering methods into a class with the exporter. enabled setting the colorscheme of the thumbnails
1 parent a5d41e9 commit 842680e

File tree

14 files changed

+217
-460
lines changed

14 files changed

+217
-460
lines changed

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"scad"
1010
],
1111
"homepage": "https://www.npmjs.com/package/eleventy-plugin-scad",
12+
"readme": "https://github.com/kevinkhill/eleventy-plugin-scad#readme",
1213
"repository": {
1314
"type": "git",
1415
"url": "git+https://github.com/kevinkhill/eleventy-plugin-scad.git"
@@ -31,6 +32,7 @@
3132
],
3233
"scripts": {
3334
"build": "tsdown",
35+
"build:test": "pnpm clean:test && cd test/_11ty && eleventy --config=test.eleventy-config.js",
3436
"bump": "bumpp",
3537
"clean": "concurrently -c 'auto' 'npm:clean:*'",
3638
"clean:dist": "rm -rvf ./dist/*",
@@ -67,6 +69,7 @@
6769
"concurrently": "9.2.1",
6870
"rollup-plugin-copy": "3.5.0",
6971
"tempy": "^3.1.0",
72+
"three": "^0.182.0",
7073
"tsdown": "0.11.13",
7174
"tsx": "4.20.6",
7275
"typescript": "5.9.3",
@@ -75,6 +78,5 @@
7578
"peerDependencies": {
7679
"@11ty/eleventy": "^3"
7780
},
78-
"packageManager": "pnpm@10.22.0",
79-
"readme": "https://github.com/kevinkhill/eleventy-plugin-scad#readme"
81+
"packageManager": "pnpm@10.25.0"
8082
}

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/assets/scad.bloomRenderer.js

Lines changed: 0 additions & 142 deletions
This file was deleted.

src/assets/scad.renderer.js

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ let renderer,
2020
scene,
2121
controls;
2222

23+
const lights = { key: undefined, fill: undefined, rim: undefined };
24+
2325
function frameObject(object, camera, controls, fitOffset = 1.2) {
2426
const box = new THREE.Box3().setFromObject(object);
2527
const size = box.getSize(new THREE.Vector3());
@@ -96,15 +98,6 @@ function setupPostProcessing({ strength, radius, threshold }) {
9698
// composer.addPass(outputPass);
9799
}
98100

99-
/**
100-
* Setup initial camera position & orbit controls.
101-
*
102-
* @param {Object} options - Configuration object for the initial camera position.
103-
* @param {number} options.x - The x-coordinate of the camera position.
104-
* @param {number} options.y - The y-coordinate of the camera position.
105-
* @param {number} options.z - The z-coordinate of the camera position.
106-
* @param {number} options.fov - The field of view of the camera
107-
*/
108101
function setupCameraAndControls() {
109102
const aspect = container.clientWidth / container.clientHeight;
110103
const frustumSize = 100;
@@ -126,31 +119,23 @@ function setupCameraAndControls() {
126119
// controls.enableDamping = true;
127120
}
128121

129-
/**
130-
* Setup the initial spotlight position.
131-
*
132-
* @param {Object} options - Configuration object for the initial spotlight position.
133-
* @param {number} options.x - The x-coordinate of the spotlight position.
134-
* @param {number} options.y - The y-coordinate of the spotlight position.
135-
* @param {number} options.z - The z-coordinate of the spotlight position.
136-
*/
137-
function setupScene({ x, y, z }) {
122+
function setupScene() {
138123
scene = new THREE.Scene();
139124

140125
// Slight ambient so shadows aren’t pure black
141126
scene.add(new THREE.AmbientLight(0xffffff, 0.25));
142127

143-
const key = new THREE.DirectionalLight(0xffffff, 1.2);
144-
key.position.set(2, 3, 4);
145-
scene.add(key);
128+
lights.key = new THREE.DirectionalLight(0xffffff, 1.2);
129+
lights.key.position.set(2, 3, 4);
130+
scene.add(lights.key);
146131

147-
const fill = new THREE.DirectionalLight(0xffffff, 0.6);
148-
fill.position.set(-3, 1, 2);
149-
scene.add(fill);
132+
lights.fill = new THREE.DirectionalLight(0xffffff, 0.6);
133+
lights.fill.position.set(-3, 1, 2);
134+
scene.add(lights.fill);
150135

151-
const rim = new THREE.DirectionalLight(0xffffff, 0.9);
152-
rim.position.set(0, 3, -4);
153-
scene.add(rim);
136+
lights.rim = new THREE.DirectionalLight(0xffffff, 0.9);
137+
lights.rim.position.set(0, 3, -4);
138+
scene.add(lights.rim);
154139
}
155140

156141
/**
@@ -212,45 +197,34 @@ function animate() {
212197

213198
function setupGUI(initialState) {
214199
const gui = new GUI();
215-
// const cameraFolder = gui.addFolder("Camera");
216-
// const lightingFolder = gui.addFolder("Lighting");
217-
// const meshFolder = gui.addFolder("Mesh");
218200
const bloomFolder = gui.addFolder("Bloom");
219-
const { lightPos, bloomPass } = initialState;
220-
const set = (target, key) => (value) => { target[key] = Number(value); };
201+
const { bloomPass } = initialState;
221202

222203
// gui.onChange((event) => {
223204
// console.log("Saving: %O", gui.save());
224205
// });
225206

226-
// meshFolder.addColor(initialState.mesh, "color").onChange(console.log);
227-
// lightingFolder.add(lightPos, "x", -500, 500, 1).onChange(set(spotLight.position, "x"));
228-
// lightingFolder.add(lightPos, "y", -500, 500, 1).onChange(set(spotLight.position, "y"));
229-
// lightingFolder.add(lightPos, "z", -500, 500, 1).onChange(set(spotLight.position, "z"));
230-
231-
bloomFolder.add(bloomPass, "radius", 0, 1, 0.1).onChange(set(bloomPass, "radius"));
232-
bloomFolder.add(bloomPass, "strength", 0, 3, 0.1).onChange(set(bloomPass, "strength"));
233-
bloomFolder.add(bloomPass, "threshold", 0, 1, 0.1).onChange(set(bloomPass, "threshold"));
207+
bloomFolder.add(bloomPass, "radius", 0, 1, 0.1).onChange(v => {
208+
bloomPass.radius = Number(v);
209+
});
210+
bloomFolder.add(bloomPass, "strength", 0, 3, 0.1).onChange(v => {
211+
bloomPass.strength = Number(v);
212+
});
213+
bloomFolder.add(bloomPass, "threshold", 0, 1, 0.1).onChange(v => {
214+
bloomPass.threshold = Number(v);
215+
});
234216
}
235217

236218
function init() {
237219
const initialState = {
238-
// mesh: {
239-
// color: "#ab98fc"
240-
// },
241-
lightPos: {
242-
x: 20,
243-
y: 50,
244-
z: 200
245-
},
246220
bloomPass: {
247221
strength: 1,
248222
radius: .5,
249223
threshold: .5
250224
}
251225
};
252226
setupRenderer();
253-
setupScene(initialState.lightPos);
227+
setupScene();
254228
setupCameraAndControls();
255229
setupPostProcessing(initialState.bloomPass);
256230
setupGUI(initialState);
Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import path from "node:path";
22
import { bold, gray, green, red, reset } from "yoctocolors";
3-
import { cache, scadExporter } from "../core";
4-
import { DOT_SCAD, DOT_STL, PLUGIN_NAME, SCAD_EXT } from "../core/const";
53
import {
64
createScadLogger,
75
createSilentLogger,
86
Debug,
97
exists,
108
resolveOpenSCAD,
119
} from "../lib";
10+
import * as cache from "./cache";
11+
import { DOT_SCAD, DOT_STL, PLUGIN_NAME, SCAD_EXT } from "./const";
12+
import { OpenSCAD } from "./openscad";
1213
import type {
1314
EleventyConfig,
1415
EleventyDirs,
15-
Files,
1616
FullPageData,
1717
ParsedPluginOptions,
1818
ScadTemplateData,
@@ -107,25 +107,27 @@ export function addScadExtensionHandler(
107107
const stlExists = exists(outFile);
108108
const hashMatch = await cache.fileHashesMatch(inFile);
109109

110-
debug.extend("compile")("pre-generate: %O", {
111-
inFile,
112-
outFile,
113-
projectRoot,
114-
stlExists,
115-
hashMatch,
116-
});
117-
118110
if (!stlExists || !hashMatch) {
119111
_log(
120112
`${reset("Writing")} ${data.stlFile} ${gray(`from ${inputPath}`)}`,
121113
);
122114

123-
const exportResult = await scadExporter(String(resolvedScadBin), {
124-
cwd: projectRoot,
125-
in: inFile,
126-
out: outFile,
115+
debug.extend("compile")("pre-generate: %O", {
116+
inFile,
117+
outFile,
118+
projectRoot,
119+
stlExists,
120+
hashMatch,
127121
});
128122

123+
const openscad = new OpenSCAD(projectRoot);
124+
125+
openscad.setInput(inFile);
126+
openscad.setOutput(outFile);
127+
openscad.setColorScheme(opts.thumbnailColorScheme);
128+
129+
const exportResult = await openscad.export(resolvedScadBin);
130+
129131
if (!exportResult.ok) {
130132
log(red("OpenSCAD encountered an issue"));
131133
for (const line of exportResult.output) {

src/core/const.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export const SCAD_EXT = "scad";
1212

1313
export const DOT_SCAD = `.${SCAD_EXT}`;
1414

15+
export const SCAD_VIEWER_JS = "scad.renderer.js";
16+
1517
export const SCAD_VIEWER_LAYOUT = "scad.viewer.njk";
1618

1719
export const SCAD_COLLECTION_LAYOUT = "scad.collection.njk";

0 commit comments

Comments
 (0)