Exemplo n.º 1
0
    void Start()
    {
        tfm = this.transform;

        cam      = GetComponent <Camera>();
        material = new Material(shader);

        cam.clearFlags = CameraClearFlags.Nothing;

        cells_order = new CellSortInfo[8];

        node_stack   = new NodeStackItem[32];
        linked_idxyz = new IdXYZ[32];
        for (int j = 0; j < node_stack.Length; j++)
        {
            node_stack[j] = new NodeStackItem();

            // the 1st and the 7 following
            var idxyz0 = new IdXYZ();
            var idxyz  = idxyz0;
            for (int i = 1; i < 8; i++)
            {
                idxyz.next = new IdXYZ();
                idxyz      = idxyz.next;
            }

            linked_idxyz[j] = idxyz0;
        }

        stopwatch = new System.Diagnostics.Stopwatch();
    }
Exemplo n.º 2
0
            public int CompareTo(object obj)
            {
                CellSortInfo other = obj as CellSortInfo;

                if (distanceToCamera < other.distanceToCamera)
                {
                    return(1);
                }
                else if (distanceToCamera > other.distanceToCamera)
                {
                    return(-1);
                }
                else
                {
                    return(0);
                }
            }
Exemplo n.º 3
0
    float CalcVertices(VoxelObject voxel_obj, out Bounds screen_bounds, out Vector4 offset_scale)
    {
        float src_local_size = voxel_obj.Scale * 0.5f;
        int   octree_size    = (1 << voxel_obj.octree.MaxDepth);

        if (voxel_obj.UseOctreeSize)
        {
            src_local_size *= octree_size;
        }

        Vector3 offset = (voxel_obj.octree.bounds.center * (1f / octree_size)) * 2f;

        if (!OffsetByBounds)
        {
            offset = Vector3.zero;
        }

        offset_scale = new Vector4(offset.x, offset.y, offset.z, src_local_size);

        float local_size   = 0;       // radius of encircling sphere
        var   obj_pos      = voxel_obj.transform.position;
        var   obj_matrix   = voxel_obj.transform.localToWorldMatrix;
        var   look_matrix  = tfm.worldToLocalMatrix;
        var   look_dir     = tfm.forward;
        var   look_pos     = tfm.position;
        var   look_obj_pos = look_matrix.MultiplyPoint3x4(obj_pos);

        float z_epsilon = Mathf.Min(0.001f, cam.nearClipPlane);

        bool bounds_initialized = false;

        screen_bounds = default(Bounds);

        var bounds_scales = voxel_obj.octree.bounds.extents * (1f / octree_size) * 2f * src_local_size;

        int i = 0;

        for (int z = -1; z <= 1; z += 2)
        {
            for (int y = -1; y <= 1; y += 2)
            {
                for (int x = -1; x <= 1; x += 2)
                {
                    var p = ((new Vector3(x, y, z)) - offset) * src_local_size;
                    p = obj_matrix.MultiplyPoint3x4(p);

                    var look_p = look_matrix.MultiplyPoint3x4(p);
                    local_size = Mathf.Max(local_size, (look_obj_pos - look_p).magnitude);

                    var proj = cam.WorldToScreenPoint(p);
                    proj.z = look_p.z;

                    var csi = new CellSortInfo(i, proj.z, p, look_p, proj);
                    if (OffsetByBounds)
                    {
                        csi.position_bounds = obj_matrix.MultiplyPoint3x4(
                            Vector3.Scale(new Vector3(x, y, z), bounds_scales));
                    }
                    else
                    {
                        csi.position_bounds = p;
                    }
                    cells_order[i] = csi;

                    look_p.z = Mathf.Max(look_p.z, z_epsilon);
                    p        = tfm.TransformPoint(look_p);
                    proj     = cam.WorldToScreenPoint(p);
                    proj.z   = look_p.z;
                    if (bounds_initialized)
                    {
                        screen_bounds.Encapsulate(proj);
                    }
                    else
                    {
                        screen_bounds      = new Bounds(proj, Vector3.zero);
                        bounds_initialized = true;
                    }

                    i += 1;
                }
            }
        }

        if (DrawBounds)
        {
            DrawCubeWire();
        }

        System.Array.Sort(cells_order, delegate(CellSortInfo csi1, CellSortInfo csi2) {
            return(csi1.distance.CompareTo(csi2.distance));
        });

        return(local_size);
    }