This commit is contained in:
2025-10-28 23:58:15 +08:00
parent e53d5c1c14
commit cd6b4ea5c9
90 changed files with 144 additions and 125 deletions

View File

@@ -161,9 +161,9 @@ namespace NBF
if (_renderTexture != null)
return;
_renderTexture = new RenderTexture(512, 512, 24, RenderTextureFormat.ARGB32)
_renderTexture = new RenderTexture(1024, 1024, 24, RenderTextureFormat.ARGB32)
{
antiAliasing = 1,
antiAliasing = 8,
filterMode = FilterMode.Bilinear,
anisoLevel = 0,
useMipMap = false
@@ -248,129 +248,7 @@ namespace NBF
}
#endregion
#region MyRegion
/// <summary>
/// The rendertexture is not transparent. So if you want to the UI elements can be seen in the back of the models/particles in rendertexture,
/// you can set a maximunm two images for background.
/// Be careful if your image is 9 grid scaling, you must make sure the place holder is inside the middle box(dont cover from border to middle).
/// </summary>
/// <param name="image"></param>
public void SetBackground(GObject image)
{
SetBackground(image, null);
}
/// <summary>
/// The rendertexture is not transparent. So if you want to the UI elements can be seen in the back of the models/particles in rendertexture,
/// you can set a maximunm two images for background.
/// </summary>
/// <param name="image1"></param>
/// <param name="image2"></param>
public void SetBackground(GObject image1, GObject image2)
{
Image source1 = (Image)image1.displayObject;
Image source2 = image2 != null ? (Image)image2.displayObject : null;
Vector3 pos = _background.position;
pos.z = _camera.farClipPlane;
_background.position = pos;
Vector2[] uv = new Vector2[4];
Vector2[] uv2 = null;
Rect rect = _image.TransformRect(new Rect(0, 0, this._width, this._height), source1);
Rect uvRect = GetImageUVRect(source1, rect, uv);
if (source2 != null)
{
rect = _image.TransformRect(new Rect(0, 0, this._width, this._height), source2);
uv2 = new Vector2[4];
GetImageUVRect(source2, rect, uv2);
}
Vector3[] vertices = new Vector3[4];
for (int i = 0; i < 4; i++)
{
Vector2 v = uv[i];
vertices[i] = new Vector3((v.x - uvRect.x) / uvRect.width * 2 - 1,
(v.y - uvRect.y) / uvRect.height * 2 - 1, 0);
}
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.uv = uv;
if (uv2 != null)
mesh.uv2 = uv2;
mesh.colors32 = new Color32[] { Color.white, Color.white, Color.white, Color.white };
mesh.triangles = new int[] { 0, 1, 2, 2, 3, 0 };
MeshFilter meshFilter = this._background.gameObject.GetComponent<MeshFilter>();
if (meshFilter == null)
meshFilter = this._background.gameObject.AddComponent<MeshFilter>();
meshFilter.mesh = mesh;
MeshRenderer meshRenderer = this._background.gameObject.GetComponent<MeshRenderer>();
if (meshRenderer == null)
meshRenderer = this._background.gameObject.AddComponent<MeshRenderer>();
#if (UNITY_5 || UNITY_5_3_OR_NEWER)
meshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
#else
meshRenderer.castShadows = false;
#endif
meshRenderer.receiveShadows = false;
Shader shader = Shader.Find("Game/FullScreen");
Material mat = new Material(shader);
mat.mainTexture = source1.texture.nativeTexture;
if (source2 != null)
mat.SetTexture("_Tex2", source2.texture.nativeTexture);
meshRenderer.material = mat;
}
Rect GetImageUVRect(Image image, Rect localRect, Vector2[] uv)
{
Rect imageRect = new Rect(0, 0, image.size.x, image.size.y);
Rect bound = ToolSet.Intersection(ref imageRect, ref localRect);
Rect uvRect = image.texture.uvRect;
if (image.scale9Grid != null)
{
Rect gridRect = (Rect)image.scale9Grid;
float sourceW = image.texture.width;
float sourceH = image.texture.height;
uvRect = Rect.MinMaxRect(Mathf.Lerp(uvRect.xMin, uvRect.xMax, gridRect.xMin / sourceW),
Mathf.Lerp(uvRect.yMin, uvRect.yMax, (sourceH - gridRect.yMax) / sourceH),
Mathf.Lerp(uvRect.xMin, uvRect.xMax, gridRect.xMax / sourceW),
Mathf.Lerp(uvRect.yMin, uvRect.yMax, (sourceH - gridRect.yMin) / sourceH));
float vw = imageRect.width - (sourceW - gridRect.width);
float vh = imageRect.height - (sourceH - gridRect.height);
uvRect = Rect.MinMaxRect(Mathf.Lerp(uvRect.xMin, uvRect.xMax, (bound.x - gridRect.x) / vw),
Mathf.Lerp(uvRect.yMin, uvRect.yMax,
(imageRect.height - bound.yMax - (sourceH - gridRect.yMax)) / vh),
Mathf.Lerp(uvRect.xMin, uvRect.xMax, (bound.xMax - gridRect.x) / vw),
Mathf.Lerp(uvRect.yMin, uvRect.yMax, (imageRect.height - bound.yMin - gridRect.y) / vh));
}
else
{
uvRect = Rect.MinMaxRect(Mathf.Lerp(uvRect.xMin, uvRect.xMax, bound.xMin / imageRect.width),
Mathf.Lerp(uvRect.yMin, uvRect.yMax, (imageRect.height - bound.yMax) / imageRect.height),
Mathf.Lerp(uvRect.xMin, uvRect.xMax, bound.xMax / imageRect.width),
Mathf.Lerp(uvRect.yMin, uvRect.yMax, (imageRect.height - bound.yMin) / imageRect.height));
}
uv[0] = uvRect.position;
uv[1] = new Vector2(uvRect.xMin, uvRect.yMax);
uv[2] = new Vector2(uvRect.xMax, uvRect.yMax);
uv[3] = new Vector2(uvRect.xMax, uvRect.yMin);
if (image.texture.rotated)
ToolSet.RotateUV(uv, ref image.texture.uvRect);
return uvRect;
}
#endregion
#region MyRegion