Ejemplo n.º 1
0
    protected bool InitiateTexture(ref Image image)
    {
        int width  = image.Width;
        int height = image.Height;

        attached_device_ = new LMDevice(GetDevice(width));
        attached_device_.UpdateRobustMode(height);
        if (attached_device_.width == 0 || attached_device_.height == 0)
        {
            attached_device_ = new LMDevice();
            Debug.LogWarning("No data in the image texture.");
            return(false);
        }
        else
        {
            switch (attached_device_.type)
            {
            case LM_DEVICE.PERIPHERAL:
                main_texture_ = new Texture2D(attached_device_.width, attached_device_.height, TextureFormat.Alpha8, false);
                break;

            case LM_DEVICE.DRAGONFLY:
                main_texture_ = new Texture2D(attached_device_.width, attached_device_.height, TextureFormat.RGBA32, false);
                break;

            default:
                main_texture_ = new Texture2D(attached_device_.width, attached_device_.height, TextureFormat.Alpha8, false);
                break;
            }
            main_texture_.wrapMode = TextureWrapMode.Clamp;
            image_pixels_          = new Color32[attached_device_.pixels];
        }
        return(true);
    }
    protected bool InitiateTexture(ref Image image)
    {
        int width = image.Width;
        int height = image.Height;

        attached_device_ = new LMDevice(GetDevice(width));
        attached_device_.UpdateRobustMode(height);
        if (attached_device_.width == 0 || attached_device_.height == 0)
        {
          attached_device_ = new LMDevice();
          Debug.LogWarning("No data in the image texture.");
          return false;
        }
        else
        {
          switch (attached_device_.type)
          {
        case LM_DEVICE.PERIPHERAL:
          main_texture_ = new Texture2D(attached_device_.width, attached_device_.height, TextureFormat.Alpha8, false);
          break;
        case LM_DEVICE.DRAGONFLY:
          main_texture_ = new Texture2D(attached_device_.width, attached_device_.height, TextureFormat.RGBA32, false);
          break;
        case LM_DEVICE.MANTIS:
          main_texture_ = new Texture2D(attached_device_.width, attached_device_.height, TextureFormat.Alpha8, false);
          break;
        default:
          main_texture_ = new Texture2D(attached_device_.width, attached_device_.height, TextureFormat.Alpha8, false);
          break;
          }
          main_texture_.wrapMode = TextureWrapMode.Clamp;
          image_pixels_ = new Color32[attached_device_.pixels];
        }
        return true;
    }
    void Update()
    {
        if (undistortImage)
        {
          switch (attached_device_.type) {
        case LM_DEVICE.PERIPHERAL:
          renderer.material = new Material(Shader.Find(IR_UNDISTORT_SHADER));
          break;
        case LM_DEVICE.DRAGONFLY:
          renderer.material = new Material(Shader.Find(RGB_UNDISTORT_SHADER));
          break;
        default:
          renderer.material = new Material(Shader.Find(IR_UNDISTORT_SHADER));
          break;
          }
        }
        else
        {
          switch (attached_device_.type)
          {
        case LM_DEVICE.PERIPHERAL:
          renderer.material = new Material(Shader.Find(IR_NORMAL_SHADER));
          break;
        case LM_DEVICE.DRAGONFLY:
          renderer.material = new Material(Shader.Find(RGB_NORMAL_SHADER));
          break;
        default:
          renderer.material = new Material(Shader.Find(IR_NORMAL_SHADER));
          break;
          }
        }

        Frame frame = leap_controller_.Frame();

        if (frame.Images.Count == 0)
        {
          image_misses_++;
          if (image_misses_ == IMAGE_WARNING_WAIT)
          {
        Debug.LogWarning("Can't find any images. " +
                          "Make sure you enabled 'Allow Images' in the Leap Motion Settings, " +
                          "you are on tracking version 2.1+ and " +
                          "your Leap Motion device is plugged in.");
          }
          return;
        }

        // Check main texture dimensions.
        Image image = frame.Images[imageIndex];
        attached_device_ = new LMDevice(SetDevice(image.Width, image.Height));

        if (attached_device_.width == 0 || attached_device_.height == 0)
        {
          Debug.LogWarning("No data in the image texture.");
          return;
        }

        if (attached_device_.width != main_texture_.width || attached_device_.height != main_texture_.height)
          SetMainTextureDimensions();

        // Check distortion texture dimensions.
        // Divide by two 2 because there are floats per pixel.
        int distortion_width = image.DistortionWidth / 2;
        int distortion_height = image.DistortionHeight;
        if (distortion_width == 0 || distortion_height == 0)
        {
          Debug.LogWarning("No data in the distortion texture.");
          return;
        }

        if (distortion_width != distortionX_.width || distortion_height != distortionX_.height)
          SetDistortionDimensions(distortion_width, distortion_height);

        // Load image texture data.
        image_data_ = image.Data;
        distortion_data_ = image.Distortion;

        LoadMainTexture();
        if (undistortImage)
          EncodeDistortion();
        ApplyDataToTextures();

        renderer.material.mainTexture = main_texture_;
        renderer.material.SetColor("_Color", imageColor);
        renderer.material.SetInt("_DeviceType", Convert.ToInt32(attached_device_.type));
        renderer.material.SetFloat("_GammaCorrection", gammaCorrection);
        renderer.material.SetInt("_BlackIsTransparent", blackIsTransparent ? 1 : 0);

        if (undistortImage)
        {
          renderer.material.SetTexture("_DistortX", distortionX_);
          renderer.material.SetTexture("_DistortY", distortionY_);

          renderer.material.SetFloat("_RayOffsetX", image.RayOffsetX);
          renderer.material.SetFloat("_RayOffsetY", image.RayOffsetY);
          renderer.material.SetFloat("_RayScaleX", image.RayScaleX);
          renderer.material.SetFloat("_RayScaleY", image.RayScaleY);
        }
    }
