当前位置: 首页 > news >正文

八、3d场景的区域光墙

        在遇到区域展示的时候我们就能看到炫酷的区域选中效果,那么代码是怎么编辑的呢,今天咱们就好好说说,下面看实现效果。

思路:

  1. 首先,光墙肯定有多个,那么必须要创建一个新的js文件来作为他的原型对象。
  2. 这个光墙是用c++写的,但是必须是拿js包裹的,否则加入不进Vue项目中。
  3. City文件加载引入,根据具体的传入参数一一对应上位置。

创建lightwall.js文件,传1表示是一个方的光柱,2是个圆的光柱

import * as THREE from "three";
import vertexShader from "@/shader/lightWall/vertex.js";
import fragmentShader from "@/shader/lightWall/fragment.js";
export default class LightWall {constructor(type = 1,radius = 5,radius1 = 5,length = 2,position = { x: 0, z: 0 },) {this.geometry = null//type是1表示方形柱,2是圆形柱if (type == 1) {this.geometry = new THREE.BoxBufferGeometry(radius,20,radius1,);}if (type == 2) {this.geometry = new THREE.CylinderBufferGeometry(radius,radius1,20,32,1,true);}this.material = new THREE.ShaderMaterial({vertexShader: vertexShader,fragmentShader: fragmentShader,transparent: true,side: THREE.DoubleSide,});this.mesh = new THREE.Mesh(this.geometry, this.material);this.mesh.position.set(position.x, 78, position.z);this.mesh.geometry.computeBoundingBox();this.mesh.scale.set(length, 2, length);//   console.log(mesh.geometry.boundingBox);let { min, max } = this.mesh.geometry.boundingBox;//   获取物体的高度差let uHeight = max.y - min.y;this.material.uniforms.uHeight = {value: uHeight,};}remove () {this.mesh.remove();this.mesh.removeFromParent();this.mesh.geometry.dispose();this.mesh.material.dispose();}
}

再就是引入光墙的c++代码,也就是上面引入的vertex.js

const fragmentShader = /*glsl*/ `
varying vec3 vPosition;
uniform float uHeight;
void main(){// 设置混合的百分比float gradMix = (vPosition.y+uHeight/2.0)/uHeight;gl_FragColor = vec4(0.7,0.5,0.35,1.0-gradMix);}`
export default fragmentShader

最后在主文件使用,引入到scene中

// 添加光墙
import LightWall from "./LightWall";
const lightWall = new LightWall(1, 12, 24, 10, { x: -78, z: -48 });
scene.add(lightWall.mesh);

以上就把这个光墙封装为一个类,当使用的时候只需要new就行了,是不是很方便呢,当然你也可以扩展增加参数使用这个东西,如果又不会的可以私信或者留言哦。

http://www.lryc.cn/news/180686.html

相关文章:

  • 深入探讨 Presto 中的缓存
  • 3.物联网射频识别,(高频)RFID应用ISO14443-2协议,(校园卡)Mifare S50卡
  • 【IDEA】IDEA 单行注释开头添加空格
  • 三等分功分器[波导]设计详细教程
  • Mysql分库分表
  • 【算法学习】-【双指针】-【复写零】
  • 【算法优选】双指针专题——叁
  • Java栈的压入、弹出序列(详解)
  • RabbitMQ学习笔记(消息发布确认,死信队列,集群,交换机,持久化,生产者、消费者)
  • PyTorch - 模型训练损失 (Loss) NaN 问题的解决方案
  • 8、Nacos服务注册服务端源码分析(七)
  • MySQL使用Xtrabackup在线做主从
  • scala基础入门
  • 【Java-LangChain:面向开发者的提示工程-5】推断
  • 【C++】手撕vector(vector的模拟实现)
  • 智能指针那些事
  • Fiddler抓取手机https包的步骤
  • idea没有maven工具栏解决方法
  • levelDB引擎
  • IM同步服务
  • MySQL 运维常用脚本
  • ABC322刷题记
  • visual studio的安装及scanf报错的解决
  • React生命周期
  • SpringBoot整合RocketMQ笔记
  • 【【萌新的RiscV学习之在写代码之前对于关键路径的分析-11】】
  • A. Sequence with Digits
  • gitlab配置webhook限制提交注释
  • 蓝桥杯Python scratch C++选拔赛stema个人如何报名?
  • Cesium实现动态旋转四棱锥(2023.9.11)