Compare commits
No commits in common. "e011cea090bc3d7e773dad3e28583a01143c7c61" and "44e0d65f10360829eafdbde6a581b87e823a339f" have entirely different histories.
e011cea090
...
44e0d65f10
|
|
@ -12,9 +12,9 @@
|
||||||
* - SpriteRenderer 精灵渲染
|
* - SpriteRenderer 精灵渲染
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <extra2d.h>
|
||||||
#include "game_scene.h"
|
#include "game_scene.h"
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <extra2d.h>
|
|
||||||
|
|
||||||
using namespace extra2d;
|
using namespace extra2d;
|
||||||
|
|
||||||
|
|
@ -22,54 +22,58 @@ using namespace extra2d;
|
||||||
* @brief 程序入口
|
* @brief 程序入口
|
||||||
*/
|
*/
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
// ========================================
|
// ========================================
|
||||||
// 1. 创建应用
|
// 1. 创建应用
|
||||||
// ========================================
|
// ========================================
|
||||||
auto app = Application::create();
|
auto app = Application::create();
|
||||||
|
|
||||||
AppConfig config;
|
AppConfig config;
|
||||||
config.title = "Scene Graph Demo - Extra2D";
|
config.title = "Scene Graph Demo - Extra2D";
|
||||||
config.width = 1280;
|
config.width = 1280;
|
||||||
config.height = 720;
|
config.height = 720;
|
||||||
|
|
||||||
if (!app->init(config)) {
|
if (!app->init(config)) {
|
||||||
printf("Failed to initialize application!\n");
|
printf("Failed to initialize application!\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
// ========================================
|
|
||||||
// 2. 获取场景模块和导演
|
|
||||||
// ========================================
|
|
||||||
SceneModule *sceneModule = app->getModule<SceneModule>();
|
|
||||||
if (!sceneModule) {
|
|
||||||
printf("Failed to get SceneModule!\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
Director *director = sceneModule->getDirector();
|
printf("Application initialized successfully\n");
|
||||||
if (!director) {
|
printf("Window size: %dx%d\n", app->getWindowWidth(), app->getWindowHeight());
|
||||||
printf("Failed to get Director!\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("Scene module and director ready\n");
|
// ========================================
|
||||||
|
// 2. 获取场景模块和导演
|
||||||
|
// ========================================
|
||||||
|
SceneModule *sceneModule = app->getModule<SceneModule>();
|
||||||
|
if (!sceneModule) {
|
||||||
|
printf("Failed to get SceneModule!\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
// ========================================
|
Director *director = sceneModule->getDirector();
|
||||||
// 3. 创建并运行游戏场景
|
if (!director) {
|
||||||
// ========================================
|
printf("Failed to get Director!\n");
|
||||||
auto gameScene = makePtr<GameScene>();
|
return -1;
|
||||||
director->runScene(gameScene);
|
}
|
||||||
|
|
||||||
printf("Game scene started\n");
|
printf("Scene module and director ready\n");
|
||||||
|
|
||||||
// ========================================
|
// ========================================
|
||||||
// 4. 运行应用主循环
|
// 3. 创建并运行游戏场景
|
||||||
// ========================================
|
// ========================================
|
||||||
app->run();
|
auto gameScene = makePtr<GameScene>();
|
||||||
|
director->runScene(gameScene);
|
||||||
|
|
||||||
// ========================================
|
printf("Game scene started\n");
|
||||||
// 5. 清理(自动进行)
|
|
||||||
// ========================================
|
|
||||||
printf("Application shutting down...\n");
|
|
||||||
|
|
||||||
return 0;
|
// ========================================
|
||||||
|
// 4. 运行应用主循环
|
||||||
|
// ========================================
|
||||||
|
app->run();
|
||||||
|
|
||||||
|
// ========================================
|
||||||
|
// 5. 清理(自动进行)
|
||||||
|
// ========================================
|
||||||
|
printf("Application shutting down...\n");
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,73 @@
|
||||||
|
#version 320 es
|
||||||
|
precision highp float;
|
||||||
|
|
||||||
|
// 全局 UBO (binding = 0) - 每帧更新一次
|
||||||
|
layout(std140, binding = 0) uniform GlobalUBO {
|
||||||
|
mat4 uViewProjection;
|
||||||
|
vec4 uCameraPosition;
|
||||||
|
float uTime;
|
||||||
|
float uDeltaTime;
|
||||||
|
vec2 uScreenSize;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 材质 UBO (binding = 1) - 每批次更新
|
||||||
|
layout(std140, binding = 1) uniform MaterialUBO {
|
||||||
|
vec4 uColor;
|
||||||
|
vec4 uTintColor;
|
||||||
|
float uOpacity;
|
||||||
|
float uPadding[3]; // std140 对齐填充
|
||||||
|
};
|
||||||
|
|
||||||
|
// 顶点属性 (每个顶点)
|
||||||
|
layout(location = 0) in vec2 aPosition;
|
||||||
|
layout(location = 1) in vec2 aTexCoord;
|
||||||
|
layout(location = 2) in vec4 aColor;
|
||||||
|
|
||||||
|
// 实例属性 (每个实例) - 使用 location 3-6
|
||||||
|
layout(location = 3) in vec2 iPosition; // 实例位置
|
||||||
|
layout(location = 4) in float iRotation; // 实例旋转
|
||||||
|
layout(location = 5) in vec2 iScale; // 实例缩放
|
||||||
|
layout(location = 6) in vec4 iColor; // 实例颜色
|
||||||
|
|
||||||
|
// 输出到片段着色器
|
||||||
|
out vec2 vTexCoord;
|
||||||
|
out vec4 vColor;
|
||||||
|
out vec4 vTintColor;
|
||||||
|
out float vOpacity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 从旋转角度构建2D变换矩阵
|
||||||
|
* @param angle 旋转角度(弧度)
|
||||||
|
* @return 2x2旋转矩阵
|
||||||
|
*/
|
||||||
|
mat2 rotate2D(float angle) {
|
||||||
|
float c = cos(angle);
|
||||||
|
float s = sin(angle);
|
||||||
|
return mat2(c, -s, s, c);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 顶点着色器入口
|
||||||
|
*
|
||||||
|
* 计算顶点在裁剪空间中的位置,
|
||||||
|
* 应用实例的变换(位置、旋转、缩放),
|
||||||
|
* 并传递纹理坐标和颜色到片段着色器
|
||||||
|
*/
|
||||||
|
void main() {
|
||||||
|
// 应用实例缩放和旋转
|
||||||
|
vec2 localPos = rotate2D(iRotation) * (aPosition * iScale);
|
||||||
|
|
||||||
|
// 应用实例位置偏移
|
||||||
|
vec2 worldPos = localPos + iPosition;
|
||||||
|
|
||||||
|
// 变换到裁剪空间
|
||||||
|
gl_Position = uViewProjection * vec4(worldPos, 0.0, 1.0);
|
||||||
|
|
||||||
|
vTexCoord = aTexCoord;
|
||||||
|
|
||||||
|
// 混合顶点颜色、实例颜色和材质颜色
|
||||||
|
vColor = aColor * iColor * uColor;
|
||||||
|
|
||||||
|
vTintColor = uTintColor;
|
||||||
|
vOpacity = uOpacity;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,35 @@
|
||||||
|
#version 320 es
|
||||||
|
precision highp float;
|
||||||
|
|
||||||
|
// 从顶点着色器输入
|
||||||
|
in vec2 vTexCoord;
|
||||||
|
in vec4 vColor;
|
||||||
|
|
||||||
|
// 纹理采样器
|
||||||
|
uniform sampler2D uTexture;
|
||||||
|
|
||||||
|
// 输出颜色
|
||||||
|
out vec4 fragColor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 片段着色器入口(实例化版本)
|
||||||
|
*
|
||||||
|
* 采样纹理并与顶点颜色混合
|
||||||
|
*/
|
||||||
|
void main() {
|
||||||
|
// 采样纹理
|
||||||
|
vec4 texColor = texture(uTexture, vTexCoord);
|
||||||
|
|
||||||
|
// 如果纹理采样结果是黑色或透明,使用白色作为默认值
|
||||||
|
if (texColor.rgb == vec3(0.0) || texColor.a < 0.01) {
|
||||||
|
texColor = vec4(1.0, 1.0, 1.0, 1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 混合:纹理 * 顶点颜色
|
||||||
|
fragColor = texColor * vColor;
|
||||||
|
|
||||||
|
// Alpha 测试:丢弃几乎透明的像素
|
||||||
|
if (fragColor.a < 0.01) {
|
||||||
|
discard;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue