forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost-effect.js
More file actions
111 lines (99 loc) · 3.93 KB
/
post-effect.js
File metadata and controls
111 lines (99 loc) · 3.93 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
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
pc.extend(pc, function () {
// Primitive for drawFullscreenQuad
var primitive = {
type: pc.PRIMITIVE_TRISTRIP,
base: 0,
count: 4,
indexed: false
};
/**
* @name pc.PostEffect
* @class Base class for all post effects. Post effects take a a render target as input
* apply effects to it and then render the result to an output render target or the screen
* if no output is specified.
* @description Creates new PostEffect
* @param {pc.GraphicsDevice} graphicsDevice The graphics device of the application
*/
var PostEffect = function (graphicsDevice) {
this.device = graphicsDevice;
this.shader = null;
this.depthMap = null;
this.vertexBuffer = pc.createFullscreenQuad(graphicsDevice);
this.needsDepthBuffer = false;
};
PostEffect.prototype = {
/**
* @function
* @name pc.PostEffect#render
* @description Render the post effect using the specified inputTarget
* to the specified outputTarget.
* @param {pc.RenderTarget} inputTarget The input render target
* @param {pc.RenderTarget} outputTarget The output render target. If null then this will be the screen.
* @param {pc.Vec4} rect (Optional) The rect of the current camera. If not specified then it will default to [0,0,1,1]
*/
render: function (inputTarget, outputTarget, rect) {
}
};
function createFullscreenQuad (device) {
// Create the vertex format
var vertexFormat = new pc.VertexFormat(device, [
{ semantic: pc.SEMANTIC_POSITION, components: 2, type: pc.TYPE_FLOAT32 }
]);
// Create a vertex buffer
var vertexBuffer = new pc.VertexBuffer(device, vertexFormat, 4);
// Fill the vertex buffer
var iterator = new pc.VertexIterator(vertexBuffer);
iterator.element[pc.SEMANTIC_POSITION].set(-1.0, -1.0);
iterator.next();
iterator.element[pc.SEMANTIC_POSITION].set(1.0, -1.0);
iterator.next();
iterator.element[pc.SEMANTIC_POSITION].set(-1.0, 1.0);
iterator.next();
iterator.element[pc.SEMANTIC_POSITION].set(1.0, 1.0);
iterator.end();
return vertexBuffer;
}
function drawFullscreenQuad (device, target, vertexBuffer, shader, rect) {
device.setRenderTarget(target);
device.updateBegin();
var w = (target !== null) ? target.width : device.width;
var h = (target !== null) ? target.height : device.height;
var x = 0;
var y = 0;
if (rect) {
x = rect.x * w;
y = rect.y * h;
w *= rect.z;
h *= rect.w;
}
device.setViewport(x, y, w, h);
device.setScissor(x, y, w, h);
var oldBlending = device.getBlending();
var oldDepthTest = device.getDepthTest();
var oldDepthWrite = device.getDepthWrite();
var oldCullMode = device.getCullMode();
var oldWR = device.writeRed;
var oldWG = device.writeGreen;
var oldWB = device.writeBlue;
var oldWA = device.writeAlpha;
device.setBlending(false);
device.setDepthTest(false);
device.setDepthWrite(false);
device.setCullMode(pc.CULLFACE_BACK);
device.setColorWrite(true, true, true, true);
device.setVertexBuffer(vertexBuffer, 0);
device.setShader(shader);
device.draw(primitive);
device.setBlending(oldBlending);
device.setDepthTest(oldDepthTest);
device.setDepthWrite(oldDepthWrite);
device.setCullMode(oldCullMode);
device.setColorWrite(oldWR, oldWG, oldWB, oldWA);
device.updateEnd();
}
return {
PostEffect: PostEffect,
createFullscreenQuad: createFullscreenQuad,
drawFullscreenQuad: drawFullscreenQuad
};
}());