Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support trail effects (#1251) #1431

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/core/src/shader/ShaderPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import blinnPhongFs from "../shaderlib/extra/blinn-phong.fs.glsl";
import blinnPhongVs from "../shaderlib/extra/blinn-phong.vs.glsl";
import particleFs from "../shaderlib/extra/particle.fs.glsl";
import particleVs from "../shaderlib/extra/particle.vs.glsl";
import trailFs from "../shaderlib/extra/trail.fs.glsl";
import trailVs from "../shaderlib/extra/trail.vs.glsl";
import pbrSpecularFs from "../shaderlib/extra/pbr-specular.fs.glsl";
import pbrFs from "../shaderlib/extra/pbr.fs.glsl";
import pbrVs from "../shaderlib/extra/pbr.vs.glsl";
Expand Down Expand Up @@ -47,6 +49,7 @@ export class ShaderPool {
Shader.create("SkyProcedural", [new ShaderPass(skyProceduralVs, skyProceduralFs, forwardPassTags)]);

Shader.create("particle-shader", [new ShaderPass(particleVs, particleFs, forwardPassTags)]);
Shader.create("trail-shader", [new ShaderPass(trailVs, trailFs, forwardPassTags)]);
Shader.create("SpriteMask", [new ShaderPass(spriteMaskVs, spriteMaskFs, forwardPassTags)]);
Shader.create("Sprite", [new ShaderPass(spriteVs, spriteFs, forwardPassTags)]);
Shader.create("background-texture", [new ShaderPass(backgroundTextureVs, backgroundTextureFs, forwardPassTags)]);
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/shaderlib/extra/trail.fs.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
varying vec4 vColor;
varying vec2 v_uv;
uniform sampler2D u_texture;
void main(void)
{
#ifdef trailTexture
vec4 textureColor = texture2D(u_texture, v_uv);
gl_FragColor = vColor * textureColor;
#else
gl_FragColor = vColor;
#endif
}
45 changes: 45 additions & 0 deletions packages/core/src/shaderlib/extra/trail.vs.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
uniform mat4 camera_ProjMat;
uniform mat4 camera_ViewMat;

uniform float u_maxLength;
uniform float u_textureScaleY;
uniform float u_textureScaleX;
uniform float u_textureDragging;

uniform vec4 u_headColor;
uniform vec4 u_tailColor;

uniform float u_currentTime;
uniform float u_trailLifeTime;

attribute vec3 a_position;
attribute vec3 a_nodeCenter;
attribute vec3 a_nodeIndexData;

varying vec2 v_uv;
varying vec4 vColor;

void main(){

float nodeIndex = a_nodeIndexData.x;
float vertexNodeIndex = a_nodeIndexData.y;
float trailBirthTime = a_nodeIndexData.z;

float normalizeTime = (u_currentTime - trailBirthTime) / u_trailLifeTime;
float s = 0.0;
float t = 0.0;
if (u_textureDragging == 1.0) {
s = normalizeTime * u_textureScaleY;
t = vertexNodeIndex * u_textureScaleX;
} else {
s = (nodeIndex / u_maxLength) * u_textureScaleY;
t = vertexNodeIndex * u_textureScaleX;
}
v_uv = vec2( s, t );
vec4 realPosition = vec4( ( 1.0 - normalizeTime ) * a_position.xyz + normalizeTime * a_position.xyz, 1.0 );
gl_Position = camera_ProjMat * camera_ViewMat * realPosition;

if (normalizeTime < 1.0){
vColor = ( 1.0 - normalizeTime ) * u_headColor + normalizeTime * u_tailColor;
}
}
25 changes: 9 additions & 16 deletions packages/core/src/trail/TrailMaterial.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import { Engine } from "../Engine";
import { Material } from "../material/Material";
import { BlendFactor, Shader } from "../shader";
import FRAG_SHADER from "./trail.fs.glsl";
import VERT_SHADER from "./trail.vs.glsl";
import { BaseMaterial, BlendMode } from "../material";
import { CullMode, Shader } from "../shader";

Shader.create("trail", VERT_SHADER, FRAG_SHADER);
export class TrailMaterial extends BaseMaterial {
constructor(engine: Engine) {
super(engine, Shader.find("trail-shader"));

export class TrailMaterial extends Material {
constructor(engine: Engine) {
super(engine, Shader.find("trail"));

const target = this.renderState.blendState.targetBlendState;
target.enabled = true;
target.sourceColorBlendFactor = target.sourceAlphaBlendFactor = BlendFactor.SourceAlpha;
target.destinationColorBlendFactor = target.destinationAlphaBlendFactor = BlendFactor.One;

this.renderState.depthState.writeEnabled = false;
}
this.isTransparent = true;
this.blendMode = BlendMode.Additive;
this.renderState.rasterState.cullMode = CullMode.Off;
}
}
Loading