【Three.js基础学习】14.Galaxy Generator

embedded/2024/10/19 19:40:32/

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

  课程知识点

        1. 实现星际编辑器

        2. 创建粒子 1000, 在随机位置

        3. 创建材质 PointsMaterial

        4. Points() 接收

        5. 放到gui 中调试  但是会发现调整size 等 属性 页面无变化

            因此有两种方法 onChange() onFinishChange()

        这个时候更改就会 将参数传到 generateGalaxy

        同样有问题 增加可以 ,因为在数值增加 不断生成新的星系

        数值缩小时候,但是没有消减

        因此要在函数外部 创建geometry,position,material 这些变量

        判断是否和之前相等 ,不相等则 dispose()  清除几何体缓存,材质缓存同时

        删除这些点 scene.remove(points)

        // 实现漩涡星系 思路

        首先将所有粒子 放到一条直线上 ,x  position[i3 + 0] = Math.random() * parameters.radius

        要实现分支  %  取余操作

        这样在循环中 就可以得到 branchAngle=0,1,2,0,1,2,0,1,2,0,1,2,

                        对应的       i    =0,1,2,3,4,5,6,7,8,9

        下面主要是数学的应用 通过更改数值 实现星系

一、代码

javascript">import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'
import * as lil from 'lil-gui'
import { AdditiveBlending } from 'three'/* 课程知识点1. 实现星际编辑器2. 创建粒子 1000, 在随机位置3. 创建材质 PointsMaterial4. Points() 接收5. 放到gui 中调试  但是会发现调整size 等 属性 页面无变化因此有两种方法 onChange() onFinishChange()这个时候更改就会 将参数传到 generateGalaxy同样有问题 增加可以 ,因为在数值增加 不断生成新的星系数值缩小时候,但是没有消减因此要在函数外部 创建geometry,position,material 这些变量判断是否和之前相等 ,不相等则 dispose()  清除几何体缓存,材质缓存同时删除这些点 scene.remove(points)// 实现漩涡星系 思路首先将所有粒子 放到一条直线上 ,x  position[i3 + 0] = Math.random() * parameters.radius要实现分支  %  取余操作这样在循环中 就可以得到 branchAngle=0,1,2,0,1,2,0,1,2,0,1,2,对应的       i    =0,1,2,3,4,5,6,7,8,9下面主要是数学的应用 通过更改数值 实现星系
*//*** Base*/
// Debug
const gui = new lil.GUI()// Canvas
const canvas = document.querySelector('canvas.webgl')// Scene
const scene = new THREE.Scene()/* 
* Galaxy 星系 Generator 编辑器
*/
const parameters = {}
parameters.count = 100000
parameters.size = 0.01
parameters.color = '#ffffff'
parameters.radius = 5 // 半径
parameters.branches = 3 // 分支
parameters.spin = 1 // 旋转
parameters.randomness = 0.2 // 随机值
parameters.randomnessPower = 3 // 
parameters.insideColor = '#ff6030' // 
parameters.outsideColor = '#1b3984' let geometry = null
let points = null
let material = null
const generateGalaxy = () =>{/* * Destroy old galaxy*/if(points !== null){geometry.dispose()material.dispose()scene.remove(points)}// Geometrygeometry = new THREE.BufferGeometry()// 创建粒子 位置参数const position = new Float32Array(parameters.count * 3)const colors = new Float32Array(parameters.count * 3)const colorInside = new THREE.Color(parameters.insideColor)const colorOutside = new THREE.Color(parameters.outsideColor)// 设置随机位置for(let i=0;i<parameters.count;i++){const i3 = i * 3// positionconst radius = Math.random() * parameters.radiusconst sponAngle = radius * parameters.spinconst branchAngle = (i % parameters.branches) / parameters.branches * Math.PI * 2const randomX = Math.pow(Math.random(), parameters.randomnessPower) * (Math.random() < 0.5 ? 1 : -1)const randomY = Math.pow(Math.random(), parameters.randomnessPower) * (Math.random() < 0.5 ? 1 : -1)const randomZ = Math.pow(Math.random(), parameters.randomnessPower) * (Math.random() < 0.5 ? 1 : -1)position[i3 + 0] = Math.cos(branchAngle + sponAngle) * radius + randomXposition[i3 + 1] = randomYposition[i3 + 2] = Math.sin(branchAngle + sponAngle) * radius + randomZ// position[i3 + 0] = (Math.random() - 0.5) * 10  // x位置// position[i3 + 1] = (Math.random() - 0.5) * 10  // y// position[i3 + 2] = (Math.random() - 0.5) * 10  // z// colorconst mixedColor = colorInside.clone()mixedColor.lerp(colorOutside,radius/parameters.radius)colors[i3 + 0] = mixedColor.rcolors[i3 + 1] = mixedColor.gcolors[i3 + 2] = mixedColor.b}geometry.setAttribute('position',new THREE.BufferAttribute(position,3))geometry.setAttribute('color',new THREE.BufferAttribute(colors,3))// Materialmaterial = new THREE.PointsMaterial({// color:parameters.color,size:parameters.size,sizeAttenuation:true, // 衰减depthWrite:false, // 深度缓冲区blending: THREE.AdditiveBlending,vertexColors:true})// material.vertexColors = true/* *   Points*/points = new THREE.Points(geometry,material)scene.add(points)
}
gui.add(parameters, 'count').min(100).max(100000).step(100).onFinishChange(generateGalaxy)
gui.add(parameters, 'size').min(0.001).max(1).step(0.001).onFinishChange(generateGalaxy)
gui.add(parameters, 'radius').min(0.01).max(20).step(0.01).onFinishChange(generateGalaxy)
gui.add(parameters, 'branches').min(2).max(20).step(1).onFinishChange(generateGalaxy)
gui.add(parameters, 'spin').min(-5).max(5).step(0.001).onFinishChange(generateGalaxy)
gui.add(parameters, 'randomness').min(0).max(2).step(0.001).onFinishChange(generateGalaxy)
gui.add(parameters, 'randomnessPower').min(1).max(10).step(0.001).onFinishChange(generateGalaxy)
gui.addColor(parameters, 'insideColor').onFinishChange(generateGalaxy)
gui.addColor(parameters, 'outsideColor').onFinishChange(generateGalaxy)generateGalaxy()/*** Sizes*/
const sizes = {width: window.innerWidth,height: window.innerHeight
}window.addEventListener('resize', () =>
{// Update sizessizes.width = window.innerWidthsizes.height = window.innerHeight// Update cameracamera.aspect = sizes.width / sizes.heightcamera.updateProjectionMatrix()// Update rendererrenderer.setSize(sizes.width, sizes.height)renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
})/*** Camera*/
// Base camera
const camera = new THREE.PerspectiveCamera(75, sizes.width / sizes.height, 0.1, 100)
camera.position.x = 3
camera.position.y = 3
camera.position.z = 3
scene.add(camera)// Controls
const controls = new OrbitControls(camera, canvas)
controls.enableDamping = true/*** Renderer*/
const renderer = new THREE.WebGLRenderer({canvas: canvas
})
renderer.setSize(sizes.width, sizes.height)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))/*** Animate*/
const clock = new THREE.Clock()const tick = () =>
{const elapsedTime = clock.getElapsedTime()// Update controlscontrols.update()// Renderrenderer.render(scene, camera)// Call tick again on the next framewindow.requestAnimationFrame(tick)
}tick()

