using UnityEngine;
using System.Collections; public class terrainTest : MonoBehaviour
{
public int terrainDeformationTextureNum = ;
private Terrain terr; // terrain to modify
protected int hmWidth; // heightmap width
protected int hmHeight; // heightmap height
protected int alphaMapWidth;
protected int alphaMapHeight;
protected int numOfAlphaLayers;
protected const float DEPTH_METER_CONVERT = 0.05f;
protected const float TEXTURE_SIZE_MULTIPLIER = 1.25f;
private float[,] heightMapBackup;
private float[, ,] alphaMapBackup; void Start()
{
terr = this.GetComponent<Terrain>();
hmWidth = terr.terrainData.heightmapWidth;
hmHeight = terr.terrainData.heightmapHeight;
alphaMapWidth = terr.terrainData.alphamapWidth;
alphaMapHeight = terr.terrainData.alphamapHeight;
numOfAlphaLayers = terr.terrainData.alphamapLayers;
if (Debug.isDebugBuild)
{
heightMapBackup = terr.terrainData.GetHeights(, , hmWidth, hmHeight);
alphaMapBackup = terr.terrainData.GetAlphamaps(, , alphaMapWidth, alphaMapHeight);
}
} //this has to be done because terrains for some reason or another terrains don't reset after you run the app
void OnApplicationQuit()
{
if (Debug.isDebugBuild)
{
terr.terrainData.SetHeights(, , heightMapBackup);
terr.terrainData.SetAlphamaps(, , alphaMapBackup);
}
} public void DestroyTerrain(Vector3 pos, float craterSizeInMeters)
{
DeformTerrain(pos, craterSizeInMeters);
TextureDeformation(pos, craterSizeInMeters * 1.5f);
} public void DeformTerrain(Vector3 pos, float craterSizeInMeters)
{
//get the heights only once keep it and reuse, precalculate as much as possible
Vector3 terrainPos = GetRelativeTerrainPositionFromPos(pos, terr, hmWidth, hmHeight);//terr.terrainData.heightmapResolution/terr.terrainData.heightmapWidth
int heightMapCraterWidth = (int)(craterSizeInMeters * (hmWidth / terr.terrainData.size.x));
int heightMapCraterLength = (int)(craterSizeInMeters * (hmHeight / terr.terrainData.size.z));
int heightMapStartPosX = (int)(terrainPos.x - (heightMapCraterWidth / ));
int heightMapStartPosZ = (int)(terrainPos.z - (heightMapCraterLength / )); float[,] heights = terr.terrainData.GetHeights(heightMapStartPosX, heightMapStartPosZ, heightMapCraterWidth, heightMapCraterLength);
float circlePosX;
float circlePosY;
float distanceFromCenter;
float depthMultiplier; float deformationDepth = (craterSizeInMeters / 3.0f) / terr.terrainData.size.y; // we set each sample of the terrain in the size to the desired height
for (int i = ; i < heightMapCraterLength; i++) //width
{
for (int j = ; j < heightMapCraterWidth; j++) //height
{ circlePosX = (j - (heightMapCraterWidth / )) / (hmWidth / terr.terrainData.size.x);
circlePosY = (i - (heightMapCraterLength / )) / (hmHeight / terr.terrainData.size.z);
distanceFromCenter = Mathf.Abs(Mathf.Sqrt(circlePosX * circlePosX + circlePosY * circlePosY));
//convert back to values without skew if (distanceFromCenter < (craterSizeInMeters / 2.0f))
{
depthMultiplier = ((craterSizeInMeters / 2.0f - distanceFromCenter) / (craterSizeInMeters / 2.0f)); depthMultiplier += 0.1f;
depthMultiplier += Random.value * .1f; depthMultiplier = Mathf.Clamp(depthMultiplier, , ); if(heights[i,j] <0.1) heights[i, j] = Mathf.Clamp(heights[i, j] +deformationDepth * depthMultiplier, , ); }
}
} // set the new height
terr.terrainData.SetHeights(heightMapStartPosX, heightMapStartPosZ, heights);
} public void TextureDeformation(Vector3 pos, float craterSizeInMeters)
{
Vector3 alphaMapTerrainPos = GetRelativeTerrainPositionFromPos(pos, terr, alphaMapWidth, alphaMapHeight);
int alphaMapCraterWidth = (int)(craterSizeInMeters * (alphaMapWidth / terr.terrainData.size.x));
int alphaMapCraterLength = (int)(craterSizeInMeters * (alphaMapHeight / terr.terrainData.size.z)); int alphaMapStartPosX = (int)(alphaMapTerrainPos.x - (alphaMapCraterWidth / ));
int alphaMapStartPosZ = (int)(alphaMapTerrainPos.z - (alphaMapCraterLength / )); float[, ,] alphas = terr.terrainData.GetAlphamaps(alphaMapStartPosX, alphaMapStartPosZ, alphaMapCraterWidth, alphaMapCraterLength); float circlePosX;
float circlePosY;
float distanceFromCenter; for (int i = ; i < alphaMapCraterLength; i++) //width
{
for (int j = ; j < alphaMapCraterWidth; j++) //height
{
circlePosX = (j - (alphaMapCraterWidth / )) / (alphaMapWidth / terr.terrainData.size.x);
circlePosY = (i - (alphaMapCraterLength / )) / (alphaMapHeight / terr.terrainData.size.z); //convert back to values without skew
distanceFromCenter = Mathf.Abs(Mathf.Sqrt(circlePosX * circlePosX + circlePosY * circlePosY)); if (distanceFromCenter < (craterSizeInMeters / 2.0f))
{
for (int layerCount = ; layerCount < numOfAlphaLayers; layerCount++)
{
//could add blending here in the future
if (layerCount == terrainDeformationTextureNum)
{
alphas[i, j, layerCount] = ;
}
else
{
alphas[i, j, layerCount] = ;
}
}
}
}
} terr.terrainData.SetAlphamaps(alphaMapStartPosX, alphaMapStartPosZ, alphas);
} protected Vector3 GetNormalizedPositionRelativeToTerrain(Vector3 pos, Terrain terrain)
{
//code based on: http://answers.unity3d.com/questions/3633/modifying-terrain-height-under-a-gameobject-at-runtime
// get the normalized position of this game object relative to the terrain
Vector3 tempCoord = (pos - terrain.gameObject.transform.position);
Vector3 coord;
coord.x = tempCoord.x / terr.terrainData.size.x;
coord.y = tempCoord.y / terr.terrainData.size.y;
coord.z = tempCoord.z / terr.terrainData.size.z; return coord;
} protected Vector3 GetRelativeTerrainPositionFromPos(Vector3 pos, Terrain terrain, int mapWidth, int mapHeight)
{
Vector3 coord = GetNormalizedPositionRelativeToTerrain(pos, terrain);
// get the position of the terrain heightmap where this game object is
return new Vector3((coord.x * mapWidth), , (coord.z * mapHeight));
}
}

