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

three完全开源扩展案例05-围栏着色器

在这里插入图片描述
https://www.threelab.cn/three-cesium-examples/public/index.html#/codeMirror?navigation=Three.js%E6%A1%88%E4%BE%8B[r166]&classify=shader&id=fenceShader

更多案例

import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'const box = document.getElementById('box')const scene = new THREE.Scene()const camera = new THREE.PerspectiveCamera(75, box.clientWidth / box.clientHeight, 0.1, 1000)camera.position.set(0, 40, 40)const renderer = new THREE.WebGLRenderer()renderer.setSize(box.clientWidth, box.clientHeight)new OrbitControls(camera, renderer.domElement)window.onresize = () => {renderer.setSize(box.clientWidth, box.clientHeight)camera.aspect = box.clientWidth / box.clientHeightcamera.updateProjectionMatrix()}box.appendChild(renderer.domElement)/* 顶点着色器 */
const vertexs = `varying vec2 vUv;
void main() {vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );vUv = uv;gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}`/* 片元着色器 */
const fragments = `
uniform float time;
uniform float opacity;
uniform vec3 color;
uniform float num;
uniform float hiz;
varying vec2 vUv;
void main() {vec4 fragColor = vec4(0.);float sin = sin((vUv.y - time * hiz) * 10. * num);float high = 0.92;float medium = 0.4;if (sin > high) {fragColor = vec4(mix(vec3(.8, 1., 1.), color, (1. - sin) / (1. - high)), 1.);} else if(sin > medium) {fragColor = vec4(color, mix(1., 0., 1.-(sin - medium) / (high - medium)));} else {fragColor = vec4(color, 0.);}vec3 fade = mix(color, vec3(0., 0., 0.), vUv.y);fragColor = mix(fragColor, vec4(fade, 1.), 0.85);gl_FragColor = vec4(fragColor.rgb, fragColor.a * opacity * (1. - vUv.y));
}`// 自定义材质
const material = new THREE.ShaderMaterial({uniforms: {time: {type: "pv2",value: 0,},color: {type: "uvs",value: new THREE.Color("#FF4127"),},opacity: {type: "pv2",value: 1.0,},num: {type: "pv2",value: 10,},hiz: {type: "pv2",value: 0.03,},},vertexShader: vertexs,fragmentShader: fragments,blending: THREE.AdditiveBlending,transparent: !0,depthWrite: !1,depthTest: !0,side: THREE.DoubleSide,
});let c = [0,0, 10, 0, 10, 10, 0, 10, 0, 0]let posArr = [];let uvrr = [];let h = 10; //围墙拉伸高度for (let i = 0; i < c.length - 2; i += 2) {// 矩形的三角形1posArr.push(c[i], c[i + 1], 0, c[i + 2], c[i + 3], 0, c[i + 2], c[i + 3], h);// 矩形的三角形2posArr.push(c[i], c[i + 1], 0, c[i + 2], c[i + 3], h, c[i], c[i + 1], h);// 注意顺序问题,和顶点位置坐标对应uvrr.push(0, 0, 1, 0, 1, 1);uvrr.push(0, 0, 1, 1, 0, 1);}const geometry = new THREE.BufferGeometry(); //声明一个空几何体对象// 设置几何体attributes属性的位置position属性
geometry.attributes.position = new THREE.BufferAttribute(new Float32Array(posArr), 3);// 设置几何体attributes属性的位置uv属性
geometry.attributes.uv = new THREE.BufferAttribute(new Float32Array(uvrr), 2);geometry.computeVertexNormals()const mesh = new THREE.Mesh(geometry, material) //网格模型对象Meshscene.add(mesh)function animate() {requestAnimationFrame(animate)mesh.rotation.x += 0.01mesh.rotation.y += 0.01material.uniforms.time.value += 0.03renderer.render(scene, camera)}animate()

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 微信小程序 - 在视图组件上绑定函数并携带参数(事件对象自定义属性传参)
  • mysql-造数据/列转行
  • Tomcat部署、优化、压力测试
  • Python实现招聘数据采集 ,并做可视化分析
  • Gson使用TypeAdapterFactory和TypeAdapter实现Json中的Key统一修改
  • Web漏洞扫描工具(AWVS、Goby)
  • modulepreload 对性能的影响
  • spring部分源码分析及Bean的生命周期理解
  • ChatGPT:ChatOpenAI 是什么?
  • 计算机网络——1
  • 云动态摘要 2024-07-23
  • 为了实现接口缓存,专门写了个缓存库 f-cache-memory
  • 邮件安全篇:邮件反垃圾系统运作机制简介
  • 邮件安全篇:企业电子邮件安全涉及哪些方面?
  • 数据结构->线性结构->顺序存储->静态链表
  • [LeetCode] Wiggle Sort
  • 【347天】每日项目总结系列085(2018.01.18)
  • egg(89)--egg之redis的发布和订阅
  • electron原来这么简单----打包你的react、VUE桌面应用程序
  • leetcode98. Validate Binary Search Tree
  • mysql innodb 索引使用指南
  • PHP那些事儿
  • springboot_database项目介绍
  • vagrant 添加本地 box 安装 laravel homestead
  • 百度小程序遇到的问题
  • 产品三维模型在线预览
  • 从0搭建SpringBoot的HelloWorld -- Java版本
  • 等保2.0 | 几维安全发布等保检测、等保加固专版 加速企业等保合规
  • 汉诺塔算法
  • 记一次用 NodeJs 实现模拟登录的思路
  • 坑!为什么View.startAnimation不起作用?
  • 前端面试题总结
  • 浅谈Kotlin实战篇之自定义View图片圆角简单应用(一)
  • 使用 @font-face
  • 通过git安装npm私有模块
  • 鱼骨图 - 如何绘制?
  • Prometheus VS InfluxDB
  • Spark2.4.0源码分析之WorldCount 默认shuffling并行度为200(九) ...
  • 格斗健身潮牌24KiCK获近千万Pre-A轮融资,用户留存高达9个月 ...
  • ​​​​​​​​​​​​​​Γ函数
  • # 日期待t_最值得等的SUV奥迪Q9:空间比MPV还大,或搭4.0T,香
  • #LLM入门|Prompt#2.3_对查询任务进行分类|意图分析_Classification
  • #预处理和函数的对比以及条件编译
  • $.each()与$(selector).each()
  • $.extend({},旧的,新的);合并对象,后面的覆盖前面的
  • (11)工业界推荐系统-小红书推荐场景及内部实践【粗排三塔模型】
  • (20050108)又读《平凡的世界》
  • (PADS学习)第二章:原理图绘制 第一部分
  • (react踩过的坑)antd 如何同时获取一个select 的value和 label值
  • (二) Windows 下 Sublime Text 3 安装离线插件 Anaconda
  • (附源码)springboot“微印象”在线打印预约系统 毕业设计 061642
  • (附源码)ssm考生评分系统 毕业设计 071114
  • (考研湖科大教书匠计算机网络)第一章概述-第五节1:计算机网络体系结构之分层思想和举例
  • (欧拉)openEuler系统添加网卡文件配置流程、(欧拉)openEuler系统手动配置ipv6地址流程、(欧拉)openEuler系统网络管理说明
  • (转)jdk与jre的区别