Search Unity

Question Retrieving a Texture from an HTTP Server within Shader Graph / custom node function

Discussion in 'Shader Graph' started by unity_zOM_q2kqy8rZtQ, Jul 6, 2021.

  1. unity_zOM_q2kqy8rZtQ

    unity_zOM_q2kqy8rZtQ

    Joined:
    Oct 30, 2017
    Posts:
    3
    Hi

    I'm been exploring using textures from an HTTP, and the texture updating on a regular basis. This works with a URP material applied to the Game Object, using the CSharp below.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4.  
    5. public class gettexture : MonoBehaviour
    6. {
    7.     public Renderer thisRenderer;
    8.  
    9.     void Start()
    10.     {
    11.         StartCoroutine(GetText());
    12.     }
    13.  
    14.     IEnumerator GetText()
    15.     {
    16.         while (true) // This creates a never-ending loop
    17.         {
    18.             yield return new WaitForSeconds(1);
    19.             using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture("https://myserverlink"))
    20.             {
    21.                 yield return uwr.SendWebRequest();
    22.  
    23.                 if (uwr.result != UnityWebRequest.Result.Success)
    24.                 {
    25.                     Debug.Log(uwr.error);
    26.                 }
    27.                 else
    28.                 {
    29.                     // Get downloaded asset bundle
    30.                     var texture = DownloadHandlerTexture.GetContent(uwr);
    31.                     thisRenderer.material.mainTexture = texture;
    32.                     Debug.Log("update");
    33.                 }
    34.             }
    35.         }
    36.     }
    37. }

    I wondering whether you can use 'UnityWebRequest' within the shader graph as one of my nodes- say by creating a custom node function (using UnityEditor.ShaderGraph;)?
    If so, is there any examples of how to do it?

    Thanks