首页 > 其他分享 >babylon.js 学习笔记(10)

babylon.js 学习笔记(10)

时间:2023-06-17 23:22:17浏览次数:44  
标签:10 const babylon Vector3 scene js new BABYLON particleSystem

今天来学习下车床(lathe)建型及粒子系统,babylon.js有一个很强大的函数CreateLathe,可以将一段路径经过旋转后,形成1个shape,这么说有点抽象,比如下面这张图:

其中的关键点坐标为:

const fountainProfile = [
    new BABYLON.Vector3(0, 0, 0),
    new BABYLON.Vector3(10, 0, 0),
    new BABYLON.Vector3(10, 4, 0),
    new BABYLON.Vector3(8, 4, 0),
    new BABYLON.Vector3(8, 1, 0),
    new BABYLON.Vector3(1, 2, 0),
    new BABYLON.Vector3(1, 15, 0),
    new BABYLON.Vector3(3, 17, 0)
];

调用CreateLathe后:

const fountain = BABYLON.MeshBuilder.CreateLathe("fountain", { shape: fountainProfile, sideOrientation: BABYLON.Mesh.DOUBLESIDE }, scene);

再给几个示例:

const fountainProfile = [
    new BABYLON.Vector3(0, 0, 0),
    new BABYLON.Vector3(10, 0, 0),
    new BABYLON.Vector3(10, 4, 0),
    new BABYLON.Vector3(8, 4, 0),
    new BABYLON.Vector3(8, 1, 0),
    new BABYLON.Vector3(1, 2, 0),
    new BABYLON.Vector3(1, 15, 0),
    new BABYLON.Vector3(3, 17, 0)
];

const myShape = [
    new BABYLON.Vector3(3, 0, 0),
    new BABYLON.Vector3(10, 5, 0),
    new BABYLON.Vector3(5, 10, 0),
    new BABYLON.Vector3(12, 15, 0),
    new BABYLON.Vector3(3, 20, 0)
];

const createScene = function () {
    const scene = new BABYLON.Scene(engine);

    const camera = new BABYLON.ArcRotateCamera("Camera", 3 * Math.PI / 2, Math.PI / 2.5, 70, BABYLON.Vector3.Zero());
    camera.attachControl(canvas, true);

    const light = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(0, 1, 0));

    //Create lathe1
    const fountain = BABYLON.MeshBuilder.CreateLathe("fountain", { shape: fountainProfile, sideOrientation: BABYLON.Mesh.DOUBLESIDE }, scene);

    //Create lathe2
    const lathe1 = BABYLON.MeshBuilder.CreateLathe("lathe1", { shape: myShape, sideOrientation: BABYLON.Mesh.DOUBLESIDE });
    lathe1.position.x = -30;
    lathe1.scaling = new BABYLON.Vector3(0.75, 0.75, 0.75);

    //Create lathe3
    const lathe2 = BABYLON.MeshBuilder.CreateLathe("lathe2", { shape: myShape, closed: false, arc: 0.75, sideOrientation: BABYLON.Mesh.DOUBLESIDE });
    lathe2.position.x = 30;
    lathe2.scaling = new BABYLON.Vector3(0.75, 0.75, 0.75);

    showAxis(24, scene);
    return scene;
}

最右侧的残缺效果,主要是 closed: false, arc: 0.75 这2个参数起了作用。

在线地址:https://yjmyzz.github.io/babylon_js_study/day10/01.html

 

接下来看看粒子系统,直接上代码,建议大家调整下这里面的参数,感受不同的效果:

