1.依赖文件
1.JS文件 three.js OBJLoader.js
2.obj文件 flower.obj
依赖文件下载
2.代码部分
<!DOCTYPE html>
<html>
<head>
<title>3D演示</title>
</head>
<body>
<script src="depency/three.js"></script>
<script src="depency/OBJLoader.js"></script>
<script type="text/javascript">
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 10;
scene.add(camera);
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
var light = new THREE.DirectionalLight(0xffffff);
light.position.set(20, 10, 5);
scene.add(light);
var objLoader = new THREE.OBJLoader();
objLoader.setPath('model/');
objLoader.load('flower.obj', function(object) {
scene.add(object);
renderer.render( scene, camera );
});
</script>
</body>
</html>
3.其他注意点
浏览器无法直接访问本地obj文件,解决方案有两个
1.谷歌浏览器的快捷方式里添加如下内容
[注意加空格]--user-data-dir="c:\ChromeDebug" --test-type --disable-web-security
2.vsCode编辑器中的应用商店搜索liveServer,可以快捷运行一个服务器
|