@@ -2,7 +2,8 @@ import * as THREE from 'three'
22import * as Enum from "./Enums.js"
33import { RGBELoader } from "three/addons/loaders/RGBELoader.js"
44import { ModelRepository } from "./ModelRepository.js"
5- import { Utils } from "./Utils.js" ;
5+ import { Utils } from "./Utils.js"
6+ import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js'
67
78export class World {
89 #scene
@@ -14,6 +15,7 @@ export class World {
1415 #raycaster
1516 #decals = [ ]
1617 #cache = { }
18+ #smokes = { }
1719 volume = 30
1820
1921 constructor ( ) {
@@ -80,7 +82,7 @@ export class World {
8082 this . volume = setting . getMasterVolume ( )
8183
8284 const anisotropy = Math . min ( setting . getAnisotropicFiltering ( ) , renderer . capabilities . getMaxAnisotropy ( ) )
83- return this . #modelRepository. init ( mapName , renderer , scene ) . then ( ( ) => {
85+ return this . #modelRepository. init ( scene , renderer , mapName ) . then ( ( ) => {
8486 scene . traverse ( function ( object ) {
8587 if ( object . isMesh && object . material . map ) {
8688 object . material . map . anisotropy = anisotropy
@@ -180,10 +182,10 @@ export class World {
180182 this . #cache[ index ] = loadCallback ( )
181183 }
182184
183- return this . #cache[ index ] ;
185+ return this . #cache[ index ]
184186 }
185187
186- spawnFlame ( size , height ) {
188+ spawnFlame ( position , size , height ) {
187189 const coneDetail = Utils . randomInt ( 5 , 7 )
188190 const lightnessValue = Utils . randomInt ( 30 , 80 )
189191 const geometry = this . loadCache ( `flame-geo-c-${ coneDetail } ` , ( ) => new THREE . ConeGeometry ( 1 , 1 , coneDetail ) )
@@ -196,20 +198,65 @@ export class World {
196198 mesh . castShadow = false
197199 mesh . receiveShadow = true
198200
201+ mesh . position . set ( position . x , position . y + ( height / 2 ) , - 1 * position . z )
202+ mesh . rotation . x = Utils . randomInt ( - 10 , 10 ) / 100
203+ mesh . rotateOnWorldAxis ( new THREE . Vector3 ( 0 , 1 , 0 ) , Math . random ( ) * 6.28 )
204+
199205 this . #scene. add ( mesh )
200206 return mesh
201207 }
202208
203- spawnSmoke ( geometry ) {
204- const mesh = new THREE . Mesh ( geometry , this . #modelRepository. getSmokeMaterial ( ) )
209+ initSmoke ( smokeId , size , maxPartCount ) {
210+ const mesh = new THREE . Mesh ( new THREE . BufferGeometry ( ) , this . #modelRepository. getSmokeMaterial ( ) )
205211 this . #scene. add ( mesh )
212+
213+ this . #smokes[ smokeId ] = { }
214+ this . #smokes[ smokeId ] [ 'size' ] = size
215+ this . #smokes[ smokeId ] [ 'count' ] = maxPartCount
216+ this . #smokes[ smokeId ] [ 'geometries' ] = [ ]
217+ this . #smokes[ smokeId ] [ 'mesh' ] = mesh
218+
206219 return mesh
207220 }
208221
222+ spawnSmoke ( position , height , smokeId ) {
223+ const size = this . #smokes[ smokeId ] [ 'size' ]
224+ const offset = Math . max ( 2 , Math . ceil ( size / 4 ) )
225+ const geo = new THREE . CylinderGeometry ( size - Utils . randomInt ( 0 , offset ) , size , height - Utils . randomInt ( 0 , offset ) , Utils . randomInt ( 5 , 22 ) )
226+ geo . translate ( position . x , position . y + ( geo . parameters . height / 2 ) , - position . z )
227+ this . #smokes[ smokeId ] [ 'geometries' ] . push ( geo )
228+
229+ let len = this . #smokes[ smokeId ] [ 'geometries' ] . length
230+ if ( len % 10 !== 0 && len < this . #smokes[ smokeId ] [ 'count' ] ) {
231+ return
232+ }
233+
234+ let geometry = BufferGeometryUtils . mergeGeometries ( this . #smokes[ smokeId ] [ 'geometries' ] )
235+ geometry . computeBoundingBox ( )
236+
237+ this . #smokes[ smokeId ] [ 'range' ] = Math . ceil ( geometry . getIndex ( ) . count / 50 )
238+ geometry . setDrawRange ( 0 , geometry . getIndex ( ) . count )
239+
240+ const mesh = this . #smokes[ smokeId ] [ 'mesh' ]
241+ mesh . geometry = geometry
242+ }
243+
244+ fadeSmoke ( smokeId ) {
245+ const mesh = this . #smokes[ smokeId ] [ 'mesh' ]
246+ const newRange = mesh . geometry . drawRange . count - this . #smokes[ smokeId ] [ 'range' ]
247+ mesh . geometry . setDrawRange ( 0 , newRange )
248+
249+ if ( newRange <= 0 ) {
250+ delete this . #smokes[ smokeId ]
251+ return true
252+ }
253+ return false
254+ }
255+
209256 bulletWallHit ( origin , hitPosition , item ) {
210257 const rayHit = new THREE . Vector3 ( hitPosition . x , hitPosition . y , - hitPosition . z )
211258 const rayDirection = rayHit . clone ( ) . sub ( new THREE . Vector3 ( origin . x , origin . y , - origin . z ) ) . normalize ( )
212- rayHit . addScaledVector ( rayDirection , - 50 ) ; // offset a bit back to give raycaster more space to match server-client geometry
259+ rayHit . addScaledVector ( rayDirection , - 50 ) // offset a bit back to give raycaster more space to match server-client geometry
213260
214261 this . #raycaster. near = 1
215262 this . #raycaster. far = 100
0 commit comments