Ejemplo n.º 1
0
        void Awake()
        {
            if (!Intrinsics.TryLoadFromFile(_intrinsicsFileName, out _intrinsics))
            {
                Debug.LogError("Intrinsics file '" + _intrinsicsFileName + "' not found.\n");
                enabled = false;
                return;
            }

            _undistortMap1 = new Mat();
            _undistortMap2 = new Mat();

            _extrinsicsCalibrator = new CameraExtrinsicsCalibrator();

            TrackingToolsHelper.RenderPattern(_chessPatternSize, TrackingToolsHelper.PatternType.Chessboard, 1024, ref _chessPatternTexture, ref _patternRenderMaterial);

            // Prepare UI.
            _aspectFitter = _processedCameraImage.GetComponent <AspectRatioFitter>();
            if (!_aspectFitter)
            {
                _aspectFitter = _processedCameraImage.gameObject.AddComponent <AspectRatioFitter>();
            }
            _aspectFitter.aspectMode       = AspectRatioFitter.AspectMode.FitInParent;
            _previewMaterial               = new Material(Shader.Find(TrackingToolsConstants.previewShaderName));
            _processedCameraImage.material = _previewMaterial;
            _processedCameraImage.color    = Color.white;
            _arImage = new GameObject("ARImage").AddComponent <RawImage>();
            _arImage.transform.SetParent(_processedCameraImage.transform);
            _arImage.rectTransform.FitParent();
            _arImage.gameObject.SetActive(false);
            _mainCamera.backgroundColor       = Color.clear;
            _chessPatternTransform            = GameObject.CreatePrimitive(PrimitiveType.Quad).transform;
            _chessPatternTransform.name       = "Chessboard";
            _chessPatternTransform.localScale = new Vector3((_chessPatternSize.x - 1) * _chessTileSize * 0.001f, (_chessPatternSize.y - 1) * _chessTileSize * 0.001f, 0);
            Shader   unlitTextureShader = Shader.Find("Unlit/Texture");
            Material chessboardMaterial = new Material(unlitTextureShader);

            chessboardMaterial.mainTexture = _chessPatternTexture;
            _chessPatternTransform.GetComponent <Renderer>().material = chessboardMaterial;
            _precisionDotsContainerObject = TrackingToolsHelper.CreatePrecisionTestDots(_chessPatternTransform, _testPrecisionDotsLayer, _chessPatternSize, _chessTileSize / 1000f);

            // Update world points.
            TrackingToolsHelper.UpdateWorldSpacePatternPoints(_chessPatternSize, _chessPatternTransform.localToWorldMatrix, TrackingToolsHelper.PatternType.Chessboard, Vector2.zero, ref _chessCornersWorldMat);
        }
