JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Three.js Lambert Shader Example</title>
<style>
body { margin: 0; }
canvas { display: block; }
</style>
</head>
<body>
</body>
</html>
JavaScript
import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.module.min.js';
const vertexShader = `
varying vec3 vNormal;
varying vec3 vViewPosition;
void main() {
vNormal = normalize( normalMatrix * normal );
vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
vViewPosition = -mvPosition.xyz;
gl_Position = projectionMatrix * mvPosition;
}
`;
const fragmentShader = `
uniform vec3 diffuse;
uniform vec3 ambientLightColor;
#if NUM_DIR_LIGHTS > 0
uniform vec3 directionalLightColor[NUM_DIR_LIGHTS];
uniform vec3 directionalLightDirection[NUM_DIR_LIGHTS];
#endif
varying vec3 vNormal;
varying vec3 vViewPosition;
void main() {
vec3 outgoingLight = vec3( 0.0 ); // outgoing light to the screen
vec4 diffuseColor = vec4( diffuse, 1.0 );
// Compute the lighting in the same way as Lambert material
vec3 normal = normalize( vNormal );
vec3 viewDirection = normalize( vViewPosition );
// Total ambient light
vec3 totalAmbientLight = ambientLightColor * diffuseColor.rgb;
// Directional light calculation
vec3 totalDirectionalLight = vec3( 0.0 );
#if NUM_DIR_LIGHTS > 0
for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {
vec3 lightDirection = normalize( directionalLightDirection[ i ] );
float dotNL = max( dot( normal, lightDirection ), 0.0 );
totalDirectionalLight += directionalLightColor[ i ] * dotNL;
}
#endif
vec3 totalLight = totalAmbientLight + totalDirectionalLight;
outgoingLight = diffuseColor.rgb * totalLight;
#ifdef USE_TRANSMISSION
diffuseColor.a *= material.transmissionAlpha;
#endif
gl_FragColor = vec4( outgoingLight, diffuseColor.a );
#if defined( TONE_MAPPING )
gl_FragColor.rgb = toneMapping( gl_FragColor.rgb );
#endif
gl_FragColor...