u3d change terrain textrue&height
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的更多相关文章
- 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 ...
- ArcGIS空间分析工具
1. 3D分析 1.1. 3D Features toolset 工具 工具 描述 3D Features toolset (3D 要素工具集) Add Z Information 添加 Z 信息 添 ...
- KVC 和 KVO
KVC 键值编码 全称是Key-value coding,翻译成键值编码.它提供了一种使用字符串而不是访问器方法去访问一个对象实例变量的机制. 1.通过key(成员变量的名称)设置 ...
- VC CComboBox用法总结
VC每日一练,虽然简单,不动手试一下不能真正记住. 大气象 CComboBox *comboBox=(CComboBox*)GetDlgItem(IDC_COMBO1); comboBox->I ...
- JAVA生成图片缩略图、JAVA截取图片局部内容
package com.ares.image.test; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; ...
- overlay-3
if(typeof Shadowbox=="undefined"){ throw"Unable to load Shadowbox, no base library ad ...
- Web 在线文件管理器学习笔记与总结(2)显示文件列表(名称,类型,大小,可读,可写,可执行,创建时间,修改时间,访问时间)
主要函数: filetype() 判断文件类型 filesize() 得到文件大小(字节) is_readable() 判断文件是否可读 is_writeable() 判断文件是否可写 is_exec ...
- unity3d 基于物理渲染的问题解决
最近1个月做了unity 次世代开发的一些程序方面的支持工作,当然也是基于物理渲染相关的,主要还是skyshop marmoset的使用吧,他算是unity4.x版本 PBR的优秀方案之一了但在使用以 ...
- v4l2采集视频和图片源码
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h&g ...
随机推荐
- 「Java Web」主页静态化的实现
一个站点的主页一般不会频繁变动,而大多数用户在訪问站点时不过浏览一下主页(未登陆).然后就离开了.对于这类訪问请求.假设每次都要通过查询数据库来显示主页的话,显然会给server带来多余的压力. 这时 ...
- OpenGl学习 SelectObject函数
SelectObject 函数功能:该函数选择一对象到指定的设备上下文环境中,该新对象替换先前的相同类型的对象. 函数原型:HGDIOBJ SelectObject(HDC hdc, HGDIOB ...
- MYSQL加入远程用户或同意远程訪问三种方法
加入远程用户admin密码为password GRANT ALL PRIVILEGES ON *.* TO admin@localhost IDENTIFIED BY \'password\' WIT ...
- Spring-两种配置容器
Spring提供了两种容器类型 SpringIOC容器是一个IOC Service Provider.提供了两种容器类型:BeanFactory和ApplicationContext.Sp ...
- 在线升级java的版本
真接rpm包安装javaxxxx.rpm 然后调整/etc/profile 如下: # tail -n /etc/profile JAVA_HOME=/usr/java/jdk1..0_45 PATH ...
- git 从远程git服务上拉代码 git服务器非默认端口
从服务器上拉代码有如下报错: fatal: Not a git repository (or any of the parent directories): .git 初始代本地版本库: [root@ ...
- oracle中用SQL语句创建和管理表
表名和列名的命名规则: 必须以字母开头 必须在1-30个字符之间 只能包含A-Z,a-z,0-9,_,$,# 不能与用户定义的其它对象重名 不能使用ORACLE的保留字 创建前具备的条件: CREAT ...
- webRTC开启摄像头
配置htts之后就可以开启webRTC了. <!DOCTYPE html> <html> <head> <title>OpenCamera</ti ...
- pyQt绘图
def paintEvent(self, e): qp = QtGui.QPainter() qp.begin(self) self.DrawChessBoard(qp) self.Draw_Ches ...
- C#中的委托应该定义在哪里
专业回答 千锋教育 中国移动互联网研发培训领导品牌 2016-01-08 14:28 需求情况而定,一般定义在与类定义平级部分,且用public修饰,便于外部的调用. 若定义于类的内部,则必须通过调用 ...