펄린 노이즈 관련 이미지

펄린 노이즈

PerlinNoise는 Ken Perlin이 1980년대 영화 “Tron”을 제작하는 동안 컴퓨터 효과를 위한 그라데이션 텍스처를 만들기 위해 만들었습니다. 개발한 노이즈 기능입니다. 노이즈 함수는 Minecraft 및 Terrari와 같은 많은 분야에서 사용됩니다. 설정된 규칙에 따라 게임 내 콘텐츠 자동 생성 “프로그램 콘텐츠 생성” 사용하는 게임에 적용할 수 있습니다. 이 노이즈 함수는 Unity에서 Mathf.PerlinNoise() 메서드로 제공됩니다. 그런 다음 텍스처에 노이즈를 칠하고 눈으로 노이즈를 검사해 보겠습니다.

노이즈 텍스처

using UnityEngine;

public class PerlinNoise : MonoBehaviour
{
    public int width = 256;
    public int height = 256;

    public float scale = 20f;

    public float offsetX;
    public float offsetY;

    private void Start()
    {
        offsetX = Random.Range(0f, 99999f);
        offsetY = Random.Range(0f, 99999f);       
    }

    private void Update()
    {
        Renderer renderer = GetComponent<Renderer>();
        renderer.material.mainTexture = GenerateTexture();
    }

    Texture2D GenerateTexture()
    {
        Texture2D texture = new Texture2D(width, height);

        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                Color color = CalculateColor(x, y);

                texture.SetPixel(x, y, color);
            }
        }

        texture.Apply();

        return texture;
    }

    Color CalculateColor(int x, int y)
    {
        float xCoord = (float)x / width * scale + offsetX;  
        float yCoord = (float)y / height * scale + offsetY; 

        float sample = Mathf.PerlinNoise(xCoord, yCoord);

        return new Color(sample, sample, sample);
    }
}


펄린 노이즈 관련 대표 이미지

오브젝트를 생성하여 텍스처를 적용하면 위와 같이 노이즈를 확인할 수 있습니다. 이 노이즈 기능은 게임의 다양한 부분에 적용될 수 있습니다.

파도

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Wave : MonoBehaviour
{
    float scale;
    float heightScale;

    int planeSize;
    public GameObject cube;

    void Start()
    {
        scale = 0.2f;
        heightScale = 2f;

        planeSize = 25;

        for (int x = 0; x < planeSize; x++)
        {
            for (int z = 0; z < planeSize; z++)
            {
                var c = Instantiate(cube, new Vector3(x, 0, z), Quaternion.identity);
                c.transform.parent = transform;
            }
        }
    }

    void Update()
    {
        foreach (Transform child in transform)
        {
            child.transform.position = new Vector3(child.transform.position.x,
            heightScale * Mathf.PerlinNoise(Time.time + (child.transform.position.x * scale),
            Time.time + (child.transform.position.z * scale)),
            child.transform.position.z);
        }
    }
}


펄린 노이즈 관련 이미지

높이 지도

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PerlinColor : MonoBehaviour
{
    public int size;
    public GameObject cube;
    public float scale;
    public float m;
    bool move;
    float height;

    void Start()
    {
        move = true;

        for (int x = 0; x < size; x++)
        {
            for (int z = 0; z < size; z++)
            {
                var c = Instantiate(cube, new Vector3(x, 0, z), Quaternion.identity);

                c.transform.parent = transform;
            }
        }
    }

    void Update()
    {
        foreach (Transform child in transform)
        {
            height = Mathf.PerlinNoise(child.transform.position.x / scale, child.transform.position.z / scale);

            child.GetComponent<MeshRenderer>().material.color = new Color(height, height, height, height);
        }

        if (move)
        {
            foreach (Transform child in transform)
            {
                height = Mathf.PerlinNoise(child.transform.position.x / scale, child.transform.position.z / scale);
                child.transform.position = new Vector3(child.transform.position.x, Mathf.RoundToInt(height * m), child.transform.position.z);

                // Mathf.RoundToInt() : 매개변수로 받은 두 정수중 가까운 정수를 반환합니다.
            }
        }
    }
}


펄린 노이즈 관련 이미지

지역

using UnityEngine;

public class TerrainGenerator : MonoBehaviour
{
    public int depth = 20;

    public int width = 256;
    public int height = 256;

    public float scale = 20f;

    public float offsetX = 100f;
    public float offsetY = 100f;

    private void Start()
    {
        offsetX = Random.Range(0f, 99999f);
        offsetY = Random.Range(0f, 99999f);
    }

