Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                  <h1>Luxo Bulb Solution</h1>
  
  <p></p>

              
            
!

CSS

              
                body {
  max-width: 100%;
}
/* feel free to style the canvas any way you want. If you want it to
      use the entire window, set width: 100% and height: 100%. */

canvas {
  width: 80%;
  height: 500px;
  display: block;
  margin: 10px auto;
}

              
            
!

JS

              
                // create a scene
var scene = new THREE.Scene();

// ====================================================================

// Phong materials created for the lamp parts and rug

var colorMaterials = [new THREE.MeshPhongMaterial( {color: new THREE.Color("black"),
                                                    specular: new THREE.Color("white"),
                                                    shininess: 10,
                                                    shading: THREE.SmoothShading,
                                                    side: THREE.DoubleSide} ), 
                      new THREE.MeshPhongMaterial( {color: new THREE.Color("brown"),
                                                    specular: new THREE.Color("white"),
                                                    shininess: 10,
                                                    shading: THREE.SmoothShading} ),
                      new THREE.MeshPhongMaterial( {color: new THREE.Color("green"),
                                                    specular: new THREE.Color("white"),
                                                    shininess: 10,
                                                    shading: THREE.SmoothShading,
                                                    side: THREE.DoubleSide} ) ];

// the luxo() function creates a container object, adds the base, arms, cone,
// and bulb to this container, and returns the object

function luxo () {
    // create container object
    var lamp = new THREE.Object3D();

    // brown base
    var baseMesh = new THREE.Mesh(new THREE.CylinderGeometry(40,40,8,16),
                                  colorMaterials[1]);
    baseMesh.position.set(0,4,0);
    lamp.add(baseMesh);

    // lower arm
    var armMesh1 = new THREE.Mesh(new THREE.CylinderGeometry(4,4,100), 
                                  colorMaterials[0]);
    armMesh1.position.set(-35,40,0);
    armMesh1.rotation.set(0,0,Math.PI/4);
    lamp.add(armMesh1);

    // upper arm
    var armMesh2 = new THREE.Mesh(new THREE.CylinderGeometry(4,4,100), 
                                  colorMaterials[0]);
    armMesh2.position.set(-30,110,0);
    armMesh2.rotation.set(0,0,-Math.PI/3.5);
    lamp.add(armMesh2);

    // black cone
    var coneObj = new THREE.Object3D();
    var coneMesh = new THREE.Mesh(new THREE.ConeGeometry(40,80,16,1,true), 
                                  colorMaterials[0]);
    coneMesh.position.set(0,-40,0);
    coneObj.add(coneMesh);
    coneObj.position.set(0,165,0);
    coneObj.rotation.set(0,0,Math.PI/4);
    lamp.add(coneObj);

    // lightbulb
    var bulbMesh = new THREE.Mesh(new THREE.SphereGeometry(10), 
                                  new THREE.MeshBasicMaterial({color: new THREE.Color("white")} ));
    bulbMesh.position.set(20,140,0);
    lamp.add(bulbMesh);

    return lamp;
}

// addLuxoBulb() adds a spotlight to the scene to simulate the light radiating 
// from the bulb of a luxo lamp - it's special-purpose, assuming that the lamp is
// positioned on the rug at (X,Z) coordinates (posX,posZ), is upright and rotated 
// by angleY around the Y axis, and has uniform scale

function addLuxoBulb (angleY, scale, posX, posZ) {
    var spotAngle = Math.atan(0.5);   // true angle for cone with radius 40, height 80
    var bulb = new THREE.SpotLight(new THREE.Color("white"), 1.5, 0, spotAngle, 1, 1);
    // height of spotlight and distance from origin of lamp to target on ground
    var dist = 165*scale;
    bulb.position.set(posX, dist, posZ);     // spotlight positioned at tip of cone
    var bulbTarget = new THREE.Object3D();
    // target positioned on ground, at distance of "dist" from origin of lamp
    bulbTarget.position.set(posX+(dist*Math.cos(angleY)), 0, posZ-(dist*Math.sin(angleY)));
    bulb.target = bulbTarget;
    scene.add(bulb);
    scene.add(bulb.target);
}

// add a rug to the scene
var rug = new THREE.Mesh(new THREE.PlaneGeometry(200,140), colorMaterials[2]);
rug.position.set(100,0,70);
rug.rotation.set(-Math.PI/2,0,0);
scene.add(rug);

// create two luxo lamps with the desired position, rotation, and scale,
// and with a spotlight for the bulb added to the scene

var lamp1 = luxo();
lamp1.position.set(60,0,60);
scene.add(lamp1);
addLuxoBulb(0,1,60,60);

var lamp2 = luxo();
lamp2.position.set(160,0,60);
lamp2.rotation.set(0,Math.PI,0);
lamp2.scale.set(0.5,0.5,0.5);
scene.add(lamp2);
addLuxoBulb(Math.PI,0.5,160,60);

// add ambient light source

var ambLight = new THREE.AmbientLight(new THREE.Color("white"), 0.8);
scene.add(ambLight);

// ====================================================================

var renderer = new THREE.WebGLRenderer();

TW.mainInit(renderer,scene);

TW.cameraSetup(renderer,
               scene,
               {minx: 0, maxx: 200,
                miny: 0, maxy: 180,
                minz: 0, maxz: 140});

              
            
!
999px

Console