Three.js 实战【3】—— 城市模型渲染

Three.js 实战【3】------ 城市模型渲染

初始化场景

初始化场景这一块就跳过了,可以参考上一篇文章:Three.js 实战【2】------ 船模型海上场景渲染,效果是一样的。

加载模型

这里我们找一个城市的模型下来,可以上这个网站下载个模型,这里以上海市模型下载地址为例。

先加载我们下载的模型到场景当中。

js 复制代码
const addCity = () => {
  const gltfLoader = new GLTFLoader();
  gltfLoader.load('./src/assets/glb/shanghai_city.glb', (gltf: any) => {
    model = gltf.scene.children[0];
    scene.add(model);
  });
};

在加载了第三方的模型发现模型并没有和坐标系的(0,0,0)保持重合,反而有偏移,这个时候我们可以利用Box3去拿到模型的包围盒,获取到模型的中心点坐标信息,再取反,就会得到模型中心点在3d世界的位置信息。(在上面scene.add(model))前调用这个toCenter方法进行居中。

  • Box3:表示三维空间中的一个轴对齐包围盒
  • expandByObject(model):扩展此包围盒的边界,使得对象及其子对象在包围盒内,包括对象和子对象的世界坐标的变换。
  • Vector3:三维向量表示的是一个有顺序的、三个为一组的数字组合(x,y,z)
  • negate:向量取反,即: x = -x, y = -y , z = -z。
js 复制代码
const toCenter = () => {
  const box = new THREE.Box3();
  box.expandByObject(model);
  console.log('box =====', box);
  const size = new THREE.Vector3();
  const center = new THREE.Vector3();
  console.log('size,center =====', size, center);
  // 获取包围盒的中心点和尺寸
  box.getCenter(center);
  box.getSize(size);
  // 将Y轴置为0
  model.position.copy(center.negate().setY(0));
};

添加材质

这里先抽离个创建材质的方法出来,这里就简单创建一个颜色的材质。

js 复制代码
const colorMaterial = (color: string) => {
  return new THREE.MeshPhongMaterial({
    color
  });
};

在前面加载的model模型部分,可以通过traverse方法去拿到模型里面所有的对象,可以这么理解。然后这里面的item.name和导入的模型是有关系的,可以打印出来看一下模型里面的name都是一些什么,再去进行匹配添加材质。

js 复制代码
const addCity = () => {
  const gltfLoader = new GLTFLoader();
  gltfLoader.load('./src/assets/glb/shanghai_city.glb', (gltf: any) => {
    model = gltf.scene.children[0];
    toCenter();
    model.traverse((item: { name: string, material: any }) => {
      console.log(' =====', item.name);
      if (item.name.includes('dongfangmingzhu')) {
        item.material = colorMaterial('#f40');
      } else if (item.name.includes('shanghaizhongxin')) {
        item.material = colorMaterial('#aa21ee');
      } else if (item.name.includes('huanqiujinrongzhongxin')) {
        item.material = colorMaterial('#008000');
      } else if (item.name.includes('jinmaodasha')) {
        item.material = colorMaterial('#0000ff');
      } else if (item.name.includes('Floor')) {
        item.material = colorMaterial('#ffffff');
      } else if (item.name.includes('city')) {
        item.material = colorMaterial('#867e76');
      }
    });
    scene.add(model);
  });
};

效果:

纹理

创建一个纹理:

  • wrapS:这个值定义了纹理贴图在水平方向上将如何包裹
  • wrapT:这个值定义了纹理贴图在垂直方向上将如何包裹
  • .repeat:决定纹理在表面的重复次数,两个方向分别表示U和V
js 复制代码
const ImageLoader = new THREE.ImageLoader();
let texture: { needsUpdate: boolean; };
ImageLoader.load(TextureImage, function (img: any) {
  texture = new THREE.Texture(img, {
    wraps: THREE.RepeatWrapping,
    repeat: new THREE.Vector2(10, 10),
    wrapT: THREE.RepeatWrapping
  });
  texture.needsUpdate = true;
});

将纹理添加到材质上,那么这个时候改一下前面那个创建材质的方法

  • map:纹理贴图
  • alphaMap:一张灰度纹理,用于控制整个表面的不透明度
  • aoMapIntensity:环境遮挡效果的强度。默认值为1。零是不遮挡效果
  • bumpMap:用于创建凹凸贴图的纹理
  • bumpScale:凹凸贴图会对材质产生多大影响。典型范围是0-1。默认值为1
js 复制代码
const colorMaterial = (color: string, texture: any) => {
  let options = {};
  if (texture) {
    options = {
      map: texture,
      alphaMap: texture,
      aoMapIntensity: 0,
      bumpMap: texture,
      bumpScale: 0
    };
  } else {
    options = {
      color,
      shininess: 100,
      flatShading: true
    };
  }
  return new THREE.MeshPhongMaterial(options);
};

效果示意:

视角限制

