赞
踩
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public static class Utils {
- // 分形布朗运动函数,静态方法方便从外部调用
- //要在热点地图中传递一个位置x,y,
-
- public static float fBM(float x, float y, int oct, float persistance)
- {
- float total = 0;
- float frequency = 1;//频率是波的紧密程度,一个八度(octave)对应着频率上的加倍或减半
- float amplitude = 1;//振幅
- float maxValue = 0;
- for (int i = 0; i < oct; i++)//遍历每个八度
- {
-
- total += Mathf.PerlinNoise(x * frequency, y * frequency) * amplitude;//通过循环叠加噪声
- maxValue += amplitude;//最大值是每个oct使用的振幅相加
- amplitude *= persistance;//以一定的比例降低噪声的振幅
- frequency *= 2;//以一定的倍数连续升高频率
- }
-
- return total / maxValue;//注意返回值降到0到1之间的范围内
- }
-
- }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- using System.Linq;
- using UnityEditor;
-
- public class CustomTerrain : MonoBehaviour {
- //首先,我们需要一些值来存储缩放值,而这些是我们将其发送到噪声函数以确定幅度的间隔,
- //如上一篇所讲,随着增量值的变大,噪音曲线将变得更加平滑
- //PERLIN NOISE ----------------------------------------------
- public float perlinXScale = 0.01f;
- public float perlinYScale = 0.01f;
- //地形整体沿X和y偏移
- public int perlinOffsetX = 0;
- public int perlinOffsetY = 0;
- //分型布朗运动新增属性
- public int perlinOctaves = 3;
- public float perlinPersistance = 8;
- public float perlinHeightScale = 0.09f;
-
- //获取地形(物体)对象
- public Terrain terrain;
- //TerrainData里有地形所有的数据
- public TerrainData terrainData;
- //每次编辑完脚本内容返回编辑器时运行后会执行此方法
- private void OnEnable()
- {
- //初始化地形数据 先获取地形再获取本身的地形数据
- terrain = gameObject.GetComponent<Terrain>();
- terrainData = Terrain.activeTerrain.terrainData;
- }
- public void Perlin()
- {
- float[,] heightMap = terrainData.GetHeights(0, 0, terrainData.heightmapWidth,
- terrainData.heightmapHeight);
- for (int y = 0; y < terrainData.heightmapHeight; y++)
- {
- for (int x = 0; x < terrainData.heightmapWidth; x++)
- {
- //heightMap[x, y] = Mathf.PerlinNoise((x + perlinOffsetX) * perlinXScale,
- // (y + perlinOffsetY) * perlinYScale);
- //分型布朗运动
- heightMap[x, y] += Utils.fBM((x + perlinOffsetX) * perlinXScale,
- (y + perlinOffsetY) * perlinYScale,
- perlinOctaves,
- perlinPersistance) * perlinHeightScale;
-
- }
- }
- terrainData.SetHeights(0, 0, heightMap);
- }
-
- }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- [CustomEditor(typeof(CustomTerrain))]
- public class CustomTerrainEditor : Editor {
- SerializedProperty perlinXScale;
- SerializedProperty perlinYScale;
- SerializedProperty perlinOffsetX;
- SerializedProperty perlinOffsetY;
- //分型布朗运动新增属性
- SerializedProperty perlinOctaves;
- SerializedProperty perlinPersistance;
- SerializedProperty perlinHeightScale;
- bool showPerlinNoise = false;
- private void OnEnable()
- {
- //找到需要反复更改的属性
- //地形幅度大小缩放值
- perlinXScale = serializedObject.FindProperty("perlinXScale");
- perlinYScale = serializedObject.FindProperty("perlinYScale");
- //地形偏移量
- perlinOffsetX = serializedObject.FindProperty("perlinOffsetX");
- perlinOffsetY = serializedObject.FindProperty("perlinOffsetY");
- //分型布朗运动新增属性
- perlinOctaves = serializedObject.FindProperty("perlinOctaves");
- perlinPersistance = serializedObject.FindProperty("perlinPersistance");
- perlinHeightScale = serializedObject.FindProperty("perlinHeightScale");
-
-
- }
- //绘制编辑面板
- public override void OnInspectorGUI()
- {
- //更新所有序列化的值
- serializedObject.Update();
- //获取自定义的地形属性脚本组件
- CustomTerrain terrain = (CustomTerrain)target;
- #region
- showPerlinNoise = EditorGUILayout.Foldout(showPerlinNoise, "Single Perlin Noise");
- if (showPerlinNoise)
- {
- EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
- GUILayout.Label("Perlin Noise", EditorStyles.boldLabel);
- EditorGUILayout.Slider(perlinXScale, 0, 1, new GUIContent("X Scale"));
- EditorGUILayout.Slider(perlinYScale, 0, 1, new GUIContent("Y Scale"));
- EditorGUILayout.IntSlider(perlinOffsetX, 0, 10000, new GUIContent("Offset X"));
- EditorGUILayout.IntSlider(perlinOffsetY, 0, 10000, new GUIContent("Offset Y"));
- EditorGUILayout.IntSlider(perlinOctaves, 1, 10, new GUIContent("Octaves"));
- EditorGUILayout.Slider(perlinPersistance, 0.1f, 10, new GUIContent("Persistance"));
- EditorGUILayout.Slider(perlinHeightScale, 0, 1, new GUIContent("Height Scale"));
- if (GUILayout.Button("Perlin"))
- {
- terrain.Perlin();
- }
- }
- #endregion
- //应用发生的所有更改
- serializedObject.ApplyModifiedProperties();
-
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。