uniapp H5使用three加载模型

跨域问题

在manifest.json源码试图中

"h5" : {
        "title" : "",
        "router" : {
            "base" : "./",
            "mode" : "hash"
        },
        "devServer" : {
            "disableHostCheck" : true,
            "proxy" : {
                "/uploads" : {
                    "target" : "https://域名",
                    "changeOrigin" : true,
                    "secure" : false
                }
            }
        }
    }

下载

yarn add three
yarn add three-obj-mtl-loader
yarn addl three-orbit-controls

<view style="width:100%;height:100%;" id="container"></view>

import * as THREE from 'three';
import OrbitControls from 'three-orbit-controls'
import {OBJLoader} from '@/common/OBJLoader.js'
import {MTLLoader} from '@/common/MTLLoader.js'
data() {
		return {
			scene: null,
			camera: null,
			renderer: null,
			object:null
		}
	},
	onHide() {
		try {
			THREE.global.clearCanvas()
		} catch (e) {
			//TODO handle the exception
		}
	},
	onUnload() {
		try {
			THREE.global.clearCanvas()
		} catch (e) {
			//TODO handle the exception
		}
	},
	onShow() {
		this.initThree()
	},
	methods: {
		// 初始化three
		initThree(data) {
			const that = this
			// 获取模型地址后缀
			const suffix = this.getFilePathSuffix(data.modelUrl)
			this.$nextTick(() => {
				this.initThreeWeb(data)
			})
		},
		// H5方法 Start
		// 加载初始化
		initThreeWeb(data) {
			const that = this
			let container = document.getElementById("container")
			/**
			 * 创建场景对象Scene
			 */
			var scene = new THREE.Scene();
			/**
			 * 创建网格模型
			 */
			let mtlLoader = new MTLLoader();
			let objloader = new OBJLoader();
			mtlLoader.load(data.mtlUrl, function(materials) {
				objloader.setMaterials(materials)
				materials.preload();
				objloader.load(data.modelUrl, function(obj) {
					that.object = obj
					that.scene.add(obj);
				});
			});
			/**
			 * 光源设置
			 */
			//环境光
			var ambient = new THREE.AmbientLight(0xffffff, 0.8);
			scene.add(ambient);
			/**
			 * 相机设置
			 */
			var width = container.offsetWidth; //窗口宽度
			var height = container.offsetHeight; //窗口高度
			//创建相机对象
			var camera = new THREE.PerspectiveCamera(45, width / height, 1, 2000);
			camera.position.z = data.cameraPosition.z//1400; //设置相机位置
			camera.position.y = data.cameraPosition.y
			camera.lookAt(scene.position); //设置相机方向(指向的场景对象)
			let pointLight = new THREE.PointLight(0xffffff, 0.1);
			camera.add(pointLight);
			scene.add(camera);
			/**
			 * 创建渲染器对象
			 */
			var renderer = new THREE.WebGLRenderer();
			renderer.setSize(width, height); //设置渲染区域尺寸
			renderer.setClearColor(0xb9d3ff, 0); //设置背景颜色
			container.appendChild(renderer.domElement); //body元素中插入canvas对象
			//执行渲染操作   指定场景、相机作为参数
			renderer.render(scene, camera);
			this.scene = scene
			this.renderer = renderer
			this.camera = camera
			this.animation()
		},
		animation() {
			try {
				this.object.rotation.y -= 0.005;
			} catch (e) {
				//TODO handle the exception
			}
			this.camera.lookAt(this.scene.position);
			this.renderer.render(this.scene, this.camera);
			requestAnimationFrame(this.animation);
		},
		// H5方法 End
		// 获取文件后缀名
		getFilePathSuffix(name) {
			let suffix = null
			const last_len = name.lastIndexOf('.')
			const len = name.length
			suffix = name.substring(last_len + 1, len)
			return suffix
		},
		render(canvas, THREE, modelUrl) {
			let window = THREE.global;
			let GLTFLoader = gLTF(THREE);
			const renderer = new THREE.WebGLRenderer({
				antialias: true
			});
			renderer.setPixelRatio(window.devicePixelRatio);
			// renderer.setSize(canvas.width, canvas.height);
			renderer.gammaOutput = true;

			const fov = 45;
			const aspect = 2; // the canvas default
			const near = 0.1;
			const far = 100;
			const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
			camera.position.set(0, 10, 20);


			const scene = new THREE.Scene();
			scene.background = new THREE.Color('black');


			{
				const skyColor = 0xB1E1FF; // light blue
				const groundColor = 0xB97A20; // brownish orange
				const intensity = 1;
				const light = new THREE.HemisphereLight(skyColor, groundColor, intensity);
				scene.add(light);
			}

			{
				const color = 0xFFFFFF;
				const intensity = 1;
				const light = new THREE.DirectionalLight(color, intensity);
				light.position.set(5, 10, 2);
				scene.add(light);
				scene.add(light.target);
			}



			{
				const gltfLoader = new GLTFLoader();
				gltfLoader.load(modelUrl, (gltf) => {
					const root = gltf.scene;
					scene.add(root);
				});
			}



			function render() {

				camera.aspect = canvas.clientWidth / canvas.clientHeight;
				camera.updateProjectionMatrix();

				renderer.render(scene, camera);
				canvas.requestAnimationFrame(render);
			}

			canvas.requestAnimationFrame(render);
		}
	}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值