From fcc773db7149df233c588eb777216876d4473312 Mon Sep 17 00:00:00 2001 From: Myway Date: Tue, 24 Dec 2019 11:01:51 +0800 Subject: [PATCH] Terrain fix and lod (#5939) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 修复地形显示bug及一些规范 * 实现lod算法 --- cocos/terrain/terrain-asset.ts | 114 +++++++---- cocos/terrain/terrain-lod.ts | 335 +++++++++++++++++++++++++++++++++ cocos/terrain/terrain.ts | 157 ++++++++++----- 3 files changed, 518 insertions(+), 88 deletions(-) create mode 100644 cocos/terrain/terrain-lod.ts diff --git a/cocos/terrain/terrain-asset.ts b/cocos/terrain/terrain-asset.ts index e793ff7e37..4e9231f206 100644 --- a/cocos/terrain/terrain-asset.ts +++ b/cocos/terrain/terrain-asset.ts @@ -1,14 +1,14 @@ /** * @category terrain */ -import { Asset } from "../core/assets"; -import { ccclass } from "../core/data/class-decorator"; +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_VERSION3 = 0x01010003; export const TERRAIN_DATA_VERSION_DEFAULT = 0x01010111; - export class TerrainBuffer { public Length: number = 0; public Buffer: Uint8Array = new Uint8Array(2048); @@ -25,7 +25,7 @@ export class TerrainBuffer { capacity += capacity; } - let temp = new Uint8Array(capacity); + const temp = new Uint8Array(capacity); for (let i = 0; i < this.Length; ++i) { temp[i] = this.Buffer[i]; } @@ -98,19 +98,19 @@ export class TerrainBuffer { } public ReadInt8 () { - let value = this._dview.getInt8(this._seekPos); + const value = this._dview.getInt8(this._seekPos); this._seekPos += 1; return value; } public ReadInt16 () { - let value = this._dview.getInt16(this._seekPos, true); + const value = this._dview.getInt16(this._seekPos, true); this._seekPos += 2; return value; } public ReadInt () { - let value = this._dview.getInt32(this._seekPos, true); + const value = this._dview.getInt32(this._seekPos, true); this._seekPos += 4; return value; } @@ -124,7 +124,7 @@ export class TerrainBuffer { } public ReadFloat () { - let value = this._dview.getFloat32(this._seekPos, true); + const value = this._dview.getFloat32(this._seekPos, true); this._seekPos += 4; return value; } @@ -138,9 +138,9 @@ export class TerrainBuffer { } public ReadString () { - let length = this.ReadInt(); + const length = this.ReadInt(); - let value = ""; + let value = ''; for (let i = 0; i < length; ++i) { value += String.fromCharCode(this.ReadInt8()); } @@ -149,18 +149,24 @@ export class TerrainBuffer { } } +export class TerrainLayerInfo{ + public slot: number = 0; + public tileSize: number = 1; + public detailMap: string = ''; +} + @ccclass('cc.TerrainAsset') -export class TerrainAsset extends Asset -{ +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 _heights: Uint16Array = new Uint16Array(); + protected _weights: Uint8Array = new Uint8Array(); protected _layerBuffer: number[] = [-1, -1, -1, -1]; - + protected _layerInfos: TerrainLayerInfo[] = []; + constructor () { super(); this.loaded = false; @@ -234,40 +240,49 @@ export class TerrainAsset extends Asset return this._weights; } - set layerBuffer(value: number[]) { + set layerBuffer (value: number[]) { this._layerBuffer = value; } - get layerBuffer() { + get layerBuffer () { return this._layerBuffer; } + set layerInfos (value: TerrainLayerInfo[]) { + this._layerInfos = value; + } + + get layerInfos () { + return this._layerInfos; + } + public getLayer (xblock: number, yblock: number, layerId: number) { - let blockId = yblock * this.blockCount[0] + xblock; - let index = blockId * 4 + layerId; + const blockId = yblock * this.blockCount[0] + xblock; + const 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) { + public _setNativeData (_nativeData: Uint8Array) { this._data = _nativeData; } public _loadNativeData (_nativeData: Uint8Array) { - let stream = new TerrainBuffer(); + const stream = new TerrainBuffer(); stream.Assign(_nativeData); // version - let version = stream.ReadInt(); - if (version == TERRAIN_DATA_VERSION_DEFAULT) { + const version = stream.ReadInt(); + if (version === TERRAIN_DATA_VERSION_DEFAULT) { return true; } - if (version != TERRAIN_DATA_VERSION && - version != TERRAIN_DATA_VERSION2) { + if (version !== TERRAIN_DATA_VERSION && + version !== TERRAIN_DATA_VERSION2 && + version !== TERRAIN_DATA_VERSION3) { return false; } @@ -276,38 +291,51 @@ export class TerrainAsset extends Asset stream.ReadIntArray(this._blockCount); this.weightMapSize = stream.ReadInt16(); this.lightMapSize = stream.ReadInt16(); - + // heights - let heightBufferSize = stream.ReadInt(); + const 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(); + const 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(); + if (version >= TERRAIN_DATA_VERSION2) { + const layerBufferSize = stream.ReadInt(); this.layerBuffer = new Array(layerBufferSize); - for (let i = 0; i < this.weights.length; ++i) { + for (let i = 0; i < this.layerBuffer.length; ++i) { this.layerBuffer[i] = stream.ReadInt16(); } } - + + // layer infos + if (version >= TERRAIN_DATA_VERSION3) { + const layerInfoSize = stream.ReadInt(); + this.layerInfos = new Array(layerInfoSize); + for (let i = 0; i < this.layerInfos.length; ++i) { + this.layerInfos[i] = new TerrainLayerInfo(); + this.layerInfos[i].slot = stream.ReadInt(); + this.layerInfos[i].tileSize = stream.ReadFloat(); + this.layerInfos[i].detailMap = stream.ReadString(); + + } + } + return true; } - public _exportNativeData() : Uint8Array { - let stream = new TerrainBuffer(); + public _exportNativeData (): Uint8Array { + const stream = new TerrainBuffer(); // version - stream.WriteInt32(TERRAIN_DATA_VERSION2); + stream.WriteInt32(TERRAIN_DATA_VERSION3); // geometry info stream.WriteFloat(this.tileSize); @@ -333,15 +361,23 @@ export class TerrainAsset extends Asset stream.WriteInt16(this.layerBuffer[i]); } + // layer infos + stream.WriteInt32(this.layerInfos.length); + for (let i = 0; i < this.layerInfos.length; ++i) { + stream.WriteInt32(this.layerInfos[i].slot); + stream.WriteFloat(this.layerInfos[i].tileSize); + stream.WriteString(this.layerInfos[i].detailMap); + } + return stream.Buffer; } - public _exportDefaultNativeData() : Uint8Array { - let stream = new TerrainBuffer(); + public _exportDefaultNativeData (): Uint8Array { + const stream = new TerrainBuffer(); stream.WriteInt32(TERRAIN_DATA_VERSION_DEFAULT); return stream.Buffer; } - } \ No newline at end of file + } diff --git a/cocos/terrain/terrain-lod.ts b/cocos/terrain/terrain-lod.ts new file mode 100644 index 0000000000..f36808ac33 --- /dev/null +++ b/cocos/terrain/terrain-lod.ts @@ -0,0 +1,335 @@ +export const TERRAIN_LOD_VERTS = 33; +export const TERRAIN_LOD_TILES = 32; +export const TERRAIN_LOD_LEVELS = 4; +export const TERRAIN_LOD_NORTH_INDEX = 0; +export const TERRAIN_LOD_SOUTH_INDEX = 1; +export const TERRAIN_LOD_WEST_INDEX = 2; +export const TERRAIN_LOD_EAST_INDEX = 3; + +export class TerrainIndexPool { + public size: number = 0; + public indices: Uint16Array|null = null; +} + +export class TerrainIndexData { + public start: number; + public size: number; + public buffer: Uint16Array|null = null; + public prim_count: number; +} + +export class TerrainLodKey { + public level: number; + public north: number; + public south: number; + public west: number; + public east: number; + + public compare (rk: TerrainLodKey) { + return this.level === rk.level && this.north === rk.north && this.south === rk.south && this.west === rk.west && this.east === rk.east; + } +} + +export class TerrainLod { + public mBodyIndex: TerrainIndexPool[] = TerrainIndexPool[TERRAIN_LOD_LEVELS]; + public mConecterIndex: TerrainIndexPool[][][] = TerrainIndexPool[TERRAIN_LOD_LEVELS][TERRAIN_LOD_LEVELS][4]; + + constructor () { + for (let i = 0; i < TERRAIN_LOD_LEVELS; ++i) { + this.mBodyIndex[i] = new TerrainIndexPool(); + } + + for (let i = 0; i < TERRAIN_LOD_LEVELS; ++i) { + for (let j = 0; j < TERRAIN_LOD_LEVELS; ++j) { + for (let k = 0; k < 4; ++k) { + this.mConecterIndex[i][j][k] = new TerrainIndexPool(); + } + } + } + + this.genBodyIndex(); + this.genConecterIndex(); + + /* + for (let l = 0; l < TERRAIN_LOD_LEVELS; ++l) { + for (let n = 0; n < TERRAIN_LOD_LEVELS; ++n) { + if (n < l) { + continue; + } + + for (let s = 0; s < TERRAIN_LOD_LEVELS; ++s) { + if (s < l) { + continue; + } + + for (let w = 0; w < TERRAIN_LOD_LEVELS; ++w) { + if (w < l) { + continue; + } + + for (let e = 0; e < TERRAIN_LOD_LEVELS; ++e) { + if (e < l) { + continue; + } + + const k = new TerrainLodKey(); + k.level = l; + k.north = n; + k.south = s; + k.west = w; + k.east = e; + getIndexData(k, true); + } + } + } + } + } + */ + } + + private genBodyIndex () { + for (let i = 0; i < TERRAIN_LOD_LEVELS; ++i) { + this._genBodyIndex(i); + } + } + + private _genBodyIndex (level: number) { + const step = 1 << level; + let tiles = TERRAIN_LOD_TILES >> level; + let start = 0; + + if (level < TERRAIN_LOD_LEVELS - 1) { + tiles -= 2; + start = step * TERRAIN_LOD_VERTS + step; + } + + if (tiles === 0 || tiles === 0) { + return; + } + + const count = tiles * tiles * 6; + this.mBodyIndex[level].indices = new Uint16Array(count); + + let index = 0; + const indeces = new Uint16Array(count); + + // generate triangle list cw + // + let row_c = start; + let row_n = row_c + TERRAIN_LOD_VERTS * step; + for (let y = 0; y < tiles; ++y) { + for (let x = 0; x < tiles; ++x) { + /* + indeces[index++] = row_n + x * step; + indeces[index++] = row_c + x * step; + indeces[index++] = row_n + (x + 1) * step; + + indeces[index++] = row_n + (x + 1) * step; + indeces[index++] = row_c + x * step; + indeces[index++] = row_c + (x + 1) * step; + */ + + indeces[index++] = row_n + x * step; + indeces[index++] = row_n + (x + 1) * step; + indeces[index++] = row_c + x * step; + + indeces[index++] = row_n + (x + 1) * step; + indeces[index++] = row_c + (x + 1) * step; + indeces[index++] = row_c + x * step; + } + + row_c += TERRAIN_LOD_VERTS * step; + row_n += TERRAIN_LOD_VERTS * step; + } + + this.mBodyIndex[level].size = index; + this.mBodyIndex[level].indices = indeces; + } + + private genConecterIndex () { + for (let i = 0; i < TERRAIN_LOD_VERTS; ++i) { + for (let j = 0; j < TERRAIN_LOD_VERTS; ++j) { + this._genConecterIndexNorth(i, j); + this._genConecterIndexSouth(i, j); + this._genConecterIndexWest(i, j); + this._genConecterIndexEast(i, j); + } + } + } + + private _genConecterIndexNorth (level: number, conecter: number) { + const TN_NorthIndex = TERRAIN_LOD_NORTH_INDEX; + + if (conecter < level || level === TERRAIN_LOD_LEVELS - 1) { + this.mConecterIndex[level][conecter][TN_NorthIndex].size = 0; + this.mConecterIndex[level][conecter][TN_NorthIndex].indices = null; + return; + } + + const self_step = 1 << level; + const neighbor_step = 1 << conecter; + const self_tile = TERRAIN_LOD_TILES >> level; + const neighbor_tile = TERRAIN_LOD_TILES >> conecter; + const count = self_tile * 2 + 2; + + let index = 0; + const indeces = new Uint16Array(count); + + // starter + indeces[index++] = 0; + indeces[index++] = 0; + + // middler + for (let i = 1; i < self_tile; ++i) { + const x1 = i * self_step; + const y1 = self_step; + const x0 = x1 / neighbor_step * neighbor_step; + const y0 = y1 - self_step; + + const index0 = y1 * TERRAIN_LOD_VERTS + x1; + const index1 = y0 * TERRAIN_LOD_VERTS + x0; + + indeces[index++] = index0; + indeces[index++] = index1; + } + + // ender + indeces[index++] = TERRAIN_LOD_VERTS - 1; + indeces[index++] = TERRAIN_LOD_VERTS - 1; + + this.mConecterIndex[level][conecter][TN_NorthIndex].size = index; + this.mConecterIndex[level][conecter][TN_NorthIndex].indices = indeces; + } + + private _genConecterIndexSouth (level: number, conecter: number) { + const TN_SouthIndex = TERRAIN_LOD_SOUTH_INDEX; + + if (conecter < level || level === TERRAIN_LOD_LEVELS - 1) { + this.mConecterIndex[level][conecter][TN_SouthIndex].size = 0; + this.mConecterIndex[level][conecter][TN_SouthIndex].indices = null; + return; + } + + const self_step = 1 << level; + const neighbor_step = 1 << conecter; + const self_tile = TERRAIN_LOD_TILES >> level; + const neighbor_tile = TERRAIN_LOD_TILES >> conecter; + const count = self_tile * 2 + 2; + + let index = 0; + const indeces = new Uint16Array(count); + + // starter + indeces[index++] = TERRAIN_LOD_TILES * TERRAIN_LOD_VERTS; + indeces[index++] = TERRAIN_LOD_TILES * TERRAIN_LOD_VERTS; + + // middler + for (let i = 1; i < self_tile; ++i) { + const x0 = i * self_step; + const y0 = TERRAIN_LOD_VERTS - 1 - self_step; + const x1 = x0 / neighbor_step * neighbor_step; + const y1 = y0 + self_step; + + const index0 = y1 * TERRAIN_LOD_VERTS + x1; + const index1 = y0 * TERRAIN_LOD_VERTS + x0; + + indeces[index++] = index0; + indeces[index++] = index1; + } + + // ender + indeces[index++] = TERRAIN_LOD_VERTS * TERRAIN_LOD_VERTS - 1; + indeces[index++] = TERRAIN_LOD_VERTS * TERRAIN_LOD_VERTS - 1; + + this.mConecterIndex[level][conecter][TN_SouthIndex].size = index; + this.mConecterIndex[level][conecter][TN_SouthIndex].indices = indeces; + } + + private _genConecterIndexWest (level: number, conecter: number) { + const TN_WestIndex = TERRAIN_LOD_WEST_INDEX; + + if (conecter < level || level === TERRAIN_LOD_LEVELS - 1) { + this.mConecterIndex[level][conecter][TN_WestIndex].size = 0; + this.mConecterIndex[level][conecter][TN_WestIndex].indices = null; + return; + } + + const self_step = 1 << level; + const neighbor_step = 1 << conecter; + const self_tile = TERRAIN_LOD_TILES >> level; + const neighbor_tile = TERRAIN_LOD_TILES >> conecter; + const count = self_tile * 2 + 2; + + let index = 0; + const indeces = new Uint16Array(count); + + // starter + indeces[index++] = 0; + indeces[index++] = 0; + + // middler + for (let i = 1; i < self_tile; ++i) { + const x0 = 0; + const y0 = i * self_step / neighbor_step * neighbor_step; + const x1 = self_step; + const y1 = i * self_step; + + const index0 = y0 * TERRAIN_LOD_VERTS + x0; + const index1 = y1 * TERRAIN_LOD_VERTS + x1; + + indeces[index++] = index0; + indeces[index++] = index1; + } + + // ender + indeces[index++] = TERRAIN_LOD_TILES * TERRAIN_LOD_VERTS; + indeces[index++] = TERRAIN_LOD_TILES * TERRAIN_LOD_VERTS; + + this.mConecterIndex[level][conecter][TN_WestIndex].size = index; + this.mConecterIndex[level][conecter][TN_WestIndex].indices = indeces; + } + + private _genConecterIndexEast (level: number, conecter: number) { + const TN_EastIndex = TERRAIN_LOD_EAST_INDEX; + + if (conecter < level || level === TERRAIN_LOD_LEVELS - 1) { + this.mConecterIndex[level][conecter][TN_EastIndex].size = 0; + this.mConecterIndex[level][conecter][TN_EastIndex].indices = null; + return; + } + + const self_step = 1 << level; + const neighbor_step = 1 << conecter; + const self_tile = TERRAIN_LOD_TILES >> level; + const neighbor_tile = TERRAIN_LOD_TILES >> conecter; + const count = self_tile * 2 + 2; + + let index = 0; + const indeces = new Uint16Array(count); + + // starter + indeces[index++] = TERRAIN_LOD_VERTS - 1; + indeces[index++] = TERRAIN_LOD_VERTS - 1; + + // middler + for (let i = 1; i < self_tile; ++i) { + const x0 = TERRAIN_LOD_VERTS - 1 - self_step; + const y0 = i * self_step; + const x1 = TERRAIN_LOD_VERTS - 1; + const y1 = i * self_step / neighbor_step * neighbor_step; + + const index0 = y0 * TERRAIN_LOD_VERTS + x0; + const index1 = y1 * TERRAIN_LOD_VERTS + x1; + + indeces[index++] = index0; + indeces[index++] = index1; + } + + // ender + indeces[index++] = TERRAIN_LOD_VERTS * TERRAIN_LOD_VERTS - 1; + indeces[index++] = TERRAIN_LOD_VERTS * TERRAIN_LOD_VERTS - 1; + + this.mConecterIndex[level][conecter][TN_EastIndex].size = index; + this.mConecterIndex[level][conecter][TN_EastIndex].indices = indeces; + } +} diff --git a/cocos/terrain/terrain.ts b/cocos/terrain/terrain.ts index 7f1c985a97..11ec8ca7ec 100644 --- a/cocos/terrain/terrain.ts +++ b/cocos/terrain/terrain.ts @@ -1,28 +1,33 @@ /** * @category terrain */ - +import { builtinResMgr } from '../core/3d/builtin'; +import { Component } from '../core/components'; +import { director } from '../core/director'; +// tslint:disable-next-line: ordered-imports import { Material } from '../core/assets/material'; import { IRenderingSubmesh } from '../core/assets/mesh'; -import { builtinResMgr } from '../core/3d/builtin'; +// tslint:disable-next-line: ordered-imports import { RenderableComponent } from '../core/3d/framework/renderable-component'; -import { Texture2D, EffectAsset } from '../core/assets'; +import { Root } from '../core/root'; +// tslint:disable-next-line: ordered-imports import { Filter, PixelFormat, WrapMode } from '../core/assets/asset-enum'; -import { Component } from '../core/components'; -import { ccclass, executeInEditMode, menu, property } from '../core/data/class-decorator'; +// tslint:disable-next-line: ordered-imports +import { ccclass, executeInEditMode, menu, property, disallowMultiple } from '../core/data/class-decorator'; import { clamp, Rect, Size, Vec2, Vec3, Vec4 } from '../core/math'; +// tslint:disable-next-line: ordered-imports import { GFXBuffer } from '../core/gfx/buffer'; import { GFXAttributeName, GFXBufferUsageBit, GFXFormat, GFXMemoryUsageBit, GFXPrimitiveMode } from '../core/gfx/define'; import { GFXDevice } from '../core/gfx/device'; import { IGFXAttribute } from '../core/gfx/input-assembler'; import { Model } from '../core/renderer/scene/model'; import { PrivateNode } from '../core/scene-graph/private-node'; +// tslint:disable-next-line: ordered-imports import { IDefineMap } from '../core/renderer/core/pass'; import { HeightField } from './height-field'; -import { TerrainAsset } from './terrain-asset'; -import { director } from '../core/director'; -import { Root } from '../core/root'; -import { validateMethodWithProps } from '../core/data/utils/preprocess-class'; +// tslint:disable-next-line: ordered-imports +import { Texture2D } from '../core/assets'; +import { TerrainAsset, TerrainLayerInfo } from './terrain-asset'; export const TERRAIN_MAX_LEVELS = 4; export const TERRAIN_MAX_BLEND_LAYERS = 4; @@ -30,12 +35,12 @@ export const TERRAIN_MAX_LAYER_COUNT = 256; export const TERRAIN_BLOCK_TILE_COMPLEXITY = 32; export const TERRAIN_BLOCK_VERTEX_COMPLEXITY = 33; export const TERRAIN_BLOCK_VERTEX_SIZE = 8; // position + normal + uv +export const TERRAIN_HEIGHT_BASE = 32768; +export const TERRAIN_HEIGHT_FACTORY = 1.0 / 512.0; 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; /** * 地形信息。 @@ -43,7 +48,7 @@ export const TERRAIN_HEIGHT_FACTORY = 1.0 / 512.0; @ccclass('cc.TerrainInfo') export class TerrainInfo { @property({ - tooltip:'地形Tile的大小', + tooltip: '地形Tile的大小', }) public tileSize: number = 1; @property @@ -54,7 +59,7 @@ export class TerrainInfo { public lightMapSize: number = 128; public get size () { - let sz = new Size(0, 0); + const 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; @@ -62,7 +67,7 @@ export class TerrainInfo { } public get tileCount () { - let _tileCount = [0, 0]; + const _tileCount = [0, 0]; _tileCount[0] = this.blockCount[0] * TERRAIN_BLOCK_TILE_COMPLEXITY; _tileCount[1] = this.blockCount[1] * TERRAIN_BLOCK_TILE_COMPLEXITY; @@ -70,7 +75,7 @@ export class TerrainInfo { } public get vertexCount () { - let _vertexCount = this.tileCount; + const _vertexCount = this.tileCount; _vertexCount[0] += 1; _vertexCount[1] += 1; @@ -81,11 +86,11 @@ export class TerrainInfo { @ccclass('cc.TerrainLayer') export class TerrainLayer { @property({ - tooltip:'当前Layer的纹理', + tooltip: '当前Layer的纹理', }) public detailMap: Texture2D|null = null; @property({ - tooltip:'纹理的平铺大小,值越小会在同样大小的区域内进行更多次的平铺', + tooltip: '纹理的平铺大小,值越小会在同样大小的区域内进行更多次的平铺', }) public tileSize: number = 1; } @@ -267,6 +272,7 @@ export class TerrainBlock { this._renderable._model = (cc.director.root as Root).createModel(Model); this._renderable._model.initialize(this._node); + this._renderable._getRenderScene().addModel(this._renderable._model); // reset weightmap this._updateWeightMap(); @@ -562,7 +568,7 @@ export class TerrainBlock { this._weightMap.uploadData(weightData); } - public _updateLightmap(tex: Texture2D|null, uoff: number, voff: number, uscale: number, vscale: number) { + 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; @@ -573,8 +579,9 @@ export class TerrainBlock { } @ccclass('cc.Terrain') -@menu('Components/Terrain') +// @menu('Components/Terrain') @executeInEditMode +@disallowMultiple export class Terrain extends Component { @property({ type: TerrainAsset, @@ -597,8 +604,8 @@ export class Terrain extends Component { protected _blockCount: number[] = [1, 1]; protected _weightMapSize: number = 128; protected _lightMapSize: number = 128; - protected _heights: Uint16Array = new Uint16Array; - protected _weights: Uint8Array = new Uint8Array; + protected _heights: Uint16Array = new Uint16Array(); + protected _weights: Uint8Array = new Uint8Array(); protected _normals: number[] = []; protected _blocks: TerrainBlock[] = []; protected _sharedIndexBuffer: GFXBuffer|null = null; @@ -615,9 +622,10 @@ export class Terrain extends Component { @property({ type: TerrainAsset, visible: true, + serializable: false, }) - public set _asset(value: TerrainAsset|null) { - if (this.__asset != value) { + public set _asset (value: TerrainAsset|null) { + if (this.__asset !== value) { this.__asset = value; if (this.__asset != null && this.valid) { // rebuild @@ -631,12 +639,12 @@ export class Terrain extends Component { } } - public get _asset() { + public get _asset () { return this.__asset; } public get size () { - let sz = new Size(0, 0); + const 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; @@ -647,15 +655,11 @@ export class Terrain extends Component { } 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; + return [this.blockCount[0] * TERRAIN_BLOCK_TILE_COMPLEXITY, this.blockCount[1] * TERRAIN_BLOCK_TILE_COMPLEXITY]; } public get vertexCount () { - let _vertexCount = this.tileCount; + const _vertexCount = this.tileCount; _vertexCount[0] += 1; _vertexCount[1] += 1; @@ -691,7 +695,7 @@ export class Terrain extends Component { visible: true, }) public get info () { - let ti = new TerrainInfo; + const ti = new TerrainInfo(); ti.tileSize = this.tileSize; ti.blockCount[0] = this.blockCount[0]; ti.blockCount[1] = this.blockCount[1]; @@ -802,10 +806,10 @@ export class Terrain extends Component { } } - public exportAsset() { + public exportAsset () { let asset = this._asset; if (asset == null) { - asset = new TerrainAsset; + asset = new TerrainAsset(); } asset.tileSize = this.tileSize; @@ -814,12 +818,24 @@ export class Terrain extends Component { asset.weightMapSize = this.weightMapSize; asset.heights = this.heights; asset.weights = this.weights; + asset.layerBuffer = new Array(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]; + 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]; + } + + for (let i = 0; i < this._layers.length; ++i) { + const tlayer = this._layers[i]; + if (tlayer != null && tlayer.detailMap != null) { + const ilayer = new TerrainLayerInfo(); + ilayer.slot = i; + ilayer.tileSize = tlayer.tileSize; + ilayer.detailMap = tlayer.detailMap._uuid; + asset.layerInfos.push(ilayer); + } } return asset; @@ -883,7 +899,7 @@ export class Terrain extends Component { } } - public onRestore() { + public onRestore () { this.onDisable(); this.onLoad(); this._buildImp(); @@ -915,7 +931,12 @@ export class Terrain extends Component { } public getLayer (id: number) { + if (id === -1) { + return null; + } + return this._layers[id]; + } public getPosition (i: number, j: number) { @@ -1138,17 +1159,20 @@ export class Terrain extends Component { return this._sharedIndexBuffer; } - public rayCheck (start: Vec3, dir: Vec3, step: number) { + public rayCheck (start: Vec3, dir: Vec3, step: number, worldspace: boolean = true) { const MAX_COUNT = 2000; - let i = 0; const trace = start; - let position: Vec3|null = null; + if (worldspace) { + Vec3.subtract(trace, start, this.node.getWorldPosition()); + } const dstep = new Vec3(); dstep.set(dir); dstep.multiplyScalar(step); + let position: Vec3|null = null; + if (dir.equals(new Vec3(0, 1, 0))) { const y = this.getHeightAt(trace.x, trace.z); if (y != null && trace.y <= y) { @@ -1162,6 +1186,8 @@ export class Terrain extends Component { } } else { + let i = 0; + // 优先大步进查找 while (i++ < MAX_COUNT) { const y = this.getHeightAt(trace.x, trace.z); @@ -1247,6 +1273,26 @@ export class Terrain extends Component { this._lightMapSize = this.__asset.lightMapSize; this._heights = this.__asset.heights; this._weights = this.__asset.weights; + + // build layers + let initial = true; + for (let i = 0; i < this._layers.length; ++i) { + if (this._layers[i] != null) { + initial = false; + } + } + + if (initial && this._asset != null) { + for (const i of this._asset.layerInfos) { + const layer = new TerrainLayer(); + layer.tileSize = i.tileSize; + cc.loader.loadRes(i.detailMap, Texture2D, (err, asset) => { + layer.detailMap = asset; + }); + + this._layers[i.slot] = layer; + } + } } if (this._blockCount[0] === 0 || this._blockCount[1] === 0) { @@ -1271,7 +1317,7 @@ export class Terrain extends Component { this._buildNormals(); } - // initialize weights + // build weights const weightMapComplexityU = this._weightMapSize * this._blockCount[0]; const weightMapComplexityV = this._weightMapSize * this._blockCount[1]; if (this._weights.length !== weightMapComplexityU * weightMapComplexityV * 4) { @@ -1289,12 +1335,24 @@ export class Terrain extends Component { this._blockInfos = []; for (let j = 0; j < this._blockCount[1]; ++j) { for (let i = 0; i < this._blockCount[0]; ++i) { - let info = new TerrainBlockInfo(); + const 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); + if (info.layers[1] === info.layers[0]) { + info.layers[1] = -1; + } + info.layers[2] = this._asset.getLayer(i, j, 2); + if (info.layers[2] === info.layers[0]) { + info.layers[2] = -1; + } + info.layers[3] = this._asset.getLayer(i, j, 3); + if (info.layers[3] === info.layers[0]) { + info.layers[3] = -1; + } } this._blockInfos.push(info); @@ -1381,10 +1439,10 @@ export class Terrain extends Component { // sample weight const sampleOldWeight = (_x: number, _y: number, _xoff: number, _yoff: number, _weights: Uint8Array) => { - let ix0 = Math.floor(_x); - let iz0 = Math.floor(_y); - let ix1 = ix0 + 1; - let iz1 = iz0 + 1; + const ix0 = Math.floor(_x); + const iz0 = Math.floor(_y); + const ix1 = ix0 + 1; + const iz1 = iz0 + 1; const dx = _x - ix0; const dz = _y - iz0; @@ -1424,8 +1482,9 @@ export class Terrain extends Component { for (let v = 0; v < this._weightMapSize; ++v) { for (let u = 0; u < this._weightMapSize; ++u) { + // tslint:disable-next-line: no-shadowed-variable let w: Vec4; - if (info.weightMapSize == oldWeightMapSize) { + if (info.weightMapSize === oldWeightMapSize) { w = getOldWeight(u + uoff, v + voff, this._weights); } else {