赞
踩
const scene = new THREE.Scene(); //创建场景
各参数详情参考 threejs官方文档
// THREE.PerspectiveCamera创建一个透视相机对象
const camera = new THREE.PerspectiveCamera(45, width / height, 1, 1000);
camera.position.set(200, 200, 200);
构造器WebGLRenderer详细参数参考Threejs官方文档
const container = threeRef.current; //被渲染的元素
const width = container.clientWidth;
const height = container.clientHeight;
const renderer = new THREE.WebGLRenderer({
antialias: true, //开启锯齿
});
renderer.setSize(width, height); //设置宽高
renderer.setPixelRatio(window.devicePixelRatio);//设置设备像素比率,防止Canvas画布输出模糊。
//renderer.setClearColor('#af3', .5) //背景颜色,默认黑色
container.appendChild(renderer.domElement); //生成的渲染的实例,放到对应的dom容器里面
renderer.render(scene, camera); //执行渲染操作
// 正面光源
var directionalLight = new THREE.DirectionalLight(0xffffff, 0.6);
directionalLight.position.set(500, 500, 500);
scene.add(directionalLight);
// 背面光源
var directionalLight2 = new THREE.DirectionalLight(0xffffff, 0.6);
directionalLight2.position.set(-500, -500, -500);
scene.add(directionalLight2);
//环境光
var ambient = new THREE.AmbientLight(0xffffff, 0.6);
scene.add(ambient);
const axisHelper = new THREE.AxesHelper(200)
scene.add(axisHelper)//插入辅助线长度为200的坐标系
Orbitcontrols(轨道控制器)可以使得相机围绕目标进行轨道运动。
const controls = new OrbitControls(camera, container);
// controls.enableZoom = false; //禁用缩放
// controls.enableRotate = false; //禁用旋转
// controls.enablePan = false; //禁用平移
// controls.update();
const animate = function () {
requestAnimationFrame(animate);
renderer.render(scene, camera);
};
animate();
效果:
地球贴图earthImg:
// r:地球半径 function createSphereMesh(r) { // TextureLoader创建一个纹理加载器对象,可以加载图片作为纹理贴图 var textureLoader = new THREE.TextureLoader(); var texture = textureLoader.load(earthImg);//加载纹理贴图 var geometry = new THREE.SphereBufferGeometry(r, 40, 40); //材质对象Material var material = new THREE.MeshLambertMaterial({ map: texture,//设置地球贴图 }); var mesh = new THREE.Mesh(geometry, material); //网格模型对象Mesh return mesh } // 创建一个地球总对象earthGroup function createEarth(R) { var earthGroup = new THREE.Group();//地球组对象 earthGroup.add(createSphereMesh(R));//球体Mesh插入earthGroup中 }); return earthGroup; } var earth = createEarth(R);// 创建地球 scene.add(earth) //添加地球模型到场景中
效果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。