创建 场景
const scene = new THREE.Scene()
创建 几何体
const geometry = new THREE.BoxGeometry(100, 100, 100)
设置几何体 材质
const material = new THREE.MeshLambertMaterial({
color: 0x00ffff,
transparent: true,
opacity: 0.5
})
创建网格模型
const mesh = new THREE.Mesh(geometry, material)
mesh.position.set(0, 0, 0)
往场景中添加网格模型
scene.add(mesh)
在场景添加辅助三维坐标
const axesHelper = new THREE.AxesHelper(130)
scene.add(axesHelper)
创建光源
const light = new THREE.PointLight(0xffffff, 1.5)
light.position.set(100, 100, 70)
scene.add(light)
const pointHelper = new THREE.PointLightHelper(light, 3, 0xff0000)
scene.add(pointHelper)
创建透视相机对象
const camera = new THREE.PerspectiveCamera(30, width / height, 0.1, 100000)
camera.position.set(200, 200, 200)
camera.lookAt(0, 0, 0)
设置 输出canvas画布大小
const width = window.innerWidth
const height = window.innerHeight
const renderer = new THREE.WebGLRenderer()
window.onresize = function () {
renderer.setSize(window.innerWidth, window.innerHeight)
camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix()
}
将场景渲染到画布当中
document.body.appendChild(renderer.domElement)