前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >babylon.js 学习笔记(10)

babylon.js 学习笔记(10)

作者头像
菩提树下的杨过
发布2023-07-10 11:04:26
2650
发布2023-07-10 11:04:26
举报

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

其中的关键点坐标为:

代码语言:javascript
复制
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后:

代码语言:javascript
复制
const fountain = BABYLON.MeshBuilder.CreateLathe("fountain", { shape: fountainProfile, sideOrientation: BABYLON.Mesh.DOUBLESIDE }, scene);

再给几个示例:

代码语言:javascript
复制
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

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

代码语言:javascript
复制
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个知识点,结合一下,就变成这样:

代码语言:javascript
复制
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

还可以给这个喷泉加点交互,鼠标点击到喷泉时,切换喷发

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

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

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

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

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-06-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档