最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

uniapp?使用?tree.js?解決模型加載不出來(lái)的問(wèn)題及解決方法

 更新時(shí)間:2025年02月11日 09:41:12   作者:hardWork_yulu  
本文介紹了在uniapp中使用tree.js時(shí)遇到的模型加載不出來(lái)的問(wèn)題,并最終發(fā)現(xiàn)是由于縮放問(wèn)題導(dǎo)致的,通過(guò)調(diào)用`getFitScaleValue()`方法解決了這個(gè)問(wèn)題,感興趣的朋友一起看看吧

網(wǎng)上有很多uniapp使用tree.js的教程,但是我在使用測(cè)試中,發(fā)現(xiàn)tree.js的官方3d模型中有很多加載不出來(lái),但是也沒(méi)有報(bào)錯(cuò),全網(wǎng)搜也沒(méi)搜到方法,最后發(fā)現(xiàn)是縮放的問(wèn)題,這里將代碼貼出來(lái),關(guān)鍵的方法是getFitScaleValue()這個(gè)方法

<template>
	<view id="app">
		<canvas id="webgl" ref="webgl" canvas-id="webgl" type="webgl"
			:style="'width:'+mSceneWidth+'px; height:'+mSceneHeight+'px;'">
		</canvas>
	</view>
</template>
<script>
	import * as THREE from 'three'
	import {
		OrbitControls
	} from 'three/examples/jsm/controls/OrbitControls.js'
	import {
		GLTFLoader
	} from 'three/examples/jsm/loaders/GLTFLoader.js';
	import {
		FBXLoader
	} from 'three/examples/jsm/loaders/FBXLoader.js';
	import {
		DRACOLoader
	} from "three/examples/jsm/loaders/DRACOLoader.js";
	import {
		OBJLoader
	} from "three/examples/jsm/loaders/OBJLoader.js";
	export default {
		//Soldier
		data() {
			return {
				mSceneWidth: 0, // 手機(jī)屏幕寬度
				mSceneHeight: 0, // 手機(jī)屏幕高度
				canvas: null,
				worldFocus: null, // 世界焦點(diǎn)(模型放置,相機(jī)圍繞的中心)
				renderer: null,
				mCanvasId: null,
				scene: null,
				mesh: null,
				camera: null,
				clock: null,
				renderAnimFrameId: null, // 渲染幀動(dòng)畫id
				controls: null,
				timeS: 0,
				changeFlag: true,
				mixer: null,
				previousTime: 0,
				modelUrl: "/static/Soldier.glb"
			};
		},
		mounted() {
			// uni.createSelectorQuery().in(this).select('#webgl').fields({
			// 	node: true
			// }).exec(res=> {
			// 	console.log(JSON.stringify(res))
			// 	this.mCanvasId = res[0].node.id;
			// 	// 注冊(cè)畫布
			// 	const mCanvas = THREE.global.registerCanvas(this.mCanvasId, res[0].node);
			// 	// 開(kāi)始初始化
			// 	this.init(mCanvas);
			// })	
		},
		// 頁(yè)面加載時(shí)
		onLoad(option) {
			// 獲取手機(jī)屏幕寬高
			this.mSceneWidth = uni.getWindowInfo().windowWidth;
			this.mSceneHeight = uni.getWindowInfo().windowHeight;
			// 設(shè)置世界中心
			this.worldFocus = new THREE.Vector3(0, 0, 0);
		},
		// 頁(yè)面加載完畢后
		onReady() {
			this.init()
		},
		onShow() {
		},
		onHide() {
			this.disposes()
			cancelAnimationFrame(this.animate())
		},
		methods: {
			// 在不需要時(shí)釋放資源
			disposes() {
				// 釋放幾何體
				this.scene.traverse((object) => {
					if (object.geometry) {
						object.geometry.dispose();
					}
					// 釋放材質(zhì)
					if (object.material) {
						if (Array.isArray(object.material)) {
							object.material.forEach(material => material.dispose());
						} else {
							object.material.dispose();
						}
					}
				});
				// 釋放渲染器
				if (this.renderer) {
					this.renderer.dispose();
				}
				// 清除場(chǎng)景
				while (this.scene.children.length > 0) {
					this.scene.remove(this.scene.children[0]);
				}
			},
			getFitScaleValue(scene) {
			    let box=new THREE.BoxGeometry(3,3,3)
			    let mail=new THREE.MeshBasicMaterial({color:0xff6600})
			    let mesh=new THREE.Mesh(box,mail)
			    var boxes = new THREE.Box3().setFromObject( scene );
			    var maxDiameter =  Math.max((boxes.max.x - boxes.min.x), (boxes.max.y - boxes.min.y), (boxes.max.z - boxes.min.z)); //數(shù)值越大,模型越小
				console.log(maxDiameter)
			    return Math.ceil(this.mSceneHeight / maxDiameter/4);
			},
			init() {
				// 創(chuàng)建一個(gè)場(chǎng)景
				this.scene = new THREE.Scene()
				//三位坐標(biāo)線
				// const axesHelper = new THREE.AxesHelper(5);
				// this.scene.add(axesHelper);
				//創(chuàng)建相機(jī)對(duì)象,45是相機(jī)的視角  , 寬高比是屏幕的寬高比 , 最近能看到0.1 , 最遠(yuǎn)能看到10000
				// this.camera = new THREE.OrthographicCamera(-s * k, s * k, s , -s, 1, 1000);
				// this.camera.position.set(0, 20, 300);
				const lod = new THREE.LOD();
				// 創(chuàng)建不同細(xì)節(jié)級(jí)別的幾何體
				const highDetailGeometry = new THREE.BoxGeometry(1, 1, 1);
				const mediumDetailGeometry = new THREE.BoxGeometry(0.5, 0.5, 0.5);
				const lowDetailGeometry = new THREE.BoxGeometry(0.25, 0.25, 0.25);
				// 創(chuàng)建材質(zhì)
				const material = new THREE.MeshBasicMaterial({
					color: 0xff0000
				});
				// 創(chuàng)建不同細(xì)節(jié)級(jí)別的網(wǎng)格
				const highDetailMesh = new THREE.Mesh(highDetailGeometry, material);
				const mediumDetailMesh = new THREE.Mesh(mediumDetailGeometry, material);
				const lowDetailMesh = new THREE.Mesh(lowDetailGeometry, material);
				// 將不同細(xì)節(jié)級(jí)別的網(wǎng)格添加到LOD對(duì)象中
				lod.addLevel(highDetailMesh, 0); // 距離0
				lod.addLevel(mediumDetailMesh, 5); // 距離5
				lod.addLevel(lowDetailMesh, 10); // 距離10
				this.scene.add(lod);
				this.camera = new THREE.PerspectiveCamera(75, this.mSceneWidth / this.mSceneHeight, 0.1, 2000);
				//100,300 ,500
				this.camera.position.set(0, 0, 5); //設(shè)置相機(jī)位置
				 //this.camera.position.set(100, -800, 500);
				 this.scene.add(this.camera)
				this.camera.lookAt(this.scene.position); //設(shè)置相機(jī)方向(指向的場(chǎng)景對(duì)象)
				// 執(zhí)行一個(gè)渲染函數(shù)
				this.rendererGLR()
				/* 光源設(shè)置*/
				this.pointLight()
				this.clock = new THREE.Clock()
				//創(chuàng)建控件對(duì)象
				this.change()
				//更新軌道控件
				let fileName = this.modelUrl.lastIndexOf(".")
				let fileFormat = this.modelUrl.substring(fileName + 1, this.modelUrl.length).toLowerCase()
				if (fileFormat == 'fbx') {
					this.fbxLoader()
				} else if (fileFormat == 'glb') {
					this.gblLoader()
				} else if (fileFormat == 'obj') {
					this.objLoader()
				}
				//this.renderer.render(this.scene, this.camera);
			},
			pointLight() {
				let ambientLight = new THREE.AmbientLight(0xffffff, 1);
				this.scene.add(ambientLight);
				const directional_light = new THREE.DirectionalLight(0xffffff, 1);
				directional_light.position.set(0, 1, 0);
				directional_light.castShadow = true;
				this.scene.add(directional_light);
				let a = 1,
					b = 0.6,
					c = 10;
				let directionalLight1 = new THREE.DirectionalLight(0xffffff, b);
				directionalLight1.position.set(-a, -a, a * c).normalize();
				let directionalLight2 = new THREE.DirectionalLight(0xffffff, b);
				directionalLight2.position.set(a, -a, -a * c).normalize();
				let directionalLight3 = new THREE.DirectionalLight(0xffffff, b);
				directionalLight3.position.set(-a, a, -a * c).normalize();
				let directionalLight4 = new THREE.DirectionalLight(0xffffff, b);
				directionalLight4.position.set(a, a, a * c).normalize();
				this.scene.add(directionalLight1);
				this.scene.add(directionalLight2);
				this.scene.add(directionalLight3);
				this.scene.add(directionalLight4);
			},
			//渲染函數(shù)
			rendererGLR() {
				this.$nextTick(() => {
					const element = document.getElementById('webgl')
					this.canvas = element
					this.renderer.setSize(element.clientWidth, element.clientHeight);
					element.appendChild(this.renderer.domElement);
				})
				this.renderer = new THREE.WebGLRenderer({
					alpha: true,
					antialias: true,
					powerPreference: "high-performance",
					precision: "mediump"
				}); //alpha:true背景透明
				this.renderer.setPixelRatio(window.devicePixelRatio * 2);
				this.renderer.toneMapping = THREE.ACESFilmicToneMapping;
				this.renderer.toneMappingExposure = 1.0;
				this.renderer.outputColorSpace = THREE.SRGBColorSpace;
				this.renderer.shadowMap.enabled = true;
				this.renderer.shadowMap.type = THREE.PCFSoftShadowMap;
			},
			//創(chuàng)建控件對(duì)象
			change() {
				this.controls = new OrbitControls(this.camera, this.renderer.domElement);
				this.controls.minDistance = 300
				this.controls.maxDistance = 1000
				this.controls.addEventListener('change', () => {
					this.renderer.render(this.scene, this.camera);
				}); //監(jiān)聽(tīng)鼠標(biāo)、鍵盤事件
				//禁止縮放
				this.controls.enableZoom = this.changeFlag
				//禁止旋轉(zhuǎn)
				this.controls.enableRotate = this.changeFlag
				//禁止右鍵拖拽
				this.controls.enablePan = this.changeFlag
			},
			//更新軌道控件
			animate() {
				if (this.renderer) {
					// console.log(this.stats)
					// this.stats.update()
					let T = this.clock.getDelta()
					let renderT = 1 / 30
					this.timeS = this.timeS + T
					if (this.timeS > renderT) {
						this.controls.update();
						this.renderer.render(this.scene, this.camera);
						this.timeS = 0
					}
					requestAnimationFrame(this.animate);
					if (!this.changeFlag) {
						this.controls.autoRotateSpeed = 16
					}
					this.controls.autoRotate = false // 是否自動(dòng)旋轉(zhuǎn)
				}
				//創(chuàng)建一個(gè)時(shí)鐘對(duì)象
				//this.clock = new THREE.Clock()
				//this.scene.rotateY(0.01)
				//獲得兩幀的時(shí)間間隔  更新混合器相關(guān)的時(shí)間
				if (this.mixer) {
					this.mixer.update(this.clock.getDelta()*100)
				}
			},
			objLoader() {
				let that = this
				const loader = new OBJLoader();
				uni.showLoading({
					title: "正在加載"
				})
				// load a resource
				loader.load(
					// resource URL
					that.modelUrl,
					// called when resource is loaded
					function(object) {
						console.log(object)
						uni.hideLoading()
						var scale = that.getFitScaleValue(object)
						console.log(scale)
						object.scale.set(scale, scale, scale);
						that.scene.add(object);
						setTimeout(function() {
							//that.renderer.render(that.scene, that.camera);
							that.animate()
						}, 1000);
					},
					// called when loading is in progress
					function(xhr) {
						console.log((xhr.loaded / xhr.total * 100) + '% loaded');
					},
					// called when loading has errors
					function(error) {
						console.log('An error happened');
					}
				);
			},
			//導(dǎo)入FBX模型文件
			fbxLoader() {
				let that = this
				const loader = new FBXLoader();
				loader.load(this.modelUrl, function(mesh) {
					that.scene.add(mesh);
					that.ownerInstance.callMethod('onload')
				})
			},
			//導(dǎo)入GLB模型文件
			gblLoader() {
				uni.showLoading({
					title: "正在加載",
				})
				let that = this
				const loader = new GLTFLoader();
				const dracoloader = new DRACOLoader();
				dracoloader.setDecoderPath("/static/draco/");
				loader.setDRACOLoader(dracoloader);
				loader.load(that.modelUrl, function(gltf) {
					uni.hideLoading()
					//that.mesh = gltf.scene
					if (gltf.animations.length > 0) {
						that.mixer = new THREE.AnimationMixer(gltf.scene)
						const action = that.mixer.clipAction(gltf.animations[0])
						// 讓動(dòng)畫進(jìn)入播放狀態(tài)
						action.play()
					}
					var scale = that.getFitScaleValue(gltf.scene)
					console.log(scale)
					 gltf.scene.scale.set(scale, scale, scale);
					that.scene.add(gltf.scene);
					setTimeout(function() {
						//that.renderer.render(that.scene, that.camera);
						that.animate()
					}, 1000);
				}, function(xhr) {
					console.log((xhr.loaded / xhr.total * 100) + '% loaded');
				}, function(err) {
					console.log(err)
				});
			},
			// 觸摸開(kāi)始
			// 觸摸事件處理
			onTouchStart(event) {
				const touch = event.touches[0];
				const rect = this.canvas.getBoundingClientRect();
				const x = touch.clientX - rect.left;
				const y = touch.clientY - rect.top;
				// 在這里處理觸摸開(kāi)始事件
			},
			onTouchMove(event) {
				const touch = event.touches[0];
				const rect = this.canvas.getBoundingClientRect();
				const x = touch.clientX - rect.left;
				const y = touch.clientY - rect.top;
				// 在這里處理觸摸移動(dòng)事件
			},
			onTouchEnd() {
				// 在這里處理觸摸結(jié)束事件
			}
		}
	}