这个在上一个船的就解释过了,直接复制过来使用就好了,去掉了最大最小距离的配置(minDistance、maxDistance)

js 复制代码
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.maxPolarAngle = Math.PI * 0.45;
// controls.minDistance = 5.0;
// controls.maxDistance = 15.0;
controls.update();

完整代码

html 复制代码
<!-- 组件 -->
<script lang="ts" setup>
import * as THREE from 'three';
import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls';
import {GLTFLoader} from 'three/examples/jsm/loaders/GLTFLoader';
import TextureImage from '@/assets/image/4.jpg';

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.001, 2000);
camera.position.set(2, 2, 2);
scene.add(camera);

// 环境光
const ambientLight = new THREE.AmbientLight('white', 0.5);
ambientLight.position.set(1, 1, 1);
scene.add(ambientLight);

const light = new THREE.DirectionalLight('#fff', 1);
light.position.set(1, 10, 1);
camera.add(light);

const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);

const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;

const axesHelper = new THREE.AxesHelper(1);
scene.add(axesHelper);

// 加载模型
let model: {
  scale: { set: (arg0: number, arg1: number, arg2: number) => void; };
  rotation: { z: number; };
  position: { z: number; };
  material: any;
};

const addCity = () => {
  const gltfLoader = new GLTFLoader();
  gltfLoader.load('./src/assets/glb/shanghai_city.glb', (gltf: any) => {
    model = gltf.scene.children[0];
    toCenter();
    model.traverse((item: { name: string, material: any }) => {
      console.log(' =====', item.name);
      if (item.name.includes('dongfangmingzhu')) {
        item.material = colorMaterial('#f40', null);
      } else if (item.name.includes('shanghaizhongxin')) {
        item.material = colorMaterial('#d90000', null);
      } else if (item.name.includes('huanqiujinrongzhongxin')) {
        item.material = colorMaterial('#008000', null);
      } else if (item.name.includes('jinmaodasha')) {
        item.material = colorMaterial('#0000ff', null);
      } else if (item.name.includes('Floor')) {
        item.material = colorMaterial('#ffffff', texture);
      } else if (item.name.includes('city')) {
        item.material = colorMaterial('#ffffff', texture);
      }
    });
    scene.add(model);
  });
};

const toCenter = () => {
  const box = new THREE.Box3();
  box.expandByObject(model);
  console.log('box =====', box);
  const size = new THREE.Vector3();
  const center = new THREE.Vector3();
  console.log('size,center =====', size, center);
  // 获取包围盒的中心点和尺寸
  box.getCenter(center);
  box.getSize(size);
  // 将Y轴置为0
  model.position.copy(center.negate().setY(0));
};


// 图片加载器
const ImageLoader = new THREE.ImageLoader();
let texture: { needsUpdate: boolean; };
ImageLoader.load(TextureImage, function (img: any) {
  texture = new THREE.Texture(img, {
    wraps: THREE.RepeatWrapping,
    repeat: new THREE.Vector2(10, 10),
    wrapT: THREE.RepeatWrapping
  });
  texture.needsUpdate = true;
});

const colorMaterial = (color: string, texture: any) => {
  let options = {};
  if (texture) {
    options = {
      map: texture,
      alphaMap: texture,
      aoMapIntensity: 0,
      bumpMap: texture,
      bumpScale: 0
    };
  } else {
    options = {
      color,
      shininess: 100,
      flatShading: true
    };
  }
  return new THREE.MeshPhongMaterial(options);
};


// 渲染
const render = () => {
  renderer.render(scene, camera);
  requestAnimationFrame(render);
};

onMounted(() => {
  document.getElementById('home')?.appendChild(renderer.domElement);
  addCity();

  render();
});
</script>

<template>
  <div id="home" class="w-full h-full"></div>
</template>
相关推荐
小兮风7 小时前
RockTrack:A 3D Robust Multi-Camera-Ken Multi-Object Tracking Framework
3d
山水阳泉曲2 天前
开发后台管理系统-开发环境搭建
vue3·cdn·后台管理·从零开始
光学测量小菜鸡2 天前
线结构光测量系统标定--导轨
数码相机·算法·3d
小彭努力中2 天前
20. gui调试3-下拉菜单、单选框
前端·3d·webgl
陈逸子风2 天前
.net core8 使用JWT鉴权(附当前源码)
vue3·webapi·权限·流程
用你的胜利博我一笑吧2 天前
vue3+ts+supermap iclient3d for cesium功能集合
前端·javascript·vue.js·3d·cesium·supermap
这是我583 天前
C++3D迷宫
c++·3d·visual studio·z·x·迷宫·y
我是瓦力3 天前
PointNet++改进策略 :模块改进 | EdgeConv | DGCNN, 动态图卷积在3d任务上应用
人工智能·深度学习·计算机视觉·3d
Mr.mjw3 天前
项目中使用简单的立体3D柱状图,不用引入外部组件纯css也能实现
前端·css·3d