优化模拟器提交过来的元素转换为Unity Texture2D数据的代码

This commit is contained in:
ALIENJACK\alien 2024-08-02 10:58:04 +08:00
parent 507ef45d3a
commit 090322a60a
6 changed files with 36 additions and 177 deletions

View File

@ -26,7 +26,7 @@ RenderSettings:
m_AmbientIntensity: 1 m_AmbientIntensity: 1
m_AmbientMode: 0 m_AmbientMode: 0
m_SubtractiveShadowColor: {r: 0.42, g: 0.478, b: 0.627, a: 1} m_SubtractiveShadowColor: {r: 0.42, g: 0.478, b: 0.627, a: 1}
m_SkyboxMaterial: {fileID: 10304, guid: 0000000000000000f000000000000000, type: 0} m_SkyboxMaterial: {fileID: 0}
m_HaloStrength: 0.5 m_HaloStrength: 0.5
m_FlareStrength: 1 m_FlareStrength: 1
m_FlareFadeSpeed: 3 m_FlareFadeSpeed: 3
@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1 m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0} m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0} m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0.44657898, g: 0.4964133, b: 0.5748178, a: 1} m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1}
m_UseRadianceAmbientProbe: 0 m_UseRadianceAmbientProbe: 0
--- !u!157 &3 --- !u!157 &3
LightmapSettings: LightmapSettings:
@ -123,99 +123,6 @@ NavMeshSettings:
debug: debug:
m_Flags: 0 m_Flags: 0
m_NavMeshData: {fileID: 0} m_NavMeshData: {fileID: 0}
--- !u!1 &708549044
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 708549046}
- component: {fileID: 708549045}
m_Layer: 0
m_Name: Directional Light
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!108 &708549045
Light:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 708549044}
m_Enabled: 1
serializedVersion: 10
m_Type: 1
m_Shape: 0
m_Color: {r: 1, g: 0.95686275, b: 0.8392157, a: 1}
m_Intensity: 1
m_Range: 10
m_SpotAngle: 30
m_InnerSpotAngle: 21.80208
m_CookieSize: 10
m_Shadows:
m_Type: 2
m_Resolution: -1
m_CustomResolution: -1
m_Strength: 1
m_Bias: 0.05
m_NormalBias: 0.4
m_NearPlane: 0.2
m_CullingMatrixOverride:
e00: 1
e01: 0
e02: 0
e03: 0
e10: 0
e11: 1
e12: 0
e13: 0
e20: 0
e21: 0
e22: 1
e23: 0
e30: 0
e31: 0
e32: 0
e33: 1
m_UseCullingMatrixOverride: 0
m_Cookie: {fileID: 0}
m_DrawHalo: 0
m_Flare: {fileID: 0}
m_RenderMode: 0
m_CullingMask:
serializedVersion: 2
m_Bits: 4294967295
m_RenderingLayerMask: 1
m_Lightmapping: 4
m_LightShadowCasterMode: 0
m_AreaSize: {x: 1, y: 1}
m_BounceIntensity: 1
m_ColorTemperature: 6570
m_UseColorTemperature: 0
m_BoundingSphereOverride: {x: 0, y: 0, z: 0, w: 0}
m_UseBoundingSphereOverride: 0
m_UseViewFrustumForShadowCasterCull: 1
m_ShadowRadius: 0
m_ShadowAngle: 0
--- !u!4 &708549046
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 708549044}
m_LocalRotation: {x: 0.40821788, y: -0.23456968, z: 0.10938163, w: 0.8754261}
m_LocalPosition: {x: 0, y: 3, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
--- !u!1 &1232273651 --- !u!1 &1232273651
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -252,7 +159,7 @@ Camera:
m_Enabled: 1 m_Enabled: 1
serializedVersion: 2 serializedVersion: 2
m_ClearFlags: 1 m_ClearFlags: 1
m_BackGroundColor: {r: 1, g: 1, b: 1, a: 0} m_BackGroundColor: {r: 0, g: 0, b: 0, a: 0}
m_projectionMatrixMode: 1 m_projectionMatrixMode: 1
m_GateFitMode: 2 m_GateFitMode: 2
m_FOVAxisMode: 0 m_FOVAxisMode: 0
@ -297,7 +204,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 2 m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1359344831 --- !u!1 &1359344831
GameObject: GameObject:
@ -363,7 +270,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 1 m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &4232056520998800727 --- !u!1001 &4232056520998800727
PrefabInstance: PrefabInstance:
@ -374,7 +281,7 @@ PrefabInstance:
m_Modifications: m_Modifications:
- target: {fileID: 4232056521131536011, guid: f8bea3f8aa351bb46ada33b2274729ea, type: 3} - target: {fileID: 4232056521131536011, guid: f8bea3f8aa351bb46ada33b2274729ea, type: 3}
propertyPath: m_RootOrder propertyPath: m_RootOrder
value: 3 value: 2
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 4232056521131536011, guid: f8bea3f8aa351bb46ada33b2274729ea, type: 3} - target: {fileID: 4232056521131536011, guid: f8bea3f8aa351bb46ada33b2274729ea, type: 3}
propertyPath: m_LocalPosition.x propertyPath: m_LocalPosition.x
@ -420,5 +327,17 @@ PrefabInstance:
propertyPath: m_Name propertyPath: m_Name
value: NesEmulator value: NesEmulator
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 4232056521759880274, guid: f8bea3f8aa351bb46ada33b2274729ea, type: 3}
propertyPath: m_Enabled
value: 1
objectReference: {fileID: 0}
- target: {fileID: 4232056521759880274, guid: f8bea3f8aa351bb46ada33b2274729ea, type: 3}
propertyPath: m_Texture
value:
objectReference: {fileID: 0}
- target: {fileID: 4232056521759880276, guid: f8bea3f8aa351bb46ada33b2274729ea, type: 3}
propertyPath: m_IsActive
value: 1
objectReference: {fileID: 0}
m_RemovedComponents: [] m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: f8bea3f8aa351bb46ada33b2274729ea, type: 3} m_SourcePrefab: {fileID: 100100000, guid: f8bea3f8aa351bb46ada33b2274729ea, type: 3}

