void RandomlySpread() { for (int i = 0; i < particleNum; ++i) //对于每个粒子 { float midRadius = (maxRadius + minRadius) / 2; //计算平均半径 float minRate = UnityEngine.Random.Range(1.0f, midRadius / minRadius); //在1到midRadius / minRadius之间产生一个随机数 float maxRate = UnityEngine.Random.Range(midRadius / maxRadius, 1.0f); //在midRadius / maxRadius到1之间产生一个随机数 float _radius = UnityEngine.Random.Range(minRadius * minRate, maxRadius * maxRate); //在minRadius * minRate到maxRadius * maxRate之间产生一个随机数 设置为当前半径 radius[i] = _radius; //设置半径 float collect_MidRadius = (collect_MaxRadius + collect_MinRadius) / 2; //计算平均半径 float collect_outRate = Random.Range(1.0f, collect_MidRadius / collect_MinRadius);; //在1到midRadius / minRadius之间产生一个随机数 float collect_inRate = Random.Range(collect_MaxRadius / collect_MidRadius, 1f); //在midRadius / maxRadius到1之间产生一个随机数 float _collect_radius = Random.Range(collect_MinRadius * collect_outRate, collect_MaxRadius * collect_inRate); //在minRadius * minRate到maxRadius * maxRate之间产生一个随机数 设置为当前半径 collect_radius[i] = _collect_radius; //设置半径 float angle = UnityEngine.Random.Range(0.0f, 360.0f); //随机获取角度 float theta = angle / 180 * Mathf.PI; //计算角度出弧度制下对应的值 float time = UnityEngine.Random.Range(0.0f, 360.0f); //随机获取时间 if (isCollected == 0) //当前为外圈 { particles[i] = new particleData(_radius, angle, time); //设置为外圈参数 } else //当前为内圈 { particles[i] = new particleData(_collect_radius, angle, time); //设置为内圈参数 } particleArr[i].position = new Vector3(particles[i].radius * Mathf.Cos(theta), 0f, particles[i].radius * Mathf.Sin(theta)); //根据半径和角度计算出所处位置 } parSys.SetParticles(particleArr, particleArr.Length); //设定粒子数组 }
private void OnEnable() { //初始化 int vector3stride = sizeof(float) * 3; //值为12 计算3个FLOAT的大小 1个单精度float是4字节 int colorStride = sizeof(float) * 4; //4字节*4=16 stride = vector3stride + colorStride; //统计一个结构体的长度 其值为12+16=28字节 Buffer = new ComputeBuffer(count, stride); //count个粒子 每个粒子占用大小 //Buffer=new ComputeBuffer(count,28);//这里吧stride改成28亦可 kernelid = compute.FindKernel("ComputeParticle"); //填充数据 给这几十万个粒子赋初始值 datas = new particleData[count]; for (int t = 0; t < datas.Length; t++) { datas[t] = new particleData(); } Buffer.SetData(datas); //往buffer里填结构体数据 compute.SetBuffer(kernelid, "outputData", Buffer); //把buffer里的数据传给compute shader去计算 mymat.SetBuffer("inputbuffer", Buffer); // 把buffer的数据也传给vertex/fragment shader去渲染 }
// Generates a single wind particle void createParticle(particleData thisData) { }