const createScene = function () {
    const scene = new BABYLON.Scene(engine);

    const camera = new BABYLON.ArcRotateCamera("Camera", 3 * Math.PI / 2, Math.PI / 2.5, 70, BABYLON.Vector3.Zero());
    camera.attachControl(canvas, true);

    const light = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(0, 1, 0));

    // 创建粒子系统
    var particleSystem = new BABYLON.ParticleSystem("particles", 5000, scene);

    //粒子的纹理图片
    particleSystem.particleTexture = new BABYLON.Texture("../assets/img/flare.png", scene);

    //粒子的发射距离
    particleSystem.emitter = new BABYLON.Vector3(0, 5, 0); // the starting object, the emitter
    particleSystem.minEmitBox = new BABYLON.Vector3(-1, -5, 0); // Starting all from
    particleSystem.maxEmitBox = new BABYLON.Vector3(1, -5, 0); // To...

    //粒子颜色
    particleSystem.color1 = new BABYLON.Color4(0.7, 0.8, 1.0, 1.0);
    particleSystem.color2 = new BABYLON.Color4(0.2, 0.5, 1.0, 1.0);
    particleSystem.colorDead = new BABYLON.Color4(0, 0, 0.2, 0.0);

    //粒子大小
    particleSystem.minSize = 0.1;
    particleSystem.maxSize = 0.6;

    //粒子存在的生命周期(时长)
    particleSystem.minLifeTime = 2;
    particleSystem.maxLifeTime = 3.8;

    //发射速率
    particleSystem.emitRate = 1500;

    //混合模式 : BLENDMODE_ONEONE, or BLENDMODE_STANDARD
    particleSystem.blendMode = BABYLON.ParticleSystem.BLENDMODE_ONEONE;

    //重力值
    particleSystem.gravity = new BABYLON.Vector3(0, -9.81, 0);

    //发射方向
    particleSystem.direction1 = new BABYLON.Vector3(-3, 8, 3);
    particleSystem.direction2 = new BABYLON.Vector3(3, 8, -3);

    //角度、半径
    particleSystem.minAngularSpeed = 0;
    particleSystem.maxAngularSpeed = Math.PI;

    //速度及力度大小
    particleSystem.minEmitPower = 1;
    particleSystem.maxEmitPower = 2.2;
    particleSystem.updateSpeed = 0.025;

    //开喷
    particleSystem.start();

    return scene;
}

其中flare.jpg长这样:

上面这段代码跑出来,效果是这样的:

在线地址:https://yjmyzz.github.io/babylon_js_study/day10/02.html

 
把今天学到的2个知识点,结合一下,就变成这样:

const fountainProfile = [
    new BABYLON.Vector3(0, 0, 0),
    new BABYLON.Vector3(10, 0, 0),
    new BABYLON.Vector3(10, 4, 0),
    new BABYLON.Vector3(8, 4, 0),
    new BABYLON.Vector3(8, 1, 0),
    new BABYLON.Vector3(1, 2, 0),
    new BABYLON.Vector3(1, 15, 0),
    new BABYLON.Vector3(3, 17, 0)
];

const createScene = function () {
    const scene = new BABYLON.Scene(engine);

    const camera = new BABYLON.ArcRotateCamera("Camera", 3 * Math.PI / 2, Math.PI / 2.5, 70, BABYLON.Vector3.Zero());
    camera.attachControl(canvas, true);

    const light = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(0, 1, 0));

    const fountain = BABYLON.MeshBuilder.CreateLathe("fountain", { shape: fountainProfile, sideOrientation: BABYLON.Mesh.DOUBLESIDE }, scene);
    fountain.position.y = -15;

    // Create a particle system
    var particleSystem = new BABYLON.ParticleSystem("particles", 5000, scene);

    //Texture of each particle
    particleSystem.particleTexture = new BABYLON.Texture("../assets/img/flare.png", scene);

    // Where the particles come from
    particleSystem.emitter = new BABYLON.Vector3(0, 5, 0); // the starting object, the emitter
    particleSystem.minEmitBox = new BABYLON.Vector3(-1, -5, 0); // Starting all from
    particleSystem.maxEmitBox = new BABYLON.Vector3(1, -5, 0); // To...

    // Colors of all particles
    particleSystem.color1 = new BABYLON.Color4(0.7, 0.8, 1.0, 1.0);
    particleSystem.color2 = new BABYLON.Color4(0.2, 0.5, 1.0, 1.0);
    particleSystem.colorDead = new BABYLON.Color4(0, 0, 0.2, 0.0);

    // Size of each particle (random between...
    particleSystem.minSize = 0.1;
    particleSystem.maxSize = 0.6;

    // Life time of each particle (random between...
    particleSystem.minLifeTime = 2;
    particleSystem.maxLifeTime = 3.8;

    // Emission rate
    particleSystem.emitRate = 1500;

    // Blend mode : BLENDMODE_ONEONE, or BLENDMODE_STANDARD
    particleSystem.blendMode = BABYLON.ParticleSystem.BLENDMODE_ONEONE;

    // Set the gravity of all particles
    particleSystem.gravity = new BABYLON.Vector3(0, -9.81, 0);

    // Direction of each particle after it has been emitted
    particleSystem.direction1 = new BABYLON.Vector3(-3, 8, 3);
    particleSystem.direction2 = new BABYLON.Vector3(3, 8, -3);

    // Angular speed, in radians
    particleSystem.minAngularSpeed = 0;
    particleSystem.maxAngularSpeed = Math.PI;

    // Speed
    particleSystem.minEmitPower = 1;
    particleSystem.maxEmitPower = 2.2;
    particleSystem.updateSpeed = 0.025;

    // Start the particle system
    particleSystem.start();

    return scene;
}

