mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 00:22:43 +08:00
@@ -3,4 +3,5 @@
|
||||
*/
|
||||
|
||||
export * from './height-field';
|
||||
export * from './terrain';
|
||||
export * from './terrain';
|
||||
export * from './terrain-asset';
|
||||
@@ -0,0 +1,347 @@
|
||||
/**
|
||||
* @category terrain
|
||||
*/
|
||||
import { Asset } from "../core/assets";
|
||||
import { ccclass } from "../core/data/class-decorator";
|
||||
|
||||
export const TERRAIN_DATA_VERSION = 0x01010001;
|
||||
export const TERRAIN_DATA_VERSION2 = 0x01010002;
|
||||
export const TERRAIN_DATA_VERSION_DEFAULT = 0x01010111;
|
||||
|
||||
|
||||
export class TerrainBuffer {
|
||||
public Length: number = 0;
|
||||
public Buffer: Uint8Array = new Uint8Array(2048);
|
||||
private _dview: DataView = new DataView(this.Buffer.buffer);
|
||||
private _seekPos: number = 0;
|
||||
|
||||
public Reserve (size: number) {
|
||||
if (this.Buffer.byteLength > size) {
|
||||
return;
|
||||
}
|
||||
|
||||
let capacity = this.Buffer.byteLength;
|
||||
while (capacity < size) {
|
||||
capacity += capacity;
|
||||
}
|
||||
|
||||
let temp = new Uint8Array(capacity);
|
||||
for (let i = 0; i < this.Length; ++i) {
|
||||
temp[i] = this.Buffer[i];
|
||||
}
|
||||
|
||||
this.Buffer = temp;
|
||||
this._dview = new DataView(this.Buffer.buffer);
|
||||
}
|
||||
|
||||
public Assign (buff: Uint8Array) {
|
||||
this.Buffer = buff;
|
||||
this.Length = buff.length;
|
||||
this._seekPos = buff.byteOffset;
|
||||
this._dview = new DataView(buff.buffer);
|
||||
}
|
||||
|
||||
public WriteInt8 (value: number) {
|
||||
this.Reserve(this.Length + 1);
|
||||
|
||||
this._dview.setInt8(this.Length, value);
|
||||
this.Length += 1;
|
||||
}
|
||||
|
||||
public WriteInt16 (value: number) {
|
||||
this.Reserve(this.Length + 2);
|
||||
|
||||
this._dview.setInt16(this.Length, value, true);
|
||||
this.Length += 2;
|
||||
}
|
||||
|
||||
public WriteInt32 (value: number) {
|
||||
this.Reserve(this.Length + 4);
|
||||
|
||||
this._dview.setInt32(this.Length, value, true);
|
||||
this.Length += 4;
|
||||
}
|
||||
|
||||
public WriteIntArray (value: number[]) {
|
||||
this.Reserve(this.Length + 4 * value.length);
|
||||
|
||||
for (let i = 0; i < value.length; ++i) {
|
||||
this._dview.setInt32(this.Length + i * 4, value[i], true);
|
||||
}
|
||||
this.Length += 4 * value.length;
|
||||
}
|
||||
|
||||
public WriteFloat (value: number) {
|
||||
this.Reserve(this.Length + 4);
|
||||
|
||||
this._dview.setFloat32(this.Length, value, true);
|
||||
this.Length += 4;
|
||||
}
|
||||
|
||||
public WriteFloatArray (value: number[]) {
|
||||
this.Reserve(this.Length + 4 * value.length);
|
||||
|
||||
for (let i = 0; i < value.length; ++i) {
|
||||
this._dview.setFloat32(this.Length + i * 4, value[i], true);
|
||||
}
|
||||
this.Length += 4 * value.length;
|
||||
}
|
||||
|
||||
public WriteString (value: string) {
|
||||
this.Reserve(this.Length + value.length + 4);
|
||||
|
||||
this._dview.setInt32(this.Length, value.length, true);
|
||||
for (let i = 0; i < value.length; ++i) {
|
||||
this._dview.setInt8(this.Length + 4 + i, value.charCodeAt(i));
|
||||
}
|
||||
this.Length += value.length + 4;
|
||||
}
|
||||
|
||||
public ReadInt8 () {
|
||||
let value = this._dview.getInt8(this._seekPos);
|
||||
this._seekPos += 1;
|
||||
return value;
|
||||
}
|
||||
|
||||
public ReadInt16 () {
|
||||
let value = this._dview.getInt16(this._seekPos, true);
|
||||
this._seekPos += 2;
|
||||
return value;
|
||||
}
|
||||
|
||||
public ReadInt () {
|
||||
let value = this._dview.getInt32(this._seekPos, true);
|
||||
this._seekPos += 4;
|
||||
return value;
|
||||
}
|
||||
|
||||
public ReadIntArray (value: number[]) {
|
||||
for (let i = 0; i < value.length; ++i) {
|
||||
value[i] = this._dview.getInt32(this._seekPos + i * 4, true);
|
||||
}
|
||||
this._seekPos += 4 * value.length;
|
||||
return value;
|
||||
}
|
||||
|
||||
public ReadFloat () {
|
||||
let value = this._dview.getFloat32(this._seekPos, true);
|
||||
this._seekPos += 4;
|
||||
return value;
|
||||
}
|
||||
|
||||
public ReadFloatArray (value: number[]) {
|
||||
for (let i = 0; i < value.length; ++i) {
|
||||
value[i] = this._dview.getFloat32(this._seekPos + i * 4, true);
|
||||
}
|
||||
this._seekPos += 4 * value.length;
|
||||
return value;
|
||||
}
|
||||
|
||||
public ReadString () {
|
||||
let length = this.ReadInt();
|
||||
|
||||
let value = "";
|
||||
for (let i = 0; i < length; ++i) {
|
||||
value += String.fromCharCode(this.ReadInt8());
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
@ccclass('cc.TerrainAsset')
|
||||
export class TerrainAsset extends Asset
|
||||
{
|
||||
protected _data: Uint8Array|null = null;
|
||||
protected _tileSize: number = 1;
|
||||
protected _blockCount: number[] = [1, 1];
|
||||
protected _weightMapSize: number = 128;
|
||||
protected _lightMapSize: number = 128;
|
||||
protected _heights: Uint16Array = new Uint16Array;
|
||||
protected _weights: Uint8Array = new Uint8Array;
|
||||
protected _layerBuffer: number[] = [-1, -1, -1, -1];
|
||||
|
||||
constructor () {
|
||||
super();
|
||||
this.loaded = false;
|
||||
}
|
||||
|
||||
get _nativeAsset (): ArrayBuffer {
|
||||
return this._data!.buffer;
|
||||
}
|
||||
|
||||
set _nativeAsset (value: ArrayBuffer) {
|
||||
if (this._data && this._data.byteLength === value.byteLength) {
|
||||
this._data.set(new Uint8Array(value));
|
||||
if (cc.loader._cache[this.nativeUrl]) {
|
||||
cc.loader._cache[this.nativeUrl].content = this._data.buffer;
|
||||
}
|
||||
}
|
||||
else {
|
||||
this._data = new Uint8Array(value);
|
||||
}
|
||||
|
||||
this._loadNativeData(this._data);
|
||||
this.loaded = true;
|
||||
this.emit('load');
|
||||
}
|
||||
|
||||
set tileSize (value: number) {
|
||||
this._tileSize = value;
|
||||
}
|
||||
|
||||
get tileSize () {
|
||||
return this._tileSize;
|
||||
}
|
||||
|
||||
set blockCount (value: number[]) {
|
||||
this._blockCount = value;
|
||||
}
|
||||
|
||||
get blockCount () {
|
||||
return this._blockCount;
|
||||
}
|
||||
|
||||
set lightMapSize (value: number) {
|
||||
this._lightMapSize = value;
|
||||
}
|
||||
|
||||
get lightMapSize () {
|
||||
return this._lightMapSize;
|
||||
}
|
||||
|
||||
set weightMapSize (value: number) {
|
||||
this._weightMapSize = value;
|
||||
}
|
||||
|
||||
get weightMapSize () {
|
||||
return this._weightMapSize;
|
||||
}
|
||||
|
||||
set heights (value: Uint16Array) {
|
||||
this._heights = value;
|
||||
}
|
||||
|
||||
get heights () {
|
||||
return this._heights;
|
||||
}
|
||||
|
||||
set weights (value: Uint8Array) {
|
||||
this._weights = value;
|
||||
}
|
||||
|
||||
get weights () {
|
||||
return this._weights;
|
||||
}
|
||||
|
||||
set layerBuffer(value: number[]) {
|
||||
this._layerBuffer = value;
|
||||
}
|
||||
|
||||
get layerBuffer() {
|
||||
return this._layerBuffer;
|
||||
}
|
||||
|
||||
public getLayer (xblock: number, yblock: number, layerId: number) {
|
||||
let blockId = yblock * this.blockCount[0] + xblock;
|
||||
let index = blockId * 4 + layerId;
|
||||
|
||||
if (xblock < this.blockCount[0] && yblock < this.blockCount[1] && index < this._layerBuffer.length) {
|
||||
return this._layerBuffer[index];
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public _setNativeData(_nativeData: Uint8Array) {
|
||||
this._data = _nativeData;
|
||||
}
|
||||
|
||||
public _loadNativeData (_nativeData: Uint8Array) {
|
||||
let stream = new TerrainBuffer();
|
||||
stream.Assign(_nativeData);
|
||||
|
||||
// version
|
||||
let version = stream.ReadInt();
|
||||
if (version == TERRAIN_DATA_VERSION_DEFAULT) {
|
||||
return true;
|
||||
}
|
||||
if (version != TERRAIN_DATA_VERSION &&
|
||||
version != TERRAIN_DATA_VERSION2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// geometry info
|
||||
this.tileSize = stream.ReadFloat();
|
||||
stream.ReadIntArray(this._blockCount);
|
||||
this.weightMapSize = stream.ReadInt16();
|
||||
this.lightMapSize = stream.ReadInt16();
|
||||
|
||||
// heights
|
||||
let heightBufferSize = stream.ReadInt();
|
||||
this.heights = new Uint16Array(heightBufferSize);
|
||||
for (let i = 0; i < this.heights.length; ++i) {
|
||||
this.heights[i] = stream.ReadInt16();
|
||||
}
|
||||
|
||||
// weights
|
||||
let WeightBufferSize = stream.ReadInt();
|
||||
this.weights = new Uint8Array(WeightBufferSize);
|
||||
for (let i = 0; i < this.weights.length; ++i) {
|
||||
this.weights[i] = stream.ReadInt8();
|
||||
}
|
||||
|
||||
// layer buffer
|
||||
if (version != TERRAIN_DATA_VERSION2) {
|
||||
let layerBufferSize = stream.ReadInt();
|
||||
this.layerBuffer = new Array<number>(layerBufferSize);
|
||||
for (let i = 0; i < this.weights.length; ++i) {
|
||||
this.layerBuffer[i] = stream.ReadInt16();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public _exportNativeData() : Uint8Array {
|
||||
let stream = new TerrainBuffer();
|
||||
|
||||
// version
|
||||
stream.WriteInt32(TERRAIN_DATA_VERSION2);
|
||||
|
||||
// geometry info
|
||||
stream.WriteFloat(this.tileSize);
|
||||
stream.WriteIntArray(this._blockCount);
|
||||
stream.WriteInt16(this.weightMapSize);
|
||||
stream.WriteInt16(this.lightMapSize);
|
||||
|
||||
// heights
|
||||
stream.WriteInt32(this.heights.length);
|
||||
for (let i = 0; i < this.heights.length; ++i) {
|
||||
stream.WriteInt16(this.heights[i]);
|
||||
}
|
||||
|
||||
// weights
|
||||
stream.WriteInt32(this.weights.length);
|
||||
for (let i = 0; i < this.weights.length; ++i) {
|
||||
stream.WriteInt8(this.weights[i]);
|
||||
}
|
||||
|
||||
// layer buffer
|
||||
stream.WriteInt32(this.layerBuffer.length);
|
||||
for (let i = 0; i < this.layerBuffer.length; ++i) {
|
||||
stream.WriteInt16(this.layerBuffer[i]);
|
||||
}
|
||||
|
||||
return stream.Buffer;
|
||||
}
|
||||
|
||||
public _exportDefaultNativeData() : Uint8Array {
|
||||
let stream = new TerrainBuffer();
|
||||
|
||||
stream.WriteInt32(TERRAIN_DATA_VERSION_DEFAULT);
|
||||
|
||||
return stream.Buffer;
|
||||
|
||||
}
|
||||
}
|
||||
+319
-147
@@ -19,7 +19,9 @@ import { Model } from '../core/renderer/scene/model';
|
||||
import { PrivateNode } from '../core/scene-graph/private-node';
|
||||
import { IDefineMap } from '../core/renderer/core/pass';
|
||||
import { HeightField } from './height-field';
|
||||
import { TerrainAsset } from './terrain-asset';
|
||||
import { director } from '../core/director';
|
||||
import { validateMethodWithProps } from '../core/data/utils/preprocess-class';
|
||||
|
||||
export const TERRAIN_MAX_LEVELS = 4;
|
||||
export const TERRAIN_MAX_BLEND_LAYERS = 4;
|
||||
@@ -31,6 +33,8 @@ export const TERRAIN_NORTH_INDEX = 0;
|
||||
export const TERRAIN_SOUTH_INDEX = 1;
|
||||
export const TERRAIN_WEST_INDEX = 2;
|
||||
export const TERRAIN_EAST_INDEX = 3;
|
||||
export const TERRAIN_HEIGHT_BASE = 32768;
|
||||
export const TERRAIN_HEIGHT_FACTORY = 1.0 / 512.0;
|
||||
|
||||
/**
|
||||
* 地形信息。
|
||||
@@ -39,40 +43,35 @@ export const TERRAIN_EAST_INDEX = 3;
|
||||
export class TerrainInfo {
|
||||
@property
|
||||
public tileSize: number = 1;
|
||||
|
||||
@property
|
||||
public blockCount: number[] = [1, 1];
|
||||
|
||||
@property
|
||||
public weightMapSize: number = 128;
|
||||
|
||||
@property
|
||||
public lightMapSize: number = 128;
|
||||
|
||||
//
|
||||
protected _tileCount: number[] = [0, 0];
|
||||
protected _vertexCount: number[] = [0, 0];
|
||||
protected _size: Size = new Size(0, 0);
|
||||
public get size () {
|
||||
let sz = new Size(0, 0);
|
||||
sz.width = this.blockCount[0] * TERRAIN_BLOCK_TILE_COMPLEXITY * this.tileSize;
|
||||
sz.height = this.blockCount[1] * TERRAIN_BLOCK_TILE_COMPLEXITY * this.tileSize;
|
||||
|
||||
public initialize () {
|
||||
this._tileCount[0] = this.blockCount[0] * TERRAIN_BLOCK_TILE_COMPLEXITY;
|
||||
this._tileCount[1] = this.blockCount[1] * TERRAIN_BLOCK_TILE_COMPLEXITY;
|
||||
this._vertexCount[0] = this._tileCount[0] + 1;
|
||||
this._vertexCount[1] = this._tileCount[1] + 1;
|
||||
this._size.width = this._tileCount[0] * this.tileSize;
|
||||
this._size.height = this._tileCount[1] * this.tileSize;
|
||||
return sz;
|
||||
}
|
||||
|
||||
public get tileCount () {
|
||||
return this._tileCount;
|
||||
let _tileCount = [0, 0];
|
||||
_tileCount[0] = this.blockCount[0] * TERRAIN_BLOCK_TILE_COMPLEXITY;
|
||||
_tileCount[1] = this.blockCount[1] * TERRAIN_BLOCK_TILE_COMPLEXITY;
|
||||
|
||||
return _tileCount;
|
||||
}
|
||||
|
||||
public get vertexCount () {
|
||||
return this._vertexCount;
|
||||
}
|
||||
let _vertexCount = this.tileCount;
|
||||
_vertexCount[0] += 1;
|
||||
_vertexCount[1] += 1;
|
||||
|
||||
public get size () {
|
||||
return this._size;
|
||||
return _vertexCount;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,7 +130,7 @@ export class TerrainRenderable extends RenderableComponent {
|
||||
this._currentMaterial = new Material();
|
||||
|
||||
this._currentMaterial.initialize({
|
||||
effectAsset: EffectAsset.get('builtin-terrain'),
|
||||
effectAsset: cc.EffectAsset.get('builtin-terrain'),
|
||||
defines: block._getMaterialDefines(nlayers),
|
||||
});
|
||||
|
||||
@@ -181,6 +180,11 @@ export class TerrainRenderable extends RenderableComponent {
|
||||
export class TerrainBlockInfo {
|
||||
@property
|
||||
public layers: number[] = [-1, -1, -1, -1];
|
||||
public lightMap: Texture2D|null = null;
|
||||
public lightMapUOff: number = 0;
|
||||
public lightMapVOff: number = 0;
|
||||
public lightMapUScale: number = 0;
|
||||
public lightMapVScale: number = 0;
|
||||
}
|
||||
|
||||
export class TerrainBlock {
|
||||
@@ -303,7 +307,7 @@ export class TerrainBlock {
|
||||
mtl.setProperty('detailMap0', l0 != null ? l0.detailMap : null);
|
||||
}
|
||||
else {
|
||||
mtl.setProperty('detailMap0', builtinResMgr.get('default-texture'));
|
||||
mtl.setProperty('detailMap0', cc.builtinResMgr.get('default-texture'));
|
||||
}
|
||||
}
|
||||
else if (nlayers === 1) {
|
||||
@@ -368,6 +372,11 @@ export class TerrainBlock {
|
||||
}
|
||||
|
||||
mtl.setProperty('UVScale', uvScale);
|
||||
|
||||
if (this.lightmap != null) {
|
||||
mtl.setProperty('lightMap', this.lightmap);
|
||||
mtl.setProperty('lightMapUVParam', this.lightmapUVParam);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -382,6 +391,14 @@ export class TerrainBlock {
|
||||
return this._info.layers;
|
||||
}
|
||||
|
||||
get lightmap () {
|
||||
return this._info.lightMap;
|
||||
}
|
||||
|
||||
get lightmapUVParam () {
|
||||
return new Vec4(this._info.lightMapUOff, this._info.lightMapVOff, this._info.lightMapUScale, this._info.lightMapVScale);
|
||||
}
|
||||
|
||||
public getTerrain () {
|
||||
return this._terrain;
|
||||
}
|
||||
@@ -428,17 +445,33 @@ export class TerrainBlock {
|
||||
}
|
||||
|
||||
public _getMaterialDefines (nlayers: number): IDefineMap {
|
||||
if (nlayers === 0) {
|
||||
return { LAYERS: 1 };
|
||||
if (this.lightmap != null) {
|
||||
if (nlayers === 0) {
|
||||
return { LAYERS: 1, LIGHT_MAP: 1};
|
||||
}
|
||||
else if (nlayers === 1) {
|
||||
return { LAYERS: 2, LIGHT_MAP: 1 };
|
||||
}
|
||||
else if (nlayers === 2) {
|
||||
return { LAYERS: 3, LIGHT_MAP: 1 };
|
||||
}
|
||||
else if (nlayers === 3) {
|
||||
return { LAYERS: 4, LIGHT_MAP: 1 };
|
||||
}
|
||||
}
|
||||
else if (nlayers === 1) {
|
||||
return { LAYERS: 2 };
|
||||
}
|
||||
else if (nlayers === 2) {
|
||||
return { LAYERS: 3 };
|
||||
}
|
||||
else if (nlayers === 3) {
|
||||
return { LAYERS: 4 };
|
||||
else {
|
||||
if (nlayers === 0) {
|
||||
return { LAYERS: 1 };
|
||||
}
|
||||
else if (nlayers === 1) {
|
||||
return { LAYERS: 2 };
|
||||
}
|
||||
else if (nlayers === 2) {
|
||||
return { LAYERS: 3 };
|
||||
}
|
||||
else if (nlayers === 3) {
|
||||
return { LAYERS: 4 };
|
||||
}
|
||||
}
|
||||
|
||||
return { LAYERS: 0 };
|
||||
@@ -497,18 +530,18 @@ export class TerrainBlock {
|
||||
else {
|
||||
if (this._weightMap == null) {
|
||||
this._weightMap = new Texture2D();
|
||||
this._weightMap.create(this._terrain.info.weightMapSize, this._terrain.info.weightMapSize, PixelFormat.RGBA8888);
|
||||
this._weightMap.create(this._terrain.weightMapSize, this._terrain.weightMapSize, PixelFormat.RGBA8888);
|
||||
this._weightMap.setFilters(Filter.LINEAR, Filter.LINEAR);
|
||||
this._weightMap.setWrapMode(WrapMode.CLAMP_TO_EDGE, WrapMode.CLAMP_TO_EDGE);
|
||||
}
|
||||
}
|
||||
|
||||
const weightData = new Uint8Array(this._terrain.info.weightMapSize * this._terrain.info.weightMapSize * 4);
|
||||
const weightData = new Uint8Array(this._terrain.weightMapSize * this._terrain.weightMapSize * 4);
|
||||
let weightIndex = 0;
|
||||
for (let j = 0; j < this._terrain.info.weightMapSize; ++j) {
|
||||
for (let i = 0; i < this._terrain.info.weightMapSize; ++i) {
|
||||
const x = this._index[0] * this._terrain.info.weightMapSize + i;
|
||||
const y = this._index[1] * this._terrain.info.weightMapSize + j;
|
||||
for (let j = 0; j < this._terrain.weightMapSize; ++j) {
|
||||
for (let i = 0; i < this._terrain.weightMapSize; ++i) {
|
||||
const x = this._index[0] * this._terrain.weightMapSize + i;
|
||||
const y = this._index[1] * this._terrain.weightMapSize + j;
|
||||
const w = this._terrain.getWeight(x, y);
|
||||
|
||||
weightData[weightIndex * 4 + 0] = Math.floor(w.x * 255);
|
||||
@@ -521,6 +554,15 @@ export class TerrainBlock {
|
||||
}
|
||||
this._weightMap.uploadData(weightData.buffer);
|
||||
}
|
||||
|
||||
public _updateLightmap(tex: Texture2D|null, uoff: number, voff: number, uscale: number, vscale: number) {
|
||||
this._info.lightMap = tex;
|
||||
this._info.lightMapUOff = uoff;
|
||||
this._info.lightMapVOff = voff;
|
||||
this._info.lightMapUScale = uscale;
|
||||
this._info.lightMapVScale = vscale;
|
||||
this._invalidMaterial();
|
||||
}
|
||||
}
|
||||
|
||||
@ccclass('cc.Terrain')
|
||||
@@ -528,32 +570,28 @@ export class TerrainBlock {
|
||||
@executeInEditMode
|
||||
export class Terrain extends Component {
|
||||
@property({
|
||||
type: TerrainInfo,
|
||||
visible: true,
|
||||
type: TerrainAsset,
|
||||
visible: false,
|
||||
})
|
||||
protected _info: TerrainInfo = new TerrainInfo();
|
||||
|
||||
protected __asset: TerrainAsset|null = null;
|
||||
|
||||
@property({
|
||||
type: TerrainLayer,
|
||||
visible: true,
|
||||
})
|
||||
protected _layers: Array<TerrainLayer|null> = [];
|
||||
|
||||
@property({
|
||||
visible: false,
|
||||
})
|
||||
protected _heights: number[] = [];
|
||||
|
||||
@property({
|
||||
visible: false,
|
||||
})
|
||||
protected _weights: Uint8Array = new Uint8Array();
|
||||
|
||||
@property({
|
||||
visible: false,
|
||||
})
|
||||
protected _blockInfos: TerrainBlockInfo[] = [];
|
||||
|
||||
protected _tileSize: number = 1;
|
||||
protected _blockCount: number[] = [1, 1];
|
||||
protected _weightMapSize: number = 128;
|
||||
protected _lightMapSize: number = 128;
|
||||
protected _heights: Uint16Array = new Uint16Array;
|
||||
protected _weights: Uint8Array = new Uint8Array;
|
||||
protected _normals: number[] = [];
|
||||
protected _blocks: TerrainBlock[] = [];
|
||||
protected _sharedIndexBuffer: GFXBuffer|null = null;
|
||||
@@ -567,36 +605,119 @@ export class Terrain extends Component {
|
||||
}
|
||||
}
|
||||
|
||||
@property({
|
||||
type: TerrainAsset,
|
||||
visible: true,
|
||||
serializable: false,
|
||||
})
|
||||
public set _asset(value: TerrainAsset|null) {
|
||||
if (this.__asset != value) {
|
||||
this.__asset = value;
|
||||
if (this.__asset != null && this.valid) {
|
||||
// rebuild
|
||||
for (const block of this._blocks) {
|
||||
block.destroy();
|
||||
}
|
||||
this._blocks = [];
|
||||
this._blockInfos = [];
|
||||
this._buildImp();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public get _asset() {
|
||||
return this.__asset;
|
||||
}
|
||||
|
||||
public get size () {
|
||||
let sz = new Size(0, 0);
|
||||
sz.width = this.blockCount[0] * TERRAIN_BLOCK_TILE_COMPLEXITY * this.tileSize;
|
||||
sz.height = this.blockCount[1] * TERRAIN_BLOCK_TILE_COMPLEXITY * this.tileSize;
|
||||
return sz;
|
||||
}
|
||||
|
||||
get tileSize () {
|
||||
return this._tileSize;
|
||||
}
|
||||
|
||||
public get tileCount () {
|
||||
let _tileCount = [0, 0];
|
||||
_tileCount[0] = this.blockCount[0] * TERRAIN_BLOCK_TILE_COMPLEXITY;
|
||||
_tileCount[1] = this.blockCount[1] * TERRAIN_BLOCK_TILE_COMPLEXITY;
|
||||
|
||||
return _tileCount;
|
||||
}
|
||||
|
||||
public get vertexCount () {
|
||||
let _vertexCount = this.tileCount;
|
||||
_vertexCount[0] += 1;
|
||||
_vertexCount[1] += 1;
|
||||
|
||||
return _vertexCount;
|
||||
}
|
||||
|
||||
get blockCount () {
|
||||
return this._blockCount;
|
||||
}
|
||||
|
||||
get lightMapSize () {
|
||||
return this._lightMapSize;
|
||||
}
|
||||
|
||||
get weightMapSize () {
|
||||
return this._weightMapSize;
|
||||
}
|
||||
|
||||
get heights () {
|
||||
return this._heights;
|
||||
}
|
||||
|
||||
get weights () {
|
||||
return this._weights;
|
||||
}
|
||||
|
||||
get valid () {
|
||||
return this._blocks.length > 0;
|
||||
}
|
||||
|
||||
@property({
|
||||
type: TerrainInfo,
|
||||
visible: true,
|
||||
})
|
||||
public get info () {
|
||||
return this._info;
|
||||
let ti = new TerrainInfo;
|
||||
ti.tileSize = this.tileSize;
|
||||
ti.blockCount[0] = this.blockCount[0];
|
||||
ti.blockCount[1] = this.blockCount[1];
|
||||
ti.weightMapSize = this.weightMapSize;
|
||||
ti.lightMapSize = this.lightMapSize;
|
||||
|
||||
return ti;
|
||||
}
|
||||
|
||||
public build (info: TerrainInfo) {
|
||||
this._info.tileSize = info.tileSize;
|
||||
this._info.blockCount[0] = info.blockCount[0];
|
||||
this._info.blockCount[1] = info.blockCount[1];
|
||||
this._info.weightMapSize = info.weightMapSize;
|
||||
this._info.lightMapSize = info.lightMapSize;
|
||||
this._info.initialize();
|
||||
this._tileSize = info.tileSize;
|
||||
this._blockCount[0] = info.blockCount[0];
|
||||
this._blockCount[1] = info.blockCount[1];
|
||||
this._weightMapSize = info.weightMapSize;
|
||||
this._lightMapSize = info.lightMapSize;
|
||||
|
||||
return this._buildImp();
|
||||
}
|
||||
|
||||
public rebuild (info: TerrainInfo) {
|
||||
info.initialize();
|
||||
|
||||
// build block info
|
||||
const blockInfos: TerrainBlockInfo[] = [];
|
||||
for (let i = 0; i < info.blockCount[0] * info.blockCount[1]; ++i) {
|
||||
blockInfos.push(new TerrainBlockInfo());
|
||||
}
|
||||
|
||||
const w = Math.min(this.info.blockCount[0], info.blockCount[0]);
|
||||
const h = Math.min(this.info.blockCount[1], info.blockCount[1]);
|
||||
const w = Math.min(this._blockCount[0], info.blockCount[0]);
|
||||
const h = Math.min(this._blockCount[1], info.blockCount[1]);
|
||||
for (let j = 0; j < h; ++j) {
|
||||
for (let i = 0; i < w; ++i) {
|
||||
const index0 = j * info.vertexCount[0] + i;
|
||||
const index1 = j * this.info.vertexCount[0] + i;
|
||||
const index1 = j * this.vertexCount[0] + i;
|
||||
|
||||
blockInfos[index0] = this._blockInfos[index1];
|
||||
}
|
||||
@@ -616,18 +737,17 @@ export class Terrain extends Component {
|
||||
this._rebuildWeights(info);
|
||||
|
||||
// update info
|
||||
this._info.tileSize = info.tileSize;
|
||||
this._info.blockCount[0] = info.blockCount[0];
|
||||
this._info.blockCount[1] = info.blockCount[1];
|
||||
this._info.weightMapSize = info.weightMapSize;
|
||||
this._info.lightMapSize = info.lightMapSize;
|
||||
this._info.initialize();
|
||||
this._tileSize = info.tileSize;
|
||||
this._blockCount[0] = info.blockCount[0];
|
||||
this._blockCount[1] = info.blockCount[1];
|
||||
this._weightMapSize = info.weightMapSize;
|
||||
this._lightMapSize = info.lightMapSize;
|
||||
|
||||
// build blocks
|
||||
this._buildNormals();
|
||||
|
||||
for (let j = 0; j < this._info.blockCount[1]; ++j) {
|
||||
for (let i = 0; i < this._info.blockCount[0]; ++i) {
|
||||
for (let j = 0; j < this._blockCount[1]; ++j) {
|
||||
for (let i = 0; i < this._blockCount[0]; ++i) {
|
||||
this._blocks.push(new TerrainBlock(this, i, j));
|
||||
}
|
||||
}
|
||||
@@ -639,10 +759,10 @@ export class Terrain extends Component {
|
||||
|
||||
public importHeightField (hf: HeightField, heightScale: number) {
|
||||
let index = 0;
|
||||
for (let j = 0; j < this._info.vertexCount[1]; ++j) {
|
||||
for (let i = 0; i < this._info.vertexCount[0]; ++i) {
|
||||
const u = i / this._info.tileCount[0];
|
||||
const v = j / this._info.tileCount[1];
|
||||
for (let j = 0; j < this.vertexCount[1]; ++j) {
|
||||
for (let i = 0; i < this.vertexCount[0]; ++i) {
|
||||
const u = i / this.tileCount[0];
|
||||
const v = j / this.tileCount[1];
|
||||
|
||||
const h = hf.getAt(u * hf.w, v * hf.h) * heightScale;
|
||||
|
||||
@@ -665,8 +785,8 @@ export class Terrain extends Component {
|
||||
const u = i / (hf.w - 1);
|
||||
const v = j / (hf.h - 1);
|
||||
|
||||
const x = u * this._info.size.width;
|
||||
const y = v * this._info.size.height;
|
||||
const x = u * this.size.width;
|
||||
const y = v * this.size.height;
|
||||
|
||||
const h = this.getHeightAt(x, y);
|
||||
if (h != null) {
|
||||
@@ -676,8 +796,31 @@ export class Terrain extends Component {
|
||||
}
|
||||
}
|
||||
|
||||
public exportAsset() {
|
||||
let asset = this._asset;
|
||||
if (asset == null) {
|
||||
asset = new TerrainAsset;
|
||||
}
|
||||
|
||||
asset.tileSize = this.tileSize;
|
||||
asset.blockCount = this.blockCount;
|
||||
asset.lightMapSize = this.lightMapSize;
|
||||
asset.weightMapSize = this.weightMapSize;
|
||||
asset.heights = this.heights;
|
||||
asset.weights = this.weights;
|
||||
asset.layerBuffer = new Array<number>(this._blocks.length * 4);
|
||||
for (let i = 0; i < this._blocks.length; ++i) {
|
||||
asset.layerBuffer[i * 4 + 0] + this._blocks[i].layers[0];
|
||||
asset.layerBuffer[i * 4 + 1] + this._blocks[i].layers[1];
|
||||
asset.layerBuffer[i * 4 + 2] + this._blocks[i].layers[2];
|
||||
asset.layerBuffer[i * 4 + 3] + this._blocks[i].layers[3];
|
||||
}
|
||||
|
||||
return asset;
|
||||
}
|
||||
|
||||
public onLoad () {
|
||||
const gfxDevice = director.root!.device as GFXDevice;
|
||||
const gfxDevice = cc.director.root.device as GFXDevice;
|
||||
|
||||
// initialize shared index buffer
|
||||
const indexData = new Uint16Array(TERRAIN_BLOCK_TILE_COMPLEXITY * TERRAIN_BLOCK_TILE_COMPLEXITY * 6);
|
||||
@@ -770,31 +913,35 @@ export class Terrain extends Component {
|
||||
}
|
||||
|
||||
public getPosition (i: number, j: number) {
|
||||
const x = i * this._info.tileSize;
|
||||
const z = j * this._info.tileSize;
|
||||
const x = i * this._tileSize;
|
||||
const z = j * this._tileSize;
|
||||
const y = this.getHeight(i, j);
|
||||
|
||||
return new Vec3(x, y, z);
|
||||
}
|
||||
|
||||
public getHeightField () {
|
||||
return this._heights;
|
||||
}
|
||||
|
||||
public setHeight (i: number, j: number, h: number) {
|
||||
this._heights[j * this._info.vertexCount[0] + i] = h;
|
||||
this._heights[j * this.vertexCount[0] + i] = TERRAIN_HEIGHT_BASE + h / TERRAIN_HEIGHT_FACTORY;
|
||||
}
|
||||
|
||||
public getHeight (i: number, j: number) {
|
||||
return this._heights[j * this._info.vertexCount[0] + i];
|
||||
return (this._heights[j * this.vertexCount[0] + i] - TERRAIN_HEIGHT_BASE) * TERRAIN_HEIGHT_FACTORY;
|
||||
}
|
||||
|
||||
public getHeightClamp (i: number, j: number) {
|
||||
i = clamp(i, 0, this._info.vertexCount[0] - 1);
|
||||
j = clamp(j, 0, this._info.vertexCount[1] - 1);
|
||||
i = clamp(i, 0, this.vertexCount[0] - 1);
|
||||
j = clamp(j, 0, this.vertexCount[1] - 1);
|
||||
|
||||
return this.getHeight(i, j);
|
||||
}
|
||||
|
||||
public getHeightAt (x: number, y: number) {
|
||||
const fx = x / this._info.vertexCount[0];
|
||||
const fy = y / this._info.vertexCount[1];
|
||||
const fx = x / this.vertexCount[0];
|
||||
const fy = y / this.vertexCount[1];
|
||||
|
||||
let ix0 = Math.floor(fx);
|
||||
let iz0 = Math.floor(fy);
|
||||
@@ -803,14 +950,14 @@ export class Terrain extends Component {
|
||||
const dx = fx - ix0;
|
||||
const dz = fy - iz0;
|
||||
|
||||
if (ix0 < 0 || ix0 > this._info.vertexCount[0] - 1 || iz0 < 0 || iz0 > this._info.vertexCount[1] - 1) {
|
||||
if (ix0 < 0 || ix0 > this.vertexCount[0] - 1 || iz0 < 0 || iz0 > this.vertexCount[1] - 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ix0 = clamp(ix0, 0, this._info.vertexCount[0] - 1);
|
||||
iz0 = clamp(iz0, 0, this._info.vertexCount[1] - 1);
|
||||
ix1 = clamp(ix1, 0, this._info.vertexCount[0] - 1);
|
||||
iz1 = clamp(iz1, 0, this._info.vertexCount[1] - 1);
|
||||
ix0 = clamp(ix0, 0, this.vertexCount[0] - 1);
|
||||
iz0 = clamp(iz0, 0, this.vertexCount[1] - 1);
|
||||
ix1 = clamp(ix1, 0, this.vertexCount[0] - 1);
|
||||
iz1 = clamp(iz1, 0, this.vertexCount[1] - 1);
|
||||
|
||||
let a = this.getHeight(ix0, iz0);
|
||||
const b = this.getHeight(ix1, iz0);
|
||||
@@ -834,7 +981,7 @@ export class Terrain extends Component {
|
||||
}
|
||||
|
||||
public _setNormal (i: number, j: number, n: Vec3) {
|
||||
const index = j * this._info.vertexCount[0] + i;
|
||||
const index = j * this.vertexCount[0] + i;
|
||||
|
||||
this._normals[index * 3 + 0] = n.x;
|
||||
this._normals[index * 3 + 1] = n.y;
|
||||
@@ -842,7 +989,7 @@ export class Terrain extends Component {
|
||||
}
|
||||
|
||||
public getNormal (i: number, j: number) {
|
||||
const index = j * this._info.vertexCount[0] + i;
|
||||
const index = j * this.vertexCount[0] + i;
|
||||
|
||||
const n = new Vec3();
|
||||
n.x = this._normals[index * 3 + 0];
|
||||
@@ -853,8 +1000,8 @@ export class Terrain extends Component {
|
||||
}
|
||||
|
||||
public getNormalAt (x: number, y: number) {
|
||||
const fx = x / this._info.vertexCount[0];
|
||||
const fy = y / this._info.vertexCount[1];
|
||||
const fx = x / this.vertexCount[0];
|
||||
const fy = y / this.vertexCount[1];
|
||||
|
||||
let ix0 = Math.floor(fx);
|
||||
let iz0 = Math.floor(fy);
|
||||
@@ -863,14 +1010,14 @@ export class Terrain extends Component {
|
||||
const dx = fx - ix0;
|
||||
const dz = fy - iz0;
|
||||
|
||||
if (ix0 < 0 || ix0 > this._info.vertexCount[0] - 1 || iz0 < 0 || iz0 > this._info.vertexCount[1] - 1) {
|
||||
if (ix0 < 0 || ix0 > this.vertexCount[0] - 1 || iz0 < 0 || iz0 > this.vertexCount[1] - 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ix0 = clamp(ix0, 0, this._info.vertexCount[0] - 1);
|
||||
iz0 = clamp(iz0, 0, this._info.vertexCount[1] - 1);
|
||||
ix1 = clamp(ix1, 0, this._info.vertexCount[0] - 1);
|
||||
iz1 = clamp(iz1, 0, this._info.vertexCount[1] - 1);
|
||||
ix0 = clamp(ix0, 0, this.vertexCount[0] - 1);
|
||||
iz0 = clamp(iz0, 0, this.vertexCount[1] - 1);
|
||||
ix1 = clamp(ix1, 0, this.vertexCount[0] - 1);
|
||||
iz1 = clamp(iz1, 0, this.vertexCount[1] - 1);
|
||||
|
||||
const a = this.getNormal(ix0, iz0);
|
||||
const b = this.getNormal(ix1, iz0);
|
||||
@@ -903,7 +1050,7 @@ export class Terrain extends Component {
|
||||
}
|
||||
|
||||
public setWeight (i: number, j: number, w: Vec4) {
|
||||
const index = j * this.info.weightMapSize * this.info.blockCount[0] + i;
|
||||
const index = j * this._weightMapSize * this._blockCount[0] + i;
|
||||
|
||||
this._weights[index * 4 + 0] = w.x * 255;
|
||||
this._weights[index * 4 + 1] = w.y * 255;
|
||||
@@ -912,7 +1059,7 @@ export class Terrain extends Component {
|
||||
}
|
||||
|
||||
public getWeight (i: number, j: number) {
|
||||
const index = j * this.info.weightMapSize * this.info.blockCount[0] + i;
|
||||
const index = j * this._weightMapSize * this._blockCount[0] + i;
|
||||
|
||||
const w = new Vec4();
|
||||
w.x = this._weights[index * 4 + 0] / 255.0;
|
||||
@@ -924,8 +1071,8 @@ export class Terrain extends Component {
|
||||
}
|
||||
|
||||
public getWeightAt (x: number, y: number) {
|
||||
const fx = x / this._info.vertexCount[0];
|
||||
const fy = y / this._info.vertexCount[1];
|
||||
const fx = x / this.vertexCount[0];
|
||||
const fy = y / this.vertexCount[1];
|
||||
|
||||
let ix0 = Math.floor(fx);
|
||||
let iz0 = Math.floor(fy);
|
||||
@@ -934,14 +1081,14 @@ export class Terrain extends Component {
|
||||
const dx = fx - ix0;
|
||||
const dz = fy - iz0;
|
||||
|
||||
if (ix0 < 0 || ix0 > this._info.vertexCount[0] - 1 || iz0 < 0 || iz0 > this._info.vertexCount[1] - 1) {
|
||||
if (ix0 < 0 || ix0 > this.vertexCount[0] - 1 || iz0 < 0 || iz0 > this.vertexCount[1] - 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ix0 = clamp(ix0, 0, this._info.vertexCount[0] - 1);
|
||||
iz0 = clamp(iz0, 0, this._info.vertexCount[1] - 1);
|
||||
ix1 = clamp(ix1, 0, this._info.vertexCount[0] - 1);
|
||||
iz1 = clamp(iz1, 0, this._info.vertexCount[1] - 1);
|
||||
ix0 = clamp(ix0, 0, this.vertexCount[0] - 1);
|
||||
iz0 = clamp(iz0, 0, this.vertexCount[1] - 1);
|
||||
ix1 = clamp(ix1, 0, this.vertexCount[0] - 1);
|
||||
iz1 = clamp(iz1, 0, this.vertexCount[1] - 1);
|
||||
|
||||
let a = this.getWeight(ix0, iz0);
|
||||
const b = this.getWeight(ix1, iz0);
|
||||
@@ -970,11 +1117,11 @@ export class Terrain extends Component {
|
||||
}
|
||||
|
||||
public getBlockInfo (i: number, j: number) {
|
||||
return this._blockInfos[j * this._info.blockCount[0] + i];
|
||||
return this._blockInfos[j * this._blockCount[0] + i];
|
||||
}
|
||||
|
||||
public getBlock (i: number, j: number) {
|
||||
return this._blocks[j * this._info.blockCount[0] + i];
|
||||
return this._blocks[j * this._blockCount[0] + i];
|
||||
}
|
||||
|
||||
public getBlocks () {
|
||||
@@ -1009,6 +1156,16 @@ export class Terrain extends Component {
|
||||
}
|
||||
}
|
||||
else {
|
||||
// 优先大步进查找
|
||||
while (i++ < MAX_COUNT) {
|
||||
const y = this.getHeightAt(trace.x, trace.z);
|
||||
if (y != null && trace.y <= y) {
|
||||
break;
|
||||
}
|
||||
|
||||
trace.add(dir);
|
||||
}
|
||||
|
||||
// 穷举法
|
||||
while (i++ < MAX_COUNT) {
|
||||
const y = this.getHeightAt(trace.x, trace.z);
|
||||
@@ -1030,7 +1187,7 @@ export class Terrain extends Component {
|
||||
let right: Vec3;
|
||||
let up: Vec3;
|
||||
|
||||
if (x < this._info.vertexCount[0] - 1) {
|
||||
if (x < this.vertexCount[0] - 1) {
|
||||
right = this.getPosition(x + 1, z);
|
||||
}
|
||||
else {
|
||||
@@ -1038,7 +1195,7 @@ export class Terrain extends Component {
|
||||
right = this.getPosition(x - 1, z);
|
||||
}
|
||||
|
||||
if (z < this._info.vertexCount[1] - 1) {
|
||||
if (z < this.vertexCount[1] - 1) {
|
||||
up = this.getPosition(x, z + 1);
|
||||
}
|
||||
else {
|
||||
@@ -1060,8 +1217,8 @@ export class Terrain extends Component {
|
||||
|
||||
public _buildNormals () {
|
||||
let index = 0;
|
||||
for (let y = 0; y < this._info.vertexCount[1]; ++y) {
|
||||
for (let x = 0; x < this._info.vertexCount[0]; ++x) {
|
||||
for (let y = 0; y < this.vertexCount[1]; ++y) {
|
||||
for (let x = 0; x < this.vertexCount[0]; ++x) {
|
||||
const n = this._calcuNormal(x, y);
|
||||
|
||||
this._normals[index * 3 + 0] = n.x;
|
||||
@@ -1073,24 +1230,31 @@ export class Terrain extends Component {
|
||||
}
|
||||
|
||||
private _buildImp () {
|
||||
if (this._blocks.length > 0) {
|
||||
if (this.valid) {
|
||||
return true;
|
||||
}
|
||||
|
||||
this._info.initialize();
|
||||
if (this.__asset != null) {
|
||||
this._tileSize = this.__asset.tileSize;
|
||||
this._blockCount = this.__asset.blockCount;
|
||||
this._weightMapSize = this.__asset.weightMapSize;
|
||||
this._lightMapSize = this.__asset.lightMapSize;
|
||||
this._heights = this.__asset.heights;
|
||||
this._weights = this.__asset.weights;
|
||||
}
|
||||
|
||||
if (this._info.blockCount[0] === 0 || this._info.blockCount[1] === 0) {
|
||||
if (this._blockCount[0] === 0 || this._blockCount[1] === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// build heights & normals
|
||||
const vcount = this._info.vertexCount[0] * this._info.vertexCount[1];
|
||||
const vcount = this.vertexCount[0] * this.vertexCount[1];
|
||||
if (this._heights.length !== vcount) {
|
||||
this._heights = new Array<number>(vcount);
|
||||
this._heights = new Uint16Array(vcount);
|
||||
this._normals = new Array<number>(vcount * 3);
|
||||
|
||||
for (let i = 0; i < vcount; ++i) {
|
||||
this._heights[i] = 0;
|
||||
this._heights[i] = TERRAIN_HEIGHT_BASE;
|
||||
this._normals[i * 3 + 0] = 0;
|
||||
this._normals[i * 3 + 1] = 1;
|
||||
this._normals[i * 3 + 2] = 0;
|
||||
@@ -1102,8 +1266,8 @@ export class Terrain extends Component {
|
||||
}
|
||||
|
||||
// initialize weights
|
||||
const weightMapComplexityU = this.info.weightMapSize * this.info.blockCount[0];
|
||||
const weightMapComplexityV = this.info.weightMapSize * this.info.blockCount[1];
|
||||
const weightMapComplexityU = this._weightMapSize * this._blockCount[0];
|
||||
const weightMapComplexityV = this._weightMapSize * this._blockCount[1];
|
||||
if (this._weights.length !== weightMapComplexityU * weightMapComplexityV * 4) {
|
||||
this._weights = new Uint8Array(weightMapComplexityU * weightMapComplexityV * 4);
|
||||
for (let i = 0; i < weightMapComplexityU * weightMapComplexityV; ++i) {
|
||||
@@ -1115,17 +1279,25 @@ export class Terrain extends Component {
|
||||
}
|
||||
|
||||
// build blocks
|
||||
if (this._blockInfos.length !== this.info.blockCount[0] * this.info.blockCount[1]) {
|
||||
if (this._blockInfos.length !== this._blockCount[0] * this._blockCount[1]) {
|
||||
this._blockInfos = [];
|
||||
for (let j = 0; j < this.info.blockCount[1]; ++j) {
|
||||
for (let i = 0; i < this.info.blockCount[0]; ++i) {
|
||||
this._blockInfos.push(new TerrainBlockInfo());
|
||||
for (let j = 0; j < this._blockCount[1]; ++j) {
|
||||
for (let i = 0; i < this._blockCount[0]; ++i) {
|
||||
let info = new TerrainBlockInfo();
|
||||
if (this._asset != null) {
|
||||
info.layers[0] = this._asset.getLayer(i, j, 0);
|
||||
info.layers[1] = this._asset.getLayer(i, j, 1);
|
||||
info.layers[2] = this._asset.getLayer(i, j, 2);
|
||||
info.layers[3] = this._asset.getLayer(i, j, 3);
|
||||
}
|
||||
|
||||
this._blockInfos.push(info);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (let j = 0; j < this.info.blockCount[1]; ++j) {
|
||||
for (let i = 0; i < this.info.blockCount[0]; ++i) {
|
||||
for (let j = 0; j < this._blockCount[1]; ++j) {
|
||||
for (let i = 0; i < this._blockCount[0]; ++i) {
|
||||
this._blocks.push(new TerrainBlock(this, i, j));
|
||||
}
|
||||
}
|
||||
@@ -1136,23 +1308,23 @@ export class Terrain extends Component {
|
||||
}
|
||||
|
||||
private _rebuildHeights (info: TerrainInfo) {
|
||||
if (this.info.vertexCount[0] === info.vertexCount[0] &&
|
||||
this.info.vertexCount[1] === info.vertexCount[1]) {
|
||||
if (this.vertexCount[0] === info.vertexCount[0] &&
|
||||
this.vertexCount[1] === info.vertexCount[1]) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const heights = new Array<number>(info.vertexCount[0] * info.vertexCount[1]);
|
||||
const heights = new Uint16Array(info.vertexCount[0] * info.vertexCount[1]);
|
||||
for (let i = 0; i < heights.length; ++i) {
|
||||
heights[i] = 0;
|
||||
heights[i] = TERRAIN_HEIGHT_BASE;
|
||||
}
|
||||
|
||||
const w = Math.min(this.info.vertexCount[0], info.vertexCount[0]);
|
||||
const h = Math.min(this.info.vertexCount[1], info.vertexCount[1]);
|
||||
const w = Math.min(this.vertexCount[0], info.vertexCount[0]);
|
||||
const h = Math.min(this.vertexCount[1], info.vertexCount[1]);
|
||||
|
||||
for (let j = 0; j < h; ++j) {
|
||||
for (let i = 0; i < w; ++i) {
|
||||
const index0 = j * info.vertexCount[0] + i;
|
||||
const index1 = j * this.info.vertexCount[0] + i;
|
||||
const index1 = j * this.vertexCount[0] + i;
|
||||
|
||||
heights[index0] = this._heights[index1];
|
||||
}
|
||||
@@ -1164,9 +1336,9 @@ export class Terrain extends Component {
|
||||
}
|
||||
|
||||
private _rebuildWeights (info: TerrainInfo) {
|
||||
const oldWeightMapSize = this.info.weightMapSize;
|
||||
const oldWeightMapComplexityU = this.info.weightMapSize * this.info.blockCount[0];
|
||||
const oldWeightMapComplexityV = this.info.weightMapSize * this.info.blockCount[1];
|
||||
const oldWeightMapSize = this._weightMapSize;
|
||||
const oldWeightMapComplexityU = this._weightMapSize * this._blockCount[0];
|
||||
const oldWeightMapComplexityV = this._weightMapSize * this._blockCount[1];
|
||||
|
||||
const weightMapComplexityU = info.weightMapSize * info.blockCount[0];
|
||||
const weightMapComplexityV = info.weightMapSize * info.blockCount[1];
|
||||
@@ -1185,8 +1357,8 @@ export class Terrain extends Component {
|
||||
weights[i * 4 + 3] = 0;
|
||||
}
|
||||
|
||||
const w = Math.min(info.blockCount[0], this.info.blockCount[0]);
|
||||
const h = Math.min(info.blockCount[1], this.info.blockCount[1]);
|
||||
const w = Math.min(info.blockCount[0], this._blockCount[0]);
|
||||
const h = Math.min(info.blockCount[1], this._blockCount[1]);
|
||||
|
||||
// get weight
|
||||
const getOldWeight = (_i: number, _j: number, _weights: Uint8Array) => {
|
||||
@@ -1244,20 +1416,20 @@ export class Terrain extends Component {
|
||||
const uoff = i * oldWeightMapSize;
|
||||
const voff = j * oldWeightMapSize;
|
||||
|
||||
for (let v = 0; v < this.info.weightMapSize; ++v) {
|
||||
for (let u = 0; u < this.info.weightMapSize; ++u) {
|
||||
for (let v = 0; v < this._weightMapSize; ++v) {
|
||||
for (let u = 0; u < this._weightMapSize; ++u) {
|
||||
let w: Vec4;
|
||||
if (info.weightMapSize == oldWeightMapSize) {
|
||||
w = getOldWeight(u + uoff, v + voff, this._weights);
|
||||
}
|
||||
else {
|
||||
const x = u / (this.info.weightMapSize - 1) * (oldWeightMapSize - 1);
|
||||
const y = v / (this.info.weightMapSize - 1) * (oldWeightMapSize - 1);
|
||||
const x = u / (this._weightMapSize - 1) * (oldWeightMapSize - 1);
|
||||
const y = v / (this._weightMapSize - 1) * (oldWeightMapSize - 1);
|
||||
w = sampleOldWeight(x, y, uoff, voff, this._weights);
|
||||
}
|
||||
|
||||
const du = i * this.info.weightMapSize + u;
|
||||
const dv = j * this.info.weightMapSize + v;
|
||||
const du = i * this._weightMapSize + u;
|
||||
const dv = j * this._weightMapSize + v;
|
||||
const index = dv * weightMapComplexityU + du;
|
||||
|
||||
weights[index * 4 + 0] = w.x * 255;
|
||||
|
||||
Reference in New Issue
Block a user