简体   繁体   English

Perlin Noise - Python 的 Ursina 游戏引擎

[英]Perlin Noise - Python's Ursina Game Engine

Is there a way to incorporate Perlin Noise into my Minecraft Clone?有没有办法将 Perlin Noise 合并到我的 Minecraft 克隆中? 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))

To generate terrain using perlin noise, you can create a Terrain entity with the heightmap with your perlin noise image:要使用 perlin 噪声生成地形,您可以使用 perlin 噪声图像创建带有高度图的Terrain实体:

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.要制作 perlin 噪音,您可以使用perlin_noise库。 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()

Yes, there's a way to incorporate perlin noise into a minecraft clone, this is a simple example of random terrain in a minecraft clone:是的,有一种方法可以将 perlin 噪声合并到我的世界克隆中,这是我的世界克隆中随机地形的一个简单示例:

from perlin_noise import PerlinNoise
import random

noise = PerlinNoise (octaves=3,seed=random.randint(1,1000000))

for z in range(-10,10):
    for x in range(-10,10):
        y = noise([x * .02,z * .02])
        y = math.floor(y * 7.5)
        voxel = Voxel(position=(x,y,z))

And this is the final result: https://imgur.com/a/VZd1Uip这是最终结果: https://imgur.com/a/VZd1Uip

Remember you need to install perlin noise, to do this just write this in the terminal: pip install perlin-noise请记住,您需要安装 perlin noise,为此只需在终端中写入:pip install perlin-noise

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM