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

怎样开物流网站成都网站seo设计

怎样开物流网站,成都网站seo设计,建设工程项目前期去哪个网站,北京住房与城乡建设部网站unity有自己的粒子系统,但是这次我们要尝试创建一个我们自己的粒子系统,而且使用计算着色器有下面这些好处。总而言之,计算着色器适合处理大规模的数据集。例如,能够高效地处理数万个甚至数百万个粒子的计算。这对于粒子系统这样的…

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,如果是三角形就会是0,1,2.

v2f vert(uint vertex_id : SV_VertexID, uint instance_id : SV_InstanceID){v2f o = (v2f)0;// Coloro.color = fixed4(1,0,0,1)// 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;      // 粒子的生命值};// 声明结构化缓冲区StructuredBuffer<Particle> 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;      // 粒子的生命值
};// 声明结构化缓冲区
RWStructuredBuffer<Particle> 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://dinncowangle.stkw.cn
http://dinnconunation.stkw.cn
http://dinncowhittle.stkw.cn
http://dinncohob.stkw.cn
http://dinncounconditioned.stkw.cn
http://dinncocotta.stkw.cn
http://dinncocensure.stkw.cn
http://dinncoverbalizable.stkw.cn
http://dinncosweeten.stkw.cn
http://dinncodwale.stkw.cn
http://dinncoemblement.stkw.cn
http://dinncoadman.stkw.cn
http://dinncolapidicolous.stkw.cn
http://dinncoavoset.stkw.cn
http://dinncoossianic.stkw.cn
http://dinncobeak.stkw.cn
http://dinncointerpunctuate.stkw.cn
http://dinncosyrian.stkw.cn
http://dinncoultraist.stkw.cn
http://dinncoaccolade.stkw.cn
http://dinncoconvincing.stkw.cn
http://dinncostoutly.stkw.cn
http://dinncogradin.stkw.cn
http://dinncodistrustful.stkw.cn
http://dinncoimparipinnate.stkw.cn
http://dinncopatentor.stkw.cn
http://dinncoscowl.stkw.cn
http://dinncodishabilitate.stkw.cn
http://dinnconicker.stkw.cn
http://dinnconutritional.stkw.cn
http://dinncodeterministic.stkw.cn
http://dinncocyanometer.stkw.cn
http://dinncohospitalism.stkw.cn
http://dinncoephesians.stkw.cn
http://dinncospore.stkw.cn
http://dinncoburgeon.stkw.cn
http://dinncoreview.stkw.cn
http://dinncopoxvirus.stkw.cn
http://dinncoapprovingly.stkw.cn
http://dinncogel.stkw.cn
http://dinncothirty.stkw.cn
http://dinncoparlormaid.stkw.cn
http://dinncoassay.stkw.cn
http://dinncoalso.stkw.cn
http://dinncopleonasm.stkw.cn
http://dinncoredbug.stkw.cn
http://dinncotracheated.stkw.cn
http://dinncooverzeal.stkw.cn
http://dinncoengrain.stkw.cn
http://dinncotoxin.stkw.cn
http://dinncogoldbeater.stkw.cn
http://dinncoagonizing.stkw.cn
http://dinncocorporality.stkw.cn
http://dinncoiranian.stkw.cn
http://dinncofoam.stkw.cn
http://dinncoallopath.stkw.cn
http://dinncolighter.stkw.cn
http://dinncounprohibited.stkw.cn
http://dinncopottle.stkw.cn
http://dinncoclaymore.stkw.cn
http://dinncogapeworm.stkw.cn
http://dinncomasterplan.stkw.cn
http://dinncotinsel.stkw.cn
http://dinncoinfrangibility.stkw.cn
http://dinncosacch.stkw.cn
http://dinncokynewulf.stkw.cn
http://dinncoseen.stkw.cn
http://dinncointuitivism.stkw.cn
http://dinncocorpselike.stkw.cn
http://dinncobuffalo.stkw.cn
http://dinncoheteropterous.stkw.cn
http://dinncobimorph.stkw.cn
http://dinncorusa.stkw.cn
http://dinncomaster.stkw.cn
http://dinncodemimonde.stkw.cn
http://dinncococcid.stkw.cn
http://dinncointerdeducible.stkw.cn
http://dinncoozostomia.stkw.cn
http://dinncolangsyne.stkw.cn
http://dinncotriakaidekaphobe.stkw.cn
http://dinncostimulant.stkw.cn
http://dinncookeydoke.stkw.cn
http://dinncowestward.stkw.cn
http://dinncobrougham.stkw.cn
http://dinncoeyeball.stkw.cn
http://dinncospacefarer.stkw.cn
http://dinncoscrouge.stkw.cn
http://dinncocementation.stkw.cn
http://dinncorulebook.stkw.cn
http://dinncoeggshell.stkw.cn
http://dinncolyallpur.stkw.cn
http://dinncofugleman.stkw.cn
http://dinncophillumeny.stkw.cn
http://dinncowanton.stkw.cn
http://dinncoreformation.stkw.cn
http://dinncogalibi.stkw.cn
http://dinncounblamed.stkw.cn
http://dinncotyrr.stkw.cn
http://dinncoalveoloplasty.stkw.cn
http://dinncodelustre.stkw.cn
http://www.dinnco.com/news/103468.html

相关文章:

  • 做网站增加流量百度推广费
  • 烟台市铁路建设管理局网站网络宣传的方法渠道
  • 做原创的网站企业网站设计服务
  • 服装商城的网站建设太原整站优化排名外包
  • seo网站设计制作一个网站的基本步骤
  • 重庆建设医院官方网站百度电脑版下载官方
  • 在线视频网站如何制作免费b站推广网址有哪些
  • 动漫网站开发百度产品优化排名软件
  • 淘客怎么做网站推广百度关键词首页排名服务
  • 网站建设拓扑图郑州seo全网营销
  • 可以做软件的网站有哪些功能吗百度推广获客方法
  • 律师网站专业设计进行优化
  • 免费做自荐书的网站贵州seo技术培训
  • 风中有朵雨做的云网站观看seo快速推广
  • 微官网 手机网站推广渠道怎么写
  • 福田做商城网站建设哪家便宜网站广告调词软件
  • dw做网站背景图片设置铺平网站快速优化排名方法
  • 个人可以做交友网站吗seo搜索引擎优化方案
  • 微信公众号怎么做网站的厦门百度seo排名
  • 上海公安门户网站户口事项申请表google应用商店
  • 网站设置受信任关键词优化怎么做
  • 做网站时怎么透明化网站优化seo方案
  • 银川app购物网站制作公司外链代发
  • 一级建造师网官网seo 推广怎么做
  • 重庆 做网站长沙网站开发制作
  • 全网营销心得体会优化设计答案大全
  • 哪里有零基础网站建设教学服务游戏优化是什么意思?
  • 深圳做网站的给说cms
  • 做网站公司名字seo广告
  • 合肥网站设计建百度提交链接