Skip to content

Commit 51c2b61

Browse files
committed
render basic cube lol
1 parent f71a7b6 commit 51c2b61

5 files changed

Lines changed: 234 additions & 19 deletions

File tree

‎package.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@
88
},
99
"devDependencies": {
1010
"clean-webpack-plugin": "^3.0.0",
11+
"css-loader": "^5.0.0",
1112
"file-loader": "^6.1.1",
1213
"html-webpack-plugin": "^4.5.0",
14+
"style-loader": "^2.0.0",
1315
"webpack": "^5.2.0",
1416
"webpack-cli": "^4.1.0",
1517
"webpack-dev-server": "^3.11.0"
18+
},
19+
"dependencies": {
20+
"three": "^0.121.1"
1621
}
1722
}

‎src/index.css‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
body { margin: 0; }
2+
canvas { display: block; }

‎src/index.js‎

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,87 @@
1111
* limitations under the License.
1212
*/
1313

14-
import rubiks from './rubiks.js';
15-
import rubiksModule from './rubiks.wasm';
14+
import rubiks from "./rubiks.js";
15+
import rubiksModule from "./rubiks.wasm";
16+
import * as THREE from "three";
17+
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
18+
19+
import "./index.css";
1620

1721
const promise = rubiks({
1822
locateFile(path) {
19-
if(path.endsWith('.wasm')) {
23+
if (path.endsWith(".wasm")) {
2024
return rubiksModule;
2125
}
2226
return path;
23-
}
27+
},
2428
});
2529

26-
promise.then(module => {
30+
promise.then((module) => {
2731
console.log(module);
2832
console.log(JSON.stringify(module._fib(12)));
2933
});
34+
35+
function init() {
36+
var scene = new THREE.Scene();
37+
var camera = new THREE.PerspectiveCamera(
38+
75,
39+
window.innerWidth / window.innerHeight,
40+
0.1,
41+
1000
42+
);
43+
44+
var renderer = new THREE.WebGLRenderer();
45+
renderer.setSize(window.innerWidth, window.innerHeight);
46+
document.body.appendChild(renderer.domElement);
47+
48+
const makeCube = (color) => {
49+
var geometry = new THREE.BoxGeometry();
50+
var material = new THREE.MeshBasicMaterial({ color });
51+
return new THREE.Mesh(geometry, material);
52+
};
53+
54+
const cube = new THREE.Group()
55+
for (var i = 0; i < 3; ++i) {
56+
for (var j = 0; j < 3; ++j) {
57+
for (var k = 0; k < 3; ++k) {
58+
const cube1 = makeCube( ((((i + 1) * 50) * 255) + ((j + 1) * 50)) * 255 + ((k + 1) * 50));
59+
cube1.position.set((i-1)*1.05, (j-1)*1.05, (k-1)*1.05)
60+
cube.add(cube1);
61+
}
62+
}
63+
}
64+
65+
scene.add(cube);
66+
67+
const controls = new OrbitControls(camera, renderer.domElement);
68+
controls.enableDamping = true
69+
controls.dampingFactor = 0.25
70+
controls.enableZoom = false
71+
72+
camera.position.z = 6;
73+
74+
75+
const onWindowResize = () => {
76+
camera.aspect = window.innerWidth / window.innerHeight;
77+
camera.updateProjectionMatrix();
78+
renderer.setSize( window.innerWidth, window.innerHeight );
79+
}
80+
81+
window.addEventListener( 'resize', onWindowResize, false );
82+
83+
var animate = function () {
84+
requestAnimationFrame(animate);
85+
86+
cube.rotation.x += 0.01;
87+
cube.rotation.y += 0.01;
88+
89+
renderer.render(scene, camera);
90+
};
91+
92+
animate();
93+
}
94+
95+
96+
97+
init();

‎webpack.config.js‎

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,47 @@
1313

1414
const webpack = require("webpack");
1515
const path = require("path");
16-
const HtmlWebpackPlugin = require('html-webpack-plugin');
17-
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
16+
const HtmlWebpackPlugin = require("html-webpack-plugin");
17+
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
1818

1919
module.exports = {
2020
context: path.resolve(__dirname, "."),
2121
entry: "./src/index.js",
2222
output: {
2323
path: path.resolve(__dirname, "dist"),
24-
filename: "bundle.js"
24+
filename: '[name].[contenthash:8].js',
2525
},
2626
plugins: [
2727
new CleanWebpackPlugin(),
28-
new HtmlWebpackPlugin({
29-
title: 'Output Management',
30-
}),
31-
],
28+
new HtmlWebpackPlugin({
29+
title: "Output Management",
30+
}),
31+
],
32+
optimization: {
33+
runtimeChunk: 'single',
34+
splitChunks: {
35+
cacheGroups: {
36+
vendor: {
37+
test: /[\\/]node_modules[\\/]/,
38+
name: 'vendors',
39+
chunks: 'all',
40+
},
41+
},
42+
},
43+
},
3244
module: {
3345
rules: [
3446
// wasm files should not be processed but just be emitted and we want
3547
// to have their public URL.
3648
{
37-
test: /rubiks\.wasm$/,
49+
test: /\.wasm$/,
3850
type: "javascript/auto",
3951
loader: "file-loader",
40-
}
41-
]
52+
},
53+
{ test: /\.css$/, use: ["style-loader", "css-loader"] },
54+
],
4255
},
4356
devServer: {
44-
contentBase: path.join(__dirname, 'dist')
45-
}
57+
contentBase: path.join(__dirname, "dist"),
58+
},
4659
};

0 commit comments

Comments
 (0)