Ejemplo n.º 2
0
        void Awake()
        {
            Application.targetFrameRate = 30;

            // Check resources.
            if (_operationMode == OperationMode.ManualSamlping && !_manualSampleButton)
            {
                Debug.LogError(logPrepend + "Missing sample button. You must provide a sample button when OperationMode is " + OperationMode.ManualSamlping);
                enabled = false;
                return;
            }

            // Load files.
            if (!Intrinsics.TryLoadFromFile(_cameraIntrinsicsFileName, out _cameraIntrinsics))
            {
                enabled = false;
                return;
            }

            // Find shaders.
            Shader unlitColorShader         = Shader.Find("Unlit/Color");
            Shader unlitTextureShader       = Shader.Find("Unlit/Texture");
            Shader unlitTintedTextureShader = Shader.Find("Unlit/TintedInvertibleTexture");

            _sb = new StringBuilder();

            // Operation mode dependent things.
            _stableSampleCountThreshold = _operationMode == OperationMode.ManualSamlping ? stableFrameCountThresholdForManualSampling : stableFrameCountThresholdForTimedSampling;
            if (_manualSampleButton)
            {
                _manualSampleButton.gameObject.SetActive(_operationMode == OperationMode.ManualSamlping);
                _manualSampleButton.interactable = false;
            }

            // Prepare OpenCV.
            _cameraExtrinsicsCalibrator    = new CameraExtrinsicsCalibrator();
            _projectorExtrinsicsCalibrator = new ProjectorFromCameraExtrinsicsCalibrator();
            _noDistCoeffs = new MatOfDouble(new double[] { 0, 0, 0, 0 });
            _circlePointsProjectorRenderImageMat = new MatOfPoint2f();
            _circlePointsRealModelMat            = new MatOfPoint3f();
            _circlePointsDetectedWorldMat        = new MatOfPoint3f();
            _undistortMap1 = new Mat();
            _undistortMap2 = new Mat();

            // Create patterns.
            TrackingToolsHelper.RenderPattern(_chessPatternSize, TrackingToolsHelper.PatternType.Chessboard, 1024, ref _chessPatternTexture, ref _patternRenderMaterial);

            // Layers.
            int uiLayer             = LayerMask.NameToLayer("UI");
            int mainCameraLayerMask = LayerMask.GetMask("Default");
            int projectorLayer      = LayerMask.NameToLayer("TransparentFX");
            int projectorLayerMask  = LayerMask.GetMask("TransparentFX");

            // Objects.
            _calibrationBoardTransform = new GameObject("CalibrationBoard").transform;

            // Create and prepare UI.
            _cameraAspectFitter = _processedCameraImage.GetComponent <AspectRatioFitter>();
            if (!_cameraAspectFitter)
            {
                _cameraAspectFitter = _processedCameraImage.gameObject.AddComponent <AspectRatioFitter>();
            }
            _cameraAspectFitter.aspectMode = AspectRatioFitter.AspectMode.FitInParent;
            _previewMaterial = new Material(Shader.Find(TrackingToolsConstants.previewShaderName));
            _processedCameraImage.gameObject.layer = uiLayer;
            _processedCameraImage.material         = _previewMaterial;
            _processedCameraImage.color            = Color.white;

            _arImage = new GameObject("ARImage").AddComponent <RawImage>();
            _arImage.transform.SetParent(_processedCameraImage.transform);
            _arImage.transform.SetAsFirstSibling();
            _arImage.raycastTarget = false;
            _arImage.rectTransform.FitParent();
            _arImage.gameObject.layer = uiLayer;

            _mainCamera.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
            _mainCamera.cullingMask = mainCameraLayerMask;

            _projectorCamera.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
            _projectorCamera.cullingMask           = projectorLayerMask;
            _projectorCamera.usePhysicalProperties = false;

            _chessPatternTransform = GameObject.CreatePrimitive(PrimitiveType.Quad).transform;
            _chessPatternTransform.SetParent(_calibrationBoardTransform);
            _chessPatternTransform.name = "Chessboard";
            Material chessboardMaterial = new Material(unlitTextureShader);

            chessboardMaterial.mainTexture = _chessPatternTexture;
            _chessPatternTransform.GetComponent <Renderer>().material = chessboardMaterial;
            float chessTileSizeMeters = _chessTileSize * 0.001f;

            _chessPatternTransform.localScale = new Vector3((_chessPatternSize.x - 1) * chessTileSizeMeters, (_chessPatternSize.y - 1) * chessTileSizeMeters, 0);
            TrackingToolsHelper.UpdateWorldSpacePatternPoints(_chessPatternSize, _chessPatternTransform.localToWorldMatrix, TrackingToolsHelper.PatternType.Chessboard, Vector2.zero, ref _chessCornersWorldMat);

            _circlePatternTransform      = GameObject.CreatePrimitive(PrimitiveType.Quad).transform;
            _circlePatternTransform.name = "Circlesboard";
            _circlePatternBoardMaterial  = new Material(unlitTintedTextureShader);
            _circlePatternTransform.GetComponent <Renderer>().material = _circlePatternBoardMaterial;
            _circlePatternTransform.position         = Vector3.forward;
            _circlePatternTransform.gameObject.layer = projectorLayer;

            _precisionDotsContainerObject = TrackingToolsHelper.CreatePrecisionTestDots(_calibrationBoardTransform, projectorLayer, _chessPatternSize, chessTileSizeMeters);
            _precisionDotsContainerObject.SetActive(false);

            _projectorSampleMeterTransform = GameObject.CreatePrimitive(PrimitiveType.Quad).transform;
            _projectorSampleMeterTransform.gameObject.layer = projectorLayer;
            _projectorSampleMeterTransform.name             = "ProjectorSampleMeter";
            _projectorSampleMeterTransform.localScale       = new Vector3(_chessPatternTransform.localScale.x, TrackingToolsConstants.precisionTestDotSize, 0);
            _projectorSampleMeterTransform.SetParent(_calibrationBoardTransform);
            float dotOffsetY = ((_chessPatternSize.y - 4) * 0.5f + 1) * chessTileSizeMeters;

            _projectorSampleMeterTransform.localPosition = new Vector3(0, -dotOffsetY - chessTileSizeMeters);
            Material sampleMeterMaterial = new Material(unlitColorShader);

            _projectorSampleMeterTransform.GetComponent <Renderer>().sharedMaterial = sampleMeterMaterial;
            _projectorSampleMeterTransform.gameObject.SetActive(false);

            _intrinsicsErrorText.gameObject.SetActive(false);
            _extrinsicsErrorText.gameObject.SetActive(false);
            _saveButton.gameObject.SetActive(false);
            _undoSampleButton.gameObject.SetActive(false);

            _screenBorderMaterial = new Material(unlitColorShader);

            _circlePatternToWorldPrevFrame = Matrix4x4.identity;

            _previewFlasher = new MaterialPropFlasher(_previewMaterial, "_Whiteout", TrackingToolsConstants.flashDuration);
            UpdateSampleCounterUI();

            // Subscribe.
            _circlePatternScaleSlider.onValueChanged.AddListener(( float v ) => _manualCirclePatternTransformationRequested   = true);            //OnCirclePatternScaleSliderChanged );
            _circlePatternOffsetXSlider.onValueChanged.AddListener(( float v ) => _manualCirclePatternTransformationRequested = true);
            _circlePatternOffsetYSlider.onValueChanged.AddListener(( float v ) => _manualCirclePatternTransformationRequested = true);
            _saveButton.onClick.AddListener(SaveToFiles);
            _undoSampleButton.onClick.AddListener(UndoSample);
            if (_manualSampleButton)
            {
                _manualSampleButton.onClick.AddListener(() => _sampleManuallyRequested = true);;
            }
        }