dat.gui库
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import gsap from 'gsap';
import * as dat from 'dat.gui'
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75,window.innerWidth/window.innerHeight,0.1,1000);
camera.position.set(0,0,10);
scene.add(camera)
const cubeGeometry = new THREE.BoxGeometry(1,1,1);
const cubeMaterial = new THREE.MeshBasicMaterial({color:0xffff00});
const cube = new THREE.Mesh(cubeGeometry,cubeMaterial);
cube.rotation.set(Math.PI/4,0,0);
scene.add(cube);
const gui = new dat.GUI();
gui.add(cube.position,"x").min(0).max(5).step(0.1).name("移动x轴").onChange((value)=>{
console.log(value)
}).onFinishChange((value)=>{
console.log('完全停下来触发',value)
});
const params = {
color:"#ffff00",
fn:()=>{
gsap.to(cube.position,{x:5,duration:2,yoyo:true,repeat:-1})
}
}
gui.addColor(params,'color').onChange((value)=>{
cube.material.color.set(value);
})
gui.add(cube,"visible").name('是否显示');
gui.add(params,'fn').name('立方体运动');
var folder = gui.addFolder("设置立方体");
folder.add(cube.material,"wireframe");
folder.add(params,'fn').name('立方体运动');
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth,window.innerHeight);
document.body.appendChild(renderer.domElement);
const controls = new OrbitControls(camera,renderer.domElement);
controls.enableDamping = true;
const clock = new THREE.Clock()
window.addEventListener('dblclick',()=>{
const fullScreenElement = document.fullscreenElement;
if(!fullScreenElement){
renderer.domElement.requestFullscreen();
}else{
document.exitFullscreen();
}
})
function render(){
controls.update();
renderer.render(scene,camera);
requestAnimationFrame(render);
}
render();
window.addEventListener('resize',()=>{
camera.aspect = window.innerWidth/window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth,window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
})
几何体
for(let i=0;i<50;i++){
const geometry = new THREE.BufferGeometry();
const positionArr = new Float32Array(9);
for(let j=0;j<9;j++){
positionArr[j] = Math.random()*5
}
geometry.setAttribute('position',new THREE.BufferAttribute(positionArr,3));
let color = new THREE.Color(Math.random(),Math.random(),Math.random());
const material = new THREE.MeshBasicMaterial({color:color,transparent:true,opacity:0.5,});
const mesh = new THREE.Mesh(geometry,material)
scene.add(mesh)
}
初始材质与纹理
const textureLoader = new THREE.TextureLoader();
const doorColorTexture = textureLoader.load('./textures/mood.jpeg');
doorColorTexture.center.set(0.5,0.5);
doorColorTexture.repeat.set(2,3);
doorColorTexture.wrapS = THREE.RepeatWrapping;
doorColorTexture.wrapT = THREE.MirroredRepeatWrapping;
doorColorTexture.minFilter = THREE.NearestFilter;
doorColorTexture.magFilter = THREE.NearestFilter;
灯光与阴影
const light = new THREE.AmbientLight(0xffffff,0.5);
scene.add(light);
const directionalLight = new THREE.DirectionalLight(0xffffff,0.5);
directionalLight.position.set(10,10,10);
directionalLight.castShadow = true
directionalLight.shadow.radius = 20;
directionalLight.shadow.mapSize.set(2048,2048);
directionalLight.shadow.camera.near = 0.5;
directionalLight.shadow.camera.far = 500;
directionalLight.shadow.camera.top = 5;
directionalLight.shadow.camera.bottom = -5;
directionalLight.shadow.camera.left = -5;
directionalLight.shadow.camera.right = 5;
scene.add(directionalLight);
聚光灯
const light = new THREE.AmbientLight(0xffffff,0.5);
scene.add(light);
const spotLight = new THREE.SpotLight(0xffffff,0.5);
spotLight.position.set(5,5,5);
spotLight.castShadow = true
spotLight.shadow.radius = 20;
spotLight.shadow.mapSize.set(4096,4096);
spotLight.target = sphere;
spotLight.angle = Math.PI/6;
spotLight.distance = 0;
scene.add(spotLight);
点光源
const light = new THREE.AmbientLight(0xffffff,0.5);
scene.add(light);
const smallBall = new THREE.Mesh(
new THREE.SphereBufferGeometry(0.1,20,20),
new THREE.MeshBasicMaterial({color:0xff0000})
);
smallBall.position.set(2,2,2)
const pointLight = new THREE.PointLight(0xff0000,0.5);
pointLight.castShadow = true
pointLight.shadow.radius = 20;
pointLight.shadow.mapSize.set(512,512);
smallBall.add(pointLight);
scene.add(smallBall);