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

33 lines
927 B
JavaScript

import Screen from '../screen'
import { Texture, Material } from '../mesh/material'
const clip = 1 / 100
const VoxelTexture = {
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
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]
]
}
}
export default VoxelTexture