diff --git a/README.md b/README.md index 67c67c6..755810d 100644 --- a/README.md +++ b/README.md @@ -242,11 +242,17 @@ For example, here we have 4 faces colliding with the bottom of our object: ### Textures -Loading textures onto the texture atlas. +Loading textures creates multiple "materials". -```game.materials.load(['obsidian', 'dirt'], function(textures) { })``` +```var materials = game.materials.load(['obsidian', 'dirt'])``` -Both of these textures will be loaded into the texture atlas and expanded creating 2 voxel block types. +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: + +```js +mesh.geometry.faces.forEach(function (face, index) { + face.materialIndex = index + 6 // obsidian texture indices 0 - 5, dirt 6 - 11 +}) +``` ### Texture-less worlds with flat colors diff --git a/index.js b/index.js index 8fb8710..e14f3ad 100644 --- a/index.js +++ b/index.js @@ -53,7 +53,7 @@ function Game(opts) { this.playerHeight = opts.playerHeight || 1.62 this.meshType = opts.meshType || 'surfaceMesh' - this.mesher = opts.mesher || voxel.meshers.culled + this.mesher = opts.mesher || voxel.meshers.greedy this.materialType = opts.materialType || THREE.MeshLambertMaterial this.materialParams = opts.materialParams || {} this.items = [] @@ -92,6 +92,7 @@ function Game(opts) { if (process.browser) { this.materials = texture({ game: this, + THREE: THREE, texturePath: opts.texturePath || './textures/', materialType: opts.materialType || THREE.MeshLambertMaterial, materialParams: opts.materialParams || {}, @@ -226,14 +227,16 @@ Game.prototype.canCreateBlock = function(pos) { } Game.prototype.createBlock = function(pos, val) { - if (typeof val === 'string') val = this.materials.find(val) + if (typeof val === 'string') val = this.materials.findIndex(val) + if (pos.chunkMatrix) return this.chunkGroups.createBlock(pos, val) if (!this.canCreateBlock(pos)) return false this.setBlock(pos, val) return true } Game.prototype.setBlock = function(pos, val) { - if (typeof val === 'string') val = this.materials.find(val) + if (typeof val === 'string') val = this.materials.findIndex(val) + if (pos.chunkMatrix) return this.chunkGroups.setBlock(pos, val) var old = this.voxels.voxelAtPosition(pos, val) var c = this.voxels.chunkAtPosition(pos) var chunk = this.voxels.chunks[c.join('|')] @@ -546,8 +549,8 @@ Game.prototype.showChunk = function(chunk) { this.voxels.meshes[chunkIndex] = mesh if (this.isClient) { if (this.meshType === 'wireMesh') mesh.createWireMesh() - else mesh.createSurfaceMesh(this.materials.material) - this.materials.paint(mesh) + else mesh.createSurfaceMesh(new THREE.MeshFaceMaterial(this.materials.get())) + this.materials.paint(mesh.geometry) } mesh.setPosition(bounds[0][0], bounds[0][1], bounds[0][2]) mesh.addToScene(this.scene) diff --git a/package.json b/package.json index 8da168c..028a51a 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "voxel-view": "git://github.com/deathcap/voxel-view.git", "voxel-raycast": "0.2.1", "voxel-control": "git://github.com/deathcap/voxel-control.git", - "voxel-texture": "0.5.6", + "voxel-texture": "0.4.0", "voxel-physical": "0.0.10", "voxel-region-change": "0.1.0", "raf": "0.0.1",