~holger-seelig/cobweb.js/1.4

« back to all changes in this revision

Viewing changes to cobweb.js/cobweb/Components/ParticleSystems/ParticleSystem.js

  • Committer: Holger Seelig
  • Date: 2016-04-30 05:14:33 UTC
  • Revision ID: holger.seelig@yahoo.de-20160430051433-i69hy9l7hqfcmef0
Added SurfaceEmitter.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
        "standard/Math/Numbers/Vector3",
13
13
        "standard/Math/Numbers/Vector4",
14
14
        "standard/Math/Numbers/Matrix4",
 
15
        "standard/Math/Numbers/Matrix3",
15
16
        "standard/Math/Algorithms/QuickSort",
16
17
        "standard/Math/Algorithm",
17
18
],
27
28
          Vector3,
28
29
          Vector4,
29
30
          Matrix4,
 
31
          Matrix3,
30
32
          QuickSort,
31
33
          Algorithm)
32
34
{
52
54
 
53
55
        var
54
56
                invModelViewMatrix = new Matrix4 (),
55
 
                matrix             = new Matrix4 (),
56
57
                billboardToScreen  = new Vector3 (0, 0, 0),
57
58
                viewerYAxis        = new Vector3 (0, 0, 0),
58
59
                normal             = new Vector3 (0, 0, 0),
 
60
                s1                 = new Vector3 (0, 0, 0),
 
61
                s2                 = new Vector3 (0, 0, 0),
 
62
                s3                 = new Vector3 (0, 0, 0),
 
63
                s4                 = new Vector3 (0, 0, 0),
59
64
                x                  = new Vector3 (0, 0, 0),
60
65
                y                  = new Vector3 (0, 0, 0),
61
66
                z                  = new Vector3 (0, 0, 0);
95
100
                this .vertexCount        = 0;
96
101
                this .shader             = this .getBrowser () .getPointShader ();
97
102
                this .modelViewMatrix    = new Matrix4 ();
 
103
                this .rotation           = new Matrix3 ();
98
104
                this .particleSorter     = new QuickSort (this .particles, compareDistance);
99
105
                this .sortParticles      = false;
100
106
        }
296
302
                                        this .colorArray  .fill (1);
297
303
                                        this .vertexArray .fill (1);
298
304
 
299
 
                                        this .primitiveType = gl .POINTS;
300
305
                                        this .texCoordCount = 0;
301
306
                                        this .vertexCount   = 1;
302
 
                                        this .shader        = this .getBrowser () .getPointShader ()
303
307
                                        break;
304
308
                                }
305
309
                                case LINE:
312
316
                                        this .colorArray  .fill (1);
313
317
                                        this .vertexArray .fill (1);
314
318
 
315
 
                                        this .primitiveType = gl .LINES;
316
319
                                        this .texCoordCount = 2;
317
320
                                        this .vertexCount   = 2;
318
321
                                        this .shader        = this .getBrowser () .getLineShader ()
334
337
                                                texCoordArray = this .texCoordArray,
335
338
                                                normalArray   = this .normalArray;
336
339
 
337
 
                                        for (var i = 0; i < maxParticles; ++ i)
 
340
                                        for (var i = 0, length = 6 * 3 * maxParticles; i < length; i += 3)
338
341
                                        {
339
 
                                                var i3 = i * 3;
340
 
 
341
 
                                                normalArray [i3]     = 0;
342
 
                                                normalArray [i3 + 1] = 0;
343
 
                                                normalArray [i3 + 2] = 1;
 
342
                                                normalArray [i]     = 0;
 
343
                                                normalArray [i + 1] = 0;
 
344
                                                normalArray [i + 2] = 1;
344
345
                                        }
345
346
 
346
347
                                        gl .bindBuffer (gl .ARRAY_BUFFER, this .normalBuffer);
387
388
 
388
389
                                        this .texCoordCount = 4;
389
390
                                        this .vertexCount   = 6;
390
 
                                        this .primitiveType = gl .TRIANGLES;
391
 
                                        this .shader        = this .getBrowser () .getDefaultShader ()
