Exemple #1
0
	// Update is called once per frame
	void Update () {
        if (_sensor == null)
        {
            var sensor = new V2Sensor();
            _depthStream = new V2DepthStream(sensor.Sensor);
            _sensor = sensor;
        }

        using (var frame = _depthStream.GetFrame())
        {
            if (frame == null)
            {
                return;
            }

            if (DepthTexture == null)
            {
                DepthTexture = new Texture2D(
                    _depthStream.Width, _depthStream.Height, TextureFormat.ARGB32, false);

                DepthTexture.filterMode = FilterMode.Point;

                _depth = new Int16[_depthStream.Width * _depthStream.Height];
            }

            Marshal.Copy(frame.Buffer, _depth, 0, _depth.Length);

            var pixel32s = _depth.Select((Int16 d) =>
            {
                var b = (Byte)(UInt16)d;
                return new Color32(
                    b, b, b, 255
                );
            }).ToArray();
            //Debug.Log(pixel32s.Length);
            DepthTexture.SetPixels32(pixel32s);
            DepthTexture.Apply();
        }
	
	}
 void StartUpdating(WaitHandle waitHandle, KinectBaseImageStream stream)
 {
     var task = waitHandle.WaitTask(Timeout.Infinite);
     task.ToObservable()
         .ObserveOnDispatcher()
         .SelectMany(_ => Observable.Using(
             () => stream.GetFrame(waitHandle.Handle)
                 , frame => Observable.Return(frame))
         )
         .Where(frame => frame != null)
         .Subscribe(
         frame => _depthSource.WritePixels(
                 new Int32Rect(0, 0, frame.Width, frame.Height),
                 frame.Ptr,
                 frame.BufferSize,
                 frame.Pitch)
         , ex => {
             Console.WriteLine(ex);
             StartUpdating(waitHandle, stream);
         }
         , () => StartUpdating(waitHandle, stream)
         )
         ;
 }