AxibugEmuOnline/AxibugEmuOnline.Client/Assets/Script/NesEmulator/VideoProvider.cs

65 lines
2.0 KiB
C#
Raw Normal View History

using AxibugEmuOnline.Client.Common;
2024-07-30 18:53:36 +08:00
using System;
2024-07-31 17:40:32 +08:00
using System.Runtime.InteropServices;
using System.Text;
2024-07-30 11:57:09 +08:00
using UnityEngine;
using UnityEngine.UI;
namespace AxibugEmuOnline.Client
{
public class VideoProvider : MonoBehaviour
{
2024-08-06 16:03:17 +08:00
public NesEmulator NesEmu;
public Canvas DrawCanvas;
2024-08-06 16:03:17 +08:00
2024-07-30 11:57:09 +08:00
public RawImage Image;
2024-07-31 17:40:32 +08:00
private IntPtr wrapTexBufferPointer;
2024-07-30 11:57:09 +08:00
private Texture2D wrapTex;
private int TexBufferSize;
2024-07-30 11:57:09 +08:00
private Texture2D pPal;
2024-08-29 17:20:01 +08:00
private void Awake()
{
DrawCanvas.worldCamera = Camera.main;
}
public unsafe void SetDrawData(uint* screenData)
2024-07-30 11:57:09 +08:00
{
2024-07-31 17:40:32 +08:00
if (wrapTex == null)
{
2024-08-16 10:39:51 +08:00
wrapTex = new Texture2D(272, 240, TextureFormat.RGBA32, false);
wrapTex.filterMode = FilterMode.Point;
2024-11-14 12:02:11 +08:00
wrapTexBufferPointer = (IntPtr)screenData;
2024-08-14 13:09:22 +08:00
Image.texture = wrapTex;
Image.material.SetTexture("_MainTex", wrapTex);
TexBufferSize = wrapTex.width * wrapTex.height * 4;
2024-07-30 18:53:36 +08:00
var palRaw = PaletteDefine.m_cnPalette[0];
2024-08-16 10:39:51 +08:00
pPal = new Texture2D(palRaw.Length, 1, TextureFormat.RGBA32, false);
pPal.filterMode = FilterMode.Point;
for (int i = 0; i < palRaw.Length; i++)
2024-07-30 18:53:36 +08:00
{
uint colorRaw = palRaw[i];
var argbColor = BitConverter.GetBytes(colorRaw);
Color temp = Color.white;
2024-08-14 13:09:22 +08:00
temp.r = argbColor[2] / 255f;
temp.g = argbColor[1] / 255f;
temp.b = argbColor[0] / 255f;
temp.a = 1;
pPal.SetPixel(i, 0, temp);
2024-07-30 18:53:36 +08:00
}
pPal.Apply();
Image.material.SetTexture("_PalTex", pPal);
2024-07-30 18:53:36 +08:00
}
wrapTex.LoadRawTextureData(wrapTexBufferPointer, TexBufferSize);
2024-07-30 11:57:09 +08:00
wrapTex.Apply();
}
}
}