protected void SetupAttractions() { var kernel = compute.FindKernel("SetupAttractions"); compute.SetBuffer(kernel, "_Attractions", attractionBuffer); GPUHelper.Dispatch1D(compute, kernel, count); }
protected void SetupSkin() { var mesh = skinnedRenderer.sharedMesh; var vertices = mesh.vertices; var weights = mesh.boneWeights; var indices = new int[weights.Length]; for (int i = 0, n = weights.Length; i < n; i++) { indices[i] = weights[i].boneIndex0; } using ( ComputeBuffer vertBuffer = new ComputeBuffer(vertices.Length, Marshal.SizeOf(typeof(Vector3))), boneBuffer = new ComputeBuffer(weights.Length, Marshal.SizeOf(typeof(uint))) ) { vertBuffer.SetData(vertices); boneBuffer.SetData(indices); var kernel = compute.FindKernel("SetupSkin"); compute.SetBuffer(kernel, "_Vertices", vertBuffer); compute.SetBuffer(kernel, "_Bones", boneBuffer); compute.SetBuffer(kernel, "_Attractions", attractionBuffer); GPUHelper.Dispatch1D(compute, kernel, attractionBuffer.count); } }
protected void Invalidate(PointCloudGrid grid) { var kernel = compute.FindKernel("Invalidate"); compute.SetBuffer(kernel, "_PointBuffer", grid.PointBuffer); compute.SetInt("_InstancesCount", grid.PointBuffer.count); GPUHelper.Dispatch1D(compute, kernel, grid.PointBuffer.count); }
protected void Search() { var kernel = compute.FindKernel("Search"); compute.SetBuffer(kernel, "_Attractions", attractionBuffer); compute.SetBuffer(kernel, "_Nodes", nodeBuffer); compute.SetFloat("_InfluenceDistance", unitDistance * influenceDistance); GPUHelper.Dispatch1D(compute, kernel, count); }
protected void Remove() { var kernel = compute.FindKernel("Remove"); compute.SetBuffer(kernel, "_Attractions", attractionBuffer); compute.SetBuffer(kernel, "_Nodes", nodeBuffer); compute.SetFloat("_KillDistance", unitDistance * killDistance); GPUHelper.Dispatch1D(compute, kernel, count); }
protected void Init() { var kernel = compute.FindKernel("Init"); compute.SetBuffer(kernel, "_Crowd", crowdBuffer.Read); compute.SetBuffer(kernel, "_EmitBounds", emitBoundsBuffer); compute.SetFloat("_Size", size); compute.SetVector("_ScaleRange", scaleRange); SetupGradient(kernel); GPUHelper.Dispatch1D(compute, kernel, count); }
protected void FloorKernel(float dt) { gridUnitLength = 0f; var kernel = new Kernel(voxelControl, "FloorGrid"); SetupControlKernel(kernel, dt); voxelControl.SetInt("_GridResolution", gridResolution); GPUHelper.Dispatch1D(voxelControl, kernel, data.Buffer.count); }
protected void Grow(float dt) { var kernel = compute.FindKernel("Grow"); compute.SetBuffer(kernel, "_Nodes", nodeBuffer); var delta = dt * growthSpeed; compute.SetFloat("_DT", delta); GPUHelper.Dispatch1D(compute, kernel, count); }
protected void KernelGrids(int kernel) { foreach (var grid in grids) { if (grid.On) { var buffer = grid.PointBuffer; compute.SetBuffer(kernel, "_PointBuffer", buffer); compute.SetBuffer(kernel, "_OriginBuffer", grid.OriginBuffer); compute.SetInt("_InstancesCount", buffer.count); GPUHelper.Dispatch1D(compute, kernel, buffer.count); } } }
protected void Attract() { var kernel = compute.FindKernel("Attract"); compute.SetBuffer(kernel, "_Attractions", attractionBuffer); compute.SetBuffer(kernel, "_Nodes", nodeBuffer); candidatePoolBuffer.SetCounterValue(0); compute.SetBuffer(kernel, "_CandidatesPoolAppend", candidatePoolBuffer); compute.SetFloat("_GrowthDistance", unitDistance * growthDistance); GPUHelper.Dispatch1D(compute, kernel, count); }
protected void Update(float t, float dt) { var kernel = compute.FindKernel("Update"); compute.SetBuffer(kernel, "_Crowd", crowdBuffer.Read); compute.SetFloat("_Throttle", Mathf.Lerp(-0.01f, 1f, _throttle)); compute.SetFloat("_Time", t); compute.SetFloat("_DT", dt); compute.SetFloat("_Decay", decay); compute.SetVector("_Center", center.position); SetupGradient(kernel); SetupGather(); GPUHelper.Dispatch1D(compute, kernel, count); }
protected void Animate() { var bones = skinnedRenderer.bones.Select(bone => bone.localToWorldMatrix).ToArray(); using (ComputeBuffer boneMatrixBuffer = new ComputeBuffer(bones.Length, Marshal.SizeOf(typeof(Matrix4x4)))) { boneMatrixBuffer.SetData(bones); var kernel = compute.FindKernel("Animate"); compute.SetBuffer(kernel, "_BindPoses", bindPoseBuffer); compute.SetBuffer(kernel, "_BoneMatrices", boneMatrixBuffer); compute.SetBuffer(kernel, "_Nodes", nodeBuffer); GPUHelper.Dispatch1D(compute, kernel, count); } }
protected void Wander(float t, float dt) { var kernel = compute.FindKernel("Wander"); compute.SetBuffer(kernel, "_Crowd", crowdBuffer.Read); compute.SetFloat("_Time", t); compute.SetFloat("_DT", dt); SetupGradient(kernel); compute.SetFloat("_WanderRadius", wanderRadius); compute.SetFloat("_WanderDistance", wanderDistance); compute.SetFloat("_WanderWeight", wanderWeight); GPUHelper.Dispatch1D(compute, kernel, count); }
protected void Bake() { var kernel = compute.FindKernel((dendrite.Type == DendriteType.Skinned) ? "BakeSkinned" : "Bake"); compute.SetTexture(kernel, "_Volume", buffer); var max = Mathf.Max(bounds.size.x, bounds.size.y, bounds.size.z); compute.SetVector("_Min", bounds.min + new Vector3(bounds.size.x - max, bounds.size.y - max, bounds.size.z - max) * 0.5f); compute.SetVector("_Size", Vector3.one * max); compute.SetFloat("_Thickness", thickness); compute.SetBuffer(kernel, "_Edges", dendrite.EdgeBuffer); compute.SetBuffer(kernel, "_Nodes", dendrite.NodeBuffer); compute.SetInt("_EdgesCount", dendrite.EdgesCount); GPUHelper.Dispatch1D(compute, kernel, dendrite.EdgesCount); }
protected virtual void Compute(Kernel kernel, float dt) { compute.SetInt(kInstancesCountKey, instancesCount); compute.SetBuffer(kernel.Index, kBonesKey, boneBuffer); compute.SetInt(kBonesCountKey, boneCount); compute.SetFloat(kBonesCountInvKey, 1f / boneCount); var t = Time.timeSinceLevelLoad; compute.SetVector(kTimeKey, new Vector4(t / 20f, t, t * 2f, t * 3f)); compute.SetVector(kDTKey, new Vector2(dt, (dt < float.Epsilon) ? 0f : 1f / dt)); compute.SetMatrix(kWorldToLocalKey, transform.worldToLocalMatrix); compute.SetMatrix(kLocalToWorldKey, transform.localToWorldMatrix); compute.SetMatrix(kBindMatrixKey, material.GetMatrix(kBindMatrixKey)); compute.SetMatrix(kBindMatrixInvKey, material.GetMatrix(kBindMatrixInvKey)); GPUHelper.Dispatch1D(compute, kernel, instancesCount); }
protected void Connect() { var kernel = compute.FindKernel("Connect"); compute.SetFloat("_MassMin", massMin); compute.SetFloat("_MassMax", massMax); compute.SetBuffer(kernel, "_Nodes", nodeBuffer); compute.SetBuffer(kernel, "_NodesPoolConsume", nodePoolBuffer); compute.SetBuffer(kernel, "_EdgesAppend", edgeBuffer); compute.SetBuffer(kernel, "_CandidatesConsume", candidateBuffer); var connectCount = Mathf.Min(nodesCount, CopyCount(candidateBuffer)); if (connectCount > 0) { compute.SetInt("_ConnectCount", connectCount); GPUHelper.Dispatch1D(compute, kernel, connectCount); } }
void SetupKernel() { var setupKer = new Kernel(particleUpdate, "Setup"); particleUpdate.SetBuffer(setupKer.Index, kVoxelBufferKey, data.Buffer); particleUpdate.SetInt(kVoxelCountKey, data.Width * data.Height * data.Depth); particleUpdate.SetInt("_Width", data.Width); particleUpdate.SetInt("_Height", data.Height); particleUpdate.SetInt("_Depth", data.Depth); particleUpdate.SetInt("_Level", level); particleUpdate.SetBuffer(setupKer.Index, kParticleBufferKey, particleBuffer); particleUpdate.SetInt(kParticleCountKey, particleBuffer.count); particleUpdate.SetVector(kSpeedKey, new Vector2(speedScaleMin, speedScaleMax)); particleUpdate.SetFloat("_UnitLength", unitLength); GPUHelper.Dispatch1D(particleUpdate, setupKer, particleBuffer.count); }
protected void Flow(float t, float dt) { var kernel = compute.FindKernel("Flow"); compute.SetBuffer(kernel, "_Crowd", crowdBuffer.Read); compute.SetFloat("_Time", t); compute.SetFloat("_DT", dt); SetupGradient(kernel); SetupGather(); compute.SetVector("_Center", center.position); compute.SetFloat("_FlowWeight", flowWeight); compute.SetFloat("_FlowRadius", flowRadius); compute.SetFloat("_MaxSpeed", maxSpeed); compute.SetFloat("_MaxSteerForce", maxSteerForce); GPUHelper.Dispatch1D(compute, kernel, count); }
protected void Gather(float t, float dt) { var kernel = compute.FindKernel("Gather"); compute.SetBuffer(kernel, "_CrowdRead", crowdBuffer.Read); compute.SetBuffer(kernel, "_Crowd", crowdBuffer.Write); compute.SetFloat("_Time", t); compute.SetFloat("_DT", dt); SetupGradient(kernel); SetupGather(); compute.SetFloat("_SeparateNeighborhoodRadius", separateNeighborhoodRadius); compute.SetFloat("_SeparateWeight", separateWeight); compute.SetFloat("_MaxSpeed", maxSpeed); compute.SetFloat("_MaxSteerForce", maxSteerForce); GPUHelper.Dispatch1D(compute, kernel, count); crowdBuffer.Swap(); }
protected void ComputeTrails(Kernel kernel, float dt) { CheckInit(); dt = Mathf.Clamp(dt, 0f, 0.1f); trailCompute.SetInt(kInstancesCountKey, instancesCount); trailCompute.SetBuffer(kernel.Index, kTrailsKey, trailBuffer); trailCompute.SetBuffer(kernel.Index, kBonesKey, boneBuffer); trailCompute.SetInt(kBonesCountKey, boneCount); trailCompute.SetFloat(kBonesCountInvKey, 1f / boneCount); var t = Time.timeSinceLevelLoad; trailCompute.SetVector(kTimeKey, new Vector4(t / 20f, t, t * 2f, t * 3f)); trailCompute.SetVector(kDTKey, new Vector2(dt, (dt < float.Epsilon) ? 0f : 1f / dt)); trailCompute.SetMatrix(kWorldToLocalKey, transform.worldToLocalMatrix); trailCompute.SetMatrix(kLocalToWorldKey, transform.localToWorldMatrix); trailCompute.SetFloat(kTrailFollowIntensityKey, trailFollowIntensity); trailCompute.SetVector(kTrailFollowDelayKey, new Vector2(trailFollowDelayMin, trailFollowDelayMax)); trailCompute.SetVector(kSpeedRangeKey, new Vector2(speedMin, speedMax)); trailCompute.SetVector(kDamperKey, new Vector2(Mathf.Exp(-drag * dt), speedLimit)); trailCompute.SetVector(kGravityKey, gravity * dt); var ap = transform.InverseTransformPoint(attractor.position); trailCompute.SetVector(kAttractorKey, new Vector4(ap.x, ap.y, ap.z, attractorSpread)); trailCompute.SetVector(kNoiseParamsKey, new Vector2(noiseFrequency, noiseAmplitude * dt)); var noiseDir = (gravity == Vector3.zero) ? Vector3.up : gravity.normalized; noiseOffset += noiseDir * noiseMotion * dt; trailCompute.SetVector(kNoiseOffsetKey, noiseOffset); trailCompute.SetFloat("_Throttle", throttle); GPUHelper.Dispatch1D(trailCompute, kernel, instancesCount); }
protected void ComputeParticleKernel(Kernel kernel, float dt = 0f) { particleUpdate.SetBuffer(kernel.Index, kVoxelBufferKey, data.Buffer); particleUpdate.SetInt(kVoxelCountKey, data.Width * data.Height * data.Depth); particleUpdate.SetInt("_Width", data.Width); particleUpdate.SetInt("_Height", data.Height); particleUpdate.SetInt("_Depth", data.Depth); particleUpdate.SetInt("_Level", level); particleUpdate.SetFloat("_UnitLength", unitLength); particleUpdate.SetBuffer(kernel.Index, kParticleBufferKey, particleBuffer); particleUpdate.SetInt(kParticleCountKey, particleBuffer.count); particleUpdate.SetVector("_Time", GetTime(Time.timeSinceLevelLoad)); particleUpdate.SetVector("_DT", new Vector2(dt, 1f / dt)); particleUpdate.SetVector("_Damper", new Vector2(Mathf.Exp(-drag * dt), speedLimit)); particleUpdate.SetVector("_Gravity", gravity * dt); var pi360dt = Mathf.PI * dt / 360; particleUpdate.SetVector("_Spin", new Vector2(maxSpin * pi360dt, speedToSpin * pi360dt)); particleUpdate.SetVector("_NoiseParams", new Vector2(noiseFrequency, noiseAmplitude * dt)); var noiseDir = (gravity == Vector3.zero) ? Vector3.up : gravity.normalized; noiseOffset += noiseDir * noiseMotion * dt; particleUpdate.SetVector("_NoiseOffset", noiseOffset); particleUpdate.SetFloat(kDelaySpeedKey, delaySpeed); particleUpdate.SetFloat(kTransformSpeedKey, transformSpeed); particleUpdate.SetFloat(kClipSpeedKey, clipSpeed); particleUpdate.SetVector(kClipMinKey, clipBounds.min); particleUpdate.SetVector(kClipMaxKey, clipBounds.max); particleUpdate.SetFloat(kFlowSpeedKey, flowSpeed); GPUHelper.Dispatch1D(particleUpdate, kernel, particleBuffer.count); }
protected void Setup(Vector3[] seeds) { var kernel = compute.FindKernel("Setup"); compute.SetBuffer(kernel, "_NodesPoolAppend", nodePoolBuffer); compute.SetBuffer(kernel, "_Nodes", nodeBuffer); GPUHelper.Dispatch1D(compute, kernel, count); using (ComputeBuffer seedBuffer = new ComputeBuffer(seeds.Length, Marshal.SizeOf(typeof(Vector3)))) { seedBuffer.SetData(seeds); kernel = compute.FindKernel("Seed"); compute.SetFloat("_MassMin", massMin); compute.SetFloat("_MassMax", massMax); compute.SetBuffer(kernel, "_Seeds", seedBuffer); compute.SetBuffer(kernel, "_NodesPoolConsume", nodePoolBuffer); compute.SetBuffer(kernel, "_Nodes", nodeBuffer); GPUHelper.Dispatch1D(compute, kernel, seedBuffer.count); } nodesCount = nodePoolBuffer.count; edgesCount = 0; }
protected void Reset() { var kernel = compute.FindKernel("Reset"); compute.SetBuffer(kernel, "_Buffer", vertexBuffer); GPUHelper.Dispatch1D(compute, kernel, vertexBuffer.count); }