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

Hi,
I,m working on the SteamVR_TestTrackedCamera script… I want to make the central pixel coloured … Here is the code …

//======= Copyright (c) Valve Corporation, All rights reserved. ===============
using UnityEngine;
namespace Valve.VR.Extras
{
    public class SteamVR_TestTrackedCamera : MonoBehaviour
    {
        public Material material;
        public Transform target;
        public bool undistorted = true;
        public bool cropped = true;
        private void OnEnable()
        {
            // The video stream must be symmetrically acquired and released in
            // order to properly disable the stream once there are no consumers.
            SteamVR_TrackedCamera.VideoStreamTexture source = SteamVR_TrackedCamera.Source(undistorted);
            source.Acquire();
            // Auto-disable if no camera is present.
            if (!source.hasCamera)
                enabled = false;
        }
        private void OnDisable()
        {
            // Clear the texture when no longer active.
            material.mainTexture = null;
            // The video stream must be symmetrically acquired and released in
            // order to properly disable the stream once there are no consumers.
            SteamVR_TrackedCamera.VideoStreamTexture source = SteamVR_TrackedCamera.Source(undistorted);
            source.Release();
        }
        private void Update()
        {
            SteamVR_TrackedCamera.VideoStreamTexture source = SteamVR_TrackedCamera.Source(undistorted);
            Texture2D texture = source.texture;
            if (texture == null)
            {
                return;
            }
            // Apply the latest texture to the material.  This must be performed
            // every frame since the underlying texture is actually part of a ring
            // buffer which is updated in lock-step with its associated pose.
            // (You actually really only need to call any of the accessors which
            // internally call Update on the SteamVR_TrackedCamera.VideoStreamTexture).
    
            material.mainTexture = texture;   //associa il material alla texture per farmi vedere il mondo esterno proiettato sul quad
// Here the part of code I have added ----------------------------------
           Color32 color = new Color32(0, 255, 0, 0);
        
           texture.SetPixel(texture.width / 2, texture.height / 2, color); //equivalente a dire texture.SetPixel(64, 64, color);
           texture.Apply();
//-----------------------------------------------------------------------------------
            // Adjust the height of the quad based on the aspect to keep the texels square.
            float aspect = (float)texture.width / texture.height;
                // The undistorted video feed has 'bad' areas near the edges where the original
                // square texture feed is stretched to undo the fisheye from the lens.
                // Therefore, you'll want to crop it to the specified frameBounds to remove this.
                if (cropped)
                {
                    VRTextureBounds_t bounds = source.frameBounds;
                    material.mainTextureOffset = new Vector2(bounds.uMin, bounds.vMin);
                    float du = bounds.uMax - bounds.uMin;
                    float dv = bounds.vMax - bounds.vMin;
                    material.mainTextureScale = new Vector2(du, dv);
                    aspect *= Mathf.Abs(du / dv);
                }
                else
                {
                    material.mainTextureOffset = Vector2.zero;
                    material.mainTextureScale = new Vector2(1, -1);
                }
                target.localScale = new Vector3(1, 1.0f / aspect, 1);
                // Apply the pose that this frame was recorded at.
                if (source.hasTracking)
                {
                    SteamVR_Utils.RigidTransform rigidTransform = source.transform;
                    target.localPosition = rigidTransform.pos;
                    target.localRotation = rigidTransform.rot;
                }
            }
        }
    }

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.

I believe SteamVR uses external texture for its camera texture:

Try to copy the texture and modify the copy instead.

Hi, thank you for your answer. I tried to make as you have suggested but I continue not the see the central pixel modified

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.

1 Like