u3d change terrain textrue&height的更多相关文章

  1. how to change the AlexNet into FCNs ?

    How to change the AlexNet into FCNs ? FCNs is a network that only contain convolution layers and no ...

  2. ArcGIS空间分析工具

    1. 3D分析 1.1. 3D Features toolset 工具 工具 描述 3D Features toolset (3D 要素工具集) Add Z Information 添加 Z 信息 添 ...

  3. KVC 和 KVO

    KVC 键值编码    全称是Key-value coding,翻译成键值编码.它提供了一种使用字符串而不是访问器方法去访问一个对象实例变量的机制.        1.通过key(成员变量的名称)设置 ...

  4. VC CComboBox用法总结

    VC每日一练,虽然简单,不动手试一下不能真正记住. 大气象 CComboBox *comboBox=(CComboBox*)GetDlgItem(IDC_COMBO1); comboBox->I ...

  5. JAVA生成图片缩略图、JAVA截取图片局部内容

    package com.ares.image.test; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; ...

  6. overlay-3

    if(typeof Shadowbox=="undefined"){ throw"Unable to load Shadowbox, no base library ad ...

  7. Web 在线文件管理器学习笔记与总结(2)显示文件列表(名称,类型,大小,可读,可写,可执行,创建时间,修改时间,访问时间)

    主要函数: filetype() 判断文件类型 filesize() 得到文件大小(字节) is_readable() 判断文件是否可读 is_writeable() 判断文件是否可写 is_exec ...

  8. unity3d 基于物理渲染的问题解决

    最近1个月做了unity 次世代开发的一些程序方面的支持工作,当然也是基于物理渲染相关的,主要还是skyshop marmoset的使用吧,他算是unity4.x版本 PBR的优秀方案之一了但在使用以 ...

  9. v4l2采集视频和图片源码

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h&g ...

随机推荐

  1. [swift]初始化方法自己主动继承

    子类默认不会继承父类的初始化方法,然而,假设某种条件满足的话.父类的初始化方法还是能够继承给子类.在通常情况下,这意味着你不必复写父类的初始化方法.在安全的前提下能够以最低的代价继承父类的初始化方法. ...

  2. [k8s]prometheus+alertmanager二进制安装实现简单邮件告警

    本次任务是用alertmanaer发一个报警邮件 本次环境采用二进制普罗组件 本次准备监控一个节点的内存,当使用率大于2%时候(测试),发邮件报警. k8s集群使用普罗官方文档 环境准备 下载二进制h ...

  3. schema.path方法

    var sampleSchema = new Schema({ name: { type: String, required: true } }); console.log(sampleSchema. ...

  4. javaperformanceoptimization

    http://javaperformanceoptimization.nirmaldelli.com/ http://stackoverflow.com/questions/42824366/sql- ...

  5. Spring Boot干货系列:(八)数据存储篇-SQL关系型数据库之JdbcTemplate的使用

    Spring Boot干货系列:(八)数据存储篇-SQL关系型数据库之JdbcTemplate的使用 原创 2017-04-13 嘟嘟MD 嘟爷java超神学堂 前言 前面几章介绍了一些基础,但都是静 ...

  6. 检测SqlServer数据库是否能连接的小技巧

    有时候可能需要检测下某台机器的服务是不是起来了,或者某台机器的某个库是不是能被连接又不能打开ssms也不想登陆服务器的话就可以用这个方法. 1.在桌面上右键创建个文本,然后改后缀名为udl以后保存(1 ...

  7. C++ - 动态申请数组空间

    // 用指针p指向由new动态分配的长度为length*sizeof(int)的内存空间. int * p = new int[length];

  8. MD5算法实现

    MD5算法的简要叙述为: MD5以512位分组来处理输入的信息(512位分组?每次处理都取出512位数据?), 每一分组又被划分为16个32位子分组(16乘32刚好是512), 经过一些列的处理后(怎 ...

  9. Spring InitializingBean和ApplicationListener<ContextRefreshedEvent>

    事件机制作为一种编程机制,在许多语言中都提供了支持.JAVA语言也不例外,java中的事件机制的参与者有3种角色: 1.event object 2.event source 3.event list ...

  10. 标准Drupal7安装中文翻译出错解决办法

    这个问题在网上解决的方案一致都是说在\sites\default\settings.php文件,在最后增加以下两行: ini_set('memory_limit', '1280M'); //加大php ...