Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Error: You are trying to upload data for texture created with external texture pointer

Discussion in 'AR' started by Federica_96, Mar 22, 2021.

  1. Federica_96

    Federica_96

    Joined:
    Feb 19, 2021
    Posts:
    30
    Hi,
    I,m working on the SteamVR_TestTrackedCamera script... I want to make the central pixel coloured ... Here is the code ...

    Code (CSharp):
    1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
    2. using UnityEngine;
    3. namespace Valve.VR.Extras
    4. {
    5.     public class SteamVR_TestTrackedCamera : MonoBehaviour
    6.     {
    7.         public Material material;
    8.         public Transform target;
    9.         public bool undistorted = true;
    10.         public bool cropped = true;
    11.         private void OnEnable()
    12.         {
    13.             // The video stream must be symmetrically acquired and released in
    14.             // order to properly disable the stream once there are no consumers.
    15.             SteamVR_TrackedCamera.VideoStreamTexture source = SteamVR_TrackedCamera.Source(undistorted);
    16.             source.Acquire();
    17.             // Auto-disable if no camera is present.
    18.             if (!source.hasCamera)
    19.                 enabled = false;
    20.         }
    21.         private void OnDisable()
    22.         {
    23.             // Clear the texture when no longer active.
    24.             material.mainTexture = null;
    25.             // The video stream must be symmetrically acquired and released in
    26.             // order to properly disable the stream once there are no consumers.
    27.             SteamVR_TrackedCamera.VideoStreamTexture source = SteamVR_TrackedCamera.Source(undistorted);
    28.             source.Release();
    29.         }
    30.         private void Update()
    31.         {
    32.             SteamVR_TrackedCamera.VideoStreamTexture source = SteamVR_TrackedCamera.Source(undistorted);
    33.             Texture2D texture = source.texture;
    34.             if (texture == null)
    35.             {
    36.                 return;
    37.             }
    38.             // Apply the latest texture to the material.  This must be performed
    39.             // every frame since the underlying texture is actually part of a ring
    40.             // buffer which is updated in lock-step with its associated pose.
    41.             // (You actually really only need to call any of the accessors which
    42.             // internally call Update on the SteamVR_TrackedCamera.VideoStreamTexture).
    43.    
    44.             material.mainTexture = texture;   //associa il material alla texture per farmi vedere il mondo esterno proiettato sul quad
    45. // Here the part of code I have added ----------------------------------
    46.            Color32 color = new Color32(0, 255, 0, 0);
    47.        
    48.            texture.SetPixel(texture.width / 2, texture.height / 2, color); //equivalente a dire texture.SetPixel(64, 64, color);
    49.            texture.Apply();
    50. //-----------------------------------------------------------------------------------
    51.             // Adjust the height of the quad based on the aspect to keep the texels square.
    52.             float aspect = (float)texture.width / texture.height;
    53.                 // The undistorted video feed has 'bad' areas near the edges where the original
    54.                 // square texture feed is stretched to undo the fisheye from the lens.
    55.                 // Therefore, you'll want to crop it to the specified frameBounds to remove this.
    56.                 if (cropped)
    57.                 {
    58.                     VRTextureBounds_t bounds = source.frameBounds;
    59.                     material.mainTextureOffset = new Vector2(bounds.uMin, bounds.vMin);
    60.                     float du = bounds.uMax - bounds.uMin;
    61.                     float dv = bounds.vMax - bounds.vMin;
    62.                     material.mainTextureScale = new Vector2(du, dv);
    63.                     aspect *= Mathf.Abs(du / dv);
    64.                 }
    65.                 else
    66.                 {
    67.                     material.mainTextureOffset = Vector2.zero;
    68.                     material.mainTextureScale = new Vector2(1, -1);
    69.                 }
    70.                 target.localScale = new Vector3(1, 1.0f / aspect, 1);
    71.                 // Apply the pose that this frame was recorded at.
    72.                 if (source.hasTracking)
    73.                 {
    74.                     SteamVR_Utils.RigidTransform rigidTransform = source.transform;
    75.                     target.localPosition = rigidTransform.pos;
    76.                     target.localRotation = rigidTransform.rot;
    77.                 }
    78.             }
    79.         }
    80.     }
    81.  
    However when I run the code I can't see the coloured pixel and the console gives me this error:

    You are trying to upload data for texture created with external texture pointer
    UnityEngine.Texture2D:Apply()
    Valve.VR.Extras.SteamVR_TestTrackedCamera:Update() (at Assets/SteamVR/Extras/SteamVR_TestTrackedCamera.cs)


    Can someone help me solve? Thank you.
     
  2. KyryloKuzyk

    KyryloKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    1,070
  3. Federica_96

    Federica_96

    Joined:
    Feb 19, 2021
    Posts:
    30
  4. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,796
    The SteamVR plugin is developed and maintained by Valve. It's recommended that you post your issues on their Github so their team can address.
     
    Federica_96 likes this.