- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我一直在开发一个统一的游戏,想将我的世界从 150x150 的 map 扩展到一个看似无限的程序世界。我的计划是以Perlin Noise为基础,使用0-1的不同值来判断地形类型。我遇到的问题是当我抽出我的 block 并相应地偏移我的 block 时,我的 block 没有正确排列,这打破了无限世界的幻觉。
(见此处)
WorldChunk.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Unity.Mathematics;
[System.Serializable]
public class WorldChunk
{
public int2 Position;
public int[,] Data;
public float[,] Sample;
public WorldChunk(int chunkSize = 16){
Data = new int[chunkSize, chunkSize];
Sample = new float[chunkSize, chunkSize];
}
}
WorldGenerator.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Unity.Mathematics;
public class WorldGenerator : MonoBehaviour
{
// Base World Data
public int ChunkSize = 75;
public string Seed = "";
[Range(1f, 40f)]
public float PerlinScale = 10f;
// Pseudo Random Number Generator
private System.Random pseudoRandom;
// Chunk Data Split into Sections (Each Chunk having Coords (x, y))
public Dictionary<string, WorldChunk> chunks = new Dictionary<string, WorldChunk>();
//============================================================
// Set Warm-Up Data
//============================================================
private void Awake() {
// Get/Create Seed
if (Seed == ""){
Seed = GenerateRandomSeed();
}
// Get Random Number Generator
pseudoRandom = new System.Random(Seed.GetHashCode());
// Using to Clear while Making Test Adjustments
chunks.Clear();
// Generate Starting Chunk
for (int x = -1; x <= 1; x++)
{
for (int y = -1; y <= 1; y++)
{
// Draw Test Chunks
GenerateChunk(x, y);
}
}
}
//============================================================
// Generation Code
//============================================================
// ===
// Create New Chunks
// ===
public void GenerateChunk(int x, int y){
// Set Key to use
string key = $"{x},{y}";
// Check if key exists if not Generate New Chunk
if (!chunks.ContainsKey(key)){
// Add Chunk, Set Position in chunk grid (for calling and block data later), Then Generate data
chunks.Add(key, new WorldChunk(ChunkSize));
chunks[key].Position = new int2(x, y);
GenerateChunkData(chunks[key]);
}
}
// ===
// Fill Chunks with Perlin Data
// ===
private void GenerateChunkData(WorldChunk chunk){
// Set Offsets
float xOffset = (float)chunk.Position.x * ChunkSize;
float yOffset = (float)chunk.Position.y * ChunkSize;
// Set Data to Chunk
for (int x = 0; x < ChunkSize; x++)
{
for (int y = 0; y < ChunkSize; y++)
{
// Get Perlin Map
float px = (float)(x) / ChunkSize * PerlinScale + xOffset;
float py = (float)(y) / ChunkSize * PerlinScale + yOffset;
// Set Temp Sample For Testing (This will change for Map Data (Hills and Water) later)
chunk.Sample[x,y] = Mathf.PerlinNoise(px, py);
}
}
}
// ===
// Generate Random Seed of Length
// ===
private string GenerateRandomSeed(int maxCharAmount = 10, int minCharAmount = 10){
//Set Characters To Pick from
const string glyphs= "abcdefghijklmnopqrstuvwxyz0123456789";
//Set Length from min to max
int charAmount = UnityEngine.Random.Range(minCharAmount, maxCharAmount);
// Set output Variable
string output = "";
// Do Random Addition
for(int i=0; i<charAmount; i++)
{
output += glyphs[UnityEngine.Random.Range(0, glyphs.Length)];
}
// Output New Random String
return output;
}
//============================================================
// Draw Example
//============================================================
private void OnDrawGizmos() {
// Do this because I'm lazy and don't want to draw pixels to generated Sprites
Awake();
// For Each WorldChunk in the chunk Data
foreach (WorldChunk c in chunks.Values)
{
// Check if it exists (Foreach is stupid sometimes... When live editing)
if (c != null){
// Get World Positions for Chunk (Should probably Set to a Variable in the Chunk Data)
Vector3 ChunkPosition = new Vector3(c.Position.x * ChunkSize, c.Position.y * ChunkSize);
// For Each X & For Each Y in the chunk
for (int x = 0; x < ChunkSize; x++)
{
for (int y = 0; y < ChunkSize; y++)
{
// Get Cell position
Vector3 cellPos = new Vector3((ChunkPosition.x - ChunkSize/2f) + x, (ChunkPosition.y - ChunkSize/2f) + y);
// Get Temp Sample and set to color
float samp = c.Sample[x,y];
Gizmos.color = new Color(samp, samp, samp);
// Draw Tile as Sample black or white.
Gizmos.DrawCube(cellPos, Vector3.one);
}
}
// Size for Cubes
Vector3 size = new Vector3(ChunkSize, ChunkSize, 1f);
// Set Color Opaque Green
Gizmos.color = new Color(0f, 1f, 0f, 0.25f);
// Draw Chunk Borders (Disable to show issue)
// Gizmos.DrawWireCube(ChunkPosition, size);
}
}
}
}
我想在使用时指出:
// Get Perlin Map
float px = (float)(x + xOffset) / ChunkSize * PerlinScale;
float py = (float)(y + yOffset) / ChunkSize * PerlinScale;
代替
// Get Perlin Map
float px = (float)(x) / ChunkSize * PerlinScale + xOffset;
float py = (float)(y) / ChunkSize * PerlinScale + yOffset;
一切都正确对齐,但柏林噪音只是重复。
对我来说,在 block 之间平滑以使所有内容匹配的最佳方法是什么?有没有更好的写法?
编辑:
感谢 Draykoon D 的帮助!如果有人需要,这里是更新信息和指向 pastebin 上更新脚本的链接!
这是任何想要它的人的更新代码:** WorldGenerator.cs**
** WorldGenerator.cs**
希望对您有所帮助!
最佳答案
您要查找的关键字是 tileable。
但我有一个好消息要告诉你,像 perlin 这样的噪声函数本质上是周期性的。因此,与其调用 ChunckSize * ChunkSize 噪声函数,不如调用它一次,然后对结果进行除法。
我会建议您阅读这个优秀的教程:
关于c# - 如何在多个 perlin 噪声 block 之间平滑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63407212/
有谁知道如何在 R 中创建模拟栅格高程数据集 - 即真实高程值的二维矩阵? R's jitter好像不太合适在 Java/Processing 中 noise()函数通过 Perlin noise 实
我正在尝试使3D Perlin噪声算法适应较低的尺寸,但是由于我不完全了解其原因,我在使用梯度函数时遇到了麻烦。 原始的Perlin梯度函数采用四个参数:hash和三维坐标(x, y, z)。该函数的
我已经阅读了一些关于柏林噪声的文章,但每篇文章似乎都有自己的实现方式: 在 this文章中,梯度函数返回单个 double 值。 在 this文章中,梯度生成为 3D 矢量。 在 this文章生成了一
我目前正在使用 Shader Model 4 (DirectX 10 HLSL) 实现 3D Perlin 噪声凹凸贴图。生成噪声本身并不是一个大问题(周围有大量的教程和代码),但我还没有找到 3D
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 8年前关闭。 Improve this q
我一直在使用 this 在 F# 中编写 Perlin 噪声生成器和 this ,并且一直成功直到算法的插值部分。这是到目前为止的工作代码(您可能不需要阅读接下来的 2 个代码块,因为它们只是为了上下
我最近一直在处理 perlin 噪声,在我使用的 tile 引擎中实现它时,我注意到 perlin 噪声函数产生了“ block ”,如下图所示。每个像素都是 500 x 500 数组中的另一个不同位
我已经编写了自己的 perlin 库,并且还使用了标准 python 库之一来生成噪声。这是我的代码: import sys from noise import pnoise2, snoise2 pe
我正在编写一个基于 perlin 噪声的 map 生成器并遇到了一个问题: 假设我想要 30% 的水和 70% 的土砖。使用普通的随机生成器没有问题: tile = rnd.nextFloat() <
我正在尝试为我的一个应用程序创建动画背景。 基本上,我希望我的 Activity 显示 animated Perlin or Simplex noise作为背景。为了低能耗,我正在尝试使用 OpenG
我可以使用 2D Perlin 噪声函数来生成高度值,但我不明白为什么要使用 3D Perlin 噪声函数。在 Notch's blog ,他提到在 Minecraft 上使用 3D Perlin 噪
我目前正在学习 Löve 2d/Lua 并尝试使用 Perlin Noise 算法生成噪声。 我在这里改编了 Ken Perlin 的改进噪声代码: 根据 Doug 的回答使用修复程序编辑的代码 --
我需要使用我编写的 perlin 噪声程序在 Java 中生成 3D 行星(球体)的纹理。但问题是左侧和右侧需要相同,上下也必须相同,这样您才能将纹理放在球体上。 我无法将柏林噪声源放在这里,因为它太
我正在尝试实现我找到的一些源代码 online使用柏林噪声生成高度图。我已经成功地使用 Noise3 函数获取了高度图,其中第三个坐标是随机“种子”,以允许随机高度图。 我的问题是生成的地形相当沉闷
我正在尝试实现 2D Perlin 噪声来创建类似 Minecraft 的地形(Minecraft 实际上并不使用 2D Perlin 噪声)而没有悬垂或洞穴之类的东西。 我这样做的方式是创建一个 [
我一直在研究我希望构建的库的噪声算法,并从 Perlin 噪声开始(更准确地说,Simplex 噪声,我想处理任意维度,或者至少最多 6 个维度)。阅读Simplex noise demystifie
Perlin 用伪代码解释:http://freespace.virgin.net/hugo.elias/models/m_perlin.htm 本教程为我提供了一个用伪代码编写的随机数生成器函数。返
更新:正式问题列表: 2D 噪声实现的浮点值(输入参数和输出)代表什么? - 部分回答,输入是坐标。输出怎么样?另外,我可以使用我的整数作为坐标的 float 吗? 1.0、122.0 等? 在 2D
昨天,我将经典的 Perlin 噪音 (src: http://mrl.nyu.edu/~perlin/doc/oscar.html#noise) 移植到了 JavaScript。奇怪的是,生成的噪音
我目前正在使用现有的 Perlin 噪声实现,它与我试图清理的一堆代码捆绑在一起。有问题的代码测试严重不足,我想确保它的每个组件都接受适当的测试,以防出现任何隐藏的错误。 但是,我不确定在这种情况下我
我是一名优秀的程序员,十分优秀!