@@ -3,8 +3,9 @@ import {CellTerrain} from './CellTerrain.js'
33
44class Terrain
55 constructor : (options ) ->
6- @cellsData = {}
7- @cells = {}
6+ _this = @
7+ @cellMesh = {}
8+ @cellNeedsUpdate = {}
89 @models = {}
910 @cellSize = options .cellSize
1011 @material = options .material
@@ -16,9 +17,7 @@ class Terrain
1617 cellSize : @cellSize
1718 }
1819 @neighbours = [[- 1 , 0 , 0 ],[1 , 0 , 0 ],[0 , - 1 , 0 ],[0 , 1 , 0 ],[0 , 0 , - 1 ],[0 , 0 , 1 ]]
19- _this = @
2020 @worker = new Worker " ./module/TerrainWorker.js" , {type : ' module' }
21- console .log @worker
2221 @worker .onmessage = (message )->
2322 _this .updateCell message .data
2423 @worker .postMessage {
@@ -35,75 +34,42 @@ class Terrain
3534 cellSize : @cellSize
3635 }
3736 }
38- computeVoxelOffset : (voxelX ,voxelY ,voxelZ ) ->
39- x = voxelX %% @cellSize | 0
40- y = voxelY %% @cellSize | 0
41- z = voxelZ %% @cellSize | 0
42- return [x,y,z]
43- computeCellForVoxel : (voxelX ,voxelY ,voxelZ ) ->
44- cellX = Math .floor voxelX / @cellSize
45- cellY = Math .floor voxelY / @cellSize
46- cellZ = Math .floor voxelZ / @cellSize
47- return [cellX,cellY,cellZ]
48- vec3 : (x ,y ,z ) ->
49- x = parseInt x
50- y = parseInt y
51- z = parseInt z
52- return " #{ x} :#{ y} :#{ z} "
53- setVoxel : (voxelX ,voxelY ,voxelZ ,value ) ->
54- @ _setVoxel voxelX,voxelY,voxelZ,value
55- voff = @ computeVoxelOffset (voxelX,voxelY,voxelZ)
56- cell = @ computeCellForVoxel (voxelX,voxelY,voxelZ)
57- cellId = @ vec3 (cell... )
58- if @cellsData [cellId] is undefined
59- @cellsData [cellId]= {
60- [@ vec3 voff... ]: value
61- }
62- else
63- prevVox = @cellsData [cellId][@ vec3 (voff... )]
64- if prevVox isnt value
65- @cellsData [cellId][@ vec3 (voff... )]= value
66- @cellsData [cellId].needsUpdate = true
67- for nei in @neighbours
68- neiCellId = @ vec3 @ computeCellForVoxel (voxelX+ nei[0 ],voxelY+ nei[1 ],voxelZ+ nei[2 ])...
69- try
70- @cellsData [neiCellId].needsUpdate = true
71- @cellsData [cellId].needsUpdate = true
37+ @neighbours = [[- 1 , 0 , 0 ],[1 , 0 , 0 ],[0 , - 1 , 0 ],[0 , 1 , 0 ],[0 , 0 , - 1 ],[0 , 0 , 1 ]]
38+ setBlock : (voxelX ,voxelY ,voxelZ ,value ) ->
39+ voxelX = parseInt voxelX
40+ voxelY = parseInt voxelY
41+ voxelZ = parseInt voxelZ
42+ if (@cellTerrain .getVoxel voxelX,voxelY,voxelZ) isnt value
43+ @ _setVoxel voxelX,voxelY,voxelZ,value
44+ @cellTerrain .setVoxel voxelX,voxelY,voxelZ,value
45+ cellId = @cellTerrain .vec3 @cellTerrain .computeCellForVoxel (voxelX,voxelY,voxelZ)...
46+ @cellNeedsUpdate [cellId]= true
47+ for nei in @neighbours
48+ neiCellId = @cellTerrain .vec3 @cellTerrain .computeCellForVoxel (voxelX+ nei[0 ],voxelY+ nei[1 ],voxelZ+ nei[2 ])...
49+ @cellNeedsUpdate [neiCellId]= true
7250 return
73- getVoxel : (voxelX ,voxelY ,voxelZ ) ->
74- cell = @ computeCellForVoxel (voxelX,voxelY,voxelZ)
75- cellId = @ vec3 (cell... )
76- voff = @ computeVoxelOffset (voxelX,voxelY,voxelZ)
77- voxId = @ vec3 (voff... )
78- if @cellsData [cellId] isnt undefined
79- voxel = @cellsData [cellId][voxId]
80- if voxel isnt undefined
81- return voxel
82- return 0 ;
51+ getBlock : (voxelX ,voxelY ,voxelZ ) ->
52+ return @cellTerrain .getVoxel voxelX,voxelY,voxelZ
8353 updateCells : ->
8454 _this = @
85- Object .keys (@cellsData ).forEach (id)->
86- if _this .cellsData [id]. needsUpdate
55+ Object .keys (@cellNeedsUpdate ).forEach (id)->
56+ if _this .cellNeedsUpdate [id]
8757 _this ._genCellGeo id .split (" :" )...
88- return
58+ delete _this . cellNeedsUpdate [id]
8959 return
9060 updateCell : (data )->
91- # console.warn "SENDING cell: #{data.info}"
92- cellId = @ vec3 data .info ...
61+ cellId = @cellTerrain .vec3 data .info ...
9362 cell = data .cell
94- if @cellsData [cellId]!= undefined
95- if @cellsData [cellId].needsUpdate
96- mesh = @cells [cellId]
97- geometry = new THREE.BufferGeometry ;
98- geometry .setAttribute ' position' ,new THREE.BufferAttribute (new Float32Array (cell .positions ), 3 )
99- geometry .setAttribute ' normal' ,new THREE.BufferAttribute (new Float32Array (cell .normals ), 3 )
100- geometry .setAttribute ' uv' ,new THREE.BufferAttribute (new Float32Array (cell .uvs ), 2 )
101- if mesh is undefined
102- @cells [cellId]= new THREE.Mesh geometry,@material
103- @scene .add @cells [cellId]
104- else
105- @cells [cellId].geometry = geometry
106- @cellsData [cellId].needsUpdate = false
63+ mesh = @cellMesh [cellId]
64+ geometry = new THREE.BufferGeometry ;
65+ geometry .setAttribute ' position' ,new THREE.BufferAttribute (new Float32Array (cell .positions ), 3 )
66+ geometry .setAttribute ' normal' ,new THREE.BufferAttribute (new Float32Array (cell .normals ), 3 )
67+ geometry .setAttribute ' uv' ,new THREE.BufferAttribute (new Float32Array (cell .uvs ), 2 )
68+ if mesh is undefined
69+ @cellMesh [cellId]= new THREE.Mesh geometry,@material
70+ @scene .add @cellMesh [cellId]
71+ else
72+ @cellMesh [cellId].geometry = geometry
10773 return
10874 intersectsRay : (start ,end ) ->
10975 start .x += 0.5
@@ -138,7 +104,7 @@ class Terrain
138104 tzMax = if tzDelta < Infinity then tzDelta * zDist else Infinity
139105 steppedIndex = - 1
140106 while t <= len
141- voxel = @ getVoxel ix, iy, iz
107+ voxel = @ getBlock ix, iy, iz
142108 if voxel
143109 return {
144110 position : [
@@ -179,8 +145,8 @@ class Terrain
179145 replaceWorld : (voxels )->
180146 _this = @
181147 Object .keys (voxels).forEach (id)->
182- if voxels[id] isnt _this .getVoxel id .split (" :" )...
183- _this .setVoxel id .split (" :" )... ,voxels[id]
148+ if voxels[id] isnt _this .getBlock id .split (" :" )...
149+ _this .setBlock id .split (" :" )... ,voxels[id]
184150 getRayBlock : ->
185151 start = new THREE.Vector3 ().setFromMatrixPosition (@camera .matrixWorld )
186152 end = new THREE.Vector3 ().set (0 ,0 , 1 ).unproject (@camera )
0 commit comments