Skip to content

Commit 00529ed

Browse files
committed
Update examples and readme
1 parent dae8aa4 commit 00529ed

32 files changed

+960
-1225
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
.vscode
77
node_modules/
88
examples/3d-core/*.jpg
9+
examples/3d-core/*.png
910
*.log
1011
test/__diff__

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2024 Luis Blanco
3+
Copyright (c) 2025 Luis Blanco
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,42 +42,41 @@ This module exports 2 methods:
4242

4343
See [TypeScript defenitions](/index.d.ts) for more details.
4444

45-
Example (also see [here](/examples/crate-lean.js)):
45+
Example (as in [crate-lean.mjs](/examples/crate-lean.mjs)):
4646

4747
```javascript
48-
const three = require('three');
49-
const { init, addThreeHelpers } = require('3d-core-raub');
48+
import * as THREE from 'three';
49+
import node3d from '../index.js';
50+
const { init, addThreeHelpers } = node3d;
5051

51-
const { doc, gl, requestAnimationFrame } = init({ isGles3: true });
52-
addThreeHelpers(three, gl);
53-
54-
const renderer = new three.WebGLRenderer();
55-
renderer.setPixelRatio( doc.devicePixelRatio );
56-
renderer.setSize( doc.innerWidth, doc.innerHeight );
57-
58-
const camera = new three.PerspectiveCamera(70, doc.innerWidth / doc.innerHeight, 1, 1000);
59-
camera.position.z = 2;
60-
const scene = new three.Scene();
61-
62-
const geometry = new three.BoxGeometry();
63-
const material = new three.MeshBasicMaterial({ color: 0xFACE8D });
64-
const mesh = new three.Mesh( geometry, material );
65-
scene.add(mesh);
66-
67-
doc.addEventListener('resize', () => {
68-
camera.aspect = doc.innerWidth / doc.innerHeight;
69-
camera.updateProjectionMatrix();
70-
renderer.setSize(doc.innerWidth, doc.innerHeight);
52+
const { gl, loop, Screen } = init({
53+
isGles3: true, vsync: true, autoEsc: true, autoFullscreen: true, title: 'Crate',
7154
});
55+
addThreeHelpers(THREE, gl);
56+
const screen = new Screen({ three: THREE, fov: 70, z: 2 });
57+
58+
const texture = new THREE.TextureLoader().load('three/textures/crate.gif');
59+
texture.colorSpace = THREE.SRGBColorSpace;
60+
const geometry = new THREE.BoxGeometry();
61+
const material = new THREE.MeshBasicMaterial({ map: texture });
62+
const mesh = new THREE.Mesh(geometry, material);
63+
screen.scene.add(mesh);
7264

73-
const animate = () => {
74-
requestAnimationFrame(animate);
65+
loop(() => {
7566
const time = Date.now();
7667
mesh.rotation.x = time * 0.0005;
7768
mesh.rotation.y = time * 0.001;
78-
79-
renderer.render(scene, camera);
80-
};
81-
82-
animate();
69+
screen.draw();
70+
});
8371
```
72+
73+
Example Notes:
74+
75+
1. You can use **mjs**, **tsx** or commonjs with `require()`.
76+
1. `loop` is a convenience method, you can use `requestAnimationFrame` too.
77+
1. `autoFullscreen` option enables "CTRL+F", "CTRL+SHIFT+F", "CTRL+ALT+F" to switch
78+
window modes.
79+
1. `Screen` helps with **three.js**-oriented resource management, but is not required.
80+
1. **three.js** uses VAO, so if not using `Screen`, handling the window mode changes
81+
(creates a separate OpenGL context) is up to you. Basically, `doc.on('mode', () => {...})` -
82+
here you should [re-create THREE.WebGLRenderer](/js/objects/screen.js).

eslint.config.js

Lines changed: 1 addition & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,3 @@
11
'use strict';
22

3-
const js = require('@eslint/js');
4-
5-
6-
module.exports = [
7-
{
8-
ignores: ['examples/clouds']
9-
},
10-
js.configs.recommended,
11-
{
12-
languageOptions: {
13-
'parserOptions': {
14-
'ecmaVersion': 2022,
15-
sourceType: 'commonjs'
16-
},
17-
globals: {
18-
global: 'readonly',
19-
require: 'readonly',
20-
Buffer: 'readonly',
21-
module: 'readonly',
22-
console: 'readonly',
23-
__dirname: 'readonly',
24-
process: 'readonly',
25-
setTimeout: 'readonly',
26-
setImmediate: 'readonly',
27-
clearImmediate: 'readonly',
28-
URL: 'readonly',
29-
},
30-
},
31-
'rules': {
32-
'arrow-parens': ['error', 'always'],
33-
'no-trailing-spaces': [
34-
'error',
35-
{
36-
'skipBlankLines': true
37-
}
38-
],
39-
'indent': [
40-
'error',
41-
'tab',
42-
{
43-
'SwitchCase': 1
44-
}
45-
],
46-
'operator-linebreak': [
47-
'error',
48-
'after',
49-
{
50-
'overrides': {
51-
'?': 'before',
52-
':': 'before'
53-
}
54-
}
55-
],
56-
'max-len': ['error', 110],
57-
'quotes': [
58-
'error',
59-
'single'
60-
],
61-
'semi': [
62-
'error',
63-
'always'
64-
],
65-
'no-multiple-empty-lines': ['error', { 'max': 3, 'maxEOF': 1, 'maxBOF': 1 }],
66-
'keyword-spacing': ['error', { 'before': true, 'after': true }],
67-
'space-before-blocks': ['error'],
68-
'space-before-function-paren': [
69-
'error', {'anonymous': 'always', 'named': 'never', 'asyncArrow': 'always'}
70-
],
71-
'camelcase': ['error'],
72-
'no-tabs': [0],
73-
'no-unused-vars': [
74-
'error',
75-
{
76-
'argsIgnorePattern': '^_',
77-
'varsIgnorePattern': '^_',
78-
'caughtErrorsIgnorePattern': '^_'
79-
}
80-
],
81-
'global-require': [0],
82-
'no-underscore-dangle': [0],
83-
'no-plusplus': [0],
84-
'no-shadow': [0],
85-
'node/no-unpublished-require': [0],
86-
'no-process-exit': [0],
87-
'linebreak-style': [0],
88-
'node/no-missing-require': [0],
89-
'no-console': [0],
90-
'node/no-unsupported-features/es-builtins': 0,
91-
'node/no-unsupported-features/node-builtins': 0,
92-
'func-names': [
93-
'error',
94-
'never',
95-
{
96-
'generators': 'never'
97-
}
98-
]
99-
}
100-
},
101-
];
3+
module.exports = require('addon-tools-raub/utils/eslint-common');

examples/3d-core/brush.js

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

examples/3d-core/brush.mjs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as three from 'three';
2+
3+
import node3d from '../../index.js';
4+
const { init } = node3d;
5+
6+
7+
const { Screen, Brush, loop } = init({
8+
isGles3: true,
9+
isWebGL2: true,
10+
autoEsc: true,
11+
autoFullscreen: true,
12+
vsync: true,
13+
title: 'Brush',
14+
});
15+
16+
const screen = new Screen({ three });
17+
loop(() => screen.draw());
18+
19+
const brush = new Brush({ screen, color: 0x00FF00 });
20+
21+
screen.on('mousemove', (e) => brush.pos = [e.x, e.y]);
Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,22 @@
1-
'use strict';
1+
import * as three from 'three';
22

3-
const three = require('three');
4-
const { init } = require('../..');
3+
import node3d from '../../index.js';
4+
const { init } = node3d;
55

66

77
const { Screen, Lines, loop, gl } = init({
88
isGles3: true,
99
isWebGL2: true,
1010
autoEsc: true,
1111
autoFullscreen: true,
12+
vsync: true,
13+
title: 'Lines',
1214
});
1315

1416
const VBO_SIZE = 10;
1517

1618
const screen = new Screen({ three });
1719

18-
const F_KEY = 70;
19-
20-
screen.on('keydown', (e) => {
21-
if (e.keyCode === F_KEY && e.ctrlKey && e.shiftKey) {
22-
screen.mode = 'windowed';
23-
} else if (e.keyCode === F_KEY && e.ctrlKey && e.altKey) {
24-
screen.mode = 'fullscreen';
25-
} else if (e.keyCode === F_KEY && e.ctrlKey) {
26-
screen.mode = 'borderless';
27-
} else {
28-
return;
29-
}
30-
});
31-
32-
3320
loop(() => screen.draw());
3421

3522
screen.camera.position.z = 300;
Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,24 @@
1-
'use strict';
1+
import * as three from 'three';
22

3-
const three = require('three');
4-
const { init } = require('../..');
3+
import node3d from '../../index.js';
4+
const { init } = node3d;
55

66

77
const { Screen, loop, Image } = init({
88
isGles3: true,
99
isWebGL2: true,
1010
autoEsc: true,
1111
autoFullscreen: true,
12+
vsync: true,
13+
title: 'Mesh',
1214
});
1315

1416
const screen = new Screen({ three });
1517

16-
const F_KEY = 70;
17-
18-
screen.on('keydown', (e) => {
19-
if (e.keyCode === F_KEY && e.ctrlKey && e.shiftKey) {
20-
screen.mode = 'windowed';
21-
} else if (e.keyCode === F_KEY && e.ctrlKey && e.altKey) {
22-
screen.mode = 'fullscreen';
23-
} else if (e.keyCode === F_KEY && e.ctrlKey) {
24-
screen.mode = 'borderless';
25-
} else {
26-
return;
27-
}
28-
});
29-
30-
3118
const icon = new Image();
32-
icon.src = __dirname + '/crate.jpg';
19+
icon.src = 'crate.jpg';
3320
icon.on('load', () => { screen.icon = icon; });
3421

35-
screen.title = 'Mesh';
36-
37-
3822
const geometry = new three.IcosahedronGeometry(2, 1);
3923
const material = new three.MeshLambertMaterial({
4024
color: 0x888888 + Math.round((0xFFFFFF - 0x888888) * Math.random()),
@@ -57,5 +41,3 @@ loop(() => {
5741
mesh.rotation.z = Date.now() * 0.0007;
5842
screen.draw();
5943
});
60-
61-
module.exports = { screen };
Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,21 @@
1-
'use strict';
1+
import * as three from 'three';
22

3-
const three = require('three');
4-
const { init } = require('../..');
3+
import node3d from '../../index.js';
4+
const { init } = node3d;
55

66

77
const { Screen, Points, loop, gl } = init({
88
isGles3: true,
99
isWebGL2: true,
1010
autoEsc: true,
1111
autoFullscreen: true,
12+
vsync: true,
13+
points: 'Points',
1214
});
1315

14-
const F_KEY = 70;
15-
1616
const screen = new Screen({ three });
1717
loop(() => screen.draw());
1818

19-
screen.on('keydown', (e) => {
20-
if (e.keyCode === F_KEY && e.ctrlKey && e.shiftKey) {
21-
screen.mode = 'windowed';
22-
} else if (e.keyCode === F_KEY && e.ctrlKey && e.altKey) {
23-
screen.mode = 'fullscreen';
24-
} else if (e.keyCode === F_KEY && e.ctrlKey) {
25-
screen.mode = 'borderless';
26-
} else {
27-
return;
28-
}
29-
});
3019
screen.camera.position.z = 200;
3120

3221
const VBO_SIZE = 10000;

0 commit comments

Comments
 (0)