Compare commits

..

No commits in common. "e011cea090bc3d7e773dad3e28583a01143c7c61" and "44e0d65f10360829eafdbde6a581b87e823a339f" have entirely different histories.

3 changed files with 154 additions and 42 deletions

View File

@ -12,9 +12,9 @@
* - SpriteRenderer
*/
#include <extra2d.h>
#include "game_scene.h"
#include <cstdio>
#include <extra2d.h>
using namespace extra2d;
@ -36,6 +36,10 @@ int main(int argc, char **argv) {
printf("Failed to initialize application!\n");
return -1;
}
printf("Application initialized successfully\n");
printf("Window size: %dx%d\n", app->getWindowWidth(), app->getWindowHeight());
// ========================================
// 2. 获取场景模块和导演
// ========================================

73
shader/instanced.vert Normal file
View File

@ -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;
}

View File

@ -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;
}
}