// template is blank
// define your own Three.js World here
function init() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
// renderer
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( width, height );
renderer.setPixelRatio( window.devicePixelRatio );
document.body.appendChild( renderer.domElement );
window.addEventListener( 'resize', onWindowResize );
document.body.addEventListener( 'mouseover', function () {
scene.traverse( function ( child ) {
if ( child.isMesh ) child.material.color.setHex( 0xaaaaff );
} );
} );
document.body.addEventListener( 'mouseout', function () {
scene.traverse( function ( child ) {
if ( child.isMesh ) child.material.color.setHex( 0xffffff );
} );
} );
// scene
scene = new THREE.Scene();
// camera
camera = new THREE.PerspectiveCamera( 40, aspect, 1, 30 );
camera.position.set( 0, 0, 18 );
}
function createObjects() {
const geometry = new THREE.SphereGeometry( 0.4, 32, 32 );
for ( let x = 0; x <= 10; x ++ ) {
for ( let y = 0; y <= 10; y ++ ) {
const material = new THREE.MeshPhysicalMaterial( {
roughness: x / 10,
metalness: y / 10,
color: 0xffffff,
envMap: radianceMap,
envMapIntensity: 1,
reflectivity: 1
} );
const mesh = new THREE.Mesh( geometry, material );
mesh.position.x = x - 5;
mesh.position.y = 5 - y;
scene.add( mesh );
}
}
}
function createEnvironment() {
return new Promise( function ( resolve ) {
const envScene = new THREE.Scene();
envScene.background = new THREE.Color( 0xcccccc );
const pmremGenerator = new THREE.PMREMGenerator( renderer );
radianceMap = pmremGenerator.fromScene( envScene ).texture;
pmremGenerator.dispose();
scene.background = radianceMap;
resolve();
} );
}
function onWindowResize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize( width, height );
}
function animate() {
renderer.setAnimationLoop( render );
}
function render() {
renderer.render( scene, camera );
}
Promise.resolve()
.then( init )
.then( createEnvironment )
.then( createObjects )
.then( animate );