Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
472 views
in Technique[技术] by (71.8m points)

Perlin Noise - Python's Ursina Game Engine

Is there a way to incorporate Perlin Noise into my Minecraft Clone? I have tried many different things that did not work.

Here is a snippet of my code:

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
from ursina.shaders import camera_grayscale_shader
app = Ursina()

grass = 'textures/grass.jpg'

class Voxel(Button):
    def __init__(self, position = (0,0,0), texture = grass):
        super().__init__(
            model='cube',
            texture=texture,
            color=color.color(0,0,random.uniform(.823,.984)),
            parent=scene,
            position=position,
        )

    def input(self, key):
        if self.hovered:
            if key == 'right mouse down':
                voxel = Voxel(position = self.position + mouse.normal, texture = plank)
                

            if key == 'left mouse down':
                destroy(self)

for z in range(16):
    for x in range(16):
            voxel = Voxel(position = (x,0,z))
question from:https://stackoverflow.com/questions/65877958/perlin-noise-pythons-ursina-game-engine

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To generate terrain using perlin noise, you can create a Terrain entity with the heightmap with your perlin noise image :

from ursina import *

app = Ursina()

noise = 'perlin_noise_file' # file 

t = Terrain(noise) # noise must be a file

app.run()

To make a perlin noise, you can use the perlin_noise library. Here is an example from the docs :

import matplotlib.pyplot as plt
from perlin_noise import PerlinNoise

noise = PerlinNoise(octaves=10, seed=1)
xpix, ypix = 100, 100
pic = [[noise([i/xpix, j/ypix]) for j in range(xpix)] for i in range(ypix)]

plt.imshow(pic, cmap='gray')
plt.show()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...