当前位置:   article > 正文

Uniapp中three.js初使用(加载fbx或者glb)_uniapp three.js

uniapp three.js
  1. 安装three包,我选择的0.149.0版本 
     
    npm install --save three@0.149.0

  2. 页面引入three.js

    1. import * as THREE from 'three'
    2. import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls.js'
    3. import {GLTFLoader} from 'three/examples/jsm/loaders/GLTFLoader.js';
    4. import {FBXLoader} from 'three/examples/jsm/loaders/FBXLoader.js';

  3. 创建three.js场景

    1. init() {
    2. // 创建一个场景
    3. this.scene = new THREE.Scene()
    4. //三位坐标线
    5. // const axesHelper = new THREE.AxesHelper(5);
    6. // this.scene.add(axesHelper);
    7. //创建相机对象,45是相机的视角 , 宽高比是屏幕的宽高比 , 最近能看到0.1 , 最远能看到10000
    8. // this.camera = new THREE.OrthographicCamera(-s * k, s * k, s , -s, 1, 1000);
    9. // this.camera.position.set(0, 20, 300);
    10. this.camera = new THREE.PerspectiveCamera()
    11. //100300500
    12. this.camera.position.set(500, 0, 700); //设置相机位置
    13. // this.camera.position.set(100, -800, 500);
    14. // this.scene.position.set(0,40,0)
    15. console.log(this.scene.position)
    16. this.camera.lookAt(this.scene.position); //设置相机方向(指向的场景对象)
    17. // 执行一个渲染函数
    18. this.rendererGLR()
    19. /* 光源设置*/
    20. this.pointLight()
    21. this.clock = new THREE.Clock()
    22. //创建控件对象
    23. this.change()
    24. //更新轨道控件
    25. this.animate()
    26. let fileName = this.modelUrl.lastIndexOf(".")
    27. let fileFormat = this.modelUrl.substring(fileName + 1, this.modelUrl.length).toLowerCase()
    28. if (fileFormat == 'fbx') {
    29. this.fbxLoader()
    30. } else if(fileFormat == 'glb') {
    31. this.gblLoader()
    32. }
    33. this.renderer.render(this.scene, this.camera);
    34. },

  4. 渲染

    1. //渲染函数
    2. rendererGLR(){
    3. this.$nextTick(() => {
    4. const element = document.getElementById('threeView')
    5. this.renderer.setSize(element.clientWidth, element.clientHeight);
    6. element.appendChild(this.renderer.domElement);
    7. })
    8. this.renderer = new THREE.WebGLRenderer({alpha:true, antialias: true});//alpha:true背景透明
    9. this.renderer.setPixelRatio( window.devicePixelRatio * 2);
    10. this.renderer.toneMapping = THREE.ACESFilmicToneMapping;
    11. this.renderer.toneMappingExposure = 1.0;
    12. this.renderer.shadowMap.enabled = true;
    13. this.renderer.shadowMap.type = THREE.PCFSoftShadowMap;
    14. },

  5. 设置光源 (我这边设置的是四束光源)

    1. let ambientLight = new THREE.AmbientLight(0xffffff,1);
    2. this.scene.add(ambientLight);
    3. const directional_light = new THREE.DirectionalLight( 0xffffff, 1 );
    4. directional_light.position.set( 0, 1, 0 );
    5. directional_light.castShadow = true;
    6. this.scene.add(directional_light);
    7. let a=1,b=0.6,c=10;
    8. let directionalLight1 = new THREE.DirectionalLight(0xffffff,b);
    9. directionalLight1.position.set(-a,-a ,a*c).normalize();
    10. let directionalLight2 = new THREE.DirectionalLight(0xffffff,b);
    11. directionalLight2.position.set(a,-a,-a*c).normalize();
    12. let directionalLight3 = new THREE.DirectionalLight(0xffffff,b);
    13. directionalLight3.position.set(-a,a,-a*c).normalize();
    14. let directionalLight4 = new THREE.DirectionalLight(0xffffff,b);
    15. directionalLight4.position.set(a,a,a*c).normalize();
    16. this.scene.add(directionalLight1);
    17. this.scene.add(directionalLight2);
    18. this.scene.add(directionalLight3);
    19. this.scene.add(directionalLight4);

  6. 创建控件对象,这步主要是为了可以手动控制3D旋转,放大缩小等效果

    1. //创建控件对象
    2. change(){
    3. this.controls = new OrbitControls(this.camera, this.renderer.domElement);
    4. this.controls.minDistance = 300
    5. this.controls.maxDistance = 1000
    6. this.controls.addEventListener('change', () => {
    7. this.renderer.render(this.scene, this.camera);
    8. }); //监听鼠标、键盘事件
    9. //禁止缩放
    10. this.controls.enableZoom = this.changeFlag
    11. //禁止旋转
    12. this.controls.enableRotate = this.changeFlag
    13. //禁止右键拖拽
    14. this.controls.enablePan = this.changeFlag
    15. },

  7. 创建轨道控件(这一步是为了3D效果自旋转)

    1. //更新轨道控件
    2. animate() {
    3. if (this.renderer) {
    4. // console.log(this.stats)
    5. // this.stats.update()
    6. let T = this.clock.getDelta()
    7. let renderT = 1 / 30
    8. this.timeS = this.timeS + T
    9. if(this.timeS > renderT) {
    10. this.controls.update();
    11. this.renderer.render(this.scene, this.camera);
    12. this.timeS = 0
    13. }
    14. requestAnimationFrame(this.animate);
    15. if (!this.changeFlag) {
    16. this.controls.autoRotateSpeed = 15
    17. }
    18. this.controls.autoRotate = true // 是否自动旋转
    19. }
    20. //创建一个时钟对象
    21. // this.clock = new THREE.Clock()
    22. // this.scene.rotateY(0.01)
    23. //获得两帧的时间间隔 更新混合器相关的时间
    24. // if (this.mixer) {this.mixer.update(this.clock.getDelta())}
    25. },

  8. 根据不同的文件类型使用不同的方法导入

    1. //导入FBX模型文件
    2. fbxLoader(){
    3. let that=this
    4. const loader = new FBXLoader();
    5. loader.load(this.modelUrl,function(mesh){
    6. that.scene.add(mesh);
    7. that.ownerInstance.callMethod('onload')
    8. })
    9. },
    10. //导入GLB模型文件
    11. gblLoader(){
    12. let that=this
    13. const loader = new GLTFLoader();
    14. loader.load(this.modelUrl, function(gltf) {
    15. console.log(gltf)
    16. that.mesh = gltf.scene
    17. let model = gltf.scene
    18. that.scene.add(model);
    19. that.ownerInstance.callMethod('onload')
    20. });
    21. },

  9. 注意事项:该效果在uniapp只能在H5运行,在APP运行需要使用renderjs 这个我后续应该会有文章出来说明

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/833207
推荐阅读
相关标签
  

闽ICP备14008679号