在线地址:https://yjmyzz.github.io/babylon_js_study/day10/03.html

 

官网还有很多粒子系统的精彩示例,感兴趣的同学可以深入研究:

标签:10,const,babylon,Vector3,scene,js,new,BABYLON,particleSystem
From: https://www.cnblogs.com/yjmyzz/p/babylon_js_study_10.html

相关文章

  • 【Python 随练】一个整数,它加上 100 后是一个完全平方数,再加上 168 又是一个完全平方
    题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?简介:在本篇博客中,我们将解决一个数学问题:找到满足一定条件的整数。我们将提供问题的解析,并给出一个完整的代码示例来找出符合条件的整数。问题分析:我们需要找到一个整数,它加上100后是一个......
  • 10. docker方式下的mysql设置主从复制(一主两从)
    上一篇【centos使用docker方式安装mysql】笔记中,我们在三个虚拟机中使用docker方式新建了三个mysql容器服务,那么我们这篇文章来记录下,如何在这三台机器中设置mysql的主从复制功能。其中111服务器作为主节点,112和113两个服务器作为两个从节点,复制111服务器mysq......
  • 使用 Vue.js 的 CDN(内容分发网络)来添加 Vue.js
    在您的HTML文件中添加script标签,并指定Vue.js的CDN地址。通常可以通过在 <head> 标签或 <body> 标签的底部添加该标签来加载Vue.js。下面是一个常用的Vue.jsCDN地址:<scriptsrc="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>一下是一个小小的案......
  • MASM error A2108: use of register assumed to ERROR
    errorA2108:useofregisterassumedtoERRORASSUMEFS:NOTHINGmoveax,fs:[0c0h]ASSUMEFS:NOTHINGErrora2108useofregisterassumedtoerror......
  • three.js 置换贴图 alpha贴图 的妙用 - 3D文字不引入字体文件
    实现将文字绘制到canvascanvas生成置换贴图alpha贴图将canvas转换成texture将texture贴到material修改shader将黑色背景区域去掉视频教程请移步b站canvas生成贴图classCanvas{canvas:HTMLCanvasElement=document.createElement("canvas");protectedctx:CanvasRen......
  • WIN10安装配置Hadoop
    原文链接:WIN10安装配置Hadoop【作者:余生】本文记录在WIN10上hadoop单节点的安装,后续再记录多节点分布式的安装。1、安装JAVA环境下载JDK8,官网链接:JavaSEDevelopmentKit8选择64版本:jdk-8u241-windows-x64.exe下载过程会提示注册oracle账号,请注册,如果是已有oracle账号的可......
  • 基类属性如何反序列化表示具体类的Json字符串
    JsonConverter可以决定类型如何被序列化或反序列化。接口属性被反序列化时,会抛出异常,因为接口没有构造函数。JsonConvert.DeserializeObject<IVehicle>("Json字符串");JsonConvert.DeserializeObject<List<IVehicle>>("Json字符串");JsonConvert.DeserializeObject<Worker>(......
  • IOT硬件&芯片安全检测工具--BTS1002多接口精密触发故障注入仪
    注1:该软硬件产品均为湖南底网安全信息技术有限公司自主研发,已申请1个发明专利,2个实用新型专利,1个软著,转载请注明出处注2:详细内容详见产品技术手册、用户手册,官网下载地址:注3:诚招区域独家代理合作伙伴,欢迎洽谈合作,湖南底网安全竭诚为您服务1.产品背景没有网络安全,就没有国家安全,“......
  • threejs-初识shader
    GLSL文件: importvertexGLSLfrom'./shaders/test1-patterns/vertex.glsl?raw' uniformmat4projectionMatrix;uniformmat4viewMatrix;uniformmat4modelMatrix;uniformvec2uFrequency;uniformfloatuTime;attributevec2uv;attributevec3po......
  • MySQL错误类型1030
     该错误类型一般为磁盘内存空间不足。常规情况下清除备份文件即可。引用:mysql出现1030Goterror28fromstorageengine解决方法_风火程序员的博客-CSDN博客......