1
+ importScripts ( "./WorkerImport.js" ) ;
2
+ var IS_WORKER_CONTEXT = true ;
3
+ var isMobile = false ;
4
+
5
+ var camera = {
6
+ position : {
7
+ x : 0 , y : 0 , z : 0
8
+ }
9
+ } ;
10
+ var REUSABLE_VECTOR = new THREE . Vector3 ( ) ;
11
+ var COS30DEG = Math . cos ( 30 * Math . PI / 180 ) ;
12
+ var SIN30DEG = Math . sin ( 30 * Math . PI / 180 ) ;
13
+
14
+ var LightningWorker = function ( ) {
15
+ this . reset ( ) ;
16
+ }
17
+
18
+ LightningWorker . prototype . startRecording = function ( ) {
19
+ this . isRecording = true ;
20
+ }
21
+
22
+ LightningWorker . prototype . dumpPerformance = function ( ) {
23
+ postMessage ( { performance : this . performance } ) ;
24
+ }
25
+
26
+ LightningWorker . prototype . invalidateTransferableBody = function ( ) {
27
+ if ( this . transferableMessageBody ) {
28
+ delete this . transferableMessageBody ;
29
+ delete this . transferableList ;
30
+ }
31
+ }
32
+
33
+ LightningWorker . prototype . reset = function ( ) {
34
+ this . isRecording = false ;
35
+ this . lightnings = new Object ( ) ;
36
+ this . lightningIDsByLightningName = new Object ( ) ;
37
+ this . idCtr = 0 ;
38
+ this . startEndPointBufferIndicesByLightningName = new Object ( ) ;
39
+ }
40
+
41
+ LightningWorker . prototype . onLightningCreation = function ( lightningDescription , isEditorLightning ) {
42
+ var lightning = new Lightning ( lightningDescription . name , lightningDescription . detailThreshold , 0 , lightningDescription . maxDisplacement , lightningDescription . count , lightningDescription . colorName , lightningDescription . radius , lightningDescription . roughness ) ;
43
+ lightning . init ( new THREE . Vector3 ( 0 , 0 , 0 ) , new THREE . Vector3 ( 0 , 100 , 0 ) ) ;
44
+ if ( ! isEditorLightning ) {
45
+ this . lightnings [ this . idCtr ++ ] = lightning ;
46
+ this . lightningIDsByLightningName [ lightning . name ] = this . idCtr - 1 ;
47
+ postMessage ( { idResponse : true , lightningName : lightning . name , id : this . idCtr - 1 } ) ;
48
+ } else {
49
+ this . editorLightning = lightning ;
50
+ }
51
+ }
52
+
53
+ LightningWorker . prototype . onLightningDeletion = function ( lightningName ) {
54
+ var id = this . lightningIDsByLightningName [ lightningName ] ;
55
+ delete this . lightnings [ id ] ;
56
+ delete this . lightningIDsByLightningName [ lightningName ] ;
57
+ delete this . startEndPointBufferIndicesByLightningName [ lightningName ] ;
58
+ }
59
+
60
+ LightningWorker . prototype . onSetCorrectionProperties = function ( lightningName , correctionRefDistance , correctionRefLength ) {
61
+ var lightningID = this . lightningIDsByLightningName [ lightningName ] ;
62
+ var lightning = this . lightnings [ lightningID ] ;
63
+ lightning . setCorrectionProperties ( correctionRefDistance , correctionRefLength ) ;
64
+ }
65
+
66
+ LightningWorker . prototype . onDisableCorrection = function ( lightningName ) {
67
+ var lightningID = this . lightningIDsByLightningName [ lightningName ] ;
68
+ var lightning = this . lightnings [ lightningID ] ;
69
+ lightning . disableCorrection ( ) ;
70
+ }
71
+
72
+ LightningWorker . prototype . saveStartEndPointBufferIndices = function ( payload ) {
73
+ for ( var lightningName in payload ) {
74
+ this . startEndPointBufferIndicesByLightningName [ lightningName ] = payload [ lightningName ] ;
75
+ }
76
+ }
77
+
78
+ LightningWorker . prototype . onEditorClose = function ( ) {
79
+ delete this . editorLightning ;
80
+ }
81
+
82
+ LightningWorker . prototype . update = function ( transferableMessageBody ) {
83
+ var startTime
84
+ if ( this . isRecording ) {
85
+ startTime = performance . now ( ) ;
86
+ }
87
+ camera . position . x = transferableMessageBody . cameraPosition [ 0 ] ;
88
+ camera . position . y = transferableMessageBody . cameraPosition [ 1 ] ;
89
+ camera . position . z = transferableMessageBody . cameraPosition [ 2 ] ;
90
+ for ( var i = 0 ; i < transferableMessageBody . updateBuffer . length ; i ++ ) {
91
+ var id = transferableMessageBody . updateBuffer [ i ] ;
92
+ if ( id == - 1 ) {
93
+ break ;
94
+ } else {
95
+ var lightning = this . editorLightning ? this . editorLightning : this . lightnings [ id ] ;
96
+ var curStartEndPointBufferIndex = this . editorLightning ? 0 : this . startEndPointBufferIndicesByLightningName [ lightning . name ] ;
97
+ lightning . startPoint . set ( transferableMessageBody . startEndPoints [ curStartEndPointBufferIndex ] , transferableMessageBody . startEndPoints [ curStartEndPointBufferIndex + 1 ] , transferableMessageBody . startEndPoints [ curStartEndPointBufferIndex + 2 ] ) ;
98
+ lightning . endPoint . set ( transferableMessageBody . startEndPoints [ curStartEndPointBufferIndex + 3 ] , transferableMessageBody . startEndPoints [ curStartEndPointBufferIndex + 4 ] , transferableMessageBody . startEndPoints [ curStartEndPointBufferIndex + 5 ] ) ;
99
+ curStartEndPointBufferIndex += 6 ;
100
+ lightning . update ( ) ;
101
+ lightning . positionsTypedAray = transferableMessageBody . buffer [ lightning . name ] ;
102
+ for ( var nodeID in lightning . renderMap ) {
103
+ lightning . updateNodePositionInShader ( lightning . renderMap [ nodeID ] , true ) ;
104
+ lightning . updateNodePositionInShader ( lightning . renderMap [ nodeID ] , false ) ;
105
+ }
106
+ }
107
+ }
108
+ if ( this . isRecording ) {
109
+ this . performance = performance . now ( ) - startTime ;
110
+ }
111
+ }
112
+
113
+ var worker = new LightningWorker ( ) ;
114
+
115
+ self . onmessage = function ( msg ) {
116
+ var data = msg . data ;
117
+ if ( data . onLightningCreation ) {
118
+ worker . onLightningCreation ( data . lightning , data . isEditorLightning ) ;
119
+ } else if ( data . onLightningDeletion ) {
120
+ worker . onLightningDeletion ( data . lightningName ) ;
121
+ } else if ( data . reset ) {
122
+ worker . reset ( ) ;
123
+ } else if ( data . onSetCorrectionProperties ) {
124
+ worker . onSetCorrectionProperties ( data . lightningName , data . correctionRefDistance , data . correctionRefLength ) ;
125
+ } else if ( data . onDisableCorrection ) {
126
+ worker . onDisableCorrection ( data . lightningName ) ;
127
+ } else if ( data . isStartEndPointBufferIndices ) {
128
+ worker . saveStartEndPointBufferIndices ( data . payload ) ;
129
+ } else if ( data . invalidateTransferableBody ) {
130
+ worker . invalidateTransferableBody ( ) ;
131
+ } else if ( data . onEditorClose ) {
132
+ worker . onEditorClose ( ) ;
133
+ } else if ( data . startRecording ) {
134
+ worker . startRecording ( ) ;
135
+ } else if ( data . dumpPerformance ) {
136
+ worker . dumpPerformance ( ) ;
137
+ } else {
138
+ worker . update ( data ) ;
139
+ if ( ! worker . transferableMessageBody ) {
140
+ worker . transferableMessageBody = data ;
141
+ worker . transferableList = [ data . cameraPosition . buffer ] ;
142
+ for ( var key in data . buffer ) {
143
+ worker . transferableList . push ( data . buffer [ key ] . buffer ) ;
144
+ }
145
+ worker . transferableList . push ( data . startEndPoints . buffer ) ;
146
+ worker . transferableList . push ( data . updateBuffer . buffer ) ;
147
+ } else {
148
+ worker . transferableMessageBody = data ;
149
+ worker . transferableList [ 0 ] = data . cameraPosition . buffer ;
150
+ var i = 1 ;
151
+ for ( var key in data . buffer ) {
152
+ worker . transferableList [ i ++ ] = data . buffer [ key ] . buffer ;
153
+ }
154
+ worker . transferableList [ i ] = data . startEndPoints . buffer ;
155
+ worker . transferableList [ i + 1 ] = data . updateBuffer . buffer ;
156
+ }
157
+ postMessage ( worker . transferableMessageBody , worker . transferableList ) ;
158
+ }
159
+ }
0 commit comments