View File

@ -14,6 +14,7 @@ namespace AxibugEmuOnline.Client
private void Start() private void Start()
{ {
Application.targetFrameRate = 60;
StartGame("Kirby.nes"); StartGame("Kirby.nes");
} }

View File

@ -1,38 +0,0 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!84 &8400000
RenderTexture:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: New Render Texture
m_ImageContentsHash:
serializedVersion: 2
Hash: 00000000000000000000000000000000
m_ForcedFallbackFormat: 4
m_DownscaleFallback: 0
m_IsAlphaChannelOptional: 0
serializedVersion: 3
m_Width: 272
m_Height: 240
m_AntiAliasing: 1
m_MipCount: -1
m_DepthFormat: 2
m_ColorFormat: 8
m_MipMap: 0
m_GenerateMips: 1
m_SRGB: 0
m_UseDynamicScale: 0
m_BindMS: 0
m_EnableCompatibleFormat: 1
m_TextureSettings:
serializedVersion: 2
m_FilterMode: 1
m_Aniso: 0
m_MipBias: 0
m_WrapU: 1
m_WrapV: 1
m_WrapW: 1
m_Dimension: 2
m_VolumeDepth: 1

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: ffe34aaf87e4b9942b4c2ac05943d444
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 8400000
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,13 +1,7 @@
using AxibugEmuOnline.Client.Assets.Script.NesEmulator; using AxibugEmuOnline.Client.Assets.Script.NesEmulator;
using Codice.CM.Client.Differences;
using System; using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text;
using UnityEngine; using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.UI; using UnityEngine.UI;
using VirtualNes.Core; using VirtualNes.Core;
@ -17,23 +11,29 @@ namespace AxibugEmuOnline.Client
{ {
public RawImage Image; public RawImage Image;
private Color32[] wrapTexBuffer; private UInt32[] wrapTexBuffer;
private IntPtr wrapTexBufferPointer; private IntPtr wrapTexBufferPointer;
private Texture2D wrapTex; private Texture2D wrapTex;
private int TexBufferSize;
private uint[] pPal;
public void SetDrawData(byte[] screenData, byte[] lineColorMode, int screenWidth, int screenHeight) public void SetDrawData(byte[] screenData, byte[] lineColorMode, int screenWidth, int screenHeight)
{ {
if (wrapTex == null) if (wrapTex == null)
{ {
wrapTex = new Texture2D(screenWidth, screenHeight, TextureFormat.BGRA32, false); wrapTex = new Texture2D(screenWidth, screenHeight, TextureFormat.BGRA32, false);
wrapTexBuffer = new Color32[screenWidth * screenHeight]; wrapTexBuffer = new UInt32[screenWidth * screenHeight];
// 固定数组,防止垃圾回收器移动它 // 固定数组,防止垃圾回收器移动它
GCHandle handle = GCHandle.Alloc(wrapTexBuffer, GCHandleType.Pinned); GCHandle handle = GCHandle.Alloc(wrapTexBuffer, GCHandleType.Pinned);
// 获取数组的指针 // 获取数组的指针
wrapTexBufferPointer = handle.AddrOfPinnedObject(); wrapTexBufferPointer = handle.AddrOfPinnedObject();
Image.texture = wrapTex;
pPal = PaletteDefine.m_cnPalette[0];
TexBufferSize = wrapTexBuffer.Length * 4;
} }
uint[] pPal;
int pScn = 0; int pScn = 0;
int width; int width;
@ -42,41 +42,26 @@ namespace AxibugEmuOnline.Client
for (int line = 0; line < screenHeight; line++) for (int line = 0; line < screenHeight; line++)
{ {
if ((lineColorMode[line] & 0x80) == 0)
{
pPal = PaletteDefine.m_cnPalette[lineColorMode[line] & 0x07];
}
else
{
pPal = PaletteDefine.m_mnPalette[lineColorMode[line] & 0x07];
}
width = screenWidth; width = screenWidth;
while (width > 0) while (width > 0)
{ {
var edx = screenData[pScn + 8]; var edx = screenData[pScn + 8];
byte index = (byte)(edx & 0xFF); int index = edx & 0xFF;
var colorData = pPal[index]; var colorData = pPal[index];
var rawData = BitConverter.GetBytes(colorData); Dst[pDst] = 0xFF000000 | colorData;
Dst[pDst] = new Color32(rawData[0], rawData[1], rawData[2], 255);
pScn += 1; pScn += 1;
pDst += 1; pDst += 1;
width -= 1; width -= 1;
} }
pScn += PPU.SCREEN_WIDTH - screenWidth; pScn += 16;// PPU.SCREEN_WIDTH - screenWidth;
} }
//wrapTex.SetPixels32(wrapTexBuffer); wrapTex.LoadRawTextureData(wrapTexBufferPointer, TexBufferSize);
wrapTex.LoadRawTextureData(wrapTexBufferPointer, screenWidth * screenHeight * 4);
wrapTex.Apply(); wrapTex.Apply();
}
Graphics.Blit(wrapTex, Image.mainTexture as RenderTexture);
}
} }
} }

