babylon.js 学习笔记(9)
接上回继续,做为一个游戏引擎,怎能没有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

下面的代码演示了,如何加载指定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
最后,综合运用一把,把UFO、棕榈树 加入先前的村庄中
const createScene = function () {
const scene = new BABYLON.Scene(engine);
const camera = new BABYLON.ArcRotateCamera("camera", -Math.PI / 2, Math.PI / 2.5, 15, new BABYLON.Vector3(0, 0, 0));
camera.upperBetaLimit = Math.PI / 2.2;
camera.attachControl(canvas, true);
const light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(1, 1, 0));
const spriteManagerTrees = new BABYLON.SpriteManager("treesManager", "../assets/img/palm.png", 2000, { width: 512, height: 1024 }, scene);
//We create trees at random positions
for (let i = 0; i < 200; i++) {
const tree = new BABYLON.Sprite("tree", spriteManagerTrees);
tree.position.x = Math.random() * (-30);
tree.position.z = Math.random() * 20 + 8;
tree.position.y = 0.5;
}
for (let i = 0; i < 200; i++) {
const tree = new BABYLON.Sprite("tree", spriteManagerTrees);
tree.position.x = Math.random() * (25) + 7;
tree.position.z = Math.random() * -35 + 8;
tree.position.y = 0.5;
}
//Skybox
const skybox = BABYLON.MeshBuilder.CreateBox("skyBox", { size: 150 }, scene);
const skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
skyboxMaterial.backFaceCulling = false;
skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("../assets/img/sky/skybox", scene);
skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
skybox.material = skyboxMaterial;
BABYLON.SceneLoader.ImportMeshAsync("", "../assets/glb/", "valleyvillage.glb");
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;
ufo.position.y = 5;
ufo.position.x = 3;
ufo.position.z = 2;
//playAnimation(from: number, to: number, loop: boolean, delay: number, onAnimationEnd: Nullable<(() => void)>): void
ufo.playAnimation(0, 16, true, 125);
return scene;
}

在线地址:https://yjmyzz.github.io/babylon_js_study/day09/05.html
参考文档:
https://doc.babylonjs.com/features/featuresDeepDive/sprites/sprite_manager
https://doc.babylonjs.com/features/introductionToFeatures/chap5/trees
babylon.js 学习笔记(9)的更多相关文章
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- Vue.js学习笔记(2)vue-router
vue中vue-router的使用:
- JS 学习笔记--9---变量-作用域-内存相关
JS 中变量和其它语言中变量最大的区别就是,JS 是松散型语言,决定了它只是在某一个特定时间保存某一特定的值的一个名字而已.由于在定义变量的时候不需要显示规定必须保存某种类型的值,故变量的值以及保存的 ...
- WebGL three.js学习笔记 使用粒子系统模拟时空隧道(虫洞)
WebGL three.js学习笔记 使用粒子系统模拟时空隧道 本例的运行结果如图: 时空隧道demo演示 Demo地址:https://nsytsqdtn.github.io/demo/sprite ...
- WebGL three.js学习笔记 法向量网格材质MeshNormalMaterial的介绍和创建360度全景天空盒的方法
WebGL学习----Three.js学习笔记(5) 点击查看demo演示 Demo地址:https://nsytsqdtn.github.io/demo/360/360 简单网格材质 MeshNor ...
- WebGL three.js学习笔记 创建three.js代码的基本框架
WebGL学习----Three.js学习笔记(1) webgl介绍 WebGL是一种3D绘图协议,它把JavaScript和OpenGL ES 2.0结合在一起,通过增加OpenGL ES 2.0的 ...
- vue.js 学习笔记3——TypeScript
目录 vue.js 学习笔记3--TypeScript 工具 基础类型 数组 元组 枚举 字面量 接口 类类型 类类型要素 函数 函数参数 this对象和类型 重载 迭代器 Symbol.iterat ...
- 2019-4-29 js学习笔记
js学习笔记一:js数据类型 1:基本数据类型 number类型(整数,小数) String类型 boolean类型 NaN类型其实是一个nu ...
- 一点感悟:《Node.js学习笔记》star数突破1000+
写作背景 笔者前年开始撰写的<Node.js学习笔记> github star 数突破了1000,算是个里程碑吧. 从第一次提交(2016.11.03)到现在,1年半过去了.突然有些感慨, ...
- JS学习笔记5_DOM
1.DOM节点的常用属性(所有节点都支持) nodeType:元素1,属性2,文本3 nodeName:元素标签名的大写形式 nodeValue:元素节点为null,文本节点为文本内容,属性节点为属性 ...
随机推荐
- 通过Linux包管理器提升权限
免责声明:本文所涉及的技术仅供学习和参考,严禁使用本文内容从事违法行为和未授权行为,如因个人原因造成不良后果,均由使用者本人负责,作者及本博客不承担任何责任. 前言 在Linux系统中,apt和yum ...
- x86花指令
花指令 参考: https://bbs.kanxue.com/thread-279604.htm#msg_header_h3_21 两种反编译算法 线性扫描算法:逐行反汇编(无法将数据和内容进行区分) ...
- 关于网传微信聊天记录提取工具"留痕"盗取个人信息的分析
今天早上看到一篇文章,是关于一个微信聊天记录提取工具泄露个人信息的内容,于是我就好奇,看了一下作者的 github,然后也是自己小小的分析了一下 1.官方地址 Github: https://gith ...
- 浅谈Spring、Spring MVC、Spring Boot和Spring Cloud的关系和区别
Spring 框架就像一个家族,有众多衍生产品,例如 boot.security.jpa等等.但它们的基础都是Spring的IOC和AOP等.IOC提供了依赖注入的容器,AOP解决了面向横切面编程 ...
- 2025 年实用、全面的 VS Code 插件推荐!
前言 VS Code是一款由微软开源免费.轻量级.功能强大的源代码编辑器.其轻量级体现在基础安装简洁,仅含核心编辑功能.功能强大则源于它支持丰富的语言环境插件拓展,这种模块化设计让VS Code在源代 ...
- 题解:CF1977D XORificator
题目链接:link. 题目大概其实就是想让我们通过翻转某些行,使得尽可能多的列成为特殊列. 众所周知,暴力肯定是不行的,所以我们需要考虑优化! 对于每一列 \(j\),枚举每一行 \(i\),通过翻转 ...
- 揭秘如何用Monaco Editor打造功能强大的日志查看器
Monaco Editor 是一个基于浏览器的代码编辑器,由 Microsoft 开发,是 Visual Studio Code 的核心编辑器组件.为用户提供了一个功能丰富.性能优异的代码编辑环境,常 ...
- 想要精准营销,从学习搭建一套对的标签体系开始丨DTVision分析洞察篇
在人与人打交道的过程中,我们会在有意无意间给周围的人通过贴标签的方式进行大致的判断,比如好说话的.难相处的.聪明的.爱热闹的--贴标签就是用最快的速度将人和事归类,这是人类运用"模式识别&q ...
- 模块与包&相对绝对路径
[一]模块与包 (1)什么是模块 在Python中,一个py文件就是一个模块,文件名为xxx.py模块名则是xxx,导入模块可以引用模块中已经写好的功能. (2)模块的来源 内置的:python解释器 ...
- 2025国内五大MES系统排名探秘:从核心架构到选型指南,解锁智造升级最优解
在智能制造浪潮席卷全球的今天,MES系统(制造执行系统)作为连接企业管理层与车间生产层的"神经中枢",其重要性日益凸显.它能有效打通信息孤岛,实现生产全流程透明化.可控化与智能化, ...