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>Ball and Jewel</h1>

<p>These two objects are the same geometry and the same material and lighting. The
  only difference is that one uses smooth shading (interpolating colors
  across each face) and the other uses flat shading (one uniform color per face).
  The yellow spikes show the face normals.</p>

<h2>Parameters</h2>

<p>The <q>detail</q> parameter is how much detail in the underlying sphere
  geometry. The more detail, the more vertices in each.</p>

<p>The <q>computeVertexNormals</q> checkbox is whether to invoke the
  Three.js <code>computeVertexNormals()</code> method on the ball
  geometry. That method averages the face normals to figure out the vertex
  normals, which is appropriate for smooth objects. It doesn't seem to
  have much effect, though, unless the detail is very small. Interpolating
  color is more powerful than interpolating normals.</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

              
                var renderer = new THREE.WebGLRenderer();

var scene = new THREE.Scene();

TW.mainInit(renderer,scene);
                        
var params = {
    detail: 8,
    wireframe: false,
    computeVertexNormals: true,
};

params.ambientLightOn = true;
params.upperLeftLightOn = false;
params.overheadLightOn = true;

var light0, light1, light2;

function makeLights() {
    // we're using globals for the lights, for the GUI

    light0 = new THREE.AmbientLight( 0x202020 ); // 10%
    scene.add(light0);

    light1 = new THREE.PointLight( TW.WHITE, 0.5 ); // 50%
    light1.position.set( -12, 15, 10 );
    scene.add(light1);

    light2 = new THREE.DirectionalLight( TW.WHITE, 0.3 );
    light2.position.set( 0, 100, 10 );
    scene.add(light2);
}
makeLights();
    
var light1helper, light2helper;

function addLightHelpers() {
    light1helper = new THREE.PointLightHelper(light1, 0.2);
    scene.add(light1helper);
    light2helper = new THREE.DirectionalLightHelper(light2, 1);
    scene.add(light2helper);
}
addLightHelpers();

function updateLightsFromParams() {
    // in case we are re-making the lights, remove the old ones
    scene.remove(light0);
    scene.remove(light1);
    scene.remove(light2);
    scene.remove(light1helper);
    scene.remove(light2helper);

    // add back in the lights that we want
    if( params.ambientLightOn ) {
        scene.add(light0);
    }
    if( params.upperLeftLightOn ) {
        scene.add(light1);
        scene.add(light1helper);
    }
    if( params.overheadLightOn ) {
        scene.add(light2);
        scene.add(light2helper);
    }
}

updateLightsFromParams();

// ================================================================
// build the two objects

var ballMaterial = new THREE.MeshPhongMaterial(
    {color: THREE.ColorKeywords.cornflowerblue,
     specular: 0xCCCCCC,
     shading: THREE.SmoothShading });

var jewelMaterial = new THREE.MeshPhongMaterial(
    {color: THREE.ColorKeywords.cornflowerblue,
     specular: 0xCCCCCC,
     shading: THREE.FlatShading });

var radius = 10, geom, ball, jewel, ballhelper, jewelhelper;

function buildObjects() {
    scene.remove(ball);
    scene.remove(jewel);
    ballgeom  = new THREE.SphereGeometry( radius, params.detail, params.detail );
    if( params.computeVertexNormals )
        ballgeom.computeVertexNormals();
    jewelgeom  = new THREE.SphereGeometry( radius, params.detail, params.detail );
    ball  = new THREE.Mesh( ballgeom, ballMaterial );
    jewel = new THREE.Mesh( jewelgeom, jewelMaterial );
    ball.position.set(-2*radius,0,0);
    jewel.position.set(2*radius,0,0);
    scene.add(ball);
    scene.add(jewel);

    scene.remove(ballhelper);
    scene.remove(jewelhelper);
    ballhelper = new THREE.FaceNormalsHelper(ball);
    scene.add(ballhelper);
    jewelhelper = new THREE.FaceNormalsHelper(jewel);
    scene.add(jewelhelper);
}

buildObjects();

var state = TW.cameraSetup(renderer,
               scene,
               {minx: -2*radius, maxx: 2*radius,
                miny: -1*radius, maxy: 1*radius,
                minz: -1*radius, maxz: 1*radius,
               });
TW.toggleAxes("show");
TW.viewFromFront();

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

function rebuild() {
    buildObjects();
    TW.render();
}

// ================================================================
var gui = new dat.GUI();
gui.add(params, 'detail',3,30).step(1).onChange(rebuild);
gui.add(params, 'wireframe').onChange(function () {
    ballMaterial.wireframe=params.wireframe;
    jewelMaterial.wireframe=params.wireframe;
    TW.render();
});
gui.add(params, 'computeVertexNormals').onChange(rebuild);

              
            
!
999px

Console