Example #1
0
    private void Awake()
    {
        _manager = CustomZedManager.Instance;

        _idPositionBuffer = Shader.PropertyToID("_PositionBuffer");
        _idColorBuffer    = Shader.PropertyToID("_ColorBuffer");
        _idXYZTex         = Shader.PropertyToID("_XYZTex");
        _idColorTex       = Shader.PropertyToID("_ColorTex");

        _idCameraViewMat = Shader.PropertyToID("_CameraViewMat");
        _idLifeParams    = Shader.PropertyToID("_LifeParams");
        _idConfig        = Shader.PropertyToID("_Config");

        _idScaleMin   = Shader.PropertyToID("_ScaleMin");
        _idScaleMax   = Shader.PropertyToID("_ScaleMax");
        _idRandomSeed = Shader.PropertyToID("_RandomSeed");

        _idUVOffset = Shader.PropertyToID("_UVOffset");

        _kernelMaterial = CreateMaterial(_kernelShader);
        _propertyBlock  = new MaterialPropertyBlock();

        _camera = Camera.main;
        // _commandBuffer = new CommandBuffer();

        if (_handMask == null)
        {
            _handMask = FindObjectOfType <HandMaskRenderer>();
        }
    }
    // private void OnRenderObject()
    // {
    // _material.SetPass(0);
    // Graphics.DrawProcedural(MeshTopology.Points, 1, _numberOfPoints);
    // }

    #endregion

    // Vector3 _pos0, _pos1;
    // Vector3 _scale0, _scale1;

    #region Zed events

    void OnZedReady()
    {
        var zedCamera = _manager.zedCamera;

        var pointWidth  = Mathf.FloorToInt(zedCamera.ImageWidth * _resolutionScale);
        var pointHeight = Mathf.FloorToInt(zedCamera.ImageHeight * _resolutionScale);

        _numberOfPoints = pointWidth * pointHeight;

        _material.SetVector("_TexelSize", new Vector4(1f / pointWidth, 1f / pointHeight, pointWidth, pointHeight));
        _monitorMaterial.SetVector("_TexelSize", new Vector4(1f / pointWidth, 1f / pointHeight, pointWidth, pointHeight));

        if (_handMask == null)
        {
            _handMask = GetComponent <HandMaskRenderer>();
        }
        if (_handMask != null)
        {
            _material.SetTexture("_HandMaskTex", _handMask.texture);
            _monitorMaterial.SetTexture("_HandMaskTex", _handMask.texture);
        }

        if (_grid != null)
        {
            _material.SetVector(_grid.idPhysicsGridSize, _grid.size);
            _material.SetVector(_grid.idPhysicsGridSizeInv, _grid.invSize);
            _material.SetTexture(_grid.idPhysicsGridPositionTex, _grid.positionTexture);
            _material.SetTexture(_grid.idPhysicsGridVelocityTex, _grid.velocityTexture);

            _monitorMaterial.SetVector(_grid.idPhysicsGridSize, _grid.size);
            _monitorMaterial.SetVector(_grid.idPhysicsGridSizeInv, _grid.invSize);
            _monitorMaterial.SetTexture(_grid.idPhysicsGridPositionTex, _grid.positionTexture);
            _monitorMaterial.SetTexture(_grid.idPhysicsGridVelocityTex, _grid.velocityTexture);
        }

        _depthTexture      = zedCamera.CreateTextureMeasureType(sl.MEASURE.DEPTH);
        _depthRightTexture = zedCamera.CreateTextureMeasureType(sl.MEASURE.DEPTH_RIGHT);
        // _normalTexture = zedCamera.CreateTextureMeasureType(sl.MEASURE.NORMALS);
        // _normalRightTexture = zedCamera.CreateTextureMeasureType(sl.MEASURE.NORMALS_RIGHT);
        _colorTexture      = zedCamera.CreateTextureImageType(sl.VIEW.LEFT);
        _colorRightTexture = zedCamera.CreateTextureImageType(sl.VIEW.RIGHT);

        // float plane_distance =0.15f;
        // Vector4 opticalCenters = zedCamera.ComputeOpticalCenterOffsets(plane_distance);

        // var pos0 = new Vector3(opticalCenters.x, -1.0f * opticalCenters.y,plane_distance);
        // var pos1 = new Vector3(opticalCenters.z, -1.0f * opticalCenters.w,plane_distance);

        // var projMatrix = zedCamera.Projection;
        // var fovY = GetFOVYFromProjectionMatrix(projMatrix);

        // var scale0 = scale(pos0, fovY);
        // var scale1 = scale(pos1, fovY);

        // _pos0 = pos0;
        // _pos1 = pos1;
        // _scale0 = scale0;
        // _scale1 = scale1;

        // pos0 += new Vector3(-1f * _offset.x, _offset.y, _offset.z);
        // pos1 += new Vector3( 1f * _offset.x, _offset.y, _offset.z);

        var pos0 = _screenOffset;
        var pos1 = _screenOffset;

        var scale0 = _screenScale;
        var scale1 = _screenScale;

        pos0 += new Vector3(-1f * _offset.x, _offset.y, _offset.z);
        pos1 += _offset;

        _planeMatrices[0] = Matrix4x4.TRS(pos0, Quaternion.identity, scale0);
        _planeMatrices[1] = Matrix4x4.TRS(pos1, Quaternion.identity, scale1);

        // _commandBuffer.Clear();
        // _commandBuffer.DrawProcedural(Matrix4x4.identity, _material, 0, MeshTopology.Points, 1, _numberOfPoints);
        // _memoizedCameraEvent = _cameraEvent;
    }