forked from galacean/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshaderLab-mrt.ts
111 lines (85 loc) · 3.21 KB
/
shaderLab-mrt.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* @title ShaderLab MRT
* @category Material
*/
import { Camera, Color, Logger, Material, MeshRenderer, PrimitiveMesh, Shader, WebGLEngine } from "@galacean/engine";
import { ShaderLab } from "@galacean/engine-shader-lab";
import { initScreenshot, updateForE2E } from "./.mockForE2E";
const shaderLab = new ShaderLab();
const shaderSource = `Shader "/custom.gs" {
SubShader "Default" {
UsePass "pbr/Default/ShadowCaster"
Pass "Pass0" {
struct Attributes {
vec3 POSITION;
vec2 TEXCOORD_0;
vec4 JOINTS_0;
vec4 WEIGHTS_0;
};
struct Varyings {
vec2 uv;
};
mat4 renderer_MVPMat;
vec4 material_BaseColor;
VertexShader = vert;
FragmentShader = frag;
Varyings vert(Attributes attr) {
Varyings v;
vec4 position = vec4(attr.POSITION, 1.0);
// Skin
#ifdef RENDERER_HAS_SKIN
mat4 skinMatrix = getSkinMatrix(attr);
position = skinMatrix * position;
#endif
gl_Position = renderer_MVPMat * position;
v.uv = attr.TEXCOORD_0;
return v;
}
struct mrt {
layout(location = 0) vec4 fragColor0;
layout(location = 1) vec4 fragColor1;
};
mrt frag(Varyings v) {
mrt o;
vec4 baseColor = material_BaseColor;
#ifdef MATERIAL_HAS_BASETEXTURE
vec4 textureColor = texture2D(material_BaseTexture, v.uv);
#ifndef ENGINE_IS_COLORSPACE_GAMMA
textureColor = gammaToLinear(textureColor);
#endif
baseColor *= textureColor;
#endif
#ifdef MATERIAL_IS_ALPHA_CUTOFF
if( baseColor.a < material_AlphaCutoff ) {
discard;
}
#endif
o.fragColor0 = baseColor;
o.fragColor1 = baseColor;
}
}
}
}`;
Logger.enable();
WebGLEngine.create({ canvas: "canvas", shaderLab }).then((engine) => {
engine.canvas.resizeByClientSize();
const shader = Shader.create(shaderSource);
const scene = engine.sceneManager.activeScene;
const rootEntity = scene.createRootEntity();
// camera
const cameraEntity = rootEntity.createChild("cameraNode");
cameraEntity.transform.setPosition(0, 0, 5);
const camera = cameraEntity.addComponent(Camera);
// sphere
{
const sphere = rootEntity.createChild("sphere");
sphere.transform.position.x = -1;
const renderer = sphere.addComponent(MeshRenderer);
renderer.mesh = PrimitiveMesh.createSphere(engine);
const material = new Material(engine, shader);
material.shaderData.setColor("material_BaseColor", new Color(1, 0, 0, 0.2));
renderer.setMaterial(material);
}
updateForE2E(engine);
initScreenshot(engine, camera);
});