feat: textures

This commit is contained in:
Florian Sylvain
2024-06-09 22:29:39 +02:00
parent dda5bdf392
commit 64ee0f1e83
4 changed files with 71 additions and 32 deletions
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

+10
View File
@@ -1,5 +1,12 @@
import { Object3D, Vector3 } from "three" import { Object3D, Vector3 } from "three"
import Face from "./face" import Face from "./face"
import { degToRad } from "three/src/math/MathUtils.js"
function randInt(min: number, max: number): number {
const minCeiled = Math.ceil(min)
const maxFloored = Math.floor(max)
return Math.floor(Math.random() * (maxFloored - minCeiled + 1) + minCeiled)
}
export default class Block { export default class Block {
faces: Face[] faces: Face[]
@@ -13,6 +20,9 @@ export default class Block {
this.faces.forEach((face) => { this.faces.forEach((face) => {
const dummy = new Object3D() const dummy = new Object3D()
dummy.position.add(coordinates) dummy.position.add(coordinates)
if (face.orientation === "Up" || face.orientation === "Down") {
dummy.rotateOnAxis(new Vector3(0, 1, 0), degToRad(90 * randInt(0, 4)))
}
dummy.updateMatrix() dummy.updateMatrix()
face.instancedMesh?.setMatrixAt(face.instaceIndex, dummy.matrix) face.instancedMesh?.setMatrixAt(face.instaceIndex, dummy.matrix)
this.facesIndexes.push(face.instaceIndex) this.facesIndexes.push(face.instaceIndex)
+11 -5
View File
@@ -1,8 +1,8 @@
import { import {
Color,
InstancedMesh, InstancedMesh,
MeshLambertMaterial, MeshBasicMaterial,
PlaneGeometry, PlaneGeometry,
Texture,
Vector3 Vector3
} from "three" } from "three"
@@ -43,14 +43,20 @@ export const FACE_ORIENTATION = [
] ]
export default class Face { export default class Face {
material: MeshLambertMaterial material: MeshBasicMaterial
orientation: Orientation orientation: Orientation
geometry: PlaneGeometry geometry: PlaneGeometry
instancedMesh: InstancedMesh | undefined instancedMesh: InstancedMesh | undefined
instaceIndex: number = 0 instaceIndex: number = 0
constructor(color: Color, orientation: Orientation, instancesCount: number) { constructor(
this.material = new MeshLambertMaterial({ color }) orientation: Orientation,
instancesCount: number,
texture: Texture
) {
this.material = new MeshBasicMaterial({
map: texture
})
this.orientation = orientation this.orientation = orientation
+32 -9
View File
@@ -19,16 +19,38 @@ const controls = new OrbitControls(camera, renderer.domElement)
controls.update() controls.update()
document.body.appendChild(renderer.domElement) document.body.appendChild(renderer.domElement)
const ambientLight = new THREE.AmbientLight(new THREE.Color("white"), 5) const image = document.createElement("img")
scene.add(ambientLight) image.src = "/grass.png"
image.addEventListener("load", onImageLoaded, false)
function onImageLoaded() {
const textures = [] as THREE.Texture[]
for (let i = 0; i < 6; i++) {
const canvas = document.createElement("canvas")
canvas.width = 16
canvas.height = 16
const context = canvas.getContext("2d")
context?.drawImage(image, 0, 16 * i, 16, 16, 0, 0, 16, 16)
const texture = new THREE.Texture(canvas)
texture.needsUpdate = true
texture.magFilter = THREE.NearestFilter
textures.push(texture)
}
onTextureLoaded(textures)
}
function onTextureLoaded(textures: THREE.Texture[]) {
const chunk = [] as THREE.Vector3[] const chunk = [] as THREE.Vector3[]
for (let i = -16; i < 16; i++) { for (let i = -16; i < 16; i++) {
for (let j = -16; j < 16; j++) { for (let j = -16; j < 16; j++) {
chunk.push( chunk.push(
new THREE.Vector3( new THREE.Vector3(
i, i,
Math.floor(Math.sin(i / 12) * 4 + Math.cos(j / 12) * 4), Math.ceil(Math.sin(i / 25) * 8 + Math.cos(j / 25) * 8),
j j
) )
) )
@@ -36,12 +58,12 @@ for (let i = -16; i < 16; i++) {
} }
const faces = [ const faces = [
new Face(new THREE.Color("white"), "Up", chunk.length), new Face("Up", chunk.length, textures[0]),
new Face(new THREE.Color("red"), "Down", chunk.length), new Face("North", chunk.length, textures[1]),
new Face(new THREE.Color("blue"), "North", chunk.length), new Face("South", chunk.length, textures[2]),
new Face(new THREE.Color("green"), "East", chunk.length), new Face("East", chunk.length, textures[3]),
new Face(new THREE.Color("yellow"), "West", chunk.length), new Face("West", chunk.length, textures[4]),
new Face(new THREE.Color("purple"), "South", chunk.length) new Face("Down", chunk.length, textures[5])
] ]
const cubes = [] as Block[] const cubes = [] as Block[]
@@ -52,6 +74,7 @@ chunk.forEach((block) => {
faces.forEach((face) => { faces.forEach((face) => {
if (face.instancedMesh) scene.add(face.instancedMesh) if (face.instancedMesh) scene.add(face.instancedMesh)
}) })
}
camera.position.x = 20 camera.position.x = 20
camera.position.y = 20 camera.position.y = 20