Skip to content

Instantly share code, notes, and snippets.

@bonau
Created December 27, 2012 09:48
Show Gist options
  • Select an option

  • Save bonau/4386976 to your computer and use it in GitHub Desktop.

Select an option

Save bonau/4386976 to your computer and use it in GitHub Desktop.
A CodePen by bonau.
<div id="container"></div>
/*
sample code refered to:
Getting started with three.js
http://www.aerotwist.com/tutorials/getting-started-with-three-js/
*/
/* Section 3. SET THE SCENE */
// set the scene size
var WIDTH = 400,
HEIGHT = 300;
// set some camera attributes
var VIEW_ANGLE = 45,
ASPECT = WIDTH / HEIGHT,
NEAR = 0.1,
FAR = 10000;
// get the DOM element to attach to
// - assume we've got jQuery to hand
var $container = $('#container');
// create a WebGL renderer, camera
// and a scene
var renderer = new THREE.WebGLRenderer();
var camera =
new THREE.PerspectiveCamera(
VIEW_ANGLE,
ASPECT,
NEAR,
FAR);
var scene = new THREE.Scene();
// add the camera to the scene
scene.add(camera);
// the camera starts at 0,0,0
// so pull it back
camera.position.z = 300;
// start the renderer
renderer.setSize(WIDTH, HEIGHT);
// attach the render-supplied DOM element
$container.append(renderer.domElement);
/* Section 4. MAKING A MESH */
// set up the sphere vars
var radius = 50,
segments = 16,
rings = 16;
// create a new mesh with
// sphere geometry - we will cover
// the sphereMaterial next!
var sphere = new THREE.Mesh(
new THREE.SphereGeometry(
radius,
segments,
rings),
sphereMaterial);
// add the sphere to the scene
scene.add(sphere);
/* Section 5. MATERIALS */
// create the sphere's material
var sphereMaterial =
new THREE.MeshLambertMaterial(
{
color: 0xCC0000
});
/* Section 6. LIGHTS! */
// create a point light
var pointLight =
new THREE.PointLight(0xFFFFFF);
// set its position
pointLight.position.x = 10;
pointLight.position.y = 50;
pointLight.position.z = 130;
// add to the scene
scene.add(pointLight);
/* Section 7. RENDER LOOP */
// draw!
renderer.render(scene, camera);
/* Section 8. COMMON OBJECT PROPERTIES */
// sphere geometry
sphere.geometry
// which contains the vertices and faces
sphere.geometry.vertices // an array
sphere.geometry.faces // also an array
// its position
sphere.position // contains x, y and z
sphere.rotation // same
sphere.scale // ... same
/* Section 9. DIRTY LITTLE SECRETS */
// set the geometry to dynamic
// so that it allow updates
sphere.geometry.dynamic = true;
// changes to the vertices
sphere.geometry.__dirtyVertices = true;
// changes to the normals
sphere.geometry.__dirtyNormals = true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment