SOURCE

console 命令行工具 X clear

                    
>
console
// 1.创建场景 
const scene = new THREE.Scene();
// 给场景添加颜色
scene.background = new THREE.Color( 0xff0000 );
// 2.创建相机
const camera = new THREE.PerspectiveCamera(80, innerWidth / innerHeight, 1, 1000);
camera.position.set(0, 10, 20);
camera.lookAt(0, 0, 0);
scene.add(camera);

// 3.创建渲染器
const renderer = new THREE.WebGLRenderer();
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);
renderer.setClearColor(0x000000);

// 4. 添加物体
const geometry = new THREE.BoxGeometry(1, 1, 1); // 几何体

for (var i = 0; i < 100; i++) {
    const material = new THREE.MeshBasicMaterial({ color: 0xffaa00 }); // 材质
    material.color = new THREE.Color(Math.random(), Math.random(), Math.random())
    var cube = new THREE.Mesh(geometry, material);
    cube.position.set(Math.random() * 20 - 10, 3, Math.random() * 20 - 10)
    scene.add(cube);// 注意要把创建好的物体添加到场景中
}
// 5. 鼠标交互
const controls = new THREE.OrbitControls(camera, renderer.domElement);

const gui = new dat.GUI();
var palette = {
  color: '#FF0000',       // CSS string
};
gui.addColor(palette, 'color').name('CSS颜色值').onChange((val)=>{
    scene.background= new THREE.Color(val);
});

// 6.渲染 
function render() {
    requestAnimationFrame(render);
    renderer.render(scene, camera);
}
render()
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body style="margin:0;">
</body>
</html>

本项目引用的自定义外部资源