Ejemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        //TMP
        if (Input.GetKey(KeyCode.G))
        {
            generateOffsetRandoms();
        }

        if (lastDownSample != downSample)
        {
            updateDownSample();
            lastDownSample = downSample;
            return;
        }

        CameraSpacePoint[] realWorldMap = multiSourceManager.GetRealWorldData();
        byte[]             bodyIndexMap = multiSourceManager.GetBodyIndexData();
        //Texture2D tex = multiSourceManager.GetColorTexture();


        bodyTree = new PointOctree <PCLPoint>(10, bodyCenter, .01f); //take previous frame
        bodyPoints.Clear();
        bodyCenter = new Vector3();



        for (int ix = 0; ix < pointsWidth; ix++)
        {
            for (int iy = 0; iy < pointsHeight; iy++)
            {
                int dsIndex = iy * pointsWidth + ix;

                Vector2 rIndex = dsOffsetRandoms[dsIndex];

                int index = Mathf.RoundToInt(rIndex.x * multiSourceManager.DepthHeight * multiSourceManager.DepthWidth) + Mathf.RoundToInt(rIndex.y * multiSourceManager.DepthWidth);

                //Vector3 dv = new Vector3(rIndex.x * 10, rIndex.y * 10);

                //Debug.DrawLine(dv, dv + Vector3.forward * .2f, Color.red);


                if (index >= realWorldMap.Length)
                {
                    continue;
                }


                CameraSpacePoint csp = realWorldMap[index];
                Vector3          p   = new Vector3(csp.X, csp.Y, csp.Z);

                Vector3 tPoint = transform.TransformPoint(p);

                int tIndex = iy * pointsWidth + ix;
                bodyMask[tIndex] = bodyIndexMap[index] != 255;

                bool isBody  = bodyMask[tIndex];
                bool isValid = true;

                if (isBody)
                {
                    if (bodyRandomProba < 1)
                    {
                        isBody = Random.value <= bodyRandomProba;
                    }
                }

                if (float.IsNaN(tPoint.x) || float.IsInfinity(tPoint.x))
                {
                    isValid = false;
                    tPoint  = Vector3.zero;
                }

                PCLPoint pp = new PCLPoint(tPoint, isBody, isValid, true, isBody ? Color.yellow : Color.white);
                points[tIndex] = pp;


                if (isBody && isValid)
                {
                    bodyPoints.Add(pp);

                    bodyTree.Add(pp, tPoint);
                    bodyCenter += tPoint;
                }

                if (debug && isValid)
                {
                    if (isBody || !debugBodyOnly)
                    {
                        Color c = isBody ? Color.yellow : Color.white;
                        Debug.DrawLine(tPoint, tPoint + Vector3.forward * .05f, c);
                    }
                }
            }
        }


        if (bodyPoints.Count > 0)
        {
            bodyCenter /= bodyPoints.Count;
        }
    }