您的位置:首页 > 游戏 > 游戏 > 哈尔滨免费自助建站模板_正能量网站推荐不需要下载_江苏建站_域名信息查询

哈尔滨免费自助建站模板_正能量网站推荐不需要下载_江苏建站_域名信息查询

2025/2/27 6:27:39 来源:https://blog.csdn.net/lilycheng1986/article/details/145886424  浏览:    关键词:哈尔滨免费自助建站模板_正能量网站推荐不需要下载_江苏建站_域名信息查询
哈尔滨免费自助建站模板_正能量网站推荐不需要下载_江苏建站_域名信息查询

vue3+threejs,做一个给模型批量CSS2D标签的案例,在导入模型的js文件里,跟着课程写的代码如下:

import * as THREE from 'three';
// 引入gltf模型加载库GLTFLoader.js
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';// 引入CSS2模型对象CSS2DObject
import {CSS2DObject
} from 'three/addons/renderers/CSS2DRenderer.js';const model = new THREE.Group();
const loader = new GLTFLoader();
// 路径要特别注意,默认是从public读取的,模型文件必须放在public下,并且路径前的public要省略
loader.load('models/简易小区.glb', gltf => {gltf.scene.getObjectByName("小区房子").traverse(function (obj) {if (obj.isMesh) {let label = tag(obj.name);const pos = new THREE.Vector3();obj.getWorldPosition(pos);label.position.copy(pos);label.name = obj.name;model.add(label);}});model.add(gltf.scene);
}, undefined, error => {console.error(error);
});function tag(name) {// 创建dom元素(作为标签)let div = document.createElement('div');div.innerHTML = name;div.classList.add('css2dtag');//使用dom元素生成CSS2模型对象CSS2DObjectlet label = new CSS2DObject(div);div.style.pointerEvents = 'none';//避免HTML标签遮挡三维场景的鼠标事件return label;//返回CSS2模型标签      }export default model;

在vue文件里设置css。

<template>
</template><script setup>
import { scene } from './init.js';
</script><style scoped>.css2dtag {background: #ffffff;color: orangered;}
</style>

运行代码后发现css没有生效。

查了资料,找到一篇参考文章:

threejs 代码创建div设置样式不生效的问题原因探讨 - 简书

尝试去掉scoped,确实生效了,但是这种处理并不理想。

在继续学习threejs课程的时候,发现老师教了另一种批量创建的办法。

对vue文件修改如何:

增加一个id为css2dtag的div,css加上scoped。

<template><div id="css2dtag" style="display: none;"></div>
</template><script setup>
import { scene } from './init.js';
</script><style scoped>#css2dtag {background: #ffffff;color: orangered;}
</style>

然后在导入模型的js文件里,通过克隆这个div来批量创建dom元素。

import * as THREE from 'three';
// 引入gltf模型加载库GLTFLoader.js
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';// 引入CSS2模型对象CSS2DObject
import {CSS2DObject
} from 'three/addons/renderers/CSS2DRenderer.js';const model = new THREE.Group();
const loader = new GLTFLoader();
// 路径要特别注意,默认是从public读取的,模型文件必须放在public下,并且路径前的public要省略
loader.load('models/简易小区.glb', gltf => {gltf.scene.getObjectByName("小区房子").traverse(function (obj) {if (obj.isMesh) {let label = tag(obj.name);const pos = new THREE.Vector3();obj.getWorldPosition(pos);label.position.copy(pos);label.name = obj.name;model.add(label);}});model.add(gltf.scene);
}, undefined, error => {console.error(error);
});function tag(name) {// 通过克隆来批量创建divlet div = document.getElementById('css2dtag').cloneNode();div.innerHTML = name;//使用dom元素生成CSS2模型对象CSS2DObjectlet label = new CSS2DObject(div);div.style.pointerEvents = 'none';//避免HTML标签遮挡三维场景的鼠标事件return label;//返回CSS2模型标签      
}export default model;

再次运行代码,css生效了。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com