コード例 #1
0
    void Awake()
    {
        //Create a cube showing full HSV range.
        //Assign a HSV color based on position to every particle

        //This uses the 'ParticleProto' API. You create some prototype particles and emit these.
        int       count = 0;
        const int num   = 80;

        ParticleProto[] p = new ParticleProto[num * num * num];

        for (int i = 0; i < num; ++i)
        {
            for (int j = 0; j < num; ++j)
            {
                for (int k = 0; k < num; ++k)
                {
                    p[count].Position = new Vector3((float)i / num, (float)j / num, (float)k / num) * 2.0f - Vector3.one;
                    p[count].Color    = Color.HSVToRGB((float)i / num, (float)j / num, (float)k / num);
                    p[count].Size     = 1.0f;                 //Note: Multiplicative with size set in TC Particles, so particles aren't 1 unit large
                    p[count].Velocity = Vector3.zero;
                    ++count;
                }
            }
        }

        //Submit the buffer to emit
        GetComponent <TCParticleSystem>().Emit(p);
    }
コード例 #2
0
    void SpawnPointCloud()
    {
        Stopwatch sw = new Stopwatch();

        sw.Start();

        Profiler.BeginSample("Setup");
        var bytes = File.ReadAllBytes(Path.Combine(Application.streamingAssetsPath, StreamPclPath));

        var binReader = new PclBinaryReader(bytes);

        StringBuilder curStr     = new StringBuilder();
        int           pointCount = 0;

        List <PropType> m_types = new List <PropType>();

        bool inVertElement = false;
        bool binary        = false;

        Profiler.EndSample();

        Profiler.BeginSample("Read headers");

        while (true)
        {
            char cur = binReader.ReadChar();

            if (cur == '\n')
            {
                string line = curStr.ToString();

                if (line == "end_header")
                {
                    break;
                }

                var lines = line.Split(' ');

                if (lines[0] == "format")
                {
                    binary = lines[1] == "binary_big_endian";
                }
                else if (lines[0] == "element")
                {
                    if (lines[1] == "vertex")
                    {
                        pointCount    = int.Parse(lines[2]);
                        inVertElement = true;
                    }
                    else
                    {
                        inVertElement = false;
                    }
                }
                else if (inVertElement && lines[0] == "property" && lines[1] != "list")
                {
                    if (lines[1] == "float" || lines[1] == "float32")
                    {
                        m_types.Add(PropType.Float);
                    }

                    if (lines[1] == "uchar" || lines[1] == "uint8")
                    {
                        m_types.Add(PropType.Byte);
                    }
                }

                curStr.Length   = 0;
                curStr.Capacity = 0;
            }
            else
            {
                curStr.Append(cur);
            }
        }
        Profiler.EndSample();

        Profiler.BeginSample("Parse points");
        var points = new ParticleProto[pointCount];

        bool hasColor  = m_types.Count(c => c == PropType.Byte) >= 3;
        int  typeCount = m_types.Count;

        float[] parseFloats = new float[typeCount];
        byte[]  parseBytes  = new byte[typeCount];

        for (int i = 0; i < pointCount; ++i)
        {
            var floatsParsed = 0;
            var bytesParsed  = 0;

            for (var prop = 0; prop < typeCount; ++prop)
            {
                var type = m_types[prop];

                if (type == PropType.Float)
                {
                    float val = binary ? binReader.ReadSingle() : binReader.ReadAsciiFloat();
                    parseFloats[floatsParsed++] = val;
                }
                else
                {
                    byte val = binary ? binReader.ReadByte() : binReader.ReadAsciiByte();
                    parseBytes[bytesParsed++] = val;
                }
            }

            float x = parseFloats[0] * Scale;
            float y = parseFloats[1] * Scale;
            float z = parseFloats[2] * Scale;

            if (hasColor && !UseRainbow)
            {
                byte r = parseBytes[0];
                byte g = parseBytes[1];
                byte b = parseBytes[2];

                points[i].Color.r = r / 255.0f;
                points[i].Color.g = g / 255.0f;
                points[i].Color.b = b / 255.0f;
                points[i].Color.a = 1.0f;
            }
            else if (UseRainbow)
            {
                //No color -> Rainbow color!
                points[i].Color = Color.HSVToRGB(Mathf.Repeat(x + y + z, 1.0f), 1, 1);
            }

            points[i].Position.x = x;
            points[i].Position.y = y;
            points[i].Position.z = z;

            points[i].Size = 1.0f;
        }
        Profiler.EndSample();

        GetComponent <TCParticleSystem>().Emit(points);
    }