Skip to content

Commit 2449f25

Browse files
committed
Advanced rayTracing
1 parent b314f85 commit 2449f25

File tree

8 files changed

+233
-241
lines changed

8 files changed

+233
-241
lines changed

coffee/client/module/World/World.coffee

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class World
102102
@cellMesh[i].material.dispose()
103103
@scene.remove @cellMesh[i]
104104
@cellMesh[i]="disposed"
105-
@renderer.renderLists.dispose()
105+
@renderer.renderLists.dispose()
106106
updateCell: (data)->
107107
#Updatowanie komórki z już obliczoną geometrią
108108
cellId=@cellTerrain.vec3 data.info...
@@ -121,7 +121,7 @@ class World
121121
_this.cellMesh[cellId].frustumCulled = true
122122
_this.cellMesh[cellId].onAfterRender = ->
123123
@scene.add @cellMesh[cellId]
124-
else
124+
else if mesh isnt "disposed"
125125
@cellMesh[cellId].geometry=geometry
126126
return
127127
intersectsRay: (start,end) ->

coffee/client/module/index.coffee

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ init = ()->
8080
inv_bar = new InventoryBar()
8181
inv_bar.listen()
8282

83+
#Kursor raycastowania
84+
cursor=new THREE.LineSegments(
85+
new THREE.EdgesGeometry(
86+
new THREE.BoxGeometry 1, 1, 1
87+
),
88+
new THREE.LineBasicMaterial {
89+
color: 0x000000,
90+
linewidth: 0.5
91+
}
92+
)
93+
scene.add cursor
8394

8495
#Komunikacja z serwerem websocket
8596
eventMap={
@@ -130,22 +141,17 @@ init = ()->
130141
return
131142
"entities":(entities)->
132143
ent.update entities
144+
"rayBlock":(block)->
145+
if block isnt false
146+
pos=block.position
147+
cursor.position.set pos.x,pos.y+16,pos.z
148+
cursor.visible=true
149+
else
150+
cursor.visible=false
133151
}
134152
for i of eventMap
135153
socket.on i,eventMap[i]
136154

137-
#Kursor raycastowania
138-
cursor=new THREE.LineSegments(
139-
new THREE.EdgesGeometry(
140-
new THREE.BoxGeometry 1, 1, 1
141-
),
142-
new THREE.LineBasicMaterial {
143-
color: 0x000000,
144-
linewidth: 0.5
145-
}
146-
)
147-
scene.add cursor
148-
149155
#Interfejs dat.gui
150156
gui = new GUI()
151157
params={
@@ -185,15 +191,6 @@ render = ->
185191
camera.aspect = width / height
186192
camera.updateProjectionMatrix()
187193

188-
#Raycastowany block
189-
rayBlock=world.getRayBlock()
190-
if rayBlock
191-
pos=rayBlock.posBreak
192-
cursor.position.set pos...
193-
cursor.visible=true
194-
else
195-
cursor.visible=false
196-
197194
#Updatowanie komórek wokół gracza
198195
world.updateCellsAroundPlayer camera.position,params.chunkdist
199196

coffee/server.coffee

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,24 @@ module.exports=(type)->
3232
res.set 'Cache-Control', 'no-store'
3333
next()
3434
server.listen config["port"],()->
35-
console.log "Server is running on \x1b[34mhttp://localhost:#{config["port"]}\x1b[0m"
35+
console.log "Server is running on \x1b[34m*:#{config["port"]}\x1b[0m"
3636
# opn("http://#{config.host}:#{config['express-port']}")
3737

38+
#Raytracing
39+
rayTraceBlock=(b)->
40+
maxSteps = 20
41+
vectorLength=5/16
42+
cursor=b.entity.position.offset 0,b.entity.height,0
43+
x = -Math.sin(b.entity.yaw) * Math.cos(b.entity.pitch)
44+
y = Math.sin(b.entity.pitch)
45+
z = -Math.cos(b.entity.yaw) * Math.cos(b.entity.pitch)
46+
step=vec3(x,y,z).scaled(vectorLength)
47+
for i in [0..maxSteps-1]
48+
cursor.add(step)
49+
block=b.blockAt(cursor)
50+
if block and block.type isnt 0 and block.name isnt "cave_air"
51+
return block
52+
return false
3853
#websocket
3954
io.sockets.on "connection", (socket)->
4055
socketInfo[socket.id]={}
@@ -106,12 +121,14 @@ module.exports=(type)->
106121
socketEventMap={
107122
"move":(state,toggle)->
108123
bot().setControlState(state,toggle)
124+
emit ["rayBlock",rayTraceBlock bot()]
109125
return
110126
"command":(com)->
111127
bot().chat(com)
112128
return
113129
"rotate":(data)->
114130
bot().look data...
131+
emit ["rayBlock",rayTraceBlock bot()]
115132
return
116133
"disconnect":()->
117134
try

0 commit comments

Comments
 (0)