Repository navigation
Expand file tree
/
Copy pathshader.js
More file actions
79 lines (72 loc) · 2.63 KB
/
Copy pathshader.js
File metadata and controls
79 lines (72 loc) · 2.63 KB
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
/**
* @class
* @name Shader
* @classdesc A shader is a program that is responsible for rendering graphical primitives on a device's
* graphics processor. The shader is generated from a shader definition. This shader definition specifies
* the code for processing vertices and fragments processed by the GPU. The language of the code is GLSL
* (or more specifically ESSL, the OpenGL ES Shading Language). The shader definition also describes how
* the PlayCanvas engine should map vertex buffer elements onto the attributes specified in the vertex
* shader code.
* @description Creates a new shader object.
* @param {GraphicsDevice} graphicsDevice - The graphics device used to manage this shader.
* @param {object} definition - The shader definition from which to build the shader.
* @param {object} definition.attributes - Object detailing the mapping of vertex shader attribute names
* to semantics SEMANTIC_*. This enables the engine to match vertex buffer data as inputs to the
* shader.
* @param {string} definition.vshader - Vertex shader source (GLSL code).
* @param {string} definition.fshader - Fragment shader source (GLSL code).
* @param {boolean} [definition.useTransformFeedback] - Specifies that this shader outputs post-VS data to a buffer.
* @example
* // Create a shader that renders primitives with a solid red color
* var shaderDefinition = {
* attributes: {
* aPosition: pc.SEMANTIC_POSITION
* },
* vshader: [
* "attribute vec3 aPosition;",
* "",
* "void main(void)",
* "{",
* " gl_Position = vec4(aPosition, 1.0);",
* "}"
* ].join("\n"),
* fshader: [
* "precision " + graphicsDevice.precision + " float;",
* "",
* "void main(void)",
* "{",
* " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);",
* "}"
* ].join("\n")
* };
*
* var shader = new pc.Shader(graphicsDevice, shaderDefinition);
*/
class Shader {
constructor(graphicsDevice, definition) {
this.device = graphicsDevice;
this.definition = definition;
this.init();
this.device.createShader(this);
}
init() {
this.attributes = [];
this.uniforms = [];
this.samplers = [];
this.ready = false;
this.failed = false;
}
/**
* @function
* @name Shader#destroy
* @description Frees resources associated with this shader.
*/
destroy() {
this.device.destroyShader(this);
}
// called when context was lost, function releases all context related resources
loseContext() {
this.init();
}
}
export { Shader };