/
glge_object.js
1305 lines (1171 loc) · 42.7 KB
/
glge_object.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
GLGE WebGL Graphics Engine
Copyright (c) 2010, Paul Brunt
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of GLGE nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL PAUL BRUNT BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @fileOverview
* @name glge_object.js
* @author me@paulbrunt.co.uk
*/
(function(GLGE){
/**
* @name GLGE.Object#downloadComplete
* @event fires when all the assets for this class have finished loading
* @param {object} data
*/
/**
* @name GLGE.Object#willRender
* @event fires when all the assets will be rendered
* @param {object} data
*/
/**
* @name GLGE.Object#willRender
* @event fires when all the assets will culled
* @param {object} data
*/
/**
* @class An object that can be rendered in a scene
* @augments GLGE.Animatable
* @augments GLGE.Placeable
* @augments GLGE.QuickNotation
* @augments GLGE.JSONLoader
*/
GLGE.Object=function(uid){
this.multimaterials=[];
this.renderCaches=[];
var that=this;
this.downloadComplete=function(){
if(that.isComplete()) that.fireEvent("downloadComplete");
}
GLGE.Assets.registerAsset(this,uid);
}
GLGE.augment(GLGE.Placeable,GLGE.Object);
GLGE.augment(GLGE.Animatable,GLGE.Object);
GLGE.augment(GLGE.QuickNotation,GLGE.Object);
GLGE.augment(GLGE.JSONLoader,GLGE.Object);
GLGE.Object.prototype.className="Object";
GLGE.Object.prototype.mesh=null;
GLGE.Object.prototype.skeleton=null;
GLGE.Object.prototype.scene=null;
GLGE.Object.prototype.transformMatrix=GLGE.identMatrix();
GLGE.Object.prototype.material=null;
GLGE.Object.prototype.gl=null;
GLGE.Object.prototype.multimaterials=null;
GLGE.Object.prototype.zTrans=false;
GLGE.Object.prototype.renderCaches=null;
GLGE.Object.prototype.id="";
GLGE.Object.prototype.pickable=true;
GLGE.Object.prototype.drawType=GLGE.DRAW_TRIS;
GLGE.Object.prototype.pointSize=1;
GLGE.Object.prototype.lineWidth=1;
GLGE.Object.prototype.cull=true;
GLGE.Object.prototype.culled=true;
GLGE.Object.prototype.visible=true;
GLGE.Object.prototype.depthTest=true;
GLGE.Object.prototype.meshFrame1=0;
GLGE.Object.prototype.meshFrame2=0;
GLGE.Object.prototype.meshBlendFactor=0;
GLGE.Object.prototype.noCastShadows=null;
GLGE.Object.prototype.noDepthMask=false;
GLGE.Object.prototype.shadowAlpha=true;
GLGE.Object.prototype.blending=[ "SRC_ALPHA","ONE_MINUS_SRC_ALPHA"];
//shadow fragment
var shfragStr=[];
shfragStr.push("#ifdef GL_ES\nprecision highp float;\n#endif\n");
shfragStr.push("uniform float distance;\n");
shfragStr.push("uniform bool shadowtype;\n");
shfragStr.push("varying vec3 eyevec;\n");
shfragStr.push("void main(void)\n ");
shfragStr.push("{\n");
shfragStr.push("float depth = gl_FragCoord.z;\n");
shfragStr.push("if(shadowtype) depth=length(eyevec)/distance;\n");
shfragStr.push("vec4 rgba=fract(depth * vec4(16777216.0, 65536.0, 256.0, 1.0));\n");
shfragStr.push("gl_FragColor=rgba-rgba.rrgb*vec4(0.0,0.00390625,0.00390625,0.00390625);\n");
shfragStr.push("}\n");
GLGE.Object.prototype.shfragStr=shfragStr.join("");
//normal fragment
var nfragStr=[];
nfragStr.push("#ifdef GL_ES\nprecision highp float;\n#endif\n");
nfragStr.push("varying vec3 n;\n");
nfragStr.push("void main(void)\n");
nfragStr.push("{\n");
nfragStr.push("float depth = gl_FragCoord.z / gl_FragCoord.w;\n");
nfragStr.push("gl_FragColor=vec4(normalize(n)/2.0+0.5,depth/1000.0);\n");
nfragStr.push("}\n");
GLGE.Object.prototype.nfragStr=nfragStr.join("");
//picking fragment
var pkfragStr=[];
pkfragStr.push("#ifdef GL_ES\nprecision highp float;\n#endif\n");
pkfragStr.push("uniform float far;\n");
pkfragStr.push("uniform vec3 pickcolor;\n");
pkfragStr.push("varying vec3 n;\n");
pkfragStr.push("varying vec4 UVCoord;\n");
pkfragStr.push("void main(void)\n");
pkfragStr.push("{\n");
pkfragStr.push("float Xcoord = gl_FragCoord.x+0.5;\n");
pkfragStr.push("if(Xcoord>0.0) gl_FragColor = vec4(pickcolor,1.0);\n");
pkfragStr.push("if(Xcoord>1.0) gl_FragColor = vec4(n,1.0);\n");
pkfragStr.push("if(Xcoord>2.0){");
pkfragStr.push("vec3 rgb=fract((gl_FragCoord.z/gl_FragCoord.w) * vec3(65536.0, 256.0, 1.0));\n");
pkfragStr.push("gl_FragColor=vec4(rgb-rgb.rrg*vec3(0.0,0.00390625,0.00390625),1.0);\n");
pkfragStr.push("}");
//x tex coord
pkfragStr.push("if(Xcoord>3.0){");
pkfragStr.push("vec3 rgb=fract(UVCoord.x * vec3(65536.0, 256.0, 1.0));\n");
pkfragStr.push("gl_FragColor=vec4(rgb-rgb.rrg*vec3(0.0,0.00390625,0.00390625),1.0);\n");
pkfragStr.push("}");
//y tex coord
pkfragStr.push("if(Xcoord>4.0){");
pkfragStr.push("vec3 rgb=fract(UVCoord.y * vec3(65536.0, 256.0, 1.0));\n");
pkfragStr.push("gl_FragColor=vec4(rgb-rgb.rrg*vec3(0.0,0.00390625,0.00390625),1.0);\n");
pkfragStr.push("}");
pkfragStr.push("}\n");
GLGE.Object.prototype.pkfragStr=pkfragStr.join("");
GLGE.Object.prototype.noDepthMask
/**
* Sets the depth mask for the object default is true
* @param {boolean} mask flag to depth masking
*/
GLGE.Object.prototype.setDepthMask=function(mask){
this.noDepthMask=!mask;
return this;
}
/**
* Gets the objects depth mask flag
* @returns flag to indicate the depth mask
*/
GLGE.Object.prototype.getDepthMask=function(){
return !this.noDepthMask;
}
/**
* Sets the object visibility
* @param {boolean} visable flag to indicate the objects visibility
*/
GLGE.Object.prototype.setVisible=function(visible){
this.visible=visible;
return this;
}
/**
* Gets the object visibility
* @returns flag to indicate the objects visibility
*/
GLGE.Object.prototype.getVisible=function(){
return this.visible;
}
/**
* Sets the object blending mode
* @param {array} gl blending funcs as strings, eg. [ "ONE", "ONE"]
*/
GLGE.Object.prototype.setBlending=function(blending){
this.blending=blending;
return this;
}
/**
* Gets the object blending mode
* @returns gl blending funcs
*/
GLGE.Object.prototype.getBlending=function(){
return this.blending;
}
/**
* Sets the first mesh frame to use when using an animated mesh
* @param {boolean} frame the inital frame
*/
GLGE.Object.prototype.setMeshFrame1=function(frame){
this.meshFrame1=frame;
return this;
}
/**
* Sets the second mesh frame to use when using an animated mesh
* @param {boolean} frame the final frame
*/
GLGE.Object.prototype.setMeshFrame2=function(frame){
this.meshFrame2=frame;
return this;
}
/**
* blending between frames
* @param {boolean} frame value 0-1 morth between frame1 and frame2
*/
GLGE.Object.prototype.setMeshBlendFactor=function(factor){
this.meshBlendFactor=factor;
return this;
}
/**
* Gets blending between frames
* @returns blender factor
*/
GLGE.Object.prototype.getMeshBlendFactor=function(){
return this.meshBlendFactor;
}
/**
* Gets the pickable flag for the object
*/
GLGE.Object.prototype.getPickable=function(){
return this.pickable;
}
/**
* Sets the pickable flag for the object
* @param {boolean} value the culling flag
*/
GLGE.Object.prototype.setPickable=function(pickable){
this.pickable=pickable;
return this;
}
/**
* Gets the depth test flag for the object
*/
GLGE.Object.prototype.getDepthTest=function(){
return this.depthTest;
}
/**
* Sets the depth test flag for the object
* @param {boolean} value the culling flag
*/
GLGE.Object.prototype.setDepthTest=function(test){
this.depthTest=test;
return this;
}
/**
* Gets the culling flag for the object
*/
GLGE.Object.prototype.getCull=function(){
return this.cull;
}
/**
* Sets the culling flag for the object
* @param {boolean} value the culling flag
*/
GLGE.Object.prototype.setCull=function(cull){
this.cull=cull;
return this;
}
/**
* Gets the objects draw type
*/
GLGE.Object.prototype.getDrawType=function(){
return this.drawType;
}
/**
* Sets the objects draw type
* @param {GLGE.number} value the draw type of this object
*/
GLGE.Object.prototype.setDrawType=function(value){
this.drawType=value;
return this;
}
/**
* Gets the objects draw point size
*/
GLGE.Object.prototype.getPointSize=function(){
return this.pointSize;
}
/**
* Sets the objects draw points size
* @param {GLGE.number} value the point size to render
*/
GLGE.Object.prototype.setPointSize=function(value){
this.pointSize=parseFloat(value);
return this;
}
/**
* Gets the objects line width
*/
GLGE.Object.prototype.getLineWidth=function(){
return this.lineWidth;
}
/**
* Sets the objects line width
* @param {GLGE.number} value the line width
*/
GLGE.Object.prototype.setLineWidth=function(value){
this.lineWidth=parseFloat(value);
return this;
}
/**
* Sets a custom usinform on this object
* @param {string} type the uniform type eg 1i, 3fv, Matrix4fv, etc
* @param {string} name the uniform name
* @param {array} value the value of the uniform
*/
GLGE.Object.prototype.setUniform=function(type,name,value){
if(!this.uniforms) this.uniforms={};
this.uniforms[name]={type:type,value:value};
}
/**
* Gets the value of a custom uniform
* @param {string} name the name of the uniform to return
* @returns {number} the value of the uniform
*/
GLGE.Object.prototype.getUniform=function(name){
if(!this.uniforms) this.uniforms={};
return this.uniforms[name].value
}
/**
* Gets the type of a custom uniform
* @param {string} name the name of the uniform to return
* @returns {number} the type of the uniform
*/
GLGE.Object.prototype.getUniformType=function(name){
if(!this.uniforms) this.uniforms={};
return this.uniforms[name].type;
}
/**
* Sets the code to inject into the vertex shader
* @param {string} shader the glsl code to inject into the vertex shader of this object GLGE will call the function GLGE_Position(vec4 position) to modify the position
*/
GLGE.Object.prototype.setVertexShaderInjection=function(shader){
this.shaderVertexInjection=shader;
this.updateProgram();
return this;
}
/**
* Gets the glsl code injected into the vertex shader of this object
* @returns {string} shader the glsl code injected into the vertex shader of this object
*/
GLGE.Object.prototype.getVertexShaderInjection=function(shader){
return this.shaderVertexInjection;
}
/**
* Gets the objects skeleton
* @returns GLGE.Group
*/
GLGE.Object.prototype.getSkeleton=function(){
return this.skeleton;
}
/**
* Sets the objects skeleton
* @param {GLGE.Group} value the skeleton group to set
*/
GLGE.Object.prototype.setSkeleton=function(value){
this.skeleton=value;
this.bones=null;
return this;
}
GLGE.Object.prototype.getBoundingVolume=function(local){
if(!local) local=0;
if(!this.boundingVolume) this.boundingVolume=[];
if(!this.boundmatrix) this.boundmatrix=[];
var matrix=this.getModelMatrix();
if(matrix!=this.boundmatrix[local] || !this.boundingVolume[local]){
var multimaterials=this.multimaterials;
var boundingVolume;
for(var i=0;i<multimaterials.length;i++){
if(multimaterials[i].lods[0].mesh){
if(!boundingVolume){
boundingVolume=multimaterials[i].lods[0].mesh.getBoundingVolume().clone();
}else{
boundingVolume.addBoundingVolume(multimaterials[i].lods[0].mesh.getBoundingVolume());
}
}
}
if(!boundingVolume) boundingVolume=new GLGE.BoundingVolume(0,0,0,0,0,0);
if(local){
boundingVolume.applyMatrix(this.getLocalMatrix());
}else{
boundingVolume.applyMatrix(this.getModelMatrix());
}
this.boundingVolume[local]=boundingVolume;
}
this.boundmatrix[local]=matrix;
return this.boundingVolume[local];
}
/**
* Sets the the show casting flag
* @param {boolean} value cast or not
*/
GLGE.Object.prototype.setCastShadows=function(value){
this.noCastShadows=!value;
return this;
}
/**
* Gets the the show casting flag
* @returns boolean
*/
GLGE.Object.prototype.getCastShadows=function(){
return !this.noCastShadows;
}
/**
* Sets the Z Transparency of this object
* @param {boolean} value Does this object need blending?
*/
GLGE.Object.prototype.setZtransparent=function(value){
this.zTrans=value;
return this;
}
/**
* Gets the z transparency
* @returns boolean
*/
GLGE.Object.prototype.isZtransparent=function(){
return this.zTrans;
}
/**
* Checks if resources have finished downloading
* @returns {boolean}
*/
GLGE.Object.prototype.isComplete=function(){
for(var i=0;i<this.multimaterials.length;i++){
if(!this.multimaterials[i].isComplete()) return false;
}
return true;
}
/**
* Sets the material associated with the object
* @param GLGE.Material
*/
GLGE.Object.prototype.setMaterial=function(material,idx){
if(typeof material=="string") material=GLGE.Assets.get(material);
if(!idx) idx=0;
if(!this.multimaterials[idx]){
this.multimaterials[idx]=new GLGE.MultiMaterial();
this.multimaterials[idx].addEventListener("downloadComplete",this.downloadComplete);
}
if(this.multimaterials[idx].getMaterial()!=material){
this.multimaterials[idx].setMaterial(material);
this.updateProgram();
}
return this;
}
/**
* Gets the material associated with the object
* @returns GLGE.Material
*/
GLGE.Object.prototype.getMaterial=function(idx){
if(!idx) idx=0;
if(this.multimaterials[idx]) {
return this.multimaterials[idx].getMaterial();
}else{
return false;
}
}
/**
* Sets the mesh associated with the object
* @param GLGE.Mesh
*/
GLGE.Object.prototype.setMesh=function(mesh,idx){
if(typeof mesh=="string") mesh=GLGE.Assets.get(mesh);
if(!idx) idx=0;
if(!this.multimaterials[idx]){
var object=this;
this.multimaterials[idx]=new GLGE.MultiMaterial();
this.multimaterials[idx].addEventListener("downloadComplete",this.downloadComplete);
this.multimaterials[idx].addEventListener("boundupdate",function(){object.boundingVolume=null});
}
this.multimaterials[idx].setMesh(mesh);
this.boundingVolume=null;
return this;
}
/**
* Gets the mesh associated with the object
* @returns GLGE.Mesh
*/
GLGE.Object.prototype.getMesh=function(idx){
if(!idx) idx=0;
if(this.multimaterials[idx]) {
return this.multimaterials[idx].getMesh();
}else{
return false;
}
}
/**
* Initiallize all the GL stuff needed to render to screen
* @private
*/
GLGE.Object.prototype.GLInit=function(gl){
this.gl=gl;
}
/**
* Cleans up all the GL stuff we sets
* @private
*/
GLGE.Object.prototype.GLDestory=function(gl){
}
/**
* Updates the GL shader program for the object
* @private
*/
GLGE.Object.prototype.updateProgram=function(){
for(var i=0; i<this.multimaterials.length;i++){
this.multimaterials[i].updateProgram();
}
}
/**
* Adds another material to this object
* @returns GLGE.Material
*/
GLGE.Object.prototype.addMultiMaterial=function(multimaterial){
if(typeof multimaterial=="string") multimaterial=GLGE.Assets.get(multimaterial);
this.multimaterials.push(multimaterial);
multimaterial.addEventListener("downloadComplete",this.downloadComplete);
var object=this;
multimaterial.addEventListener("boundupdate",function(){object.boundingVolume=null});
this.boundingVolume=null;
return this;
}
/**
* gets all of the objects materials and meshes
* @returns array of GLGE.MultiMaterial objects
*/
GLGE.Object.prototype.getMultiMaterials=function(){
return this.multimaterials;
}
/**
* Creates the shader program for the object
* @private
*/
GLGE.Object.prototype.GLGenerateShader=function(gl){
//create the programs strings
//Vertex Shader
var colors=UV=joints1=joints2=false;
var lights=gl.lights;
var vertexStr=["#ifdef GL_ES\nprecision mediump float;\n#endif\n#define GLGE_VERTEX\n"];
var tangent=false;
if(!this.mesh.normals) this.mesh.calcNormals();
vertexStr.push("attribute vec3 position;\n");
vertexStr.push("attribute vec3 normal;\n");
for(var i=0;i<this.mesh.buffers.length;i++){
if(this.mesh.buffers[i].name=="tangent0") tangent=true;
if(this.mesh.buffers[i].exclude) continue;
if(this.mesh.buffers[i].size>1){
vertexStr.push("attribute vec"+this.mesh.buffers[i].size+" "+this.mesh.buffers[i].name+";\n");
}else{
vertexStr.push("attribute float "+this.mesh.buffers[i].name+";\n");
}
if(this.mesh.buffers[i].name=="UV") UV=true;
if(this.mesh.buffers[i].name=="color") colors=true;
if(this.mesh.buffers[i].name=="joints1") joints1=this.mesh.buffers[i];
if(this.mesh.buffers[i].name=="joints2") joints2=this.mesh.buffers[i];
}
if(this.mesh.framePositions.length>1){
var morph=true;
vertexStr.push("attribute vec3 position2;\n");
vertexStr.push("attribute vec3 normal2;\n");
vertexStr.push("uniform float framesBlend;\n");
if(tangent) vertexStr.push("attribute vec3 tangent2;\n");
}
if(tangent) vertexStr.push("attribute vec3 tangent;\n");
vertexStr.push("uniform mat4 worldView;\n");
vertexStr.push("uniform mat4 projection;\n");
vertexStr.push("uniform mat4 worldInverseTranspose;\n");
vertexStr.push("uniform mat4 envMat;\n");
//vertexStr.push("uniform vec3 cameraPos;\n");
vertexStr.push("uniform float cascadeLevel;\n");
for(var i=0; i<lights.length;i++){
if(lights[i].type==GLGE.L_OFF) continue;
vertexStr.push("uniform vec3 lightpos"+i+";\n");
vertexStr.push("uniform vec3 lightdir"+i+";\n");
if((lights[i].type==GLGE.L_SPOT || lights[i].type==GLGE.L_DIR) && lights[i].getCastShadows() ){
vertexStr.push("uniform mat4 lightmat"+i+";\n");
vertexStr.push("varying vec4 spotcoord"+i+";\n");
}
}
vertexStr.push("varying vec3 eyevec;\n");
for(var i=0; i<lights.length;i++){
if(lights[i].type==GLGE.L_OFF) continue;
vertexStr.push("varying vec3 lightvec"+i+";\n");
vertexStr.push("varying float lightdist"+i+";\n");
}
if(this.mesh.joints && this.mesh.joints.length>0){
vertexStr.push("uniform vec4 jointMat["+(3*this.mesh.joints.length)+"];\n");
}
if(this.material) vertexStr.push(this.material.getVertexVarying(vertexStr));
vertexStr.push("varying vec3 n;\n");
vertexStr.push("varying vec3 t;\n");
if(colors) vertexStr.push("varying vec4 vcolor;\n");
vertexStr.push("varying vec4 UVCoord;\n");
vertexStr.push("varying vec3 OBJCoord;\n");
if(this.shaderVertexInjection){
vertexStr.push(this.shaderVertexInjection);
}
vertexStr.push("void main(void)\n");
vertexStr.push("{\n");
if(colors) vertexStr.push("vcolor=color;\n");
if(UV) vertexStr.push("UVCoord=UV;\n");
else vertexStr.push("UVCoord=vec4(0.0,0.0,0.0,0.0);\n");
vertexStr.push("OBJCoord = position;\n");
vertexStr.push("vec3 tang;\n");
vertexStr.push("vec4 pos = vec4(0.0, 0.0, 0.0, 1.0);\n");
vertexStr.push("vec4 norm = vec4(0.0, 0.0, 0.0, 1.0);\n");
if(tangent) vertexStr.push("vec4 tang4 = vec4(0.0, 0.0, 0.0, 1.0);\n");
if(joints1){
if(joints1.size==1){
vertexStr.push("pos += vec4(dot(jointMat[int(3.0*joints1)],vec4(position,1.0)),\n"+
" dot(jointMat[int(3.0*joints1+1.0)],vec4(position,1.0)),\n"+
" dot(jointMat[int(3.0*joints1+2.0)],vec4(position,1.0)),1.0)*weights1;\n");
vertexStr.push("norm += vec4(dot(jointMat[int(3.0*joints1)].xyz,normal),\n"+
" dot(jointMat[int(3.0*joints1+1.0)].xyz,normal),\n"+
" dot(jointMat[int(3.0*joints1+2.0)].xyz,normal),1.0)*weights1;\n");
if (tangent)
vertexStr.push("tang4 += vec4(dot(jointMat[int(3.0*joints1)].xyz,tangent),\n"+
" dot(jointMat[int(3.0*joints1+1.0)].xyz,tangent),\n"+
" dot(jointMat[int(3.0*joints1+2.0)].xyz,tangent),1.0)*weights1;\n");
}else{
for(var i=0;i<joints1.size;i++){
vertexStr.push("pos += vec4(dot(jointMat[int(3.0*joints1["+i+"])],vec4(position,1.0)),\n"+
" dot(jointMat[int(3.0*joints1["+i+"]+1.0)],vec4(position,1.0)),\n"+
" dot(jointMat[int(3.0*joints1["+i+"]+2.0)],vec4(position,1.0)),1.0)*weights1["+i+"];\n");
vertexStr.push("norm += vec4(dot(jointMat[int(3.0*joints1["+i+"])].xyz,normal),\n"+
" dot(jointMat[int(3.0*joints1["+i+"]+1.0)].xyz,normal),\n"+
" dot(jointMat[int(3.0*joints1["+i+"]+2.0)].xyz,normal),1.0)*weights1["+i+"];\n");
if (tangent)
vertexStr.push("tang4 += vec4(dot(jointMat[int(3.0*joints1["+i+"])].xyz,tangent),\n"+
" dot(jointMat[int(3.0*joints1["+i+"]+1.0)].xyz,tangent),\n"+
" dot(jointMat[int(3.0*joints1["+i+"]+2.0)].xyz,tangent),1.0)*weights1["+i+"];\n");
}
}
if(joints2){
if(joints2.size==1){
vertexStr.push("pos += vec4(dot(jointMat[int(3.0*joints2)],vec4(position,1.0)),\n"+
" dot(jointMat[int(3.0*joints2+1.0)],vec4(position,1.0)),\n"+
" dot(jointMat[int(3.0*joints2+2.0)],vec4(position,1.0)),1.0)*weights2;\n");
vertexStr.push("norm += vec4(dot(jointMat[int(3.0*joints2)].xyz,normal),\n"+
" dot(jointMat[int(3.0*joints2+1.0)].xyz,normal),\n"+
" dot(jointMat[int(3.0*joints2+2.0)].xyz,normal),1.0)*weights2;\n");
if (tangent)
vertexStr.push("tang4 += vec4(dot(jointMat[int(3.0*joints2)].xyz,tangent),\n"+
" dot(jointMat[int(3.0*joints2+1.0)].xyz,tangent),\n"+
" dot(jointMat[int(3.0*joints2+2.0)].xyz,tangent),1.0)*weights2;\n");
}else{
for(var i=0;i<joints2.size;i++){
vertexStr.push("pos += vec4(dot(jointMat[int(3.0*joints2["+i+"])],vec4(position,1.0)),\n"+
" dot(jointMat[int(3.0*joints2["+i+"]+1.0)],vec4(position,1.0)),\n"+
" dot(jointMat[int(3.0*joints2["+i+"]+2.0)],vec4(position,1.0)),1.0)*weights2["+i+"];\n");
vertexStr.push("norm += vec4(dot(jointMat[int(3.0*joints2["+i+"])].xyz,normal),\n"+
" dot(jointMat[int(3.0*joints2["+i+"]+1.0)].xyz,normal),\n"+
" dot(jointMat[int(3.0*joints2["+i+"]+2.0)].xyz,normal),1.0)*weights2["+i+"];\n");
if (tangent)
vertexStr.push("tang4 += vec4(dot(jointMat[int(3.0*joints2["+i+"])].xyz,tangent),\n"+
" dot(jointMat[int(3.0*joints2["+i+"]+1.0)].xyz,tangent),\n"+
" dot(jointMat[int(3.0*joints2["+i+"]+2.0)].xyz,tangent),1.0)*weights2["+i+"];\n");
}
}
}
for(var i=0; i<lights.length;i++){
if(lights[i].type==GLGE.L_OFF) continue;
if((lights[i].type==GLGE.L_SPOT || lights[i].type==GLGE.L_DIR) && lights[i].getCastShadows() ){
vertexStr.push("spotcoord"+i+"=lightmat"+i+"*vec4(pos.xyz,1.0);\n");
}
}
if(this.shaderVertexInjection && this.shaderVertexInjection.indexOf("GLGE_Position")>-1){
vertexStr.push("pos=GLGE_Position(vec4(pos.xyz, 1.0));\n");
}
vertexStr.push("pos = worldView * vec4(pos.xyz, 1.0);\n");
vertexStr.push("norm = worldInverseTranspose * vec4(norm.xyz, 1.0);\n");
if(tangent) vertexStr.push("tang = (worldInverseTranspose*vec4(tang4.xyz,1.0)).xyz;\n");
}else{
if(morph){
vertexStr.push("vec4 pos4=vec4(mix(position,position2,framesBlend),1.0);\n");
}else{
vertexStr.push("vec4 pos4=vec4(position,1.0);\n");
}
if(this.shaderVertexInjection && this.shaderVertexInjection.indexOf("GLGE_Position")>-1){
vertexStr.push("pos4=GLGE_Position(pos4);\n");
}
//vertexStr.push("pos4.xyz = (pos4.xyz-cameraPos.xyz)/(pow(length(pos4.xyz-cameraPos.xyz),0.5))+cameraPos.xyz;\n");
for(var i=0; i<lights.length;i++){
if(lights[i].type==GLGE.L_OFF) continue;
if((lights[i].type==GLGE.L_SPOT || lights[i].type==GLGE.L_DIR) && lights[i].getCastShadows() ){
vertexStr.push("spotcoord"+i+"=lightmat"+i+"*pos4;\n");
}
}
vertexStr.push("pos = worldView * pos4;\n");
if(morph){
vertexStr.push("norm = worldInverseTranspose * vec4(mix(normal,normal2,framesBlend), 1.0);\n");
if(tangent) vertexStr.push("tang = (worldInverseTranspose*vec4(mix(tangent,tangent2,framesBlend),1.0)).xyz;\n");
}else{
vertexStr.push("norm = worldInverseTranspose * vec4(normal, 1.0);\n");
if(tangent) vertexStr.push("tang = (worldInverseTranspose*vec4(tangent,1.0)).xyz;\n");
}
}
vertexStr.push("eyevec = -pos.xyz;\n");
if(tangent) vertexStr.push("t = normalize(tang);");
else vertexStr.push("t = vec3(0.0,0.0,0.0);");
vertexStr.push("n = normalize(norm.rgb);");
for(var i=0; i<lights.length;i++){
if(lights[i].type==GLGE.L_OFF) continue;
if(lights[i].getType()==GLGE.L_DIR){
vertexStr.push("lightvec"+i+" = -lightdir"+i+";\n");
}else{
vertexStr.push("lightvec"+i+" = pos.xyz-lightpos"+i+";\n");
}
vertexStr.push("lightdist"+i+" = length(lightpos"+i+".xyz-pos.xyz);\n");
}
if(this.material) vertexStr.push(this.material.getLayerCoords(this.shaderVertexInjection));
vertexStr.push("gl_Position = projection * pos;\n");
vertexStr.push("gl_PointSize="+(this.pointSize.toFixed(5))+";\n");
vertexStr.push("}\n");
vertexStr=vertexStr.join("");
//Fragment Shader
fragStr=this.material.getFragmentShader(lights,colors,this.shaderVertexInjection,false);
if(this.shadowAlpha){
shfragStr=this.material.getFragmentShader(lights,colors,this.shaderVertexInjection,true);
}else{
shfragStr=this.shfragStr;
}
this.GLFragmentShaderNormal=GLGE.getGLShader(gl,gl.FRAGMENT_SHADER,this.nfragStr);
this.GLFragmentShaderShadow=GLGE.getGLShader(gl,gl.FRAGMENT_SHADER,shfragStr);
this.GLFragmentShaderPick=GLGE.getGLShader(gl,gl.FRAGMENT_SHADER,this.pkfragStr);
this.GLFragmentShader=GLGE.getGLShader(gl,gl.FRAGMENT_SHADER,fragStr);
this.GLVertexShader=GLGE.getGLShader(gl,gl.VERTEX_SHADER,vertexStr+"//default");
this.GLVertexShaderShadow=GLGE.getGLShader(gl,gl.VERTEX_SHADER,vertexStr+"//shadow");
this.GLVertexShaderPick=GLGE.getGLShader(gl,gl.VERTEX_SHADER,vertexStr+"//pick");
this.GLVertexShaderNormal=GLGE.getGLShader(gl,gl.VERTEX_SHADER,vertexStr+"//normal");
this.GLShaderProgramPick=GLGE.getGLProgram(gl,this.GLVertexShaderPick,this.GLFragmentShaderPick);
this.GLShaderProgramNormal=GLGE.getGLProgram(gl,this.GLVertexShaderNormal,this.GLFragmentShaderNormal);
this.GLShaderProgramShadow=GLGE.getGLProgram(gl,this.GLVertexShaderShadow,this.GLFragmentShaderShadow);
this.GLShaderProgram=GLGE.getGLProgram(gl,this.GLVertexShaderShadow,this.GLFragmentShader);
//if we failed then check for fallback option
if (!gl.getProgramParameter(this.GLShaderProgram, gl.LINK_STATUS)) {
if(this.material.fallback){
this.material=this.material.fallback;
this.multimaterial.material=this.material;
this.GLGenerateShader(gl);
}
}
}
/**
* creates shader programs;
* @param multimaterial the multimaterial object to create the shader programs for
* @private
*/
GLGE.Object.prototype.createShaders=function(multimaterial){
if(this.gl){
this.mesh=multimaterial.mesh;
this.material=multimaterial.material;
this.multimaterial=multimaterial;
this.GLGenerateShader(this.gl);
multimaterial.GLShaderProgramPick=this.GLShaderProgramPick;
multimaterial.GLShaderProgramShadow=this.GLShaderProgramShadow;
multimaterial.GLShaderProgram=this.GLShaderProgram;
}
}
/**
* Sets the shader program uniforms ready for rendering
* @private
*/
GLGE.Object.prototype.GLUniforms=function(gl,renderType,pickindex){
var program;
switch(renderType){
case GLGE.RENDER_DEFAULT:
program=this.GLShaderProgram;
GLGE.setUniform(gl,"1i",GLGE.getUniformLocation(gl,program, "emitpass"), 0);
break;
case GLGE.RENDER_EMIT:
program=this.GLShaderProgram;
GLGE.setUniform(gl,"1i",GLGE.getUniformLocation(gl,program, "emitpass"), 1);
break;
case GLGE.RENDER_SHADOW:
program=this.GLShaderProgramShadow;
GLGE.setUniform(gl,"1i",GLGE.getUniformLocation(gl,program, "shadowtype"), 1);
break;
case GLGE.RENDER_DEPTH:
program=this.GLShaderProgramShadow;
GLGE.setUniform(gl,"1f",GLGE.getUniformLocation(gl,program, "cascadeLevel"), 2);
GLGE.setUniform(gl,"1i",GLGE.getUniformLocation(gl,program, "shadowtype"), 0);
break;
case GLGE.RENDER_NORMAL:
program=this.GLShaderProgramNormal;
break;
case GLGE.RENDER_PICK:
program=this.GLShaderProgramPick;
var b = pickindex >> 16 & 0xFF;
var g = pickindex >> 8 & 0xFF;
var r = pickindex & 0xFF;
GLGE.setUniform3(gl,"3f",GLGE.getUniformLocation(gl,program, "pickcolor"), r/255,g/255,b/255);
break;
}
//set the line width
gl.lineWidth(this.lineWidth);
//set custom uinforms
for(var key in this.uniforms){
var uniform=this.uniforms[key];
if(uniform.type=="Matrix4fv"){
GLGE.setUniformMatrix(gl,"Matrix4fv",GLGE.getUniformLocation(gl,program, key),false,uniform.value);
}else{
GLGE.setUniform(gl,uniform.type,GLGE.getUniformLocation(gl,program, key),uniform.value);
}
}
if(!program.caches) program.caches={};
if(!program.glarrays) program.glarrays={};
var pc=program.caches;
var pgl=program.glarrays;
var scene=gl.scene;
var camera=scene.camera;
if(pc.far!=camera.far){
GLGE.setUniform(gl,"1i",GLGE.getUniformLocation(gl,program, "far"), camera.far);
pc.far=camera.far;
}
if(renderType==GLGE.RENDER_DEFAULT || renderType==GLGE.RENDER_EMIT){
if(pc.ambientColor!=scene.ambientColor){
var ambientColor=scene.ambientColor;
GLGE.setUniform3(gl,"3f",GLGE.getUniformLocation(gl,program, "amb"), ambientColor.r,ambientColor.g,ambientColor.b);
pc.ambientColor=ambientColor;
}
if(pc.fogFar!=scene.fogFar){
GLGE.setUniform(gl,"1f",GLGE.getUniformLocation(gl,program, "fogfar"), scene.fogFar);
pc.fogFar=scene.fogFar;
}
if(pc.fogNear!=scene.fogNear){
GLGE.setUniform(gl,"1f",GLGE.getUniformLocation(gl,program, "fognear"), scene.fogNear);
pc.fogNear=scene.fogNear;
}
if(pc.fogType!=scene.fogType){
GLGE.setUniform(gl,"1i",GLGE.getUniformLocation(gl,program, "fogtype"), scene.fogType);
pc.fogType=scene.fogType;
}
if(pc.fogType!=scene.fogcolor){
GLGE.setUniform3(gl,"3f",GLGE.getUniformLocation(gl,program, "fogcolor"), scene.fogColor.r,scene.fogColor.g,scene.fogColor.b);
pc.fogcolor=scene.fogcolor;
}
}
if(pc.meshBlendFactor!=this.meshBlendFactor){
GLGE.setUniform(gl,"1f",GLGE.getUniformLocation(gl,program, "framesBlend"), this.meshBlendFactor);
pc.meshBlendFactor=this.meshBlendFactor;
}
var cameraMatrix=camera.getViewMatrix();
var objMatrix=modelMatrix=this.getModelMatrix();
if(!pc.mvMatrix) pc.mvMatrix={cameraMatrix:null,modelMatrix:null};
var mvCache=pc.mvMatrix;
if(mvCache.cameraMatrix!=cameraMatrix || mvCache.modelMatrix!=modelMatrix){
//generate and set the modelView matrix
if(!this.caches.mvMatrix) this.caches.mvMatrix=GLGE.mulMat4(cameraMatrix,modelMatrix);
mvMatrix=this.caches.mvMatrix;
if(this.mesh.joints){
mvMatrix=cameraMatrix;
}
var mvUniform = GLGE.getUniformLocation(gl,program, "worldView");
var M1=GLGE.transposeMat4(mvMatrix);
if(!pgl.mvMatrix){
pgl.mvMatrixT=new Float32Array(M1);
}else{
GLGE.mat4gl(M1,pgl.mvMatrixT);
}
//GLGE.reuseMatrix4(M1);
pgl.mvMatrix=mvMatrix;
GLGE.setUniformMatrix(gl,"Matrix4fv",mvUniform, false, program.glarrays.mvMatrixT);
//invCamera matrix
var icUniform = GLGE.getUniformLocation(gl,program, "envMat");
if(icUniform){
if(!this.caches.envMat){
var envMat = GLGE.inverseMat4(cameraMatrix);
envMat[3]=0;
envMat[7]=0;
envMat[11]=0;
this.caches.envMat = envMat;
}
envMat=this.caches.envMat;
M1=GLGE.transposeMat4(envMat);
if(!program.glarrays.envMat){
pgl.envMatT=new Float32Array(M1);
}else{
GLGE.mat4gl(M1,pgl.envMatT);
}
pgl.envMat=envMat;
GLGE.setUniformMatrix(gl,"Matrix4fv",icUniform, false, pgl.envMatT);
}
//normalising matrix
if(!this.caches.normalMatrix){
var normalMatrix = GLGE.inverseMat4(mvMatrix);
this.caches.normalMatrix = normalMatrix;
}
normalMatrix=this.caches.normalMatrix;
var nUniform = GLGE.getUniformLocation(gl,program, "worldInverseTranspose");
if(!pgl.normalMatrix) pgl.normalMatrix=new Float32Array(normalMatrix);
else GLGE.mat4gl(normalMatrix,pgl.normalMatrix);
GLGE.setUniformMatrix(gl,"Matrix4fv",nUniform, false, pgl.normalMatrix);
var cUniform = GLGE.getUniformLocation(gl,program, "view");
M1=GLGE.transposeMat4(cameraMatrix);
if(!pgl.cameraMatrix){
pgl.cameraMatrixT=new Float32Array(M1);
}else{
GLGE.mat4gl(M1,pgl.cameraMatrixT);
}
//GLGE.reuseMatrix4(M1);
pgl.cameraMatrix=cameraMatrix;
GLGE.setUniformMatrix(gl,"Matrix4fv",cUniform, false, pgl.cameraMatrixT);