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

中山自助建站系统wordpress建博客网站吗

中山自助建站系统,wordpress建博客网站吗,怎么推广自己的网站?,网站建设有哪些困难unity有自己的粒子系统#xff0c;但是这次我们要尝试创建一个我们自己的粒子系统#xff0c;而且使用计算着色器有下面这些好处。总而言之#xff0c;计算着色器适合处理大规模的数据集。例如#xff0c;能够高效地处理数万个甚至数百万个粒子的计算。这对于粒子系统这样的…unity有自己的粒子系统但是这次我们要尝试创建一个我们自己的粒子系统而且使用计算着色器有下面这些好处。总而言之计算着色器适合处理大规模的数据集。例如能够高效地处理数万个甚至数百万个粒子的计算。这对于粒子系统这样的效果特别重要因为粒子数量通常很大。 首先创建一个粒子结构体然后给上要用到的属性以及一些相关的变量 struct Particle{public Vector3 position;public Vector3 velocity;public float life;}const int SIZE_PARTICLE 7 * sizeof(float);public int particleCount 1000000; 然后通过init方法初始化粒子数据分别随机位置和生命周期然后重置速度为0.很明显位置会随机分布在-0.5-0.5之间。然后填充粒子数据到computebuffer中分别传递buffer到computeshader和shader中这里很关键的一部分就是我们在computershader中修改粒子数据可以在shader中的buffer访问到修改后的数据 void Init(){// initialize the particlesParticle[] particleArray new Particle[particleCount];for (int i 0; i particleCount; i){// Initialize particleVector3 v new Vector3();v.x Random.value * 2 - 1.0f;v.y Random.value * 2 - 1.0f;v.z Random.value * 2 - 1.0f;v.Normalize();v * Random.value * 0.5f;// Assign particle propertiesparticleArray[i].position.x v.x;particleArray[i].position.y v.y;particleArray[i].position.z v.z 3;//远离摄像机particleArray[i].velocity.x 0;particleArray[i].velocity.y 0;particleArray[i].velocity.z 0;particleArray[i].life Random.value * 5.0f 1.0f;//1-6}// create compute bufferparticleBuffer new ComputeBuffer(particleCount, SIZE_PARTICLE);particleBuffer.SetData(particleArray);// find the id of the kernelkernelID shader.FindKernel(CSParticle);uint threadsX;shader.GetKernelThreadGroupSizes(kernelID, out threadsX, out _, out _);groupSizeX Mathf.CeilToInt((float)particleCount / (float)threadsX);// bind the compute buffer to the shader and the compute shadershader.SetBuffer(kernelID, particleBuffer, particleBuffer);material.SetBuffer(particleBuffer, particleBuffer);material.SetInt(_PointSize, pointSize);} 然后是OnRenderObject函数每当 Unity 需要渲染一个对象时这个函数就会被调用确保在渲染期间可以执行自定义的渲染操作下面图片是对DrawProceduralNow函数的解释 void OnRenderObject()//相机的每个渲染过程自动调用{material.SetPass(0);//使用第一个PassGraphics.DrawProceduralNow(MeshTopology.Points, 1, particleCount);//程序化绘制顶点} 然后看一下我们的顶点着色器首先是两个参数第二实例ID就是逐渐增加的从0增加到particleCount第一个参数是每个实例的顶点索引因为这次粒子都是点所以永远是0如果是三角形就会是012. v2f vert(uint vertex_id : SV_VertexID, uint instance_id : SV_InstanceID){v2f o (v2f)0;// Coloro.color fixed4(1001// Positiono.position UnityObjectToClipPos(float4(particleBuffer[instance_id].position,1));o.size 1;return o;} 好了现在运行会得到一个在屏幕中央半径为0.5左右的红色小球。就像下面这样这是因为我们并没有对粒子进行任何处理只是设置了位置和颜色。 接下来就是让这些粒子动起来我们让粒子跟着鼠标的位置移动首先找到鼠标的位置。然后设置粒子的速度并且更改粒子的位置。然后如果生命变成0就调用函数重新生成粒子。重新生成函数代码与获取鼠标位置代码在后面完整代码里 下面这个链接是将鼠标位置转换为世界空间坐标 gamedevbeginner.com/ how-to-convert-the-mouse-position-to-world-space-in-unity-2d-3d/ [numthreads(256, 1, 1)] void CSParticle(uint3 id : SV_DispatchThreadID) {Particle particle particleBuffer[id.x];// 减少粒子的生命值particle.life - deltaTime;// 计算粒子位置与鼠标位置的差值float3 delta float3(mousePosition.xy, 3) - particle.position;// 计算粒子运动的方向float3 dir normalize(delta);// 更新粒子的速度particle.velocity dir;// 根据速度更新粒子的位置particle.position particle.velocity * deltaTime;// 将更新后的粒子数据存储回缓冲区particleBuffer[id.x] particle;// 如果粒子的生命值小于 0则重新生成粒子if (particle.life 0){respawn(id.x);} } 现在因为只有红色有点单调所以我们要丰富一下颜色。 随着粒子的生命周期减少这是每个通道的颜色变化 v2f vert(uint vertex_id : SV_VertexID, uint instance_id : SV_InstanceID){v2f o (v2f)0;// Colorfloat life particleBuffer[instance_id].life;float lerpVal life * 0.25;// 计算颜色值o.color fixed4(1 - lerpVal 0.1, // Red componentlerpVal 0.1, // Green component1, // Blue componentlerpVal // Alpha component);// Positiono.position UnityObjectToClipPos(float4(particleBuffer[instance_id].position,1));o.size _PointSize;return o;} 最后就来看一下最终效果把 完整代码 using System.Collections; using System.Collections.Generic; using UnityEngine;#pragma warning disable 0649public class ParticleFun : MonoBehaviour {private Vector2 cursorPos;// structstruct Particle{public Vector3 position;public Vector3 velocity;public float life;}const int SIZE_PARTICLE 7 * sizeof(float);public int particleCount 1000000;public Material material;public ComputeShader shader;[Range(1, 10)]public int pointSize 2;int kernelID;ComputeBuffer particleBuffer;int groupSizeX; // Use this for initializationvoid Start(){Init();}void Init(){// initialize the particlesParticle[] particleArray new Particle[particleCount];for (int i 0; i particleCount; i){// Initialize particleVector3 v new Vector3();v.x Random.value * 2 - 1.0f;v.y Random.value * 2 - 1.0f;v.z Random.value * 2 - 1.0f;v.Normalize();v * Random.value * 0.5f;// Assign particle propertiesparticleArray[i].position.x v.x;particleArray[i].position.y v.y;particleArray[i].position.z v.z 3;//远离摄像机particleArray[i].velocity.x 0;particleArray[i].velocity.y 0;particleArray[i].velocity.z 0;particleArray[i].life Random.value * 5.0f 1.0f;//1-6}// create compute bufferparticleBuffer new ComputeBuffer(particleCount, SIZE_PARTICLE);particleBuffer.SetData(particleArray);// find the id of the kernelkernelID shader.FindKernel(CSParticle);uint threadsX;shader.GetKernelThreadGroupSizes(kernelID, out threadsX, out _, out _);groupSizeX Mathf.CeilToInt((float)particleCount / (float)threadsX);// bind the compute buffer to the shader and the compute shadershader.SetBuffer(kernelID, particleBuffer, particleBuffer);material.SetBuffer(particleBuffer, particleBuffer);material.SetInt(_PointSize, pointSize);}void OnRenderObject()//相机的每个渲染过程自动调用{material.SetPass(0);//使用第一个PassGraphics.DrawProceduralNow(MeshTopology.Points, 1, particleCount);//程序化绘制顶点}void OnDestroy(){if (particleBuffer ! null)particleBuffer.Release();}// Update is called once per framevoid Update(){float[] mousePosition2D { cursorPos.x, cursorPos.y };// Send datas to the compute shadershader.SetFloat(deltaTime, Time.deltaTime);shader.SetFloats(mousePosition, mousePosition2D);// Update the Particlesshader.Dispatch(kernelID, groupSizeX, 1, 1);}void OnGUI(){Vector3 p new Vector3();Camera c Camera.main;Event e Event.current;Vector2 mousePos new Vector2();// Get the mouse position from Event.// Note that the y position from Event is inverted.mousePos.x e.mousePosition.x;mousePos.y c.pixelHeight - e.mousePosition.y;p c.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, c.nearClipPlane 14));// z 3.cursorPos.x p.x;cursorPos.y p.y;} }Shader Custom/Particle {Properties { _PointSize(Point size, Float) 5.0 } SubShader {Pass {Tags{ RenderType Opaque }LOD 200Blend SrcAlpha oneCGPROGRAM// Physically based Standard lighting model, and enable shadows on all light types#pragma vertex vert#pragma fragment fraguniform float _PointSize;#include UnityCG.cginc// Use shader model 3.0 target, to get nicer looking lighting#pragma target 5.0struct v2f{float4 position : SV_POSITION;float4 color : COLOR;float life : LIFE;float size: PSIZE;};// 定义粒子结构体struct Particle{float3 position; // 粒子位置float3 velocity; // 粒子速度float life; // 粒子的生命值};// 声明结构化缓冲区StructuredBufferParticle particleBuffer;v2f vert(uint vertex_id : SV_VertexID, uint instance_id : SV_InstanceID){v2f o (v2f)0;// Colorfloat life particleBuffer[instance_id].life;float lerpVal life * 0.25;// 计算颜色值o.color fixed4(1 - lerpVal 0.1, // Red componentlerpVal 0.1, // Green component1, // Blue componentlerpVal // Alpha component);// Positiono.position UnityObjectToClipPos(float4(particleBuffer[instance_id].position,1));o.size _PointSize;return o;}float4 frag(v2f i) : COLOR{return i.color;}ENDCG}}FallBack Off }#pragma kernel CSParticle// Variables set from the CPU float deltaTime; float2 mousePosition;uint rng_state;// 定义粒子结构体 struct Particle {float3 position; // 粒子位置float3 velocity; // 粒子速度float life; // 粒子的生命值 };// 声明结构化缓冲区 RWStructuredBufferParticle particleBuffer;uint rand_xorshift()//随机数范围0-4,294,967,295 {// Xorshift 算法来自 George Marsaglia 的论文rng_state ^ (rng_state 13); // 将状态左移13位并与原状态进行异或rng_state ^ (rng_state 17); // 将状态右移17位并与原状态进行异或rng_state ^ (rng_state 5); // 将状态左移5位并与原状态进行异或return rng_state; // 返回新的状态作为随机数 }void respawn(uint id) {rng_state id;float tmp (1.0 / 4294967296.0);float f0 float(rand_xorshift()) * tmp - 0.5;float f1 float(rand_xorshift()) * tmp - 0.5;float f2 float(rand_xorshift()) * tmp - 0.5;float3 normalF3 normalize(float3(f0, f1, f2)) * 0.8f;normalF3 * float(rand_xorshift()) * tmp;particleBuffer[id].position float3(normalF3.x mousePosition.x, normalF3.y mousePosition.y, normalF3.z 3.0);// reset the life of this particleparticleBuffer[id].life 4;particleBuffer[id].velocity float3(0,0,0); }[numthreads(256, 1, 1)] void CSParticle(uint3 id : SV_DispatchThreadID) {Particle particle particleBuffer[id.x];// 减少粒子的生命值particle.life - deltaTime;// 计算粒子位置与鼠标位置的差值float3 delta float3(mousePosition.xy, 3) - particle.position;// 计算粒子运动的方向float3 dir normalize(delta);// 更新粒子的速度particle.velocity dir;// 根据速度更新粒子的位置particle.position particle.velocity * deltaTime;// 将更新后的粒子数据存储回缓冲区particleBuffer[id.x] particle;// 如果粒子的生命值小于 0则重新生成粒子if (particle.life 0){respawn(id.x);} }
http://www.dnsts.com.cn/news/257601.html

