/
RenderSystem.js
180 lines (147 loc) · 5.11 KB
/
RenderSystem.js
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import gl from "../gl.js";
import Fungi from "../Fungi.js";
import Camera from "../components/Camera.js";
import { System } from "../Ecs.js";
const QUERY_COM = [ "Transform", "Drawable" ];
class RenderSystem extends System{
constructor(){
super();
//Render Objects
this.frameBuffer = null;
this.material = null;
this.shader = null;
this.vao = null;
//UBOs for Updating
this.UBOModel = Fungi.getUBO("UBOModel");
this.UBOTransform = Fungi.getUBO("UBOTransform");
this.UBOWorld = Fungi.getUBO("UBOWorld");
//GL Option states
this.options = {
blend : { state : false, id : gl.ctx.BLEND },
sampleAlphaCoverage : { state : false, id : gl.ctx.SAMPLE_ALPHA_TO_COVERAGE },
depthTest : { state : true, id : gl.ctx.DEPTH_TEST },
depthMask : { state : true },
cullFace : { state : true, id : gl.ctx.CULL_FACE },
cullDir : { state : gl.ctx.BACK },
blendMode : { state : gl.BLEND_ALPHA },
}
}
update(ecs){
//............................................
//Update Main UBO
this.UBOTransform
.updateItem("projViewMatrix", Camera.getProjectionViewMatrix( Fungi.camera.com.Camera ) )
.updateItem("cameraPos", Fungi.camera.com.Transform.position )
.updateItem("globalTime", Fungi.sinceStart )
.updateGL();
//............................................
let d, e, ary = ecs.queryEntities( QUERY_COM );
gl.clear(); //Clear Frame Buffer
//Draw all active Entities
for( e of ary ){
//......................................
if(!e.active) continue;
d = e.com.Drawable;
//......................................
if(! d.draw){ //Use standard drawing
// Check if there is anything to render
if(!d.vao || d.vao.elmCount == 0) continue; //console.log("VAO has no index/vertices or null : ", e.name);
//Load and Draw
this.loadMaterial( d.material );
this.loadEntity( e );
this.draw( d );
}else d.draw( this, e, d ); //Component has custom drawing instructions
}
}
//===============================================================
// LOADERS
//Load up a shader
loadShader(s){
if(this.shader === s) return;
this.shader = s;
gl.ctx.useProgram(s.program);
return this;
}
//Load Material and its shader
loadMaterial(mat){
//...............................
//If material is the same, exit.
if(this.material === mat) return;
this.material = mat;
//...............................
//Is the shader for the material different
if(this.shader !== mat.shader){
this.shader = mat.shader;
gl.ctx.useProgram(this.shader.program);
}
//...............................
mat.applyUniforms(); //Push any saved uniform values to shader.
this.loadOptions(mat.options); //Enabled/Disable GL Options
return this;
}
loadOptions(aryOption){
var k, v;
for(k in aryOption){
v = aryOption[k];
if(this.options[k] && this.options[k].state != v){
this.options[k].state = v;
switch(k){
case "blendMode": gl.blendMode( v ); break;
case "depthMask": gl.ctx.depthMask( v ); break;
case "cullDir": gl.ctx.cullFace( v ); break;
default:
gl.ctx[ (this.options[k].state)? "enable" : "disable" ]( this.options[k].id );
break;
}
}
}
return this;
}
loadEntity(e){ //console.log("Load Entity ", e.name);
//..........................................
//Handle UBOModel Data
let uboChanged = false;
if( this.shader.options.modelMatrix ){
this.UBOModel.updateItem("modelMatrix", e.com.Transform.modelMatrix);
//TODO, Experimenting with World Space Transform without Matrices
var comTransform = e.com.TransformNode || e.com.Transform;
this.UBOWorld.updateItem("rotation", comTransform.rotation);
this.UBOWorld.updateItem("position", comTransform.position);
this.UBOWorld.updateItem("scale", comTransform.scale);
uboChanged = true;
}
if( this.shader.options.normalMatrix ){
console.log("Need to implement handling Normal Matrix");
//this.UBOModel.updateItem("normalMatrix", e.com.Transform.localMatrix);
//uboChanged = true;
}
if( uboChanged ){
this.UBOModel.updateGL();
this.UBOWorld.updateGL(); //TODO: Testing
}
//..........................................
this.loadOptions( e.com.Drawable.options );
}
//===============================================================
// DRAWING
draw(d){
//...............................
if(this.vao !== d.vao){
this.vao = d.vao;
gl.ctx.bindVertexArray(d.vao.id);
//console.log("Draw", r.entityPtr.name, r.vao.elmCount);
}
//...............................
if(!d.vao.isInstanced){
if(d.vao.isIndexed) gl.ctx.drawElements(d.drawMode, d.vao.elmCount, gl.ctx.UNSIGNED_SHORT, 0);
else gl.ctx.drawArrays(d.drawMode, 0, d.vao.elmCount);
}else{
if(d.vao.isIndexed) gl.ctx.drawElementsInstanced(d.drawMode, d.vao.elmCount, gl.ctx.UNSIGNED_SHORT, 0, d.vao.instanceCount);
else gl.ctx.drawArraysInstanced(d.drawMode, 0, d.vao.elmCount, d.vao.instanceCount);
}
//...............................
//gl.ctx.bindVertexArray(null);
return this;
}
}
export default RenderSystem;