</script>
<style lang="scss">
</style>

未調(diào)用縮放方法,就是空白,調(diào)用后:

到此這篇關(guān)于uniapp 使用 tree.js 解決模型加載不出來(lái)的問(wèn)題的文章就介紹到這了,更多相關(guān)uniapp tree.js 模型加載不出來(lái)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 微信小程序?qū)崿F(xiàn)自定義動(dòng)畫彈框/提示框的方法實(shí)例

    微信小程序?qū)崿F(xiàn)自定義動(dòng)畫彈框/提示框的方法實(shí)例

    這篇文章主要給大家介紹了關(guān)于微信小程序?qū)崿F(xiàn)自定義動(dòng)畫彈框/提示框的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • 原生JS發(fā)送異步數(shù)據(jù)請(qǐng)求

    原生JS發(fā)送異步數(shù)據(jù)請(qǐng)求

    這篇文章主要為大家詳細(xì)介紹了原生JS發(fā)送異步數(shù)據(jù)請(qǐng)求的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • 動(dòng)態(tài)加載腳本提升javascript性能

    動(dòng)態(tài)加載腳本提升javascript性能

    動(dòng)態(tài)加載腳本可以有效提升javascript性能,下面有個(gè)不錯(cuò)的示例,大家可以參考下
    2014-02-02
  • JavaScript中日期大小比較的全面解析

    JavaScript中日期大小比較的全面解析

    本文將詳細(xì)介紹如何使用JavaScript內(nèi)置的Date對(duì)象來(lái)創(chuàng)建、操作和比較日期時(shí)間,以及如何使用getTime()方法和比較運(yùn)算符來(lái)判斷兩個(gè)日期的大小,感興趣的朋友跟隨小編一起看看吧
    2025-08-08
  • javascript full screen 全屏顯示頁(yè)面元素的方法

    javascript full screen 全屏顯示頁(yè)面元素的方法

    要想讓頁(yè)面的某個(gè)元素全屏顯示,就像在網(wǎng)頁(yè)上看視頻的時(shí)候,可以全屏觀看一樣,該怎么實(shí)現(xiàn)呢
    2013-09-09
  • big.js與bignumber.js的差異、統(tǒng)一配置與避坑技巧完全指南

    big.js與bignumber.js的差異、統(tǒng)一配置與避坑技巧完全指南

    big.js是一個(gè)小巧、快速且易于使用的JavaScript庫(kù),專門用于任意精度的十進(jìn)制算術(shù)運(yùn)算,這篇文章主要介紹了big.js與bignumber.js的差異、統(tǒng)一配置與避坑技巧完全指南,需要的朋友可以參考下
    2026-01-01
  • 一篇文章弄懂ECMAScript中的操作符

    一篇文章弄懂ECMAScript中的操作符

    這篇文章主要給大家介紹了如何通過(guò)一篇文章弄懂ECMAScript中操作符的相關(guān)資料,其中包括一元操作符、布爾操作符、乘性操作符、加性操作符、關(guān)系操作符、相等操作符、條件操作符、賦值操作符 以及逗號(hào)操作符,需要的朋友可以參考下
    2021-07-07
  • javascript 循環(huán)語(yǔ)句 while、do-while、for-in、for用法區(qū)別

    javascript 循環(huán)語(yǔ)句 while、do-while、for-in、for用法區(qū)別

    本文章介紹了在學(xué)習(xí)javascript中的循環(huán)語(yǔ)句的用法,包while、do-while、for-in、for它們之間的區(qū)別,也是常用的循環(huán)語(yǔ)句了,有需要的朋友可以了解一下
    2012-03-03
  • Javascript控制input輸入時(shí)間格式的方法

    Javascript控制input輸入時(shí)間格式的方法

    這篇文章主要介紹了Javascript控制input輸入時(shí)間格式的方法,涉及鼠標(biāo)事件及正則表達(dá)式的靈活應(yīng)用,需要的朋友可以參考下
    2015-01-01
  • 詳解JavaScript中的坐標(biāo)和距離

    詳解JavaScript中的坐標(biāo)和距離

    在前端開(kāi)發(fā)中總會(huì)遇到各種各樣需要使用或計(jì)算坐標(biāo)和距離的情況,于是便有了整理記錄的想法,即加深了印象,又方便隨時(shí)查閱。
    2019-05-05

最新評(píng)論

甘德县| 杭州市| 天台县| 天镇县| 崇阳县| 开化县| 涟水县| 吴江市| 喀喇沁旗| 屏南县| 湘潭县| 华亭县| 秦安县| 商都县| 乐东| 阿尔山市| 通化县| 伽师县| 曲沃县| 称多县| 新乡县| 武城县| 宜丰县| 芷江| 长阳| 鹤山市| 鄂伦春自治旗| 南陵县| 安康市| 诏安县| 聂拉木县| 黄梅县| 剑川县| 将乐县| 石棉县| 闸北区| 孟津县| 漠河县| 周口市| 郁南县| 平塘县|