Skip to content

Commit 47a05ba

Browse files
committed
Revert to pre-atlas voxel-texture. Fixes voxel/voxelmetaverse#14
voxel-texture 0.5.0 switched to use texture atlases, packing all the block textures into one graphic for efficiency purposes. However, this introduces two problems: - With the culled mesher (default as of voxel-engine 0.17+), I get serious lag spikes (30-60 ms frames, or unplayably higher) when placing/breaking blocks, and also slow page load as the meshes are built (see voxel/voxelmetaverse#10) - With the greedy mesher, performance is much better (fixes first issue) but textures are stretched across each of the greedily constructed meshes, instead of tiled across each block (see voxel/voxelmetaverse#14) Example: https://f.cloud.github.com/assets/5897956/1651233/1f478c9e-5aca-11e3-82cc-4028f1e1b1da.png There are techniques for texturing greedy meshes, detailed in this article: http://0fps.wordpress.com/2013/07/09/texture-atlases-wrapping-and-mip-mapping/ - Using array textures is ideal, but they won't be supported until WebGL 2.0 at earliest https://www.khronos.org/registry/webgl/specs/latest/2.0/#3.7 - Otherwise, they can be tiled manually, but this requires a few tricks, would certainly be worthwhile to implement, but is fairly complex. Maybe later, once I understand it better. So for now, (unfortunately) I've reverted back to voxel-texture without atlases. This keeps mesh recalculation fast (when breaking/placing blocks) and avoids stretched textures but may slow down complex scenes with many materials (see performance improvement @shama noted in max-mapper#69).
1 parent 586908e commit 47a05ba

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,17 @@ For example, here we have 4 faces colliding with the bottom of our object:
242242

243243
### Textures
244244

245-
Loading textures onto the texture atlas.
245+
Loading textures creates multiple "materials".
246246

247-
```game.materials.load(['obsidian', 'dirt'], function(textures) { })```
247+
```var materials = game.materials.load(['obsidian', 'dirt'])```
248248

249-
Both of these textures will be loaded into the texture atlas and expanded creating 2 voxel block types.
249+
Both of these textures come with 6 materials, one for each side of a cube, giving a total of 12 materials. By default, faces 1 to 6 are assigned materials 1 to 6. You can assign materials to faces in however you want. For example, we could load materials 7 to 12 (e.g. the dirt materials) like so:
250+
251+
```js
252+
mesh.geometry.faces.forEach(function (face, index) {
253+
face.materialIndex = index + 6 // obsidian texture indices 0 - 5, dirt 6 - 11
254+
})
255+
```
250256

251257
### Texture-less worlds with flat colors
252258

index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function Game(opts) {
5353

5454
this.playerHeight = opts.playerHeight || 1.62
5555
this.meshType = opts.meshType || 'surfaceMesh'
56-
this.mesher = opts.mesher || voxel.meshers.culled
56+
this.mesher = opts.mesher || voxel.meshers.greedy
5757
this.materialType = opts.materialType || THREE.MeshLambertMaterial
5858
this.materialParams = opts.materialParams || {}
5959
this.items = []
@@ -92,6 +92,7 @@ function Game(opts) {
9292
if (process.browser) {
9393
this.materials = texture({
9494
game: this,
95+
THREE: THREE,
9596
texturePath: opts.texturePath || './textures/',
9697
materialType: opts.materialType || THREE.MeshLambertMaterial,
9798
materialParams: opts.materialParams || {},
@@ -226,14 +227,16 @@ Game.prototype.canCreateBlock = function(pos) {
226227
}
227228

228229
Game.prototype.createBlock = function(pos, val) {
229-
if (typeof val === 'string') val = this.materials.find(val)
230+
if (typeof val === 'string') val = this.materials.findIndex(val)
231+
if (pos.chunkMatrix) return this.chunkGroups.createBlock(pos, val)
230232
if (!this.canCreateBlock(pos)) return false
231233
this.setBlock(pos, val)
232234
return true
233235
}
234236

235237
Game.prototype.setBlock = function(pos, val) {
236-
if (typeof val === 'string') val = this.materials.find(val)
238+
if (typeof val === 'string') val = this.materials.findIndex(val)
239+
if (pos.chunkMatrix) return this.chunkGroups.setBlock(pos, val)
237240
var old = this.voxels.voxelAtPosition(pos, val)
238241
var c = this.voxels.chunkAtPosition(pos)
239242
var chunk = this.voxels.chunks[c.join('|')]
@@ -546,8 +549,8 @@ Game.prototype.showChunk = function(chunk) {
546549
this.voxels.meshes[chunkIndex] = mesh
547550
if (this.isClient) {
548551
if (this.meshType === 'wireMesh') mesh.createWireMesh()
549-
else mesh.createSurfaceMesh(this.materials.material)
550-
this.materials.paint(mesh)
552+
else mesh.createSurfaceMesh(new THREE.MeshFaceMaterial(this.materials.get()))
553+
this.materials.paint(mesh.geometry)
551554
}
552555
mesh.setPosition(bounds[0][0], bounds[0][1], bounds[0][2])
553556
mesh.addToScene(this.scene)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"voxel-view": "git://github.com/deathcap/voxel-view.git",
1313
"voxel-raycast": "0.2.1",
1414
"voxel-control": "git://github.com/deathcap/voxel-control.git",
15-
"voxel-texture": "0.5.6",
15+
"voxel-texture": "0.4.0",
1616
"voxel-physical": "0.0.10",
1717
"voxel-region-change": "0.1.0",
1818
"raf": "0.0.1",

0 commit comments

Comments
 (0)