Three.js是JavaScript编写的WebGL第三方库。提供了非常多的3D显示功能。
项目初始化
- 【详情】使用可视化面板创建项目
vue_threejs_demo
- 【预设】选择手动配置
- 【功能】选择
Babel
、Router
、VueX
、CSS Pre-processors
- 【配置】版本选择
vue2.x
;选择history router
;Css预处理语言选择Less
- 创建项目,不保存为预设,等待npm包安装完成。
- 安装依赖,选择运行依赖,搜索three,点击安装
- 运行并启动项目
第一个Three.js案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| <template> <div> <div id="container"></div> </div> </template>
<script> import * as Three from "three";
export default { name: "ThreeTest", data() { return { camera: null, scene: null, renderer: null, mesh: null, }; }, methods: { init() { let container = document.getElementById("container");
this.camera = new Three.PerspectiveCamera( 70, container.clientWidth / container.clientHeight, 0.01, 1000 ); this.camera.position.z = 0.6;
this.scene = new Three.Scene();
let geometry = new Three.CylinderBufferGeometry(0.2, 0.2, 0.2); let material = new Three.MeshNormalMaterial(); this.mesh = new Three.Mesh(geometry, material); this.scene.add(this.mesh);
this.renderer = new Three.WebGLRenderer({ antialias: true }); this.renderer.setSize(container.clientWidth, container.clientHeight); container.appendChild(this.renderer.domElement); }, animate() { requestAnimationFrame(this.animate); this.mesh.rotation.x += 0.01; this.mesh.rotation.y += 0.02; this.renderer.render(this.scene, this.camera); }, }, mounted() { this.init(); this.animate(); }, }; </script>
<style lang="less" scoped> #container { height: 400px; } </style>
|
【参考内容】:
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 百里飞洋! 技术内容: 若存在错误或不当之处,还望兄台不吝赐教,期待与您交流!