refactor(animation):优化动画系统字段不可变性与getter方法格式- 将AnimationClip中的creationTime字段设为final
- 将AnimationLayer中的parameterOverrides字段设为final - 将AnimationParameter中的id、defaultValue、minValue、maxValue字段设为final - 将LightSource中的position、color、intensity字段设为final - 统一所有getter方法的代码格式,增加换行与大括号 - 优化Mesh2D中部分条件判断逻辑与字段final声明- 调整部分JavaDoc注释格式与空行位置提升可读性
This commit is contained in:
@@ -2,25 +2,30 @@ package com.chuangzhou.vivid2D.render;
|
||||
|
||||
import com.chuangzhou.vivid2D.render.model.Model2D;
|
||||
import com.chuangzhou.vivid2D.render.model.ModelPart;
|
||||
import com.chuangzhou.vivid2D.render.systems.Camera;
|
||||
import com.chuangzhou.vivid2D.render.systems.buffer.BufferBuilder;
|
||||
import com.chuangzhou.vivid2D.render.systems.buffer.Tesselator;
|
||||
import com.chuangzhou.vivid2D.render.model.util.LightSource;
|
||||
import com.chuangzhou.vivid2D.render.model.util.Mesh2D;
|
||||
import com.chuangzhou.vivid2D.render.model.util.PhysicsSystem;
|
||||
import com.chuangzhou.vivid2D.render.systems.Camera;
|
||||
import com.chuangzhou.vivid2D.render.systems.RenderSystem;
|
||||
import com.chuangzhou.vivid2D.render.systems.buffer.BufferBuilder;
|
||||
import com.chuangzhou.vivid2D.render.systems.buffer.Tesselator;
|
||||
import com.chuangzhou.vivid2D.render.systems.sources.CompleteShader;
|
||||
import com.chuangzhou.vivid2D.render.systems.sources.ShaderProgram;
|
||||
import com.chuangzhou.vivid2D.render.systems.sources.ShaderManagement;
|
||||
import com.chuangzhou.vivid2D.render.systems.sources.ShaderProgram;
|
||||
import org.joml.Matrix3f;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector4f;
|
||||
import org.lwjgl.opengl.*;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL15;
|
||||
import org.lwjgl.opengl.GL20;
|
||||
import org.lwjgl.opengl.GL30;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
@@ -65,6 +70,7 @@ public final class ModelRender {
|
||||
|
||||
/**
|
||||
* 渲染系统初始化状态标志,确保系统只初始化一次
|
||||
*
|
||||
* @see #initialize()
|
||||
* @see #isInitialized()
|
||||
*/
|
||||
@@ -73,6 +79,7 @@ public final class ModelRender {
|
||||
/**
|
||||
* 视口宽度(像素),定义渲染区域的大小
|
||||
* 默认值:800像素
|
||||
*
|
||||
* @see #setViewport(int, int)
|
||||
*/
|
||||
static int viewportWidth = 800;
|
||||
@@ -80,6 +87,7 @@ public final class ModelRender {
|
||||
/**
|
||||
* 视口高度(像素),定义渲染区域的大小
|
||||
* 默认值:600像素
|
||||
*
|
||||
* @see #setViewport(int, int)
|
||||
*/
|
||||
static int viewportHeight = 600;
|
||||
@@ -87,6 +95,7 @@ public final class ModelRender {
|
||||
/**
|
||||
* 清除颜色(RGBA),用于在每帧开始时清空颜色缓冲区
|
||||
* 默认值:黑色不透明 (0.0f, 0.0f, 0.0f, 1.0f)
|
||||
*
|
||||
* @see RenderSystem#clearColor(float, float, float, float)
|
||||
*/
|
||||
private static final Vector4f CLEAR_COLOR = new Vector4f(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
@@ -95,6 +104,7 @@ public final class ModelRender {
|
||||
* 深度测试启用标志,控制是否进行深度缓冲测试
|
||||
* 在2D渲染中通常禁用以提高性能
|
||||
* 默认值:false(禁用)
|
||||
*
|
||||
* @see RenderSystem#enableDepthTest()
|
||||
* @see RenderSystem#disableDepthTest()
|
||||
*/
|
||||
@@ -103,6 +113,7 @@ public final class ModelRender {
|
||||
/**
|
||||
* 混合功能启用标志,控制透明度和颜色混合
|
||||
* 默认值:true(启用)
|
||||
*
|
||||
* @see RenderSystem#enableBlend()
|
||||
* @see RenderSystem#disableBlend()
|
||||
*/
|
||||
@@ -119,6 +130,7 @@ public final class ModelRender {
|
||||
/**
|
||||
* 默认着色器程序,用于大多数模型的渲染
|
||||
* 包含基础的光照、纹理和变换功能
|
||||
*
|
||||
* @see #compileDefaultShader()
|
||||
*/
|
||||
private static ShaderProgram defaultProgram = null;
|
||||
@@ -127,6 +139,7 @@ public final class ModelRender {
|
||||
* 网格GPU资源缓存,管理已上传到GPU的网格数据
|
||||
* 键:Mesh2D对象
|
||||
* 值:对应的OpenGL资源(VAO、VBO、EBO)
|
||||
*
|
||||
* @see MeshGLResources
|
||||
*/
|
||||
private static final Map<Mesh2D, MeshGLResources> meshResources = new HashMap<>();
|
||||
@@ -141,6 +154,7 @@ public final class ModelRender {
|
||||
/**
|
||||
* 默认白色纹理ID,当模型没有指定纹理时使用
|
||||
* 这是一个1x1的纯白色纹理,确保模型有基本的颜色显示
|
||||
*
|
||||
* @see #createDefaultTexture()
|
||||
*/
|
||||
private static int defaultTextureId = 0;
|
||||
@@ -189,7 +203,7 @@ public final class ModelRender {
|
||||
*/
|
||||
private static final Camera camera = new Camera();
|
||||
|
||||
// ================== 字体管理 ==================
|
||||
// ================== 字体管理 ==================
|
||||
private static TextRenderer defaultTextRenderer = null;
|
||||
private static final int FONT_BITMAP_WIDTH = 512;
|
||||
private static final int FONT_BITMAP_HEIGHT = 512;
|
||||
@@ -290,9 +304,18 @@ public final class ModelRender {
|
||||
boolean initialized = false;
|
||||
|
||||
void dispose() {
|
||||
if (ebo != 0) { GL15.glDeleteBuffers(ebo); ebo = 0; }
|
||||
if (vbo != 0) { GL15.glDeleteBuffers(vbo); vbo = 0; }
|
||||
if (vao != 0) { GL30.glDeleteVertexArrays(vao); vao = 0; }
|
||||
if (ebo != 0) {
|
||||
GL15.glDeleteBuffers(ebo);
|
||||
ebo = 0;
|
||||
}
|
||||
if (vbo != 0) {
|
||||
GL15.glDeleteBuffers(vbo);
|
||||
vbo = 0;
|
||||
}
|
||||
if (vao != 0) {
|
||||
GL30.glDeleteVertexArrays(vao);
|
||||
vao = 0;
|
||||
}
|
||||
initialized = false;
|
||||
}
|
||||
}
|
||||
@@ -722,12 +745,13 @@ public final class ModelRender {
|
||||
}
|
||||
}
|
||||
// 恢复原始颜色
|
||||
setUniformVec4Internal(defaultProgram, "uColor", new Vector4f(1,1,1,1));
|
||||
setUniformVec4Internal(defaultProgram, "uColor", new Vector4f(1, 1, 1, 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* 绘制简洁的灯泡形状
|
||||
* @param position 灯泡位置
|
||||
*
|
||||
* @param position 灯泡位置
|
||||
* @param intensity 光源强度,用于控制灯泡大小
|
||||
*/
|
||||
private static void drawLightBulb(Vector2f position, float intensity) {
|
||||
@@ -868,16 +892,14 @@ public final class ModelRender {
|
||||
|
||||
RenderSystem.checkGLError("before_render_collider_" + enabledColliders);
|
||||
|
||||
if (collider instanceof PhysicsSystem.CircleCollider) {
|
||||
PhysicsSystem.CircleCollider c = (PhysicsSystem.CircleCollider) collider;
|
||||
if (collider instanceof PhysicsSystem.CircleCollider c) {
|
||||
if (c.getCenter() != null && c.getRadius() > 0) {
|
||||
drawCircleColliderWire(c.getCenter(), c.getRadius());
|
||||
enabledColliders++;
|
||||
} else {
|
||||
logger.warn("Invalid CircleCollider: center={}, radius={}", c.getCenter(), c.getRadius());
|
||||
}
|
||||
} else if (collider instanceof PhysicsSystem.RectangleCollider) {
|
||||
PhysicsSystem.RectangleCollider r = (PhysicsSystem.RectangleCollider) collider;
|
||||
} else if (collider instanceof PhysicsSystem.RectangleCollider r) {
|
||||
if (r.getCenter() != null && r.getWidth() > 0 && r.getHeight() > 0) {
|
||||
drawRectangleColliderWire(r.getCenter(), r.getWidth(), r.getHeight());
|
||||
enabledColliders++;
|
||||
@@ -977,17 +999,25 @@ public final class ModelRender {
|
||||
ModelPart.BlendMode bm = part.getBlendMode();
|
||||
if (bm != null) {
|
||||
switch (bm) {
|
||||
case ADDITIVE: blend = 1; break;
|
||||
case MULTIPLY: blend = 2; break;
|
||||
case SCREEN: blend = 3; break;
|
||||
case NORMAL: default: blend = 0;
|
||||
case ADDITIVE:
|
||||
blend = 1;
|
||||
break;
|
||||
case MULTIPLY:
|
||||
blend = 2;
|
||||
break;
|
||||
case SCREEN:
|
||||
blend = 3;
|
||||
break;
|
||||
case NORMAL:
|
||||
default:
|
||||
blend = 0;
|
||||
}
|
||||
} else {
|
||||
blend = 0;
|
||||
}
|
||||
setUniformIntInternal(sp, "uBlendMode", blend);
|
||||
// 这里保留为白色,若需要部件 tint 请替换为 part 的 color 属性
|
||||
setUniformVec4Internal(sp, "uColor", new Vector4f(1,1,1,1));
|
||||
setUniformVec4Internal(sp, "uColor", new Vector4f(1, 1, 1, 1));
|
||||
}
|
||||
|
||||
public static TextRenderer getTextRenderer() {
|
||||
@@ -1009,9 +1039,10 @@ public final class ModelRender {
|
||||
|
||||
/**
|
||||
* 渲染文字
|
||||
* @param text 文字内容
|
||||
* @param x 世界坐标 X
|
||||
* @param y 世界坐标 Y ,反转的
|
||||
*
|
||||
* @param text 文字内容
|
||||
* @param x 世界坐标 X
|
||||
* @param y 世界坐标 Y ,反转的
|
||||
* @param color RGBA 颜色
|
||||
*/
|
||||
public static void renderText(String text, float x, float y, Vector4f color) {
|
||||
@@ -1025,6 +1056,7 @@ public final class ModelRender {
|
||||
|
||||
/**
|
||||
* 获取默认摄像机与当前摄像机之间的偏移量
|
||||
*
|
||||
* @return Vector2f 偏移向量 (dx, dy)
|
||||
*/
|
||||
public static Vector2f getCameraOffset() {
|
||||
@@ -1033,15 +1065,16 @@ public final class ModelRender {
|
||||
float zoom = camera.getZoom();
|
||||
Vector2f pos = camera.getPosition();
|
||||
float tx = -1.0f - (2.0f * zoom * pos.x / width);
|
||||
float ty = 1.0f + (2.0f * zoom * pos.y / height);
|
||||
float ty = 1.0f + (2.0f * zoom * pos.y / height);
|
||||
float tx0 = -1.0f;
|
||||
float ty0 = 1.0f;
|
||||
float ty0 = 1.0f;
|
||||
float offsetX = tx - tx0;
|
||||
float offsetY = ty - ty0;
|
||||
offsetX = -offsetX * width / 2.0f / zoom;
|
||||
offsetY = offsetY * height / 2.0f / zoom;
|
||||
return new Vector2f(offsetX, offsetY);
|
||||
}
|
||||
|
||||
public static void setViewport(int width, int height) {
|
||||
viewportWidth = Math.max(1, width);
|
||||
viewportHeight = Math.max(1, height);
|
||||
@@ -1049,7 +1082,12 @@ public final class ModelRender {
|
||||
}
|
||||
|
||||
// ================== 辅助:外部获取状态 ==================
|
||||
public static boolean isInitialized() { return initialized; }
|
||||
public static int getLoadedMeshCount() { return meshResources.size(); }
|
||||
public static boolean isInitialized() {
|
||||
return initialized;
|
||||
}
|
||||
|
||||
public static int getLoadedMeshCount() {
|
||||
return meshResources.size();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user