feat(render): 使用Color类替换Vector3f表示光源颜色
- 在LightSource类中引入java.awt.Color类型 - 添加colorToVector3f和vector3fToColor静态转换方法- 修改构造函数以接受Color参数并自动转换 - 更新LightSourceData反序列化逻辑以使用新颜色格式 - 在测试类中使用标准Color常量设置光源 - 移除旧的直接Vector3f颜色构造方式
This commit is contained in:
@@ -3,6 +3,8 @@ package com.chuangzhou.vivid2D.render.model.util;
|
|||||||
import org.joml.Vector2f;
|
import org.joml.Vector2f;
|
||||||
import org.joml.Vector3f;
|
import org.joml.Vector3f;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
public class LightSource {
|
public class LightSource {
|
||||||
private Vector2f position;
|
private Vector2f position;
|
||||||
private Vector3f color;
|
private Vector3f color;
|
||||||
@@ -10,20 +12,43 @@ public class LightSource {
|
|||||||
private boolean enabled = true;
|
private boolean enabled = true;
|
||||||
private boolean isAmbient = false; // 是否为环境光
|
private boolean isAmbient = false; // 是否为环境光
|
||||||
|
|
||||||
public LightSource(Vector2f pos, Vector3f color, float intensity) {
|
public LightSource(Vector2f pos, Color color, float intensity) {
|
||||||
this.position = pos;
|
this.position = pos;
|
||||||
this.color = color;
|
this.color = colorToVector3f(color);
|
||||||
this.intensity = intensity;
|
this.intensity = intensity;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 环境光构造函数
|
// 环境光构造函数
|
||||||
public LightSource(Vector3f color, float intensity) {
|
public LightSource(Color color, float intensity) {
|
||||||
this.position = new Vector2f(0, 0);
|
this.position = new Vector2f(0, 0);
|
||||||
this.color = color;
|
this.color = colorToVector3f(color);
|
||||||
this.intensity = intensity;
|
this.intensity = intensity;
|
||||||
this.isAmbient = true;
|
this.isAmbient = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Vector3f colorToVector3f(Color color) {
|
||||||
|
if (color == null) return new Vector3f(1, 1, 1);
|
||||||
|
return new Vector3f(
|
||||||
|
color.getRed() / 255.0f,
|
||||||
|
color.getGreen() / 255.0f,
|
||||||
|
color.getBlue() / 255.0f
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Color vector3fToColor(Vector3f colorVec) {
|
||||||
|
if (colorVec == null) return new java.awt.Color(255, 255, 255);
|
||||||
|
|
||||||
|
float r = Math.min(1.0f, Math.max(0.0f, colorVec.x));
|
||||||
|
float g = Math.min(1.0f, Math.max(0.0f, colorVec.y));
|
||||||
|
float b = Math.min(1.0f, Math.max(0.0f, colorVec.z));
|
||||||
|
|
||||||
|
int red = (int) (r * 255 + 0.5f);
|
||||||
|
int green = (int) (g * 255 + 0.5f);
|
||||||
|
int blue = (int) (b * 255 + 0.5f);
|
||||||
|
|
||||||
|
return new Color(red, green, blue);
|
||||||
|
}
|
||||||
|
|
||||||
public Vector2f getPosition() { return position; }
|
public Vector2f getPosition() { return position; }
|
||||||
public Vector3f getColor() { return color; }
|
public Vector3f getColor() { return color; }
|
||||||
public float getIntensity() { return intensity; }
|
public float getIntensity() { return intensity; }
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package com.chuangzhou.vivid2D.render.model.util;
|
|||||||
|
|
||||||
import org.joml.Vector2f;
|
import org.joml.Vector2f;
|
||||||
import org.joml.Vector3f;
|
import org.joml.Vector3f;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -49,9 +51,9 @@ public class LightSourceData implements Serializable {
|
|||||||
|
|
||||||
LightSource light;
|
LightSource light;
|
||||||
if (isAmbient) {
|
if (isAmbient) {
|
||||||
light = new LightSource(col, intensity);
|
light = new LightSource(LightSource.vector3fToColor(col), intensity);
|
||||||
} else {
|
} else {
|
||||||
light = new LightSource(pos, col, intensity);
|
light = new LightSource(pos, LightSource.vector3fToColor(col), intensity);
|
||||||
}
|
}
|
||||||
light.setEnabled(enabled);
|
light.setEnabled(enabled);
|
||||||
return light;
|
return light;
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import org.lwjgl.glfw.GLFWVidMode;
|
|||||||
import org.lwjgl.opengl.GL;
|
import org.lwjgl.opengl.GL;
|
||||||
import org.lwjgl.system.MemoryUtil;
|
import org.lwjgl.system.MemoryUtil;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
@@ -135,15 +136,15 @@ public class ModelRenderLightingTest {
|
|||||||
body.addChild(rightLeg);
|
body.addChild(rightLeg);
|
||||||
|
|
||||||
LightSource ambientLight = new LightSource(
|
LightSource ambientLight = new LightSource(
|
||||||
new Vector3f(0.5f, 0.5f, 0.5f), // 灰色
|
Color.GRAY,
|
||||||
0.3f
|
0.3f
|
||||||
);
|
);
|
||||||
ambientLight.setAmbient(true);
|
ambientLight.setAmbient(true);
|
||||||
model.addLight(ambientLight);
|
model.addLight(ambientLight);
|
||||||
|
|
||||||
// 添加光源
|
// 添加光源
|
||||||
model.addLight(new LightSource(new Vector2f(-100, -100), new Vector3f(1f, 0f, 0f), 20f));
|
model.addLight(new LightSource(new Vector2f(-100, -100), Color.ORANGE, 100f));
|
||||||
model.addLight(new LightSource(new Vector2f(150, 150), new Vector3f(0f, 0f, 1f), 20f));
|
//model.addLight(new LightSource(new Vector2f(150, 150), new Color(1f, 1f, 1f), 200f));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Texture createSolidTexture(int w, int h, int rgba) {
|
private Texture createSolidTexture(int w, int h, int rgba) {
|
||||||
|
|||||||
Reference in New Issue
Block a user