当前位置: 首页 > news >正文

LayaBox1.8.4实现战争迷雾效果

实现思路:

和Unity实现思路一样,可看我写的下面的一篇文章

战争迷雾FogOfWar---Unity中实现-CSDN博客

根据碰撞点可以计算出需要透明的位置,怎样计算如下:

根据迷雾mesh的长宽和纵向横向的的像素数可以得出,每个小方格的长和宽,然后就可以计算出碰撞点属于第几个格子,然后将周围一定范围内的格子透明即可。 

实现代码:

自定义迷雾mesh代码:

var FogOfWarMesh=(function(_super){function FogOfWarMesh(long,width,stacks,slices){/**@private */this._long=NaN;/**@private */this._width=NaN;/**@private */this._stacks=0;/**@private */this._slices=0;(long===void 0)&& (long=10);(width===void 0)&& (width=10);(stacks===void 0)&& (stacks=100);(slices===void 0)&& (slices=100);FogOfWarMesh.__super.call(this);this._long=long;this._width=width;this._stacks=stacks;this._slices=slices;this.activeResource();this._positions=this._getPositions();this._generateBoundingObject();this.pos = new Laya.Vector2();this.isInit = true;}Laya.class(FogOfWarMesh,'laya.d3.resource.models.FogOfWarMesh',_super);var __proto= FogOfWarMesh.prototype;__proto.recreateResource=function(){this._numberVertices=(this._stacks+1)*(this._slices+1);this._numberIndices=this._stacks *this._slices *2 *3;var indices=new Uint16Array(this._numberIndices);var vertexDeclaration=Laya.VertexPositionNormalColor.vertexDeclaration;var vertexFloatStride=vertexDeclaration.vertexStride / 4;this.vertices= this.vertices || new Float32Array(this._numberVertices *vertexFloatStride);var halfLong=this._long / 2;var halfWidth=this._width / 2;var stacksLong=this._long / this._stacks;var slicesWidth=this._width / this._slices;this.FillVert(this.vertices,stacksLong,slicesWidth,halfLong,halfWidth,this.pos);var indiceIndex=0;for (i=0;i < this._slices;i++){for (j=0;j < this._stacks;j++){indices[indiceIndex++]=(i+1)*(this._stacks+1)+j;indices[indiceIndex++]=i *(this._stacks+1)+j;indices[indiceIndex++]=(i+1)*(this._stacks+1)+j+1;indices[indiceIndex++]=i *(this._stacks+1)+j;indices[indiceIndex++]=i *(this._stacks+1)+j+1;indices[indiceIndex++]=(i+1)*(this._stacks+1)+j+1;}}this._vertexBuffer=new Laya.VertexBuffer3D(vertexDeclaration,this._numberVertices,/*laya.webgl.WebGLContext.STATIC_DRAW*/0x88E4,true);this._indexBuffer=new Laya.IndexBuffer3D(/*laya.d3.graphics.IndexBuffer3D.INDEXTYPE_USHORT*/"ushort",this._numberIndices,/*laya.webgl.WebGLContext.STATIC_DRAW*/0x88E4,true);this._vertexBuffer.setData(this.vertices);this._indexBuffer.setData(indices);this.memorySize=(this._vertexBuffer._byteLength+this._indexBuffer._byteLength)*2;this.completeCreate();}__proto.FillVert = function(vertices,stacksLong,slicesWidth,halfLong,halfWidth,pos){var verticeCount=0;for (var i=0;i <=this._slices;i++){for (var j=0;j <=this._stacks;j++){vertices[verticeCount++]=j *stacksLong-halfLong;vertices[verticeCount++]=0;vertices[verticeCount++]=i *slicesWidth-halfWidth;vertices[verticeCount++]=0;vertices[verticeCount++]=1;vertices[verticeCount++]=0;vertices[verticeCount++]=0;vertices[verticeCount++]=0;vertices[verticeCount++]=0;vertices[verticeCount]= pos && ((Math.abs(i - pos.x - 15) < 10 && Math.abs(j - pos.y) < 10)?0:this.isInit? 1 :vertices[verticeCount]);verticeCount++}};this.isInit = false;}__proto.updateVert = function (pos) {this.pos = pos;this.recreateResource();}return FogOfWarMesh;
})(Laya.PrimitiveMesh)

