-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathparticle-grid.js
102 lines (82 loc) · 2.94 KB
/
particle-grid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"use strict"; // good practice - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode
////////////////////////////////////////////////////////////////////////////////
// Particle System
////////////////////////////////////////////////////////////////////////////////
/*global THREE, document, window, $*/
var path = ""; // STUDENT: set to "" to run on your computer, "/" for submitting code to Udacity
var camera, scene, renderer;
var cameraControls;
var clock = new THREE.Clock();
function fillScene() {
scene = new THREE.Scene();
var geometry = new THREE.Geometry();
// Student: rewrite the following vertex generation code so that
// vertices are generated every 100 units:
// -1000,-1000,-1000 to 1000,1000,1000, e.g.
// at -1000,-1000,-1000, -900,-1000,-1000,
// and so on, for the 21*21*21 = 9261 points.
for ( var i = 0; i < 8000; i ++ ) {
var vertex = new THREE.Vector3();
// accept the point only if it's in the sphere
do {
vertex.x = 2000 * Math.random() - 1000;
vertex.y = 2000 * Math.random() - 1000;
vertex.z = 2000 * Math.random() - 1000;
} while ( vertex.length() > 1000 );
geometry.vertices.push( vertex );
}
var disk = THREE.ImageUtils.loadTexture( path + 'media/img/cs291/disc.png' );
var material = new THREE.ParticleBasicMaterial(
{ size: 35, sizeAttenuation: false, map: disk, transparent: true } );
material.color.setHSL( 0.9, 0.2, 0.6 );
var particles = new THREE.ParticleSystem( geometry, material );
particles.sortParticles = true;
scene.add( particles );
}
function init() {
var canvasWidth = 846;
var canvasHeight = 494;
// For grading the window is fixed in size; here's general code:
//var canvasWidth = window.innerWidth;
//var canvasHeight = window.innerHeight;
var canvasRatio = canvasWidth / canvasHeight;
// RENDERER
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer = new THREE.WebGLRenderer( { clearAlpha: 1 } );
renderer.gammaInput = true;
renderer.gammaOutput = true;
renderer.setSize(canvasWidth, canvasHeight);
renderer.setClearColorHex( 0xAAAAAA, 1.0 );
// CAMERA
camera = new THREE.PerspectiveCamera( 55, canvasRatio, 2, 8000 );
camera.position.set( 10, 5, 15 );
// CONTROLS
cameraControls = new THREE.OrbitAndPanControls(camera, renderer.domElement);
cameraControls.target.set(0,0,0);
}
function addToDOM() {
var container = document.getElementById('container');
var canvas = container.getElementsByTagName('canvas');
if (canvas.length>0) {
container.removeChild(canvas[0]);
}
container.appendChild( renderer.domElement );
}
function animate() {
window.requestAnimationFrame(animate);
render();
}
function render() {
var delta = clock.getDelta();
cameraControls.update(delta);
renderer.render(scene, camera);
}
try {
init();
fillScene();
addToDOM();
animate();
} catch(e) {
var errorReport = "Your program encountered an unrecoverable error, can not draw on canvas. Error was:<br/><br/>";
$('#container').append(errorReport+e);
}