// Use this for initialization
    void Start()
    {
        if (null == target) {
            target = renderer;
        }

        Image = OpenNIContext.OpenNode(NodeType.Image) as ImageGenerator;
        inputSize = new Vector2(Image.MapOutputMode.XRes, Image.MapOutputMode.YRes);
        outputSize = (null != target) ?
            new Vector2(Mathf.NextPowerOfTwo((int)inputSize.x), Mathf.NextPowerOfTwo((int)inputSize.y)) :
            new Vector2(inputSize.x, inputSize.y);

        imageMapTexture = new Texture2D((int)outputSize.x, (int)outputSize.y, TextureFormat.RGB24, false);
        rawImageMap = new byte[Image.GetMetaData().BytesPerPixel * Image.MapOutputMode.XRes * Image.MapOutputMode.YRes];
        imageMapPixels = new Color32[(int)(outputSize.x * outputSize.y)];

        if (null != target) {
            float uScale = inputSize.x / outputSize.x;
            float vScale = inputSize.y / outputSize.y;
            target.material.SetTextureScale("_MainTex", new Vector2(uScale, -vScale));
            target.material.SetTextureOffset("_MainTex", new Vector2(0.0f, vScale - 1.0f));
            target.material.mainTexture = imageMapTexture;
        }

        openGl = SystemInfo.graphicsDeviceVersion.Contains("OpenGL");
        if (openGl) {
            print("Running in OpenGL mode");
        }
    }
        private void InitOpenNI()
        {
            // ContextとImageGeneratorの作成
            ScriptNode node;
            context = Context.CreateFromXmlFile( "SamplesConfig.xml", out node );
            context.GlobalMirror = false;
            image = context.FindExistingNode( NodeType.Image ) as ImageGenerator;

            // 画像更新のためのスレッドを作成
            shouldRun = true;
            readerThread = new Thread( new ThreadStart( () =>
            {
                while ( shouldRun ) {
                    context.WaitAndUpdateAll();
                    ImageMetaData imageMD = image.GetMetaData();

                    // ImageMetaDataをBitmapSourceに変換する(unsafeにしなくてもOK!!)
                    this.Dispatcher.BeginInvoke( DispatcherPriority.Background, new Action( () =>
                    {
                        imageOpenNI.Source = BitmapSource.Create( imageMD.XRes, imageMD.YRes,
                            96, 96, PixelFormats.Rgb24, null, imageMD.ImageMapPtr,
                            imageMD.DataSize, imageMD.XRes * imageMD.BytesPerPixel );
                    } ) );
                }
            } ) );
            readerThread.Start();
        }
Exemplo n.º 3
0
        public MainWindow()
        {
            InitializeComponent();

            try {
                // ContextとImageGeneratorの作成
                ScriptNode node;
                context = Context.CreateFromXmlFile( "../../SamplesConfig.xml", out node );
                context.GlobalMirror = false;
                image = context.FindExistingNode( NodeType.Image ) as ImageGenerator;
                audio = context.FindExistingNode( NodeType.Audio ) as AudioGenerator;

                wavePlayer = new StreamingWavePlayer( audio.WaveOutputMode.SampleRate,
                    audio.WaveOutputMode.BitsPerSample, audio.WaveOutputMode.Channels, 100 );

                // 画像更新のためのスレッドを作成
                shouldRun = true;
                readerThread = new Thread( new ThreadStart( () =>
                {
                    while ( shouldRun ) {
                        context.WaitAndUpdateAll();
                        ImageMetaData imageMD = image.GetMetaData();

                        // WAVEデータの出力
                        wavePlayer.Output( audio.AudioBufferPtr, audio.DataSize );

                        // ImageMetaDataをBitmapSourceに変換する(unsafeにしなくてもOK!!)
                        this.Dispatcher.BeginInvoke( DispatcherPriority.Background, new Action( () =>
                        {
                            image1.Source = BitmapSource.Create( imageMD.XRes, imageMD.YRes,
                                96, 96, PixelFormats.Rgb24, null, imageMD.ImageMapPtr,
                                imageMD.DataSize, imageMD.XRes * imageMD.BytesPerPixel );
                        } ) );
                    }
                } ) );
                readerThread.Start();
            }
            catch ( Exception ex ) {
                MessageBox.Show( ex.Message );
            }
        }
Exemplo n.º 4
0
        public Sensor(string config)
        {
            if (string.IsNullOrEmpty(config))
                throw new ArgumentNullException();

            try
            {
                _context = Context.CreateFromXmlFile(config, out _scriptNode);
                _depthGenerator = _context.FindExistingNode(NodeType.Depth) as DepthGenerator;
                _imageGenerator = _context.FindExistingNode(NodeType.Image) as ImageGenerator;
                _userGenerator = _context.FindExistingNode(NodeType.User) as UserGenerator;

                if (_depthGenerator == null)
                    throw new ApplicationException("No depth node found.");

                if (_imageGenerator == null)
                    throw new ApplicationException("No image node found.");

                if (_userGenerator == null)
                    throw new ApplicationException("No user node found.");

                if (_depthGenerator.MapOutputMode.FPS != _imageGenerator.MapOutputMode.FPS)
                    throw new ApplicationException("Depth and image node must have common framerates.");

                if (_depthGenerator.MapOutputMode.XRes != _imageGenerator.MapOutputMode.XRes)
                    throw new ApplicationException("Depth and image node must have common horizontal resolutions.");

                if (_depthGenerator.MapOutputMode.YRes != _imageGenerator.MapOutputMode.YRes)
                    throw new ApplicationException("Depth and image node must have common vertical resolutions.");

                _depthMetaData = new DepthMetaData();
                _imageMetaData = new ImageMetaData();

                _imageWidth = _depthGenerator.MapOutputMode.XRes;
                _imageHeight = _depthGenerator.MapOutputMode.YRes;

                _userGenerator.NewUser += new EventHandler<NewUserEventArgs>(_userGenerator_NewUser);
                _userGenerator.LostUser += new EventHandler<UserLostEventArgs>(_userGenerator_LostUser);
                _userGenerator.StartGenerating();

                _bitmapGenerator = new BitmapGenerator(this);

                _readerWaitHandle = new AutoResetEvent(false);

                _readerThread = new Thread(delegate()
                {
                    try
                    {
                        while (_run)
                        {
                            if (_pause)
                                _readerWaitHandle.WaitOne();

                            _context.WaitAndUpdateAll();

                            _depthGenerator.GetMetaData(_depthMetaData);
                            _imageGenerator.GetMetaData(_imageMetaData);

                            if (_depthMetaData.XRes != _imageWidth || _imageMetaData.XRes != _imageWidth)
                                throw new ApplicationException("Image width must not change.");

                            if (_depthMetaData.YRes != _imageHeight || _imageMetaData.YRes != _imageHeight)
                                throw new ApplicationException("Image height must not change.");

                            if (GeneratorUpdate != null)
                                GeneratorUpdate(this, EventArgs.Empty);
                        }
                    }
                    catch (ThreadInterruptedException)
                    {
                        Console.WriteLine("Reader thread interrupted.");
                    }
                    catch (Exception e)
                    {
                        throw new ApplicationException("Error while processing sensor data.", e);
                    }
                }) { Name = "ONI Reader Thread" };
            }
            catch (Exception)
            {
                throw;
            }
        }