大家好,我是梦奇,今天和大家一起学习ThreeJs,早日实现ThreeJs的自由。首先,我们需要安装threejsnpmithree然后,从'three'导入threeimport*asTHREE;然后开始创建场景/相机/渲染器//定义场景varscene=newTHREE.Scene()//定义相机varcamera=newTHREE.PerspectiveCamera(75,window.innerHeight/window.innerWidth,0.01,10)//定义渲染器constrenderer=newTHREE.WebGLRenderer({antialias:true});renderer.setSize(window.innerWidth,window.innerHeight);将渲染器添加到页面document.body.appendChild(renderer.domElement);Set相机位置camera.position.set(0.3,0.3,0.5);添加一个自带三个的鼠标控件import{OrbitControls}from"three/examples/jsm/controls/OrbitControls";constcontrols=newOrbitControls(camera,renderer.domElement);添加一些环境光并将其加载到场景中constdirectionLight=newTHREE.DirectionalLight(0xffffff,0.4);scene.add(方向灯);加载我们的甜甜圈模型(我把它弄得很丑PS:不可能,绝对不可能,还是熊猫先生做的)newGLTFLoader().load('../resources/models/donuts.glb',(gltf)=>{scene.add(gltf.scene);donuts=gltf.scene;mixer=newTHREE.AnimationMixer(gltf.scene);constclips=gltf.animations;//播放所有动画clips.forEach(function(clip){constaction=mixer.clipAction(clip);action.loop=THREE.LoopOnce;//在最后一帧停止action.clampWhenFinished=true;action.play();});})加载环境纹理,使我们的背景不那么单调newRGBELoader().load('../resources/sky.hdr',function(texture){scene.background=texture;texture.mapping=THREE.EquirectangularReflectionMapping;scene.environment=texture;renderer.outputEncoding=THREE.sRGBEncoding;renderer.render(scene,camera);});最后,整个动画让甜甜圈移动functionanimate(){requestAnimationFrame(animate);renderer.render(场景,相机);控制.更新();如果(甜甜圈){donuts.rotation.y+=0.01;}if(mixer){mixer.update(0.02);}}动画();如下:最后加入源创影(v:dashhuailaoyuan)一起交流学习
