mirror of
https://github.com/Floriansylvain/generic-threejs-game.git
synced 2026-08-19 19:53:23 +02:00
fix: overlap and texture wrapping
This commit is contained in:
+27
-15
@@ -1,10 +1,17 @@
|
|||||||
import { RepeatWrapping, TextureLoader, Vector2, Vector3 } from "three";
|
import {
|
||||||
|
ClampToEdgeWrapping,
|
||||||
|
RepeatWrapping,
|
||||||
|
TextureLoader,
|
||||||
|
Vector2,
|
||||||
|
Vector3,
|
||||||
|
} from "three";
|
||||||
import { ImprovedNoise } from "three/examples/jsm/Addons.js";
|
import { ImprovedNoise } from "three/examples/jsm/Addons.js";
|
||||||
|
|
||||||
export function World(props: { position: Vector2; seed: number }): JSX.Element {
|
export function World(props: { position: Vector2; seed: number }): JSX.Element {
|
||||||
const size = 100;
|
const size = 100;
|
||||||
const scale = 3;
|
const scale = 3;
|
||||||
const amplitude = 30;
|
const amplitude = 30;
|
||||||
|
const overlap = 0.5; // Amount of overlap between chunks
|
||||||
|
|
||||||
const noise = (x: number, y: number): number => {
|
const noise = (x: number, y: number): number => {
|
||||||
const noise = new ImprovedNoise();
|
const noise = new ImprovedNoise();
|
||||||
@@ -12,22 +19,29 @@ export function World(props: { position: Vector2; seed: number }): JSX.Element {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const canvas = document.createElement("canvas");
|
const canvas = document.createElement("canvas");
|
||||||
canvas.width = size;
|
canvas.width = size + overlap * 2;
|
||||||
canvas.height = size;
|
canvas.height = size + overlap * 2;
|
||||||
const context = canvas.getContext("2d");
|
const context = canvas.getContext("2d");
|
||||||
if (!context) throw new Error("Canvas context not found");
|
if (!context) throw new Error("Canvas context not found");
|
||||||
const imageData = context.getImageData(0, 0, size, size);
|
|
||||||
|
const offsetX = -overlap + props.position.x;
|
||||||
|
const offsetY = -overlap + props.position.y;
|
||||||
|
|
||||||
|
const imageData = context.getImageData(
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
size + overlap * 2,
|
||||||
|
size + overlap * 2
|
||||||
|
);
|
||||||
const data = imageData.data;
|
const data = imageData.data;
|
||||||
for (let x = 0; x < size; x++) {
|
|
||||||
for (let y = 0; y < size; y++) {
|
for (let x = 0; x < size + overlap * 2; x++) {
|
||||||
|
for (let y = 0; y < size + overlap * 2; y++) {
|
||||||
const n =
|
const n =
|
||||||
noise(
|
noise((x + offsetX) / (size / scale), (y + offsetY) / (size / scale)) *
|
||||||
(x + props.position.x) / (size / scale),
|
|
||||||
(y + props.position.y) / (size / scale)
|
|
||||||
) *
|
|
||||||
127 +
|
127 +
|
||||||
127;
|
127;
|
||||||
const id = (x + y * size) * 4;
|
const id = (x + y * (size + overlap * 2)) * 4;
|
||||||
data[id] = n;
|
data[id] = n;
|
||||||
data[id + 1] = n;
|
data[id + 1] = n;
|
||||||
data[id + 2] = n;
|
data[id + 2] = n;
|
||||||
@@ -36,8 +50,7 @@ export function World(props: { position: Vector2; seed: number }): JSX.Element {
|
|||||||
}
|
}
|
||||||
context.putImageData(imageData, 0, 0);
|
context.putImageData(imageData, 0, 0);
|
||||||
const displacementMap = new TextureLoader().load(canvas.toDataURL());
|
const displacementMap = new TextureLoader().load(canvas.toDataURL());
|
||||||
displacementMap.wrapS = displacementMap.wrapT = RepeatWrapping;
|
displacementMap.wrapS = displacementMap.wrapT = ClampToEdgeWrapping;
|
||||||
displacementMap.wrapT = displacementMap.wrapS = RepeatWrapping;
|
|
||||||
|
|
||||||
const grassTexture = new TextureLoader().load("./texture_grass.jpg");
|
const grassTexture = new TextureLoader().load("./texture_grass.jpg");
|
||||||
grassTexture.wrapS = grassTexture.wrapT = RepeatWrapping;
|
grassTexture.wrapS = grassTexture.wrapT = RepeatWrapping;
|
||||||
@@ -53,10 +66,9 @@ export function World(props: { position: Vector2; seed: number }): JSX.Element {
|
|||||||
>
|
>
|
||||||
<planeGeometry
|
<planeGeometry
|
||||||
attach="geometry"
|
attach="geometry"
|
||||||
args={[100, 100, 100, 100]}
|
args={[100, 100, 100 + overlap * 2, 100 + overlap * 2]}
|
||||||
></planeGeometry>
|
></planeGeometry>
|
||||||
<meshStandardMaterial
|
<meshStandardMaterial
|
||||||
wireframe
|
|
||||||
toneMapped={false}
|
toneMapped={false}
|
||||||
attach="material"
|
attach="material"
|
||||||
map={grassTexture}
|
map={grassTexture}
|
||||||
|
|||||||
Reference in New Issue
Block a user