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

babylon.js 学习笔记(9)

时间:2023-06-04 15:44:40浏览次数:46  
标签:const Sprite babylon 笔记 js position new height BABYLON

继续学习sprite用法,做为一个游戏引擎,怎能没有Sprite(精灵),下面是基本示例:

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

            const camera = new BABYLON.ArcRotateCamera("Camera", -Math.PI / 2, Math.PI / 2, 8, new BABYLON.Vector3(0, 0, 0));
            camera.attachControl(canvas, true);
            const light = new BABYLON.PointLight("Point", new BABYLON.Vector3(5, 10, 5));

            // Create a sprite manager
            // Parameters : name, imgUrl, capacity(最大容量,即树的总数), cellSize, scene
            const spriteManagerTrees = new BABYLON.SpriteManager("treesManager", "../assets/img/palm.png", 2000, { width: 512, height: 1024 });
            const tree1 = new BABYLON.Sprite("tree", spriteManagerTrees);
            //注:树的高宽比例,应该跟cellSize的比例一致,避免失真
            tree1.width = 1;
            tree1.height = 2;
            tree1.position.y = -0.5;


            const tree2 = new BABYLON.Sprite("tree2", spriteManagerTrees);
            tree2.width = 1;
            tree2.height = 2;
            tree2.position.x = -1.5;
            tree2.position.y = 0.8;
            //逆时针转45度
            tree2.angle = Math.PI / 4;

            const tree3 = new BABYLON.Sprite("tree3", spriteManagerTrees);
            tree3.width = 1;
            tree3.height = 2;
            tree3.position.x = 1.5;
            tree3.position.y = 0.8;
            //顺时针转45度
            tree3.angle = -Math.PI / 4;

            const tree4 = new BABYLON.Sprite("tree4", spriteManagerTrees);
            tree4.width = 1;
            tree4.height = 2;
            tree4.position.x = -1.5;
            tree4.position.y = -1.5;
            //垂直翻转
            tree4.invertV = true;

            const tree5 = new BABYLON.Sprite("tree5", spriteManagerTrees);
            tree5.width = 1;
            tree5.height = 2;
            tree5.position.x = 1.5;
            tree5.position.y = -1.5;
            //水平翻转
            tree5.invertU = true;


            showText();

            return scene;
        }


        const showText = function () {
            const ui = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("ui");
            const text1 = new BABYLON.GUI.TextBlock("t0", "tree1");
            const text2 = new BABYLON.GUI.TextBlock("t1", "tree2");
            const text3 = new BABYLON.GUI.TextBlock("t2", "tree3");
            const text4 = new BABYLON.GUI.TextBlock("t1", "tree4");
            const text5 = new BABYLON.GUI.TextBlock("t2", "tree5");

            text1.color = 'white';
            text2.color = 'white';
            text3.color = 'white';
            text4.color = 'white';
            text5.color = 'white';

            text1.top = '-10%';
            text2.top = '-27%';
            text3.top = '-27%';
            text4.top = '4%';
            text5.top = '4%';

            text1.left = '-0%';
            text2.left = '-18%';
            text3.left = '18%';
            text4.left = '-15%';
            text5.left = '15%';

            ui.addControl(text1);
            ui.addControl(text2);
            ui.addControl(text3);
            ui.addControl(text4);
            ui.addControl(text5);
        }

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

有1点要注意:

Sprite没有roration、scaling 这类mesh对象常有的属性, 要调整Sprite的大小,只能通过width/height来设置,想旋转/翻转sprite,可以通过angle、invertV、insertU来控制。

把这颗树复制一堆,就可以模拟1片树林:

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

    const camera = new BABYLON.ArcRotateCamera("Camera", -Math.PI / 2, Math.PI / 2, 8, new BABYLON.Vector3(0, 0, 0));
    camera.attachControl(canvas, true);
    const light = new BABYLON.PointLight("Point", new BABYLON.Vector3(5, 10, 5));

    // Create a sprite manager
    // Parameters : name, imgUrl, capacity(最大容量,即树的总数), cellSize, scene
    const spriteManagerTrees = new BABYLON.SpriteManager("treesManager", "../assets/img/palm.png", 500, { width: 512, height: 1024 });
    //Mutliple trees
    for (let i = 0; i < 500; i++) {
        const tree = new BABYLON.Sprite("tree", spriteManagerTrees);
        tree.width = 1;
        tree.height = 2;
        tree.position.x = BABYLON.Scalar.RandomRange(-25, 25);
        tree.position.z = BABYLON.Scalar.RandomRange(-25, 25);
    }


    return scene;
}

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

纯静态的Sprite从效果上看,跟html中的img标签差不多,只是简单导入一张图片而已。Sprite还可以实现类似gif的动画,比如有下面这张图:

const spriteManagerUFO = new BABYLON.SpriteManager("ufoManager", "../assets/img/ufo.png", 1, { width: 128, height: 76 });
const ufo = new BABYLON.Sprite("ufo", spriteManagerUFO);
ufo.width = 1;
ufo.height = 0.5;

//playAnimation(from: number, to: number, loop: boolean, delay: number, onAnimationEnd: Nullable<(() => void)>): void
ufo.playAnimation(0, 16, true, 125);

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

其大致原理,就是把ufo.png这张表,按width:128,height:76 分割成17个cell(单元格),调 playAnimation 方法时,从0~16(注:下标索引从0开始)依次播放,每1个cell播放的时间间隔为125ms, 第3个true表示循环播放,详情可见官方API文档

