赞
踩
<template> <view class="collection-bg"> <n-text-loading v-if="loading"></n-text-loading> <view id="threeView" @click="thress.onClick" :prop="url" :change:prop="thress.updataModelUrl" :propz="cameraZ" :change:propz="thress.updataModelZ" class="media-wrap" ></view> </view> </template> <script> export default{ props:{ url: String, cameraZ: Number }, data(){ return{ loading: true, } }, methods:{ completed(option){ this.loading = option.loading || false; } } } </script>
<script module="thress" lang="renderjs"> // import THREE from 'three/build/three.min.js' const THREE = require('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' export default { name:"ModelScale", data(){ return{ scene:null, camera:null, renderer:null, cube:null, sphere:null, step:0, stats:null, group:null, orbitcontrols: null, } }, props:{ width:{type:Number,default: 375}, height:{type:Number,default: 380}, }, methods:{ updataModelUrl(newValue, oldValue, ownerInstance, instance) { // 监听 service 层数据变更 console.log(newValue,'层数据变更'); }, updataModelZ(newValue, oldValue, ownerInstance, instance){ console.log(newValue,'层数据变更2'); }, onClick(event, ownerInstance){ ownerInstance.callMethod('completed', { loading: false }) }, init(){ const cameraZ = this.cameraZ <= 0 ? 100 : this.cameraZ; console.log(cameraZ,'cameraZ'); this.scene = new THREE.Scene(); this.camera = new THREE.PerspectiveCamera(45,this.width/this.height,0.1,1000); this.camera.position.set(0,0,cameraZ); this.renderer = new THREE.WebGLRenderer({antialias: true }); this.renderer.setClearColor(0xffffff,0); this.renderer.setSize(this.width/1.2,this.height/1.2); this.renderer.setPixelRatio(window.devicePixelRatio); this.renderer.shadowMapEnabled = true; document.getElementById("threeView").appendChild(this.renderer.domElement); this.orbitcontrols = new OrbitControls(this.camera, this.renderer.domElement); //移动控件 this.orbitcontrols.enabled = true; this.orbitcontrols.enableRotate =true; this.orbitcontrols.enableZoom = false; this.orbitcontrols.autoRotate = true; this.orbitcontrols.minPolarAngle = Math.PI / 4; this.orbitcontrols.maxPolarAngle = 3 - (Math.PI / 4); // let axes = new THREE.AxesHelper(100);//辅助线 // this.scene.add(axes); if(this.url.endsWith('gltf') || this.url.endsWith('glb')){ //设置了六个平行光 有些材质不接受环境光会很暗 const directionLight1 = new THREE.DirectionalLight(0xffffff, 1); directionLight1.position.set(-300,0,0) this.scene.add(directionLight1); const directionLight2 = new THREE.DirectionalLight(0xffffff, 1); directionLight2.position.set(300,0,0) this.scene.add(directionLight2); const directionLight3 = new THREE.DirectionalLight(0xffffff, 1); directionLight3.position.set(0,300,0) this.scene.add(directionLight3); const directionLight4 = new THREE.DirectionalLight(0xffffff, 1); directionLight4.position.set(0,300,0) this.scene.add(directionLight4); const directionLight5 = new THREE.DirectionalLight(0xffffff, 1); directionLight5.position.set(0,0,-300) this.scene.add(directionLight5); const directionLight6 = new THREE.DirectionalLight(0xffffff, 1); directionLight6.position.set(0,0,300) this.scene.add(directionLight6); } let Sun = new THREE.DirectionalLight(0xffffff, 1); Sun.position.set(0,300,0); Sun.castShadow = true; let Ambient = new THREE.AmbientLight(0xffffff, 1); this.scene.add(Ambient); this.scene.add(Sun); }, loadModel(){ let self = this; //这一点很重要。。 let loader1 = new GLTFLoader(); let FBXloader = new FBXLoader(); let rotateObj = []; const loadLoader = this.url.endsWith('fbx') ? FBXloader : loader1; loadLoader.load(this.url,function (gltf){ const loadscene = gltf.scene || gltf; loadscene.scale.set(1,1,1); let group = new THREE.Group(); group.add(loadscene); let bbox = new THREE.Box3().setFromObject(group); // console.log(bbox,'bbox---'); let mdlen=bbox.max.x-bbox.min.x; //边界的最小坐标值 边界的最大坐标值 let mdhei=bbox.max.y-bbox.min.y; let mdwid=bbox.max.z-bbox.min.z; group.position.set(0,0,0); // console.log(self.camera,'相机的信息',group,'组的信息'); let dist =Math.abs(self.camera.position.z - group.position.z- (mdwid/2)); //console.log('dist值为:' + dist ); let vFov = self.camera.fov * Math.PI/180; //弧度=角度*Math.PI/180 //console.log('vFov值为:' + vFov ); let vheight = 2 * Math.tan(vFov * 0.5) *dist; //console.log('vheight值为:' + vheight ); let fraction = mdhei / vheight; // console.log('fraction值为:' + fraction ); let finalHeight = self.height * fraction ; //console.log('finalHeight值为:' + finalHeight); let finalWidth = (finalHeight*mdlen) /mdhei; //console.log('finalWidth值为:' + finalWidth ); let value1 = self.width/finalWidth; // console.log('value1缩放比例值为:' + value1); let value2 = self.height/finalHeight; // console.log('value2缩放比例值为:' + value2); if(value1 >= value2){ group.scale.set(value2,value2,value2); } else{ group.scale.set(value1 /2,value1/2,value1/2); // group.scale.set(value1,value1,value1); } let bbox2= new THREE.Box3().setFromObject(group) // console.log(bbox2,'bbox2'); let mdlen2=bbox2.max.x-bbox2.min.x; let mdhei2=bbox2.max.y-bbox2.min.y; let mdwid2=bbox2.max.z-bbox2.min.z; group.position.set(-(bbox2.max.x+bbox2.min.x)/2, -(bbox2.max.y+bbox2.min.y)/2, -(bbox2.max.z+bbox2.min.z)/2); document.getElementById("threeView").click(); //去掉加载效果 self.scene.add(group); // let boxhelper = new THREE.BoxHelper(group,0xbe1915); //辅助线外面红色框 // self.scene.add(boxhelper); }); }, animate(){ requestAnimationFrame(this.animate); this.orbitcontrols.update(); //自动旋转 this.renderer.render(this.scene,this.camera); } }, mounted(){ window.ob = this; this.init(); this.animate(); this.loadModel(); } } </script>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。