From 782834fe15071b8296c285dd0ab062111dbace66 Mon Sep 17 00:00:00 2001 From: "wumaolin.wml" Date: Mon, 2 Dec 2024 22:18:28 +0800 Subject: [PATCH 1/2] fix: sprite billboard --- .../src/components/base-render-component.ts | 10 ++++++++- .../src/plugins/sprite/sprite-item.ts | 2 +- .../src/plugins/text/text-item.ts | 2 +- .../effects-core/src/shader/item.vert.glsl | 22 +++++++++++++++++-- 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/packages/effects-core/src/components/base-render-component.ts b/packages/effects-core/src/components/base-render-component.ts index caabadda0..1e81daa1c 100644 --- a/packages/effects-core/src/components/base-render-component.ts +++ b/packages/effects-core/src/components/base-render-component.ts @@ -73,7 +73,7 @@ export class BaseRenderComponent extends RendererComponent { super(engine); this.renderer = { - renderMode: spec.RenderMode.BILLBOARD, + renderMode: spec.RenderMode.MESH, blending: spec.BlendingMode.ALPHA, texture: this.engine.emptyTexture, occlusion: false, @@ -148,6 +148,14 @@ export class BaseRenderComponent extends RendererComponent { renderer.setGlobalMatrix('effects_ObjectToWorld', this.transform.getWorldMatrix()); } this.material.setVector2('_Size', this.transform.size); + + if (this.renderer.renderMode === spec.RenderMode.BILLBOARD || + this.renderer.renderMode === spec.RenderMode.VERTICAL_BILLBOARD || + this.renderer.renderMode === spec.RenderMode.HORIZONTAL_BILLBOARD + ) { + this.material.setVector3('_Scale', this.transform.scale); + } + renderer.drawGeometry(geo, material); } diff --git a/packages/effects-core/src/plugins/sprite/sprite-item.ts b/packages/effects-core/src/plugins/sprite/sprite-item.ts index c118e77d6..4eda818a5 100644 --- a/packages/effects-core/src/plugins/sprite/sprite-item.ts +++ b/packages/effects-core/src/plugins/sprite/sprite-item.ts @@ -293,7 +293,7 @@ export class SpriteComponent extends BaseRenderComponent { this.interaction = interaction; this.renderer = { - renderMode: renderer.renderMode ?? spec.RenderMode.BILLBOARD, + renderMode: renderer.renderMode ?? spec.RenderMode.MESH, blending: renderer.blending ?? spec.BlendingMode.ALPHA, texture: renderer.texture ?? this.engine.emptyTexture, occlusion: !!renderer.occlusion, diff --git a/packages/effects-core/src/plugins/text/text-item.ts b/packages/effects-core/src/plugins/text/text-item.ts index dafb4e2d6..13014cf2d 100644 --- a/packages/effects-core/src/plugins/text/text-item.ts +++ b/packages/effects-core/src/plugins/text/text-item.ts @@ -106,7 +106,7 @@ export class TextComponent extends BaseRenderComponent { this.interaction = interaction; this.renderer = { - renderMode: renderer.renderMode ?? spec.RenderMode.BILLBOARD, + renderMode: renderer.renderMode ?? spec.RenderMode.MESH, blending: renderer.blending ?? spec.BlendingMode.ALPHA, texture: renderer.texture ?? this.engine.emptyTexture, occlusion: !!renderer.occlusion, diff --git a/packages/effects-core/src/shader/item.vert.glsl b/packages/effects-core/src/shader/item.vert.glsl index 86a0fe61c..9c0111128 100644 --- a/packages/effects-core/src/shader/item.vert.glsl +++ b/packages/effects-core/src/shader/item.vert.glsl @@ -8,12 +8,14 @@ varying vec3 vParams;// texIndex mulAplha varying vec4 vColor; uniform vec2 _Size; +uniform vec3 _Scale; uniform vec4 _Color; uniform vec4 _TexParams;//transparentOcclusion blending renderMode uniform vec4 _TexOffset;// x y sx sy uniform mat4 effects_MatrixVP; uniform mat4 effects_MatrixInvV; uniform mat4 effects_ObjectToWorld; +uniform mat4 effects_MatrixV; #ifdef ENV_EDITOR uniform vec4 uEditorTransform; @@ -24,8 +26,24 @@ void main() { vTexCoord = vec2(atlasOffset.xy * _TexOffset.zw + _TexOffset.xy); vColor = _Color; vParams = vec3(0.0, texParams.y, texParams.x); - vec4 pos = vec4(aPos.xy * _Size, aPos.z, 1.0); - gl_Position = effects_MatrixVP * effects_ObjectToWorld * pos; + + if(texParams.z == 1.0) { + vec4 pos = vec4(aPos.xy * _Size, aPos.z, 1.0); + gl_Position = effects_MatrixVP * effects_ObjectToWorld * pos; + } else { // Billboard + mat4 view = effects_MatrixV; + // 提取摄像机的右向量和上向量 + vec3 camRight = vec3(view[0][0], view[1][0], view[2][0]); // 视图矩阵的第一列 + vec3 camUp = vec3(view[0][1], view[1][1], view[2][1]); // 视图矩阵的第二列 + + // 计算模型转换后的位置 + vec3 worldPosition = vec3(effects_ObjectToWorld * vec4(0.0, 0.0, 0.0, 1.0)); + + // 根据局部顶点在 billboarding 平面上的方向,调整到面向摄像机的世界坐标 + vec3 vertexPosition = worldPosition + camRight * aPos.x * _Size.x * _Scale.x + camUp * aPos.y * _Size.y * _Scale.y; + gl_Position = effects_MatrixVP * vec4(vertexPosition, 1.0); + } + #ifdef ENV_EDITOR gl_Position = vec4(gl_Position.xy * uEditorTransform.xy + uEditorTransform.zw * gl_Position.w, gl_Position.zw); #endif From f77fe604ff713c6f9528d83bcd9cd5c9cdd60257 Mon Sep 17 00:00:00 2001 From: "wumaolin.wml" Date: Mon, 2 Dec 2024 22:23:49 +0800 Subject: [PATCH 2/2] chore: remove sprite shader unused uniform --- packages/effects-core/src/shader/item.vert.glsl | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/effects-core/src/shader/item.vert.glsl b/packages/effects-core/src/shader/item.vert.glsl index 9c0111128..f5cb93181 100644 --- a/packages/effects-core/src/shader/item.vert.glsl +++ b/packages/effects-core/src/shader/item.vert.glsl @@ -13,7 +13,6 @@ uniform vec4 _Color; uniform vec4 _TexParams;//transparentOcclusion blending renderMode uniform vec4 _TexOffset;// x y sx sy uniform mat4 effects_MatrixVP; -uniform mat4 effects_MatrixInvV; uniform mat4 effects_ObjectToWorld; uniform mat4 effects_MatrixV;