public void Dispatch() { if (useBoidsFlocking) { if (m_boidsFlock == null) { CreateBoids(); } m_boidsFlock.UpdateBoids(); } if (MaxForces == 0) { return; } if (m_forcesCount[0] > 0) { m_forcesBuffer[0].SetData(m_forcesStruct[0]); ComputeShader.SetBuffer(UpdateForcesKernel, "forces", m_forcesBuffer[0]); Manager.SetPariclesToKernel(ComputeShader, UpdateForcesKernel); ComputeShader.Dispatch(UpdateForcesKernel, Manager.DispatchCount, m_forcesCount[0], 1); } if (m_forcesCount[1] > 0) { m_forcesBuffer[1].SetData(m_forcesStruct[1]); ComputeShader.SetBuffer(UpdateTurbulenceForcesKernel, "turbulenceForces", m_forcesBuffer[1]); for (int k = 0; k < m_forcesCount[1]; ++k) { TCForce force = m_forcesReference[1][k]; if (force.CurrentForceVolume == null) { continue; } ComputeShader.SetTexture(UpdateTurbulenceForcesKernel, "turbulenceTexture", force.CurrentForceVolume); Matrix4x4 rotation = Matrix4x4.TRS(Vector3.zero, force.transform.rotation, Vector3.one); TCHelper.SetMatrix3(ComputeShader, "turbulenceRotation", rotation); TCHelper.SetMatrix3(ComputeShader, "invTurbulenceRotation", rotation.inverse); ComputeShader.SetInt("turbulenceKernelOffset", k); Manager.SetPariclesToKernel(ComputeShader, UpdateTurbulenceForcesKernel); ComputeShader.Dispatch(UpdateTurbulenceForcesKernel, Manager.DispatchCount, 1, 1); } } }
void UpdateMatrix(ParticleEmitterShape emitShape, Transform trans) { var localScale = trans.localScale; Profiler.BeginSample("Set matrices"); Matrix4x4 id = Matrix4x4.identity; switch (Manager.SimulationSpace) { case Space.World: var rot = trans.rotation; if (m_lastRot != rot || m_lastScale != localScale) { m_emitMatrix = Matrix4x4.TRS(Vector3.zero, rot, localScale); m_emitRotationMatrix = Matrix4x4.TRS(Vector3.zero, rot, Vector3.one); m_lastRot = rot; m_lastScale = localScale; } TCHelper.SetMatrix4(ComputeShader, "emitterMatrix", m_emitMatrix); TCHelper.SetMatrix3(ComputeShader, "emitterRotationMatrix", m_emitRotationMatrix); break; case Space.Local: TCHelper.SetMatrix4(ComputeShader, "emitterMatrix", Matrix4x4.TRS(Vector3.zero, Quaternion.identity, localScale)); TCHelper.SetMatrix3(ComputeShader, "emitterRotationMatrix", id); break; case Space.LocalWithScale: TCHelper.SetMatrix4(ComputeShader, "emitterMatrix", id); TCHelper.SetMatrix3(ComputeShader, "emitterRotationMatrix", id); break; case Space.Parent: if (trans.parent != null) { var rot2 = trans.localRotation; TCHelper.SetMatrix4(ComputeShader, "emitterMatrix", Matrix4x4.TRS(Vector3.zero, rot2, localScale)); TCHelper.SetMatrix3(ComputeShader, "emitterRotationMatrix", Matrix4x4.TRS(Vector3.zero, rot2, Vector3.one)); } break; } if (emitShape.startDirectionType == StartDirectionType.Vector) { TCHelper.SetMatrix3(ComputeShader, "emitterStartRotationMatrix", Matrix4x4.TRS(Vector3.zero, Quaternion.FromToRotation(Vector3.forward, emitShape.startDirectionVector), Vector3.one)); } Profiler.EndSample(); }