172 lines
5.3 KiB
C#
172 lines
5.3 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.Networking;
|
|
using Fusion;
|
|
using System.Collections;
|
|
using System.Security.Policy;
|
|
using System;
|
|
using GLTFast.Schema;
|
|
using System.Buffers.Text;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
public class ImagePlayer : NetworkBehaviour
|
|
{
|
|
public RawImage imageDisplay;
|
|
private BoxCollider boxCollider;
|
|
private Transform objectTransform;
|
|
|
|
private const float maxWidth = 0.3f; // حداکثر عرض آبجکت
|
|
public string imageUrl;
|
|
|
|
void Start()
|
|
{
|
|
if (imageDisplay == null)
|
|
{
|
|
imageDisplay = GetComponentInChildren<RawImage>();
|
|
}
|
|
|
|
objectTransform = transform; // تغییر اندازه خود آبجکت اصلی
|
|
boxCollider = GetComponent<BoxCollider>(); // گرفتن BoxCollider از TV
|
|
|
|
if (boxCollider == null)
|
|
{
|
|
Debug.LogError("BoxCollider not found in TV object!");
|
|
}
|
|
}
|
|
|
|
public override void Spawned()
|
|
{
|
|
// اگر نیاز دارید که کاری پس از اسپاون آبجکت انجام شود، اینجا بنویسید
|
|
}
|
|
|
|
public void Show(string url)
|
|
{
|
|
imageUrl = url;
|
|
Invoke("send", 1);
|
|
}
|
|
|
|
public void send()
|
|
{
|
|
if (Object.HasInputAuthority) // اطمینان از اجرای دستور در سرور
|
|
{
|
|
RPC_SetImage(imageUrl); // ارسال URL به تمام کلاینتها
|
|
}
|
|
}
|
|
|
|
[Rpc(RpcSources.InputAuthority, RpcTargets.All)]
|
|
private void RPC_SetImage(string url)
|
|
{
|
|
print("RECIVED URL:" + url);
|
|
imageUrl = url;
|
|
StartCoroutine(LoadImage(imageUrl)); // بارگذاری تصویر برای تمامی کلاینتها
|
|
}
|
|
|
|
private IEnumerator LoadImage(string url)
|
|
{
|
|
using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(url))
|
|
{
|
|
yield return request.SendWebRequest();
|
|
|
|
if (request.result == UnityWebRequest.Result.Success)
|
|
{
|
|
Texture2D texture = ((DownloadHandlerTexture)request.downloadHandler).texture;
|
|
imageDisplay.texture = texture;
|
|
|
|
AdjustObjectSize(texture); // تنظیم اندازه آبجکت بر اساس ابعاد تصویر
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("Error loading image: " + request.error);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void AdjustObjectSize(Texture2D texture)
|
|
{
|
|
float imageWidth = texture.width / 100f; // مقیاسبندی برای یونیتی
|
|
float imageHeight = texture.height / 100f;
|
|
|
|
// حفظ نسبت تصویر
|
|
float aspectRatio = imageHeight / imageWidth;
|
|
|
|
// **کاهش تدریجی مقیاس تا رسیدن به عرض ≤ 0.3**
|
|
while (imageWidth > maxWidth)
|
|
{
|
|
imageWidth *= 0.9f; // کاهش ۱۰٪ مقیاس در هر تکرار
|
|
imageHeight = imageWidth * aspectRatio; // حفظ نسبت تصویر
|
|
}
|
|
|
|
// تنظیم مقیاس آبجکت
|
|
objectTransform.localScale = new Vector3(imageWidth, imageHeight, objectTransform.localScale.z);
|
|
}
|
|
|
|
// متد جدید برای دریافت RawImage و ارسال به سرور
|
|
public RawImage RecievedRaw;
|
|
internal void Show(RawImage rawImage)
|
|
{
|
|
print("RECIVED RAWIMAGE");
|
|
RecievedRaw = rawImage;
|
|
StartCoroutine(ConvertTextureToTexture2DAsync(rawImage.texture, UploadImage));
|
|
// تبدیل تصویر به Base64
|
|
|
|
}
|
|
public IEnumerator ConvertTextureToTexture2DAsync(UnityEngine.Texture texture, System.Action<Texture2D> onDone)
|
|
{
|
|
yield return new WaitForEndOfFrame(); // صبر کن تا فریم کامل شه (مناسب در UI یا بازی)
|
|
|
|
RenderTexture tmp = RenderTexture.GetTemporary(
|
|
texture.width,
|
|
texture.height,
|
|
0,
|
|
RenderTextureFormat.Default,
|
|
RenderTextureReadWrite.Linear
|
|
);
|
|
|
|
Graphics.Blit(texture, tmp);
|
|
|
|
RenderTexture previous = RenderTexture.active;
|
|
RenderTexture.active = tmp;
|
|
|
|
Texture2D result = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
|
|
result.ReadPixels(new Rect(0, 0, tmp.width, tmp.height), 0, 0);
|
|
result.Apply();
|
|
|
|
RenderTexture.active = previous;
|
|
RenderTexture.ReleaseTemporary(tmp);
|
|
|
|
onDone?.Invoke(result); // کالبک وقتی تکمیل شد
|
|
}
|
|
public string uploadURL = "http://185.116.161.39:8012/hivad/vr/upload.php";
|
|
|
|
public void UploadImage(Texture2D image)
|
|
{
|
|
StartCoroutine(Upload(image));
|
|
}
|
|
|
|
IEnumerator Upload(Texture2D texture)
|
|
{
|
|
byte[] imageBytes = texture.EncodeToPNG();
|
|
string base64Image = System.Convert.ToBase64String(imageBytes);
|
|
|
|
WWWForm form = new WWWForm();
|
|
form.AddField("image", base64Image);
|
|
|
|
UnityWebRequest www = UnityWebRequest.Post(uploadURL, form);
|
|
yield return www.SendWebRequest();
|
|
|
|
if (www.result == UnityWebRequest.Result.Success)
|
|
{
|
|
Debug.Log("Upload successful: " + www.downloadHandler.text);
|
|
Show(www.downloadHandler.text);
|
|
}
|
|
else
|
|
{
|
|
Runner.Despawn(Object);
|
|
Debug.LogError("Upload failed: " + www.error);
|
|
}
|
|
|
|
}
|
|
|
|
}
|