View File

@ -45,7 +45,7 @@ namespace VirtualNes.Core
private APU apu; private APU apu;
internal R6502 R = new R6502(); internal R6502 R = new R6502();
private byte[] ZN_Table = new byte[256]; private byte[] ZN_Table = new byte[256];
private Memory<byte> STACK; private ByteArrayRef STACK;
public CPU(NES parent) public CPU(NES parent)
{ {
@ -1361,12 +1361,12 @@ namespace VirtualNes.Core
private byte POP() private byte POP()
{ {
return STACK.Span[(++R.S) & 0xFF]; return STACK[(++R.S) & 0xFF];
} }
private void PUSH(byte V) private void PUSH(byte V)
{ {
STACK.Span[(R.S--) & 0xFF] = V; STACK[(R.S--) & 0xFF] = V;
} }
private void SEI() private void SEI()
@ -2010,7 +2010,7 @@ namespace VirtualNes.Core
DMA_cycles = 0; DMA_cycles = 0;
// STACK quick access // STACK quick access
STACK = new Memory<byte>(MMU.RAM, 0x0100, MMU.RAM.Length - 0x100); STACK = new ByteArrayRef(MMU.RAM, 0x0100, MMU.RAM.Length - 0x100);
// Zero/Negative FLAG // Zero/Negative FLAG
ZN_Table[0] = Z_FLAG; ZN_Table[0] = Z_FLAG;