<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>
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;
}
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);

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://s3.amazonaws.com/m.mr-pc.org/t/cisc3620/2020sp/three.min.js
  2. https://s3.amazonaws.com/m.mr-pc.org/t/cisc3620/2020sp/OrbitControls.js
  3. https://s3.amazonaws.com/m.mr-pc.org/t/cisc3620/2020sp/tw.js
  4. https://s3.amazonaws.com/m.mr-pc.org/t/cisc3620/2020sp/dat.gui.min.js