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

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

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

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    This sounds pretty specific to the API you're using. You might find people more familiar with it over in the AR/VR/XR group on this forum.
     
  3. Federica_96

    Federica_96

    Joined:
    Feb 19, 2021
    Posts:
    30
    Ok, Thank you!