コード例 #1
0
ファイル: Main.cs プロジェクト: 409544041/unity-cj-lib
        void OnEnable()
        {
            m_mesh = new Mesh();
            m_mesh = PrimitiveMeshFactory.BoxFlatShaded();

            m_spherePhase    = 0.0f;
            m_aSphere        = new GameObject[kNumSpheres];
            m_aSpherePrevPos = new Vector3[kNumSpheres];
            m_aSphereVel     = new Vector4[kNumSpheres];
            for (int i = 0; i < kNumSpheres; ++i)
            {
                m_aSphere[i]        = Instantiate(m_spherePrefab);
                m_aSpherePrevPos[i] = Vector3.zero;
            }

            m_floor = Instantiate(m_floorPrefab);

            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_csInitKernelId = m_shader.FindKernel("Init");
            m_csStepKernelId = m_shader.FindKernel("Step");

            m_csParticleBufferId = Shader.PropertyToID("particleBuffer");
            m_csScaleId          = Shader.PropertyToID("scale");
            m_csSpeedId          = Shader.PropertyToID("speed");
            m_csLifetimeId       = Shader.PropertyToID("lifetime");
            m_csNumParticlesId   = Shader.PropertyToID("numParticles");
            m_csTimeId           = Shader.PropertyToID("time");
            m_csDynamics         = Shader.PropertyToID("dynamics");
            m_csASphere          = Shader.PropertyToID("aSphere");
            m_csASphereVel       = Shader.PropertyToID("aSphereVel");
            m_csPlane            = Shader.PropertyToID("plane");

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

            m_shader.SetFloats(m_csScaleId, new float[] { 0.1f, 0.15f });
            m_shader.SetFloats(m_csSpeedId, new float[] { 1.0f, 1.5f, 1.0f, 6.0f });
            m_shader.SetFloats(m_csLifetimeId, new float[] { 0.1f, 3.0f, 3.0f, 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);
        }
コード例 #2
0
        //--------------------------------------- End of Property Block ------------------------------------//

        void OnEnable()
        {
            m_mesh = new Mesh();
            m_mesh = PrimitiveMeshFactory.SphereWireframe(20, 20);

            m_controlSphere  = Instantiate(m_controlSpherePrefab);
            m_cSpherePrevPos = Vector3.zero;

            int particleStride = sizeof(float) * 20 + sizeof(uint) * 3;

            m_computeBuffer = new ComputeBuffer(kNumParticles, particleStride);
            int gridSize = m_gridDimension[0] * m_gridDimension[1] * m_gridDimension[2];

            m_gridBuffer = new ComputeBuffer(gridSize, sizeof(uint) * 4);

            uint[] instanceArgs = new uint[] { 0, 0, 0, 0, 0 }; //5 indices: index count per instance, instance count, start index location, base vertex location, start instance location.
            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); //start instance location is always 0
            m_instanceArgsBuffer.SetData(instanceArgs);

            m_csInitKernelId = m_shader.FindKernel("Init"); // Kernel used to initialize particle location. (in ParticleLogic.compute)
            m_csStepKernelId = m_shader.FindKernel("Step"); // Kernel used to update particles per frame

            m_gsLoadGridKernelId     = m_gridShader.FindKernel("LoadGrid");
            m_gsInitParIndexKernelId = m_gridShader.FindKernel("InitParIndex");
            m_gsAssignGridKernelId   = m_gridShader.FindKernel("AssignGrid");

            // We'll need ids to manage uniforms inside the shaders.
            // Note how to get them... They share a global namespace!
            m_csParticleBufferId = Shader.PropertyToID("particleBuffer");
            m_csGridBufferId     = Shader.PropertyToID("gridBuffer");
            m_csScaleId          = Shader.PropertyToID("scale");
            m_csSpeedId          = Shader.PropertyToID("speed");
            m_csNumParticlesId   = Shader.PropertyToID("numParticles");
            m_csTimeId           = Shader.PropertyToID("time");
            m_csDynamicsId       = Shader.PropertyToID("dynamics");
            m_csASphereId        = Shader.PropertyToID("aSphere");
            m_csASphereVelId     = Shader.PropertyToID("aSphereVel");
            m_csWallGroupId      = Shader.PropertyToID("plane");
            m_csDampId           = Shader.PropertyToID("damping");

            m_gsGridDimensionId = Shader.PropertyToID("gridDimension");
            m_gsGridSizeId      = Shader.PropertyToID("gridSize");
            m_gsHighCornerId    = Shader.PropertyToID("gridHighCorner");
            m_gsLowCornerId     = Shader.PropertyToID("gridLowCorner");

            m_materialProperties = new MaterialPropertyBlock();

            InitConstraints(); //Setup grid corners and walls. Store them inside member variables for easy pass later.
            InitParticles();   // Init: Setup materials & shaders
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        void OnEnable()
        {
            m_mesh = new Mesh();
            m_mesh = PrimitiveMeshFactory.BoxFlatShaded();

            m_controlSphere  = Instantiate(m_controlSpherePrefab);
            m_cSpherePrevPos = Vector3.zero;

            m_floor = Instantiate(m_floorPrefab);

            int particleStride = sizeof(float) * 24;

            m_computeBuffer = new ComputeBuffer(kNumParticles, particleStride);

            uint[] instanceArgs = new uint[] { 0, 0, 0, 0, 0 }; //5 indices: index count per instance, instance count, start index location, base vertex location, start instance location.
            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); //start instance location is always 0
            m_instanceArgsBuffer.SetData(instanceArgs);

            m_csInitKernelId = m_shader.FindKernel("Init"); // Kernel used to initialize particle location. (in ParticleLogic.compute)
            m_csStepKernelId = m_shader.FindKernel("Step"); // Kernel used to update particles per frame

            // We'll need ids to manage uniforms inside the shaders.
            // Note how to get them... They share a global namespace!
            m_csParticleBufferId = Shader.PropertyToID("particleBuffer");
            m_csScaleId          = Shader.PropertyToID("scale");
            m_csSpeedId          = Shader.PropertyToID("speed");
            m_csLifetimeId       = Shader.PropertyToID("lifetime");
            m_csNumParticlesId   = Shader.PropertyToID("numParticles");
            m_csTimeId           = Shader.PropertyToID("time");
            m_csDynamics         = Shader.PropertyToID("dynamics");
            m_csASphere          = Shader.PropertyToID("aSphere");
            m_csASphereVel       = Shader.PropertyToID("aSphereVel");
            m_csPlane            = Shader.PropertyToID("plane");
            m_csDamp             = Shader.PropertyToID("damping");

            m_materialProperties = new MaterialPropertyBlock();

            InitParticles(); // Init: Setup materials & shaders
        }