import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
var scene, camera, controls, renderer, ambient, directional,grid
var urls = [
'./static/skyBox/day/px.jpg',
'./static/skyBox/day/nx.jpg',
'./static/skyBox/day/py.jpg',
'./static/skyBox/day/ny.jpg',
'./static/skyBox/day/pz.jpg',
'./static/skyBox/day/nz.jpg'
function initScene() {
scene = new THREE.Scene()
scene.background = new THREE.Color( 0x000000 )
scene.background = new THREE.CubeTextureLoader().load(urls)
function initCamera() {
camera = new THREE.PerspectiveCamera(60,
document.getElementById('threecanvas').offsetWidth / document.getElementById('threecanvas').offsetHeight, 2, 5000)
camera.position.set(0, 100, -100)
camera.lookAt(new THREE.Vector3(0, 0, 0))
camera.aspect = document.getElementById('threecanvas').offsetWidth / document.getElementById('threecanvas').offsetHeight
camera.updateProjectionMatrix()
scene.add(camera)
function initRenderer() {
renderer = new THREE.WebGLRenderer({
alpha: false,
antialias: true,
logarithmicDepthBuffer: true,
precision: 'lowp',
preserveDrawingBuffer: true,
autoClear: true
renderer.setPixelRatio(window.devicePixelRatio)
renderer.setSize(document.getElementById('threecanvas').offsetWidth, document.getElementById('threecanvas').offsetHeight)
renderer.shadowMap.enabled = true
renderer.shadowMap.type = THREE.PCFSoftShadowMap
renderer.outputEncoding = THREE.sRGBEncoding
document.getElementById('threecanvas').appendChild(renderer.domElement)
function initControls() {
controls = new OrbitControls(camera, renderer.domElement)
controls.listenToKeyEvents(window)
controls.enableDamping = true
controls.dampingFactor = 0.05
controls.enableZoom = true
controls.minDistance = 2
controls.maxDistance = 1000
controls.enablePan = true
controls.maxPolarAngle = Math.PI / 2.05
controls.screenSpacePanning = false
controls.target = new THREE.Vector3(0, 0, 0)
function update() {
controls.update()
function initLight() {
ambient = new THREE.AmbientLight(0xffffff, 1)
ambient.name = '环境光'
scene.add(ambient)
directional = new THREE.DirectionalLight(0xffffff, 1.2, 100)
directional.name = '平行光'
directional.position.set(directionalx1, directionaly1, directionalz1)
directional.castShadow = true
directional.shadowDarkness = 1
directional.shadow.mapSize.width = 512 * 4
directional.shadow.mapSize.height = 512 * 4
directional.shadow.camera.left = -directionalvalue
directional.shadow.camera.right = directionalvalue
directional.shadow.camera.top = directionalvalue
directional.shadow.camera.bottom = -directionalvalue
directional.shadow.camera.near = 0.5
directional.shadow.camera.far = 1500
directional.shadow.bias = 0.0001
scene.add(directional)
const directionalhelper = new THREE.CameraHelper(directional.shadow.camera)
directionalhelper1.visible = true
scene.add(directionalhelper)
function initGrid(){
grid = new THREE.GridHelper( 300, 8, 0xffffff, 0xffffff );
grid.position.set(0,-5,0)
scene.add( grid );
function init() {
initScene()
initCamera()
initRenderer()
initControls()
initLight()
initGrid()
function render() {
update()
requestAnimationFrame(render)
renderer.render(scene, camera)
init()
render()
个人写的vue,分开function函数,原生HTML和vue都能用,包括小程序,可以单独把代码提取成js,使用export{…}导出属性或方法函数,直接在页面import引入即可。
正好今天又修改模型改动代码,重构的时候把代码扣出来,一边整合代码,一边写博客,可能时间有限,写的比较拉。
记录一下学习笔记
一年前从网上爬了个web端的三维demo,从此接触到了一个新的方向——webgl。第一次尝试自己写三维用的webgl,各种顶点信息、坐标定位,烦不胜烦。
后续又各种搜索引擎,cesiumjs、 babylonjs、unity、threejs等等,比较下来就选择了three。
说一下原因:
cesiumjs:注重地形,支持模型文件格式只有gltf。
babylonjs:比较针对游
上一篇主要写了如何创建一个场景,本篇写一下模型加载方面的细节。
目前threejs支持的模型格式有很多,gltf/glb、obj+mtl、fbx、dea等等…
主要推荐的模型格式:obj+mtl 或者 gltf/glb。
obj+mtl这种格式将模型信息、材质信息、贴图拆分开,相比较于其他来说,文件分散,分散后文件体积小,唯一的缺点是,文件套多了,注意命名,小心弄乱。
gltf呢,被称为模型界的JPEG,它可以在原有的基础上有损压缩(这部分放在后边),压缩后体积更小。
个人做了一下比较:同一个