二、知识点

1.思路图

2.效果


总结

五一前看的,全被自己吃进肚子里了,想想怎么实现的一头雾水!还是要看之前代码,很烦...


http://www.ppmy.cn/embedded/35405.html

相关文章

CI/CD笔记.Gitlab系列.新用户管理

CI/CD笔记.Gitlab系列 新用户管理 - 文章信息 - Author: 李俊才 (jcLee95) Visit me at CSDN: https://jclee95.blog.csdn.netMy WebSite&#xff1a;http://thispage.tech/Email: 291148484163.com. Shenzhen ChinaAddress of this article:https://blog.csdn.net/qq_285502…

Go 语言(四)【常用包使用】

1、命令行参数包 flag flag 包就是一个用来解析命令行参数的工具。 1.1、os.Args import ("fmt""os" )func main() {if len(os.Args) > 0 {for index, arg : range os.Args {fmt.Printf("args[%d]%v\n", index, arg)}} } 运行结果&#…

算法训练营第二十天 | LeetCode 110平衡二叉树、LeetCode 257 二叉树的所有路径、LeetCode 404 左叶子之和

LeetCode 110 平衡二叉树 递归写法很简单&#xff0c;直接自底向上每个节点判断是否为空&#xff0c;为空说明该层高度为0。不为空用一个int型变量l记录左子树高度&#xff08;递归调用该函数自身&#xff09;&#xff0c;一个int型变量r记录右子树高度&#xff08;同样递归调…

NFS服务器(linux-linux)

目录 简介 NFS背景介绍 生产应用场景 NFS工作原理 示例图 流程 NFS的使用 安装 配置文件 主配置文件分析 实验1 NFS账户映射 实验2&#xff1a; 实验3 autofs自动挂载服务 产生原因 安装 配置文件分析 实验4 实验5 简介 NFS背景介绍 NFS是一种古老的用于…

「C/C++ 01」volatile关键字 和 修改const修饰的变量

目录 一、修改const修饰的局部变量 二、无法修改const修饰的全局变量 三、volatile关键字 面试题】 一、修改const修饰的局部变量 可以通过指针和强转来修改const修饰的局部变量。 #include <iostream> using namespace std;int main(void) {const int a 1;int* pa (in…

Android 拼音解析库 Pinyin4j 的介绍及其使用

拼音是汉语的一种辅助拼音文字&#xff0c;用于帮助人们学习汉语的读音和拼写。拼音解析库能够将汉字转换为拼音&#xff0c;并提供多种功能&#xff0c;例如声调标注、拼音格式转换、多音字处理等。 拼音解析库 Pinyin4j 是一个用于将汉字转换为汉语拼音的 Java 库。它提供了…

java中多线程的3种实现方法

1.继承Thread类 优点&#xff1a;代码简单&#xff0c;可以直接使用Thread类里面的方法。 缺点&#xff1a;扩张性较差&#xff0c;应为在java中&#xff0c;一个类只能继承一个父类。 2.实现Runnable接口 3.实现Callable接口 2和3的优缺点是一样的 优点&#xff1a;扩展性强&…

快速入门!学习鸿蒙App开发的终极指南!

鸿蒙&#xff08;HarmonyOS&#xff09;是华为推出的一款分布式操作系统&#xff0c;旨在为不同设备提供统一的操作体验。鸿蒙App开发可以让应用程序在多个设备上实现流畅运行。本文将介绍鸿蒙App开发的终极指南&#xff0c;帮助您快速入门。 开发环境搭建 鸿蒙App开发过程需要…