123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- import * as THREE from 'three';
- import TWEEN from 'three/addons/libs/tween.module.js';
- let glbWidth = 300;
- let glbHeight = 300;
- export default {
- data() {
- return {
- gltfSpaceUrl: "https://dm.static.elab-plus.com/miniProgram/model/BP_AutoWall_WoodFloor1.glb",
-
-
-
- gltfSpace1Url: "https://dm.static.elab-plus.com/miniProgram/model/BP_G_tiles01.glb",
- instancedSpaceMeshList:[],
- spaceTypes : ["卧室","客厅","餐厅","厨房","玄关","卫生间","衣帽间","收纳","阳台","飘窗","链接空间","自定义","楼梯","花园"],
- }
- },
- watch: {},
- methods: {
-
- loaderSpaceArr(list){
- if(!list || list.length==0){
- return false;
- }
- this.instancedSpaceMeshList = [];
- this.gltfSpaces = [];
- let comlist = list.filter(it=>it.spaceType!=14);
- this.loaderCommonSpace(this.gltfSpaceUrl,comlist,1);
- let arrlist = list.filter(it=>it.spaceType==14);
- this.loaderCommonSpace(this.gltfSpace1Url,arrlist,2);
- },
- loaderCommonSpace(gltfSpaceUrl,list,type=1){
- var that = this;
- this.loader.load(gltfSpaceUrl, ( gltf ) => {
- console.log("地板模型加载成功",list)
-
- gltf.scene.traverse((child)=> {
- if (child.isMesh && child.visible) {
- let instancedMesh = new THREE.InstancedMesh(child.geometry.clone(), child.material.clone(), list.length);
- this.instancedSpaceMeshList.push(instancedMesh);
-
- list.forEach((obj,i)=>{
- let positionX = obj.centerX / 100;
- let positionY = obj.centerY / 100;
- if(type==1){
-
- }
- let scaleX = obj.spaceWidth / glbWidth;
- let scaleY = obj.spaceHeight / glbHeight;
-
- gltf.scene.position.set(positionX, 0, -positionY);
- gltf.scene.scale.set(scaleX, 1, scaleY);
- gltf.scene.updateMatrixWorld();
-
- instancedMesh.setMatrixAt(i, child.matrixWorld);
- instancedMesh.instanceMatrix.needsUpdate = true;
- instancedMesh.setColorAt(i, child.material.color);
- instancedMesh.instanceColor.needsUpdate = true;
- let gltfSpace = that.gltfSpaces.find(gltfSpace=>gltfSpace.spaceId == obj.spaceId);
- if(!gltfSpace){
- let position = new THREE.Vector3();
- let scale = new THREE.Vector3();
- position.set(positionX, 0, -positionY);
- scale.set(scaleX, 1, scaleY);
- let md = {
- spaceId:obj.spaceId,
- instancedMeshIndexList:[
- {instancedMeshIndex: this.instancedSpaceMeshList.length - 1, instancedAtIndex : i},
- ],
- spaceName:obj.spaceName,
- spaceType:obj.spaceType,
- position:position,
- scale:scale,
- isSizeLock:obj.isSizeLock,
- };
- that.gltfSpaces.push(md);
- }else{
- gltfSpace.instancedMeshIndexList.push({
- instancedMeshIndex:this.instancedSpaceMeshList.length - 1, instancedAtIndex:i
- })
- }
- })
- instancedMesh.userType = "mesh";
- if(type==2){
- instancedMesh.name = "花园";
- }else{
- instancedMesh.name = "地板";
- }
- instancedMesh.receiveShadow = true;
- that.scene.add(instancedMesh);
- }
- });
-
- if(true){
-
- list && list.forEach(obj=>{
- if(obj.spaceType!=14){
- let positionX = obj.centerX / 100;
- let positionY = obj.centerY / 100;
-
- const planeGeometry = new THREE.PlaneGeometry(obj.spaceWidth / 100,obj.spaceHeight / 100);
- const planeMaterial = new THREE.MeshStandardMaterial({
- color:0xffffff,
- metalness: 0.4,
- roughness: 1
- });
- const planeMesh = new THREE.Mesh(planeGeometry, planeMaterial)
- planeMesh.rotation.x = Math.PI / 2 ;
- planeMesh.position.set(positionX, 2.8, -positionY);
- planeMesh.userType = "mesh";
- that.scene.add(planeMesh);
- }
- })
- }
- });
- },
-
- changeSpacesAnimOld(curSpace){
-
- const cube = this.gltfSpaces.find((item)=>{
- return item.spaceId == curSpace.spaceId;
- })
- console.log("空间移动目标",cube.spaceId, JSON.stringify(cube.position),JSON.stringify(cube.scale),curSpace.toScaleX, curSpace.toScaleZ, curSpace.toPx, curSpace.toPz);
-
- cube.instancedMeshIndexList.forEach(instanced=>{
- let _index = instanced.instancedMeshIndex;
- let instancedMesh = this.instancedSpaceMeshList[_index];
- let stratMatrix = new THREE.Matrix4();
- instancedMesh.getMatrixAt(instanced.instancedAtIndex,stratMatrix);
-
- let endMatrix = stratMatrix.clone();
- let scaleMatrix = new THREE.Matrix4();
- let panMatrix = new THREE.Matrix4();
-
- scaleMatrix.makeScale(curSpace.toScaleX / cube.scale.x,1,curSpace.toScaleZ / cube.scale.z);
- panMatrix.makeTranslation(curSpace.toPx - cube.position.x,0,curSpace.toPz - cube.position.z);
- endMatrix.multiply(scaleMatrix).premultiply(panMatrix);
-
- var tween = new TWEEN.Tween(stratMatrix.elements)
- .to(endMatrix.elements, 2000)
- .easing(TWEEN.Easing.Quadratic.InOut)
- .onUpdate((matrixWorld)=> {
- let m4 = new THREE.Matrix4();
- m4.set(...matrixWorld);
- instancedMesh.instanceMatrix.needsUpdate = true;
- instancedMesh.setMatrixAt(instanced.instancedAtIndex,m4.transpose());
- }).onComplete(()=>{
- instancedMesh.setMatrixAt(instanced.instancedAtIndex,endMatrix);
- this.tweenCameraAnmaChange(false)
- });
-
- tween.start();
- this.tweenCameraAnmaChange(true)
- cube.position.x = curSpace.toPx;
- cube.position.z = curSpace.toPz;
- cube.scale.x = curSpace.toScaleX;
- cube.scale.z = curSpace.toScaleZ;
- });
- },
-
- changeSpacesAnim(curSpace){
-
- const cube = this.gltfSpaces.find((item)=>{
- return item.spaceId == curSpace.spaceId;
- })
- console.log("空间移动目标",cube.spaceId, JSON.stringify(cube.position),JSON.stringify(cube.scale),
- curSpace.toPx, curSpace.toPz,curSpace.toScaleX, curSpace.toScaleZ);
-
- let x = cube.position.x;
- let z = cube.position.z;
- let scaleX = cube.scale.x;
- let scaleZ = cube.scale.z;
-
- let toPx = curSpace.toPx;
- let toPz = curSpace.toPz;
- let toScaleX = curSpace.toScaleX;
- let toScaleZ = curSpace.toScaleZ;
-
- let spaceInitMatrix = [];
- cube.instancedMeshIndexList.forEach(instanced=>{
- let _index = instanced.instancedMeshIndex;
- let instancedMesh = this.instancedSpaceMeshList[_index];
- let startMatrix = new THREE.Matrix4();
- instancedMesh.getMatrixAt(instanced.instancedAtIndex,startMatrix);
- spaceInitMatrix.push({
- index:_index,
- matrix:startMatrix.clone(),
- color:instancedMesh.material.color.clone(),
- })
- });
-
- var tween = new TWEEN.Tween({
- x: cube.position.x,
- z: cube.position.z,
- sx:cube.scale.x,
- sz:cube.scale.z
- })
- .to({
- x: curSpace.toPx,
- z: curSpace.toPz,
- sx:curSpace.toScaleX,
- sz:curSpace.toScaleZ
- }, 2000)
- .easing(TWEEN.Easing.Quadratic.InOut)
- .onUpdate((object)=> {
-
- cube.instancedMeshIndexList.forEach(instanced=>{
- let _index = instanced.instancedMeshIndex;
- let instancedMesh = this.instancedSpaceMeshList[_index];
-
- let tmp = spaceInitMatrix.find(it=>it.index==_index);
- let stratMatrix = tmp.matrix.clone();
- let scaleMatrix = new THREE.Matrix4();
- let panMatrix = new THREE.Matrix4();
-
- scaleMatrix.makeScale(object.sx / scaleX,1,object.sz / scaleZ);
- panMatrix.makeTranslation(object.x - x,0,object.z - z);
- stratMatrix.multiply(scaleMatrix).premultiply(panMatrix);
- instancedMesh.instanceMatrix.needsUpdate = true;
- instancedMesh.setMatrixAt(instanced.instancedAtIndex,stratMatrix);
- if(this.curSpaceObj.spaceId==cube.spaceId){
- instancedMesh.instanceColor.needsUpdate = true;
- let color = new THREE.Color(0xFF9F40);
-
- instancedMesh.setColorAt(instanced.instancedAtIndex, color);
- }
- });
- }).onComplete(()=>{
- this.tweenCameraAnmaChange(false);
- console.warn("***changeSpacesAnim-over***")
- if(this.curSpaceObj.spaceId==cube.spaceId){
- cube.instancedMeshIndexList.forEach(instanced=>{
- let _index = instanced.instancedMeshIndex;
- let instancedMesh = this.instancedSpaceMeshList[_index];
- let tmp = spaceInitMatrix.find(it=>it.index==_index);
- instancedMesh.instanceColor.needsUpdate = true;
- instancedMesh.setColorAt(instanced.instancedAtIndex, tmp.color);
- });
- }
- });
-
- tween.start();
- this.tweenCameraAnmaChange(true)
- cube.position.x = curSpace.toPx;
- cube.position.z = curSpace.toPz;
- cube.scale.x = curSpace.toScaleX;
- cube.scale.z = curSpace.toScaleZ;
- },
-
- allSpaceScale(){
- const centerOffset = new THREE.Vector3(0, 0, 0);
- const scale = new THREE.Vector3(0.9, 1, 0.9);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- this.gltfSpaces.forEach(cube=>{
- let dis = centerOffset.clone().sub(cube.position);
-
- let x = dis.x * (1 - scale.x);
- let y = dis.y * (1 - scale.y);
- let z = dis.z * (1 - scale.z);
-
- let pi = new THREE.Vector3();
- pi.x = cube.position.x + x;
- pi.y = cube.position.y + y;
- pi.z = cube.position.z + z;
- cube.scale.x = cube.scale.x * scale.x;
- cube.scale.z = cube.scale.z * scale.z;
- cube.position.copy(pi);
-
- let sapce = this.spaceList.find(it=>{ return it.spaceId==cube.spaceId});
- if(sapce){
-
- sapce.centerX = pi.x * 100;
- sapce.centerY = -1* pi.z * 100;
-
- sapce.spaceWidth = cube.scale.x * glbWidth;
- sapce.spaceHeight = cube.scale.z * glbHeight;
- }
-
-
-
-
-
-
-
-
- cube.instancedMeshIndexList.forEach(instanced=>{
- let _index = instanced.instancedMeshIndex;
- let instancedMesh = this.instancedSpaceMeshList[_index];
- let stratMatrix = new THREE.Matrix4();
- instancedMesh.getMatrixAt(instanced.instancedAtIndex,stratMatrix);
- let scaleMatrix = new THREE.Matrix4().makeScale(scale.x, scale.y, scale.z);
- let inverseTranslationMatrix = new THREE.Matrix4().makeTranslation(x, y, z);
- stratMatrix.premultiply(inverseTranslationMatrix);
- stratMatrix.multiply(scaleMatrix);
- instancedMesh.instanceMatrix.needsUpdate = true;
- instancedMesh.setMatrixAt(instanced.instancedAtIndex,stratMatrix);
- });
- })
- this.$nextTick(()=>{
- this.updateAllWallHandle();
- })
- setTimeout(() =>{
- this.updataPageData();
- this.calculateLayoutModelSize();
- this.updateLables();
- this.$nextTick(()=>{
- this.updateCareFul();
- this.updateSpanceData();
- })
- }, 100);
- }
- }
- }
|