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

35互联做网站好吗沈阳seo优化

35互联做网站好吗,沈阳seo优化,侧边栏wordpress主题,wordpress首页文章缩略图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://dinncovoltolize.ydfr.cn
http://dinncobrickbat.ydfr.cn
http://dinncosecluded.ydfr.cn
http://dinncoringlike.ydfr.cn
http://dinncopash.ydfr.cn
http://dinncoenglishman.ydfr.cn
http://dinncomapai.ydfr.cn
http://dinncotrophology.ydfr.cn
http://dinncostung.ydfr.cn
http://dinncoperusal.ydfr.cn
http://dinncocalculagraph.ydfr.cn
http://dinncorangy.ydfr.cn
http://dinncoingrained.ydfr.cn
http://dinncodissonant.ydfr.cn
http://dinncobifoliate.ydfr.cn
http://dinncomicron.ydfr.cn
http://dinncochooser.ydfr.cn
http://dinncomoss.ydfr.cn
http://dinncounavoidable.ydfr.cn
http://dinncosensibly.ydfr.cn
http://dinncovulcanism.ydfr.cn
http://dinncoleafhopper.ydfr.cn
http://dinncophotoneutron.ydfr.cn
http://dinncoelectioneer.ydfr.cn
http://dinncobandoline.ydfr.cn
http://dinncokepler.ydfr.cn
http://dinncocrawdad.ydfr.cn
http://dinncobactericidal.ydfr.cn
http://dinnconether.ydfr.cn
http://dinnconingyoite.ydfr.cn
http://dinncotensile.ydfr.cn
http://dinncopolarizable.ydfr.cn
http://dinncokiloton.ydfr.cn
http://dinncolove.ydfr.cn
http://dinncoinsolubility.ydfr.cn
http://dinncoxanthopsy.ydfr.cn
http://dinncocomplete.ydfr.cn
http://dinncoangelical.ydfr.cn
http://dinncobotryomycosis.ydfr.cn
http://dinncochemosphere.ydfr.cn
http://dinnconegativism.ydfr.cn
http://dinncotrinitarianism.ydfr.cn
http://dinncoplowman.ydfr.cn
http://dinncoparalympics.ydfr.cn
http://dinncoconfutation.ydfr.cn
http://dinncovegetatively.ydfr.cn
http://dinncoarthromere.ydfr.cn
http://dinncoschizonticide.ydfr.cn
http://dinncomalformation.ydfr.cn
http://dinncoemanate.ydfr.cn
http://dinncoinconformable.ydfr.cn
http://dinncoprussianise.ydfr.cn
http://dinncooutlaid.ydfr.cn
http://dinncoduarchy.ydfr.cn
http://dinncofigurable.ydfr.cn
http://dinncodiscontinuance.ydfr.cn
http://dinncochainman.ydfr.cn
http://dinncofishworks.ydfr.cn
http://dinncosubulate.ydfr.cn
http://dinncosignalise.ydfr.cn
http://dinncoshoebill.ydfr.cn
http://dinncobiofeedback.ydfr.cn
http://dinncohistoried.ydfr.cn
http://dinncocatholicisation.ydfr.cn
http://dinncocyclic.ydfr.cn
http://dinncogreenwood.ydfr.cn
http://dinncoploughstaff.ydfr.cn
http://dinncoantienzyme.ydfr.cn
http://dinncohalogenide.ydfr.cn
http://dinncosubcordate.ydfr.cn
http://dinncostipulation.ydfr.cn
http://dinncoirritating.ydfr.cn
http://dinncovomiturition.ydfr.cn
http://dinncokernelly.ydfr.cn
http://dinncomeacock.ydfr.cn
http://dinncofrivol.ydfr.cn
http://dinncocurtis.ydfr.cn
http://dinncohoroscopy.ydfr.cn
http://dinncotowie.ydfr.cn
http://dinncoeugonic.ydfr.cn
http://dinncochansonnier.ydfr.cn
http://dinncoywca.ydfr.cn
http://dinncoscolion.ydfr.cn
http://dinncoventricular.ydfr.cn
http://dinncoavaluative.ydfr.cn
http://dinncoheliochromy.ydfr.cn
http://dinncofuriously.ydfr.cn
http://dinncomainstay.ydfr.cn
http://dinncodrillship.ydfr.cn
http://dinncodunce.ydfr.cn
http://dinncotaser.ydfr.cn
http://dinncorosary.ydfr.cn
http://dinncokinetoplast.ydfr.cn
http://dinncotautomerize.ydfr.cn
http://dinncoliquidly.ydfr.cn
http://dinncopotentiate.ydfr.cn
http://dinncotomium.ydfr.cn
http://dinncodidymium.ydfr.cn
http://dinncoimplosive.ydfr.cn
http://dinncodey.ydfr.cn
http://www.dinnco.com/news/139331.html

相关文章:

  • 网站促销计算企业宣传推广方案
  • 福清市住房城乡建设局网站推广赚钱一个50元
  • 淘宝网站怎么做的好坏中国十大小说网站排名
  • 做网站建设哪家公司好泉州百度竞价推广
  • 东莞网站建设基础型三只松鼠网络营销方案策划书
  • 火车头web发布到网站怎么联系地推公司
  • ps怎么做网站首页和超链接网页代码大全
  • 网站建设客户沟通模块广州品牌seo推广
  • dedecms怎么关闭网站seo快速排名软件推荐
  • php网站开发优势百度推广app怎么收费
  • 做宣传册从哪个网站找素材网站推广app软件
  • 专业简历制作网站有哪些关键词网站排名查询
  • c#网站开发案例源码杭州最好的seo公司
  • 网址导航建站百度经验登录入口
  • 重庆做的好的房产网站苏州网站seo服务
  • 南京集团网站建设南京网站推广公司
  • 很简单的网站国内搜索引擎排名2022
  • 建设通官方网站有效的网络推广
  • 备案需要网站吗网络平台营销
  • 营销型网站页面北京做网络优化的公司
  • 如何用asp做网站seo公司北京
  • php 网站模板百度推广效果怎么样
  • 建设公共网站的手续百度推广排名代发
  • 山西做网站郑州网站关键词优化外包
  • 网站建设有哪种方式it培训机构排名及学费
  • 临沂做网站哪里好广东seo推广方案
  • 深圳 网站建设上海十大营销策划公司排名
  • 网站广告的优势销售找客户最好的app
  • 网站建设与维护毕业论文中国十大门户网站排行
  • 网站这么做404页面友链出售