First commit, sphere with individually painted faces

This commit is contained in:
Tyler Hallada 2019-02-03 04:02:39 -05:00
commit ec037d219a
6 changed files with 123 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
node_modules/
dist
package-lock.json

24
package.json Normal file
View File

@ -0,0 +1,24 @@
{
"name": "planet",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "webpack-dev-server --mode development --watch-poll --progress --hot",
"build": "webpack"
},
"author": "",
"license": "ISC",
"dependencies": {
"three": "^0.101.1",
"three-orbit-controls": "^82.1.0"
},
"devDependencies": {
"file-loader": "^3.0.1",
"html-webpack-plugin": "^3.2.0",
"webpack": "^4.29.0",
"webpack-cli": "^3.2.1",
"webpack-dev-server": "^3.1.14"
}
}

13
src/index.html Normal file
View File

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>My first three.js app</title>
<style>
html, body { margin: 0; padding: 0; overflow: hidden; }
canvas { width: 100%; height: 100%; }
</style>
</head>
<body>
</body>
</html>

51
src/index.js Normal file
View File

@ -0,0 +1,51 @@
import * as THREE from "three";
var OrbitControls = require("three-orbit-controls")(THREE);
const scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 1000);
var geometry = new THREE.IcosahedronBufferGeometry(5, 7);
var position = geometry.getAttribute("position");
var colors = [];
var color = new THREE.Color(Math.random(), Math.random(), Math.random());
for (var i = 0; i < position.count / 3; i += 1) {
color.setRGB(i / (position.count / 3), i / (position.count / 3), i / (position.count / 3));
colors.push(color.r, color.g, color.b);
colors.push(color.r, color.g, color.b);
colors.push(color.r, color.g, color.b);
}
function disposeArray() {
this.array = null;
}
geometry.addAttribute("color", new THREE.Float32BufferAttribute(colors, 3).onUpload(disposeArray));
var material = new THREE.MeshBasicMaterial({ vertexColors: THREE.FaceColors });
var sphere = new THREE.Mesh(geometry, material);
scene.add(sphere);
var controls = new OrbitControls(camera);
camera.position.z = 15;
controls.update()
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
window.addEventListener('resize', onWindowResize, false);
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
controls.update();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();

BIN
src/world.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 129 KiB

32
webpack.config.js Normal file
View File

@ -0,0 +1,32 @@
const path = require("path");
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry: "./src/index.js",
output: {
filename: "main.js",
path: path.resolve(__dirname, "dist")
},
module: {
rules: [
{
test: /\.(png|jpg|gif)$/,
use: [
{
loader: 'file-loader',
options: {},
},
],
},
],
},
plugins: [
new HtmlWebpackPlugin({ template: "src/index.html" })
],
devtool: "source-map",
devServer: {
watchOptions: {
ignored: /node_modules/
}
}
};