相关文章:

  • 自建网站步骤模板王网页模板
  • 做新闻类网站wordpress百家号模版
  • 做一个网站需要多长时间备案的网站换空间
  • 互站网wordpress文章空两格
  • 常德市住房和城乡建设局网站南京网站建设网站设计
  • 北京医疗机构网站前置审批需要的材料有哪些深圳做企业网站的公司推荐
  • 建设网站可选择的方案电商网站建设怎么样
  • 可以货代从哪些网站开发客户简洁的网站地图模板
  • 网站建设收费标准不一巴中微小网站建设案例
  • 临海市建设局官网站专门做辅助的网站
  • 公司网站方案福田我要做网站优化比较好
  • 巨腾外贸网站建设公司怎么做无损mp3下载网站
  • 有域名了 怎么做网站使用WordPress做论坛
  • 东莞市专注网站建设怎么样厦门企业网站建设补贴
  • 网页设计和网站建设是同一回事吗开网站买自己做的东西
  • 成都家具企业网站建设湖南岳阳网站开发网络公司
  • 最权威的网站推广设计为企业做贡献
  • 舒兰网站建设开展农业信息网站建设工作总结
  • 肥城市住房和城乡建设厅网站做货代在上面网站找客户比较多
  • 杭州 电子商务网站建设 网络服务微网站开发平台 开源
  • 给自己的网站做关键词流程制作图片视频的软件
  • c2c网站的特点及主要功能杭州高端网站设计
  • 兰州正规seo整站优化怀化最新通知今天
  • 怎么在电脑上做网站上海松江网站制作
  • 青州网站建设青州今天刚刚最新消息2023
  • 淘宝客优惠卷网站模板wordpress主题购买网站
  • 山东网站空间洛阳生活网
  • 网站的关键字 设置太原市外贸网站建设
  • 网页设计与制作是什么意思上海网站seo牛巨微
  • h5移动端网站模板下载做旅游的网站的要素