392
391
                                        break;
393
392
                                }
394
393
                                case GEOMETRY:
395
394
                                {
396
395
                                        this .texCoordCount = 0;
397
396
                                        this .vertexCount   = 0;
398
 
                                        this .primitiveType = gl .TRIANGLES; // geomtry make each its own type
399
 
                                        this .shader        = this .getBrowser () .getDefaultShader ()
 
397
                                        break;
 
398
                                }
 
399
                        }
 
400
 
 
401
                        this .set_shader__ ();
 
402
                },
 
403
                set_shader__: function ()
 
404
                {
 
405
                        switch (this .geometryType)
 
406
                        {
 
407
                                case POINT:
 
408
                                {
 
409
                                        this .shader = this .getBrowser () .getPointShader ()
 
410
                                        break;
 
411
                                }
 
412
                                case LINE:
 
413
                                {
 
414
                                        this .shader = this .getBrowser () .getLineShader ()
 
415
                                        break;
 
416
                                }
 
417
                                case TRIANGLE:
 
418
                                case QUAD:
 
419
                                case SPRITE:
 
420
                                {
 
421
                                        this .shader = this .getBrowser () .getDefaultShader ()
 
422
                                        break;
 
423
                                }
 
424
                                case GEOMETRY:
 
425
                                {
 
426
                                        this .shader = this .getBrowser () .getDefaultShader ()
400
427
                                        break;
401
428
                                }
402
429
                        }
750
777
                           numParticles    = this .numParticles,
751
778
                                colorArray      = this .colorArray,
752
779
                                texCoordArray   = this .texCoordArray,
 
780
                                normalArray     = this .normalArray,
753
781
                                vertexArray     = this .vertexArray,
754
782
                                sx1_2           = this .particleSize_ .x / 2,
755
783
                                sy1_2           = this .particleSize_ .y / 2,
883
911
 
884
912
                        // Vertices
885
913
 
886
 
                        for (var i = 0; i < numParticles; ++ i)
 
914
                        if (this .geometryType === SPRITE)
887
915
                        {
 
916
                                // Normals
 
917
 
888
918
                                var
889
 
                                        position = particles [i] .position,
890
 
                                        x        = position .x,
891
 
                                        y        = position .y,
892
 
                                        z        = position .z,
893
 
                                        i24      = i * 24;
894
 
 
895
 
                                // p4 ------ p3
896
 
                                // |       / |
897
 
                                // |     /   |
898
 
                                // |   /     |
899
 
                                // | /       |
900
 
                                // p1 ------ p2
901
 
 
902
 
                                // p1
903
 
                                vertexArray [i24]     = x - sx1_2;
904
 
                                vertexArray [i24 + 1] = y - sy1_2;
905
 
                                vertexArray [i24 + 2] = z;
906
 
 
907
 
                                // p2
908
 
                                vertexArray [i24 + 4] = x + sx1_2;
909
 
                                vertexArray [i24 + 5] = y - sy1_2;
910
 
                                vertexArray [i24 + 6] = z;
911
 
 
912
 
                                // p3
913
 
                                vertexArray [i24 + 8]  = x + sx1_2;
914
 
                                vertexArray [i24 + 9]  = y + sy1_2;
915
 
                                vertexArray [i24 + 10] = z;
916
 
 
917
 
                                // p1
918
 
                                vertexArray [i24 + 12] = x - sx1_2;
919
 
                                vertexArray [i24 + 13] = y - sy1_2;
920
 
                                vertexArray [i24 + 14] = z;
921
 
 
922
 
                                // p3
923
 
                                vertexArray [i24 + 16] = x + sx1_2;
924
 
                                vertexArray [i24 + 17] = y + sy1_2;
925
 
                                vertexArray [i24 + 18] = z;
926
 
 
927
 
                                // p4
928
 
                                vertexArray [i24 + 20] = x - sx1_2;
929
 
                                vertexArray [i24 + 21] = y + sy1_2;
930
 
                                vertexArray [i24 + 22] = z;
 
919
                                        rotation = this .getScreenAlignedRotation (this .modelViewMatrix),
 
920
                                        nx       = rotation [6],
 
921
                                        ny       = rotation [7],
 
922
                                        nz       = rotation [8];
 
923
 
 
924
                                for (var i = 0, length = 6 * 3 * maxParticles; i < length; i += 3)
 
925
                                {
 
926
                                        normalArray [i]     = nx;
 
927
                                        normalArray [i + 1] = ny;
 
928
                                        normalArray [i + 2] = nz;
 
929
                                }
 
930
 
 
931
                                gl .bindBuffer (gl .ARRAY_BUFFER, this .normalBuffer);
 
932
                                gl .bufferData (gl .ARRAY_BUFFER, this .normalArray, gl .STATIC_DRAW);
 
933
 
 
934
                                // Vertices
 
935
 
 
936
                                s1 .set (-sx1_2, -sy1_2, 0);
 
937
                                s2 .set ( sx1_2, -sy1_2, 0);
 
938
                                s3 .set ( sx1_2,  sy1_2, 0);
 
939
                                s4 .set (-sx1_2,  sy1_2, 0);
 
940
 
 
941
                                rotation .multVecMatrix (s1);
 
942
                                rotation .multVecMatrix (s2);
 
943
                                rotation .multVecMatrix (s3);
 
944
                                rotation .multVecMatrix (s4);
 
945
 
 
946
                                for (var i = 0; i < numParticles; ++ i)
 
947
                                {
 
948
                                        var
 
949
                                                position = particles [i] .position,
 
950
                                                x        = position .x,
 
951
                                                y        = position .y,
 
952
                                                z        = position .z,
 
953
                                                i24      = i * 24;
 
954
        
 
955
                                        // p4 ------ p3
 
956
                                        // |       / |
 
957
                                        // |     /   |
 
958
                                        // |   /     |
 
959
                                        // | /       |
 
960
                                        // p1 ------ p2
 
961
        
 
962
 
 
963
                                        // p1
 
964
                                        vertexArray [i24]     = vertexArray [i24 + 12] = x + s1 .x;
 
965
                                        vertexArray [i24 + 1] = vertexArray [i24 + 13] = y + s1 .y;
 
966
                                        vertexArray [i24 + 2] = vertexArray [i24 + 14] = z + s1 .z;
 
967
        
 
968
                                        // p2
 
969
                                        vertexArray [i24 + 4] = x + s2 .x;
 
970
                                        vertexArray [i24 + 5] = y + s2 .y;
 
971
                                        vertexArray [i24 + 6] = z + s2 .z;
 
972
        
 
973
                                        // p3
 
974
                                        vertexArray [i24 + 8]  = vertexArray [i24 + 16] = x + s3 .x;
 
975
                                        vertexArray [i24 + 9]  = vertexArray [i24 + 17] = y + s3 .y;
 
976
                                        vertexArray [i24 + 10] = vertexArray [i24 + 18] = z + s3 .z;
 
977
        
 
978
                                        // p4
 
979
                                        vertexArray [i24 + 20] = x + s4 .x;
 
980
                                        vertexArray [i24 + 21] = y + s4 .y;
 
981
                                        vertexArray [i24 + 22] = z + s4 .z;
 
982
                                }
 
983
                        }
 
984
                        else
 
985
                        {
 
986
                                for (var i = 0; i < numParticles; ++ i)
 
987
                                {
 
988
                                        var
 
989
                                                position = particles [i] .position,
 
990
                                                x        = position .x,
 
991
                                                y        = position .y,
 
992
                                                z        = position .z,
 
993
                                                i24      = i * 24;
 
994
        
 
995
                                        // p4 ------ p3
 
996
                                        // |       / |
 
997
                                        // |     /   |
 
998
                                        // |   /     |
 
999
                                        // | /       |
 
1000
                                        // p1 ------ p2
 
1001
        
 
1002
                                        // p1
 
1003
                                        vertexArray [i24]     = vertexArray [i24 + 12] = x - sx1_2;
 
1004
                                        vertexArray [i24 + 1] = vertexArray [i24 + 13] = y - sy1_2;
 
1005
                                        vertexArray [i24 + 2] = vertexArray [i24 + 14] = z;
 
1006
        
 
1007
                                        // p2
 
1008
                                        vertexArray [i24 + 4] = x + sx1_2;
 
1009
                                        vertexArray [i24 + 5] = y - sy1_2;
 
1010
                                        vertexArray [i24 + 6] = z;
 
1011
        
 
1012
                                        // p3
 
1013
                                        vertexArray [i24 + 8]  = vertexArray [i24 + 16] = x + sx1_2;
 
1014
                                        vertexArray [i24 + 9]  = vertexArray [i24 + 17] = y + sy1_2;
 
1015
                                        vertexArray [i24 + 10] = vertexArray [i24 + 18] = z;
 
1016
        
 
1017
                                        // p4
 
1018
                                        vertexArray [i24 + 20] = x - sx1_2;
 
1019
                                        vertexArray [i24 + 21] = y + sy1_2;
 
1020
                                        vertexArray [i24 + 22] = z;
 
1021
                                }
931
1022
                        }
932
1023
 
933
1024
                        gl .bindBuffer (gl .ARRAY_BUFFER, this .vertexBuffer);
942
1033
                        {
943
1034
                                case TraverseType .DISPLAY:
944
1035
                                {
945
 
                                        var modelViewMatrix = this .getBrowser () .getModelViewMatrix ();
946
 
 
947
 
                                        this .modelViewMatrix .assign (modelViewMatrix .get ());
948
 
                                        
949
 
                                        switch (this .geometryType)
950
 
                                        {
951
 
                                                case SPRITE:
952
 
                                                {
953
 
                                                        modelViewMatrix .push ();
954
 
                                                        modelViewMatrix .multLeft (this .getScreenAlignedRotation (modelViewMatrix .get ()));
955
 
                        
956
 
                                                        this .getCurrentLayer () .addShape (this);
957
 
                        
958
 
                                                        modelViewMatrix .pop ();
959
 
                                                        break;
960
 
                                                }
961
 
                                                default:
962
 
                                                {
963
 
                                                        this .getCurrentLayer () .addShape (this);
964
 
                                                        break;
965
 
                                                }
966
 
                                        }
967
 
 
 
1036
                                        this .modelViewMatrix .assign (this .getBrowser () .getModelViewMatrix () .get ());
 
1037
                
 
1038
                                        this .getCurrentLayer () .addShape (this);
968
1039
                                        break;
969
1040
                                }
970
1041
                        }
986
1057
                        y .normalize ();
987
1058
                        z .normalize ();
988
1059
                
989
 
                        return matrix .set (x [0], x [1], x [2], 0,
990
 
                                            y [0], y [1], y [2], 0,
991
 
                                            z [0], z [1], z [2], 0,
992
 
                             0, 0, 0, 1);
 
1060
                        return this .rotation .set (x .x, x .y, x .z,
 
1061
                                                    y .x, y .y, y .z,
 
1062
                                                    z .x, z .y, z .z);
993
1063
                },
994
1064
                display: function (context)
995
1065
                {
1041
1111
 
1042
1112
                        // Wireframes are always solid so only one drawing call is needed.
1043
1113
 
 
1114
                        var
 
1115
                                positiveScale = Matrix4 .prototype .determinant3 .call (context .modelViewMatrix) > 0,
 
1116
                           frontFace     = gl .CCW;
 
1117
 
 
1118
                        gl .frontFace (positiveScale ? frontFace : (frontFace === gl .CCW ? gl .CW : gl .CCW));
1044
1119
                        gl .enable (gl .CULL_FACE);
1045
1120
                        gl .cullFace (gl .BACK);
1046
1121
 
1047
 
                        gl .drawArrays (this .primitiveType, 0, this .numParticles * this .vertexCount);
 
1122
                        gl .drawArrays (this .shader .primitiveMode, 0, this .numParticles * this .vertexCount);
1048
1123
 
1049
1124
                        if (shader .color >= 0) gl .disableVertexAttribArray (shader .color);
1050
1125
                        gl .disableVertexAttribArray (shader .vertex);