此外,加载图片时,还能人为指定加载第几个cell ,比如下面这张图,总共有45个cell

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

    const camera = new BABYLON.ArcRotateCamera("Camera", -Math.PI / 2, Math.PI / 2, 8, new BABYLON.Vector3(0, 0, 0));
    camera.attachControl(canvas, true);
    const light = new BABYLON.PointLight("Point", new BABYLON.Vector3(5, 10, 5));

    const spriteManagerPlayer = new BABYLON.SpriteManager("playerManager", "../assets/img/player.png", 10, 64);

    const player1 = new BABYLON.Sprite("player1", spriteManagerPlayer);
    player1.cellIndex=0;
    player1.position.x = -1;

    const player2 = new BABYLON.Sprite("player2", spriteManagerPlayer);
    player2.cellIndex=11;
    player2.position.x = 0;

    const player3 = new BABYLON.Sprite("player3", spriteManagerPlayer);
    player3.cellIndex=44;
    player3.position.x = 1;


    const player4 = new BABYLON.Sprite("player4", spriteManagerPlayer);
    player4.position.y = 1.5;
    player4.position.x = -0.8;
    player4.playAnimation(0,40,true,100);

    const player5 = new BABYLON.Sprite("player5", spriteManagerPlayer);
    player5.position.y = 1.5;
    player5.position.x = 0.8;
    player5.playAnimation(0,9,true,100);

    return scene;
}

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

参考文档:

https://doc.babylonjs.com/features/featuresDeepDive/sprites/sprite_manager

https://doc.babylonjs.com/features/introductionToFeatures/chap5/trees

标签:const,Sprite,babylon,笔记,js,position,new,height,BABYLON
From: https://www.cnblogs.com/yjmyzz/p/babylon_js_study_9.html

相关文章

  • 《程序员修炼之道-从小工到专家》阅读笔记
    第15节Shell游戏1、对于操纵文本的文件的程序员,命令Shell就是工作台。我们可以利用Shell启动各种应用、搜索文件、查询系统状态,甚至还可以构建复杂的宏命令,完成各种常见活动。2、对于习惯GUI的开发者来说一直使用Shell有些极端。GUI的好处是所见即所得,但他的缺点却是......
  • 阅读笔记之《程序员修炼之道-从小工到专家》五
    第21节按合约设计1、注重实效的程序员会不信任自己,所以他们针对自己的错误行为进行防卫性编码。2、按合约设计(DesignByContract,简写DBC)是BertrandMeyer为Eiffel语言发展的概念。它的核心是用文档记载模块的权利与责任,并进行校验。它的目的是对函数做一些前置检查和后置......
  • CSS选择器笔记
    [color=red][b]一、基本选择器[/b][/color][table]序号选择器含义|1.|*|通用元素选择器,匹配任何元素|2.|E|标签选择器,匹配所有使用E标签的元素|3.|.info|class选择器,匹配所有class属性中包含info的元素|4.|#footer|id选择器,匹配所有id属性等于footer的元素[/table]......
  • adm-jclass: 一个面向对象编程的js库
    [url]https://code.google.com/p/adm-jclass/[/url]:Ajavascriptlibrarythatallowshighlyobject-orientedapplicationprogramminginyourbrowser.介绍:[url]https://code.google.com/p/adm-jclass/wiki/jClassDocumentation[/url]jClassisa......
  • set笔记
    set函数用来去重加排序,非常的方便set(集合)1.介绍set容器中的元素不会重复,当插入集合中已有的元素时,并不会插入进去,而且set容器里的元素自动从小到大排序。即:set里面的元素不重复且有序2.函数方法代码 含义s.begin() 返回set容器的第一个元素的地址(迭代器)s.end() 返回set容器......
  • Spring返回json格式数据的三种方式
    SVN多版本库环境的搭建OAuth2.0是什么?看这篇文章就够了。前端JavaPython等资源合集大放送目前前后端分离大行其道,如何进行前后分类是各个项目需要考虑的问题。如何使用Spring进行前后端分离呢?返回json格式数据是前后端分离的最佳选择。下面介绍使用Spring进行前后端分离的常用三......
  • nodejs vuejs java python 入门到高级视频
    多抽出一分钟学习,让你的生命更加精彩!高性能高可用Yii2.0电商平台仿京东商城高级组件MySQLLVSDocker+Kubernetes(k8s)微服务容器化实战实战Docker到Kubernetes技术系列视频教程@黑马JAVAEE19期⑭jQuery实战经典【No0066】尚学堂架构师视频06、微服务架构00、SpringBoot微服务架......
  • 构建之法阅读笔记06
    通过学习软件工程这门课程以及阅读《构建之法》,我对BUG的概念有了新的更加正确的认识。我以前认为BUG就是软件的漏洞,故障,在软件进行正常运行时会出现不知名的错误。其实这种想法是不正确的,BUG是不能这样来定义的。软件行业有这样一句著名的笑话:(BUG)这不是缺陷,这是一个功能。所谓的B......
  • 构建之法阅读笔记05
    “不要一开始就想着找到并拼对所有的拼图块,以为能够打造一个巨大的创新。”   在书中的这句话给我的感触很大,对于这句话我很认同。这句话并不是教导我们目光只看到当前,我们要在有统筹观念的同时,注重脚下的每一步,过于追求结果只会使事不如人愿。一步一步,不急不躁,踏实稳步的走......
  • 构建之法阅读笔记01
    构建之法这本书通过生活实例,启发我对什么是程序,什么是软件,什么是软件工程,没有使用到算法需不需要学习、掌握。软件和算法,数据结构有无关联等问题的思考。仔细想想发现自己对这些概念很模糊。通过继续往下看,我才渐渐的明白了能满足各种功能的是应用软件,能保证维修的是软件服务…........