// shadedSphere1 (per-vertex) vs shadedSphere2 (per-fragment). // As coded in shadedSphere1.mjs / shadedSphere2.mjs, both use VERTEX normals // (each vertex's normal is its own position on the unit sphere). import { CompareApp } from './lib/CompareApp.mjs'; import { sphereScene } from './lib/scenes.mjs'; new CompareApp(document.getElementById('app'), { title: 'shadedSphere1 vs shadedSphere2: per-vertex vs per-fragment shading', summary: 'shadedSphere1 (left) evaluates the lighting equation once per vertex and interpolates the resulting colour. ' + 'shadedSphere2 (right) interpolates the normal, light and eye vectors and evaluates the equation at every pixel. ' + 'Both use vertex normals: each vertex carries the sphere’s own surface normal there, so the surface looks smooth.', scene: sphereScene({ normalMode: 'vertex' }), panes: [ { title: 'shadedSphere1', subtitle: 'vertex normals, per-vertex shading', sources: () => ({ vert: 'perVertexLighting.vert', frag: 'colorPassthrough.frag' }), }, { title: 'shadedSphere2', subtitle: 'vertex normals, per-fragment shading', sources: () => ({ vert: 'perFragmentLighting.vert', frag: 'perFragmentLighting.frag' }), }, ], notes: [ { heading: 'What to look for', html: `
This is the pair where the gap is easiest to see, because the normal changes smoothly across each triangle.
A note on names. The textbook README describes shadedSphere1/2 as using true normals and shadedSphere3/4 as using vertex normals. The code in your shadedSphere1.mjs and shadedSphere2.mjs sets each normal to the vertex position, which is a vertex normal, and 3/4 compute face normals with a cross product. These pages follow the code.
Another difference to know about. Your perFragmentLighting.frag uses the interpolated N, L and E as they arrive, without renormalising them. That saves three normalize calls per pixel (see the cost table), but interpolated unit vectors are slightly shorter than unit length between the vertices, so the right-hand picture differs a little from the left for that reason as well as for where the equation is evaluated. Subdivide more and that effect shrinks.