3dexperiments/src/engine/voxel/voxeltexture.js

33 lines
927 B
JavaScript
Raw Normal View History

2020-04-09 12:57:00 +00:00
import Screen from '../screen'
2020-04-09 16:37:35 +00:00
import { Texture, Material } from '../mesh/material'
2020-04-09 12:57:00 +00:00
2020-04-09 16:37:35 +00:00
const clip = 1 / 100
2020-04-09 12:57:00 +00:00
const VoxelTexture = {
2020-04-09 16:37:35 +00:00
size: 0,
voxelSize: 0,
texture: null,
material: null,
loadFromFile: async (fileName, size) => {
VoxelTexture.texture = await Texture.fromFile(Screen.gl, fileName, true, Screen.gl.NEAREST, Screen.gl.CLAMP_TO_EDGE)
VoxelTexture.size = size
VoxelTexture.voxelSize = 1 / size
VoxelTexture.material = new Material([VoxelTexture.texture])
},
faceUVs: (textureIndex) => {
let x = textureIndex / VoxelTexture.size
let y = textureIndex - (x * VoxelTexture.size)
y = 1 - y - VoxelTexture.voxelSize
2020-04-09 12:57:00 +00:00
2020-04-09 16:37:35 +00:00
return [
[x + clip, y + clip],
[x + clip, y + VoxelTexture.voxelSize - clip],
[x - clip + VoxelTexture.voxelSize, y + clip],
[x - clip + VoxelTexture.voxelSize, y - clip + VoxelTexture.voxelSize]
]
}
2020-04-09 12:57:00 +00:00
}
2020-04-09 16:37:35 +00:00
export default VoxelTexture