迷雾调用代码:

        //添加3D场景var scene = Laya.stage.addChild(new Laya.Scene());//添加照相机var camera = (scene.addChild(new Laya.Camera(0, 0.1, 100)));camera.transform.translate(new Laya.Vector3(0, 10, 10));camera.transform.rotate(new Laya.Vector3(-60, 0, 0), true, false);camera.clearColor = null;//添加方向光var directionLight = scene.addChild(new Laya.PointLight());directionLight.transform.translate(new Laya.Vector3(0,2,0));directionLight.color = new Laya.Vector3(0.5, 0.5, 0.5);directionLight.direction = new Laya.Vector3(0, 1, 0);var player = scene.addChild(new Laya.MeshSprite3D(new Laya.BoxMesh(1,1,1)));player.transform.translate(new Laya.Vector3(0,0,-1));player.transform.rotate(new Laya.Vector3(0, 0, 0), false, false);var st = new Laya.BlinnPhongMaterial();st.albedoTexture = Laya.Texture2D.load("res/layabox.png");player.meshRender.shareMaterial = st;//添加自定义模型var width = 50;var height = 50;var stacks = 200;var slices = 200;this.mesh = new FogOfWarMesh(width,height,stacks,slices);//new CustomMesh(10, 10);var box = scene.addChild(new Laya.MeshSprite3D(this.mesh));box.transform.translate(new Laya.Vector3(0,3,-1));box.transform.rotate(new Laya.Vector3(0, 0, 0), false, false);var material = new Laya.StandardMaterial();material.blend = Laya.BaseMaterial.BLEND_ENABLE_SEPERATE;box.meshRender.material = material;var boxCollider = box.addComponent(Laya.BoxCollider);boxCollider.setFromBoundBox(this.mesh.boundingBox)var w = width/stacks;var h = height/stacks;var fogPos = new Laya.Vector3();Laya.Vector3.subtract(box.transform.position, new Laya.Vector3(width/2,0,height/2),fogPos);var self = this;Laya.timer.loop(100,this,function(){var pos = player.transform.position;var up = new Laya.Vector3(0, 100, 0);Laya.Vector3.add(pos,up,up);var ray = new Laya.Ray(pos, up);var _outHitInfo = new Laya.RaycastHit();Laya.Physics.rayCast(ray, _outHitInfo, 30, 0);self.mesh.updateVert(new Laya.Vector2((_outHitInfo.position.z - fogPos.z)/h,(_outHitInfo.position.x - fogPos.x)/w));});

 实现效果:


http://www.mrgr.cn/news/78505.html

相关文章:

  • vue3 ajax获取json数组排序举例
  • 【Petri网导论学习笔记】Petri网导论入门学习(十) —— 3.2 关联矩阵与状态方程
  • Scala关于成绩的常规操作
  • 【UE5 C++课程系列笔记】04——创建可操控的Pawn
  • Table 滚动条始终停靠在可视区域的底部
  • [工具分享] 根据Excel数据根据Word文档模板,批量创建生成Word文档并重命名,方便快速查找打印
  • 数据结构与算法——1125—面试题位运算
  • redis的主从复制
  • 【通用】操作系统 知识总结:IPC方式 / 进程线程 / 死锁 / 虚拟内存 / 段页存储
  • Oracle对比表与表之间的结构
  • 20241128解决Ubuntu20.04安装libwxgtk3.0-dev异常的问题
  • linux内核面试题精选及参考答案
  • 探讨播客的生态系统
  • 零基础快速掌握——C语言基础【数据类型】【运算符】
  • python array矩阵相关操作
  • 《操作系统 - 清华大学》6 -1:局部页面置换算法:最优页面置换算法
  • 针对Qwen-Agent框架的Function Call及ReAct的源码阅读与解析:Agent基类篇
  • Robot Framework框架中常用的变量
  • A052-基于SpringBoot的酒店管理系统
  • Flink 离线计算
  • ais_server 学习笔记
  • mongodb文档字符串批量替换
  • JAVA项目-------医院挂号系统
  • vue3 tinymce7版本 完美适配基本需求(特殊需求外)
  • 【JavaEE初阶 — 网络编程】TCP流套接字编程
  • 《Learn Three.js》学习(2)构建Three.js基本组件