Ejemplo n.º 4
0
    void Update()
    {
        if (undistortImage)
        {
            switch (attached_device_.type)
            {
            case LM_DEVICE.PERIPHERAL:
                renderer.material = new Material(Shader.Find(IR_UNDISTORT_SHADER));
                break;

            case LM_DEVICE.DRAGONFLY:
                renderer.material = new Material(Shader.Find(RGB_UNDISTORT_SHADER));
                break;

            default:
                renderer.material = new Material(Shader.Find(IR_UNDISTORT_SHADER));
                break;
            }
        }
        else
        {
            switch (attached_device_.type)
            {
            case LM_DEVICE.PERIPHERAL:
                renderer.material = new Material(Shader.Find(IR_NORMAL_SHADER));
                break;

            case LM_DEVICE.DRAGONFLY:
                renderer.material = new Material(Shader.Find(RGB_NORMAL_SHADER));
                break;

            default:
                renderer.material = new Material(Shader.Find(IR_NORMAL_SHADER));
                break;
            }
        }

        Frame frame = leap_controller_.Frame();

        if (frame.Images.Count == 0)
        {
            image_misses_++;
            if (image_misses_ == IMAGE_WARNING_WAIT)
            {
                Debug.LogWarning("Can't find any images. " +
                                 "Make sure you enabled 'Allow Images' in the Leap Motion Settings, " +
                                 "you are on tracking version 2.1+ and " +
                                 "your Leap Motion device is plugged in.");
            }
            return;
        }

        // Check main texture dimensions.
        Image image = frame.Images[imageIndex];

        attached_device_ = new LMDevice(SetDevice(image.Width, image.Height));

        if (attached_device_.width == 0 || attached_device_.height == 0)
        {
            Debug.LogWarning("No data in the image texture.");
            return;
        }

        if (attached_device_.width != main_texture_.width || attached_device_.height != main_texture_.height)
        {
            SetMainTextureDimensions();
        }

        // Check distortion texture dimensions.
        // Divide by two 2 because there are floats per pixel.
        int distortion_width  = image.DistortionWidth / 2;
        int distortion_height = image.DistortionHeight;

        if (distortion_width == 0 || distortion_height == 0)
        {
            Debug.LogWarning("No data in the distortion texture.");
            return;
        }

        if (distortion_width != distortionX_.width || distortion_height != distortionX_.height)
        {
            SetDistortionDimensions(distortion_width, distortion_height);
        }

        // Load image texture data.
        image_data_      = image.Data;
        distortion_data_ = image.Distortion;

        LoadMainTexture();
        if (undistortImage)
        {
            EncodeDistortion();
        }
        ApplyDataToTextures();

        renderer.material.mainTexture = main_texture_;
        renderer.material.SetColor("_Color", imageColor);
        renderer.material.SetInt("_DeviceType", Convert.ToInt32(attached_device_.type));
        renderer.material.SetFloat("_GammaCorrection", gammaCorrection);
        renderer.material.SetInt("_BlackIsTransparent", blackIsTransparent ? 1 : 0);

        if (undistortImage)
        {
            renderer.material.SetTexture("_DistortX", distortionX_);
            renderer.material.SetTexture("_DistortY", distortionY_);

            renderer.material.SetFloat("_RayOffsetX", image.RayOffsetX);
            renderer.material.SetFloat("_RayOffsetY", image.RayOffsetY);
            renderer.material.SetFloat("_RayScaleX", image.RayScaleX);
            renderer.material.SetFloat("_RayScaleY", image.RayScaleY);
        }
    }