Github项目上的错误-“值不能为空”,“未设置内核索引(1)的属性(particleBuffer)”

时间:2019-07-01 03:55:55

标签: c# unity3d kernel shader

我正在尝试了解计算着色器,这个项目真的很棒。是https://github.com/TheAllenChou/unity-cj-lib

但是当我运行它时,有一些错误我无法弄清楚如何摆脱:

  

ArgumentNullException:值不能为null。参数名称:着色器

     

ArgumentNullException:值不能为null。参数名称:材料

     

计算着色器(ParticleLogic):未设置内核索引(1)的属性(particleBuffer)

关于如何解决的任何想法?我正在使用Unity 2019.3.0a7

仅发生在“ 湍流Rainbow GPU粒子”示例的代码中,因此,我一直在转到文件并更改文件路径,并尝试手动为其提供材质。

我只是认为我不了解发生了什么足以独自解决它,所以我正在寻求帮助。感谢任何帮助。

相关代码...

using UnityEngine;

using CjLib;

namespace TurbulentRainbowGpuParticles
{
  public class Main : MonoBehaviour
  {
    public ComputeShader m_shader;

    private const int kNumParticles = 10000;

    /*
    private struct Particle
    {
      // 4 floats
      Vector3 m_position;
      float m_damping;

      // 4 floats
      Quaternion m_rotation;

      // 4 floats
      Vector3 m_linearVelocity;
      float m_scale;

      // 4 floats
      Quaternion m_angularVelocity;

      // 4 floats
      Vector4 m_lifetime;

      // 4 floats
      Color m_color;
    };
    */

    private ComputeBuffer m_computeBuffer;
    private ComputeBuffer m_instanceArgsBuffer;
    //private Particle[] m_debugBuffer;

    private Mesh m_mesh;
    private Material m_material;
    private MaterialPropertyBlock m_materialProperties;

    private int m_csInitKernelId;
    private int m_csStepKernelId;

    private int m_csParticleBufferId;
    private int m_csScaleId;
    private int m_csDampingId;
    private int m_csSpeedId;
    private int m_csLifetimeId;
    private int m_csNumParticlesId;
    private int m_csTimeId;

    void OnEnable()
    {
      m_mesh = new Mesh();
      m_mesh = PrimitiveMeshFactory.BoxFlatShaded();

      int particleStride = sizeof(float) * 24;
      m_computeBuffer = new ComputeBuffer(kNumParticles, particleStride);

      uint[] instanceArgs = new uint[] { 0, 0, 0, 0, 0 };
      m_instanceArgsBuffer = new ComputeBuffer(1, instanceArgs.Length * sizeof(uint), ComputeBufferType.IndirectArguments);
      instanceArgs[0] = (uint) m_mesh.GetIndexCount(0);
      instanceArgs[1] = (uint) kNumParticles;
      instanceArgs[2] = (uint) m_mesh.GetIndexStart(0);
      instanceArgs[3] = (uint) m_mesh.GetBaseVertex(0);
      m_instanceArgsBuffer.SetData(instanceArgs);

      //m_debugBuffer = new Particle[kNumParticles];

      m_csInitKernelId = m_shader.FindKernel("Init");
      m_csStepKernelId = m_shader.FindKernel("Step");

      m_csParticleBufferId = Shader.PropertyToID("particleBuffer");
      m_csScaleId = Shader.PropertyToID("scale");
      m_csDampingId = Shader.PropertyToID("damping");
      m_csSpeedId = Shader.PropertyToID("speed");
      m_csLifetimeId = Shader.PropertyToID("lifetime");
      m_csNumParticlesId = Shader.PropertyToID("numParticles");
      m_csTimeId = Shader.PropertyToID("time");

      m_material = new Material(Shader.Find("CjLib/Example/TurbulentRainbowGpuParticles"));
      m_material.enableInstancing = true;
      m_material.SetBuffer(m_csParticleBufferId, m_computeBuffer);
      m_materialProperties = new MaterialPropertyBlock();

      m_shader.SetFloats(m_csScaleId, new float[] { 0.15f, 0.3f });
      m_shader.SetFloat(m_csDampingId, 6.0f);
      m_shader.SetFloats(m_csSpeedId, new float[] { 3.0f, 4.0f, 1.0f, 6.0f });
      m_shader.SetFloats(m_csLifetimeId, new float[] { 0.1f, 0.5f, 0.5f, 0.1f });
      m_shader.SetInt(m_csNumParticlesId, kNumParticles);

      m_shader.SetBuffer(m_csInitKernelId, m_csParticleBufferId, m_computeBuffer);
      m_shader.SetBuffer(m_csStepKernelId, m_csParticleBufferId, m_computeBuffer);

      m_shader.Dispatch(m_csInitKernelId, kNumParticles, 1, 1);

      //m_computeBuffer.GetData(m_debugBuffer);
    }

    void Update()
    {
      m_shader.SetFloats(m_csTimeId, new float[] { Time.time, Time.fixedDeltaTime });
      m_shader.Dispatch(m_csStepKernelId, kNumParticles, 1, 1);

      //m_computeBuffer.GetData(m_debugBuffer);

      Graphics.DrawMeshInstancedIndirect(m_mesh, 0, m_material, new Bounds(Vector3.zero, 20.0f * Vector3.one), m_instanceArgsBuffer, 0, m_materialProperties, UnityEngine.Rendering.ShadowCastingMode.On);
    }

    void OnDisable()
    {
      if (m_computeBuffer != null)
      {
        m_computeBuffer.Dispose();
        m_computeBuffer = null;
      }

      if (m_instanceArgsBuffer != null)
      {
        m_instanceArgsBuffer.Dispose();
        m_instanceArgsBuffer = null;
      }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

修复:

  1. 切换到最新的稳定 Unity版本

  2. 修复着色器。通过此方法(unity Shader returns a NULL when using Shader.Find

  3. (可选),如果像我一样遇到C#语言规范错误,请访问TimeWalk-Org的帖子https://forum.unity.com/threads/feature-xxx-cannot-be-used-because-it-is-not-part-of-the-c-4-0-language-specification.575008/