    private void Update()
    {
        Terrain terrain = GetComponent<Terrain>();
        terrain.terrainData = GenerateTerrain(terrain.terrainData);

        //offsetX += Time.deltaTime * 5f;
    }

    TerrainData GenerateTerrain(TerrainData terrainData)
    {
        terrainData.heightmapResolution = width + 1;

        terrainData.size = new Vector3(width, depth, height);
        terrainData.SetHeights(0, 0, GenerateHeights());

        return terrainData;
    }

    float(,) GenerateHeights()
    {
        float(,) heights = new float(width, height);

        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                heights(x, y) = CalculateHeight(x, y);
            }
        }

        return heights;
    }

    float CalculateHeight(int x, int y)
    {
        float xCoord = (float)x / width * scale + offsetX;
        float yCoord = (float)y / height * scale + offsetY;

        return Mathf.PerlinNoise(xCoord, yCoord);
    }
}


펄린 노이즈 관련 이미지


펄린 노이즈 관련 이미지

Similar Posts

  • 4/7-9 프로야구 주말시리즈 결과

    (누에) 삼성 라이온즈 / LG 트윈스 4/7 LG 승리4/8 LG 승리4/9 LG 승리 오스틴 딘 웬푸징 LG 트윈스가 오승환을 상대로 오스틴, 이승현을 상대로 문보경을 제치고 2일 연속 삼성시리즈를 싹쓸이하는 등 이번 주말 삼성시리즈를 싹쓸이했다. (사직하다) KT 위즈 / 롯데 자이언츠 4/7 KT 원4/8 KT 원4/9 라쿠텐 우승 박병호 장바이후 8일 박병호는 롯데 스트렐리와의 3회말 맹수…

  • 터키 지도, 지진 상태

    앞으로 앞으로 0 하나 2 삼 4 5 6 7 8 9 10 11 12 13 14 반응형 터키 지진 지도의 위치를 ​​자세히 살펴보겠습니다. 현지 시간으로 6일 오전, 규모 7.8의 지진이 터키 남부와 시리아를 강타했습니다. 현재 알려진 바 뉴스에 따르면 최소 1,800명이 사망하고 거의 10,000명이 부상당했습니다. 터키 지진 지도 위치 지진은 자고 있던 새벽에 닥쳤다고…

  • 지루한 직업

    20230204 강화도전망대 지루한 직업 답답할 땐 사방팔방으로 싸운다. 두들겨 맞고 쓰러졌을 때, ‘아, 이 침침함 때문에 내 안의 예수님이 빛나네 하겠다’는 생각을 하셔야 합니다. 그 빛 때문에 어떤 어려움이 닥쳐도 이것은 다른 사람에게 위로가 됩니다. 김양재 목사의 가족 유언

  • 파파야 차 효능 및 영양 정보

    파파야 차 효능 및 영양 정보 모과차는 모과로 만든 차입니다.. 중국과 한국의 모과, 일본에서 온 과일., 다양한 건강상의 이점 때문에 오늘날에도 가장 인기 있는 과일 중 하나입니다.. 파파야로 만든 차는 쓴맛이 나고 건강한 성분을 함유하고 있습니다., 다양한 건강 문제의 예방과 치료를 기대할 수 있습니다.. 반품, 파파야 차는 설탕 함량이 낮고 당뇨병 환자에게 적합합니다.. 모과 차는…

  • Quine 및 지수 이론

    Quine의 확장 이론과 Quine의 확장 이론,그리고에서 주장하는 철학 사이의 관계 어떤 사람들은 콰인의 외연주의가 그 철학적 입장과 무관한 개인적인 취향이라고 생각하지만, 콰인 자신은 그렇게 생각하지 않으며, 나도 이해하지 못한다. Quine이 확장주의를 따르는 것은 우연이 아닙니다. 콰인의 철학의 확립, 철학적 분석은 다른 개별 과학과는 다른 분야에 속하지만 개별 과학에서 언어 사용의 보편성을 보장해야 합니다. 이러한 이유로…

  • (O Q&A) 9:00 “Kanu Coffee Machine” 실시간 Q&A/(2월 16일 Q&A)

    휴대폰 홈 화면에서 ‘질의응답실‘ 바로가기 만들기 시험 감정답변 : 9시에 공유합니다. (파이 코인) 돈을 벌 수 있는 무료 마이닝 코인↓※ 1일 1열쇠 매일 수만 원 이상의 수익을 올릴 것으로 예상됩니다.전 세계 4천만 명 이상의 채굴자▼ 받는 방법 ▼https://quizbang./8090 ( Pi Coin _ Pi ) 무료 마이닝 코인 _ 가입 / 실드 / 지갑 설치 /…