3d v1.0.0 terrain (#4953)

* 上传地形系统

* 上传地形系统

* 新增地形导出

* 新增Terrain Shader

* vmath相关修改

* 修复TerrainSample启动崩溃问题
This commit is contained in:
Myway
2019-07-23 17:37:58 +08:00
committed by Weiliang Wang
parent 5bb031d889
commit ac0d84f92d
12 changed files with 2412 additions and 0 deletions
+74
View File
@@ -249,6 +249,44 @@ export default [
}
]
},
{
"name": "builtin-terrain",
"techniques": [
{"name":"opaque", "passes":[{"program":"builtin-terrain|terrain-vs:vert|terrain-fs:frag", "properties":{"UVScale":{"value":[1, 1, 1, 1], "type":16}, "weightMap":{"value":"black", "type":28}, "detailMap0":{"value":"grey", "type":28}, "detailMap1":{"value":"grey", "type":28}, "detailMap2":{"value":"grey", "type":28}, "detailMap3":{"value":"grey", "type":28}}}]}
],
"shaders": [
{
"name": "builtin-terrain|terrain-vs:vert|terrain-fs:frag",
"hash": 2211091679,
"glsl3": {
"vert": `\nprecision mediump float;\nuniform CCGlobal {\n vec4 cc_time;\n vec4 cc_screenSize;\n vec4 cc_screenScale;\n vec4 cc_nativeSize;\n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos;\n vec4 cc_exposure;\n vec4 cc_mainLitDir;\n vec4 cc_mainLitColor;\n vec4 cc_ambientSky;\n vec4 cc_ambientGround;\n};\nuniform CCLocal {\n highp mat4 cc_matWorld;\n mat4 cc_matWorldIT;\n};\nin vec3 a_position;\nin vec3 a_normal;\nin vec2 a_texCoord;\nout vec2 uvw;\nout vec2 uv0;\nout vec2 uv1;\nout vec2 uv2;\nout vec2 uv3;\nout vec3 diffuse;\nuniform TexCoords {\n vec4 UVScale;\n};\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n pos = cc_matWorld * pos;\n pos = cc_matViewProj * pos;\n uvw = a_texCoord;\n uv0 = a_position.xz * UVScale.x;\n uv1 = a_position.xz * UVScale.y;\n uv2 = a_position.xz * UVScale.z;\n uv3 = a_position.xz * UVScale.w;\n vec3 L = normalize(-cc_mainLitDir.xyz);\n vec3 N = a_normal;\n float fAmb = dot(N, vec3(0.0, -1.0, 0.0)) * 0.5 + 0.5;\n vec3 ambDiff = mix(cc_ambientSky.rgb, cc_ambientGround.rgb, fAmb) * cc_ambientSky.w;\n diffuse = ambDiff + vec3(dot(N, L));\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n`,
"frag": `\nprecision mediump float;\nuniform CCGlobal {\n vec4 cc_time;\n vec4 cc_screenSize;\n vec4 cc_screenScale;\n vec4 cc_nativeSize;\n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos;\n vec4 cc_exposure;\n vec4 cc_mainLitDir;\n vec4 cc_mainLitColor;\n vec4 cc_ambientSky;\n vec4 cc_ambientGround;\n};\nvec3 SRGBToLinear(vec3 gamma)\n{\n return pow(gamma, vec3(2.2));\n}\nvec3 LinearToSRGB(vec3 linear)\n{\n return pow(linear, vec3(0.454545));\n}\nvec4 CCFragOutput(vec4 color) {\n #if CC_USE_HDR\n color.rgb = mix(color.rgb, SRGBToLinear(color.rgb) * cc_exposure.w, vec3(cc_exposure.z));\n #endif\n return color;\n}\n in vec2 uvw;\n in vec2 uv0;\n in vec2 uv1;\n in vec2 uv2;\n in vec2 uv3;\n in vec3 diffuse;\n uniform sampler2D weightMap;\n uniform sampler2D detailMap0;\n uniform sampler2D detailMap1;\n uniform sampler2D detailMap2;\n uniform sampler2D detailMap3;\nvec4 frag () {\n vec4 color = vec4(0, 0, 0, 0);\n #if LAYERS == 1\n color = texture(detailMap0, uv0);\n #elif LAYERS == 2\n vec4 w = texture(weightMap, uvw);\n color += texture(detailMap0, uv0) * w.r;\n color += texture(detailMap1, uv1) * w.g;\n #elif LAYERS == 3\n vec4 w = texture(weightMap, uvw);\n color += texture(detailMap0, uv0) * w.r;\n color += texture(detailMap1, uv1) * w.g;\n color += texture(detailMap2, uv2) * w.b;\n #elif LAYERS == 4\n vec4 w = texture(weightMap, uvw);\n color += texture(detailMap0, uv0) * w.r;\n color += texture(detailMap1, uv1) * w.g;\n color += texture(detailMap2, uv2) * w.b;\n color += texture(detailMap3, uv3) * w.a;\n #else\n color = texture(detailMap0, uv0);\n #endif\n color.rgb *= diffuse;\n return CCFragOutput(color);\n}\nout vec4 cc_FragColor;\nvoid main() { cc_FragColor = frag(); }\n`
},
"glsl1": {
"vert": `\nprecision mediump float;\nuniform mat4 cc_matViewProj;\nuniform vec4 cc_mainLitDir;\nuniform vec4 cc_ambientSky;\nuniform vec4 cc_ambientGround;\nuniform highp mat4 cc_matWorld;\nattribute vec3 a_position;\nattribute vec3 a_normal;\nattribute vec2 a_texCoord;\nvarying vec2 uvw;\nvarying vec2 uv0;\nvarying vec2 uv1;\nvarying vec2 uv2;\nvarying vec2 uv3;\nvarying vec3 diffuse;\nuniform vec4 UVScale;\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n pos = cc_matWorld * pos;\n pos = cc_matViewProj * pos;\n uvw = a_texCoord;\n uv0 = a_position.xz * UVScale.x;\n uv1 = a_position.xz * UVScale.y;\n uv2 = a_position.xz * UVScale.z;\n uv3 = a_position.xz * UVScale.w;\n vec3 L = normalize(-cc_mainLitDir.xyz);\n vec3 N = a_normal;\n float fAmb = dot(N, vec3(0.0, -1.0, 0.0)) * 0.5 + 0.5;\n vec3 ambDiff = mix(cc_ambientSky.rgb, cc_ambientGround.rgb, fAmb) * cc_ambientSky.w;\n diffuse = ambDiff + vec3(dot(N, L));\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n`,
"frag": `\nprecision mediump float;\nuniform vec4 cc_exposure;\nvec3 SRGBToLinear(vec3 gamma)\n{\n return pow(gamma, vec3(2.2));\n}\nvec3 LinearToSRGB(vec3 linear)\n{\n return pow(linear, vec3(0.454545));\n}\nvec4 CCFragOutput(vec4 color) {\n #if CC_USE_HDR\n color.rgb = mix(color.rgb, SRGBToLinear(color.rgb) * cc_exposure.w, vec3(cc_exposure.z));\n #endif\n return color;\n}\n varying vec2 uvw;\n varying vec2 uv0;\n varying vec2 uv1;\n varying vec2 uv2;\n varying vec2 uv3;\n varying vec3 diffuse;\n uniform sampler2D weightMap;\n uniform sampler2D detailMap0;\n uniform sampler2D detailMap1;\n uniform sampler2D detailMap2;\n uniform sampler2D detailMap3;\nvec4 frag () {\n vec4 color = vec4(0, 0, 0, 0);\n #if LAYERS == 1\n color = texture2D(detailMap0, uv0);\n #elif LAYERS == 2\n vec4 w = texture2D(weightMap, uvw);\n color += texture2D(detailMap0, uv0) * w.r;\n color += texture2D(detailMap1, uv1) * w.g;\n #elif LAYERS == 3\n vec4 w = texture2D(weightMap, uvw);\n color += texture2D(detailMap0, uv0) * w.r;\n color += texture2D(detailMap1, uv1) * w.g;\n color += texture2D(detailMap2, uv2) * w.b;\n #elif LAYERS == 4\n vec4 w = texture2D(weightMap, uvw);\n color += texture2D(detailMap0, uv0) * w.r;\n color += texture2D(detailMap1, uv1) * w.g;\n color += texture2D(detailMap2, uv2) * w.b;\n color += texture2D(detailMap3, uv3) * w.a;\n #else\n color = texture2D(detailMap0, uv0);\n #endif\n color.rgb *= diffuse;\n return CCFragOutput(color);\n}\nvoid main() { gl_FragColor = frag(); }\n`
},
"builtins": {"globals":{"blocks":[{"name":"CCGlobal", "defines":[]}], "samplers":[]}, "locals":{"blocks":[{"name":"CCLocal", "defines":[]}], "samplers":[]}},
"defines": [
{"name":"CC_USE_HDR", "type":"boolean", "defines":[]},
{"name":"LAYERS", "type":"number", "defines":[], "range":[0, 3]}
],
"blocks": [
{"name": "TexCoords", "defines": [], "binding": 0, "members": [
{"name":"UVScale", "type":16, "count":1}
]}
],
"samplers": [
{"name":"weightMap", "type":28, "count":1, "defines":[], "binding":30},
{"name":"detailMap0", "type":28, "count":1, "defines":[], "binding":31},
{"name":"detailMap1", "type":28, "count":1, "defines":[], "binding":32},
{"name":"detailMap2", "type":28, "count":1, "defines":[], "binding":33},
{"name":"detailMap3", "type":28, "count":1, "defines":[], "binding":34}
],
"dependencies": {}
}
]
},
{
"name": "builtin-unlit",
"techniques": [
@@ -289,6 +327,42 @@ export default [
}
]
},
{
"name": "editor/terrain-circle-brush",
"techniques": [
{"name":"transparent", "passes":[{"blendState":{"targets":[{"blend":true, "blendSrc":2, "blendDst":4, "blendDstAlpha":4}]}, "program":"editor/terrain-circle-brush|terrain-brush-vs:vert|terrain-brush-fs:frag", "depthStencilState":{"depthTest":true, "depthWrite":false}, "properties":{"BrushPos":{"value":[0, 0, 0, 1], "type":16}, "BrushParams":{"value":[2.5, 2.5, 0, 0], "type":16}}}]}
],
"shaders": [
{
"name": "editor/terrain-circle-brush|terrain-brush-vs:vert|terrain-brush-fs:frag",
"hash": 3864386462,
"glsl3": {
"vert": `\nprecision mediump float;\nuniform CCGlobal {\n vec4 cc_time;\n vec4 cc_screenSize;\n vec4 cc_screenScale;\n vec4 cc_nativeSize;\n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos;\n vec4 cc_exposure;\n vec4 cc_mainLitDir;\n vec4 cc_mainLitColor;\n vec4 cc_ambientSky;\n vec4 cc_ambientGround;\n};\nuniform CCLocal {\n highp mat4 cc_matWorld;\n mat4 cc_matWorldIT;\n};\nin vec3 a_position;\nout vec4 wposition;\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n wposition = cc_matWorld * pos;\n wposition.y += 0.01;\n pos = cc_matViewProj * wposition;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n`,
"frag": `\nprecision mediump float;\nuniform CCGlobal {\n vec4 cc_time;\n vec4 cc_screenSize;\n vec4 cc_screenScale;\n vec4 cc_nativeSize;\n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos;\n vec4 cc_exposure;\n vec4 cc_mainLitDir;\n vec4 cc_mainLitColor;\n vec4 cc_ambientSky;\n vec4 cc_ambientGround;\n};\nvec3 SRGBToLinear(vec3 gamma)\n{\n return pow(gamma, vec3(2.2));\n}\nvec3 LinearToSRGB(vec3 linear)\n{\n return pow(linear, vec3(0.454545));\n}\nvec4 CCFragOutput(vec4 color) {\n #if CC_USE_HDR\n color.rgb = mix(color.rgb, SRGBToLinear(color.rgb) * cc_exposure.w, vec3(cc_exposure.z));\n #endif\n return color;\n}\nin vec4 wposition;\nuniform TexCoords {\n vec4 BrushPos;\n vec4 BrushParams;\n};\nvec4 frag () {\n float Radius = BrushParams.x;\n float Falloff = BrushParams.y;\n float Distance = length(wposition.xz - BrushPos.xz);\n float k = 0.0;\n #if LINEAR\n if (Distance <= Radius) {\n k = 1.0;\n }\n else if (Distance > Radius + Falloff) {\n k = 0.0;\n }\n else {\n k = max(0.0, 1.0 - (Distance - Radius) / Falloff);\n }\n #elif SMOOTH\n if (Distance <= Radius) {\n k = 1.0;\n }\n else if (Distance > Radius + Falloff) {\n k = 0.0;\n }\n else {\n float y = (Distance - Radius) / Falloff;\n k = sqrt(1.0 - y * y);\n }\n #elif SPHERICAL\n if (Distance <= Radius) {\n k = 1.0;\n }\n else if (Distance > Radius + Falloff) {\n k = 0.0;\n }\n else {\n k = max(0.0, 1.0 - (Distance - Radius) / Falloff);\n }\n k = k*k*(3 - 2 * k);\n #elif TIP\n if (Distance <= Radius) {\n k = 1.0;\n }\n else if (Distance > Radius + Falloff) {\n k = 0.0;\n }\n else {\n float y = (Falloff + Radius - Distance) / Falloff;\n k = 1.0 - sqrt(1.0 - y * y);\n }\n #endif\n vec4 color = vec4(0, 0, 0, 0);\n color.rgb = vec3(100, 100, 135) / 255.0;\n color.a = 0.85 * k;\n return CCFragOutput(color);\n}\nout vec4 cc_FragColor;\nvoid main() { cc_FragColor = frag(); }\n`
},
"glsl1": {
"vert": `\nprecision mediump float;\nuniform mat4 cc_matViewProj;\nuniform highp mat4 cc_matWorld;\nattribute vec3 a_position;\nvarying vec4 wposition;\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n wposition = cc_matWorld * pos;\n wposition.y += 0.01;\n pos = cc_matViewProj * wposition;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n`,
"frag": `\nprecision mediump float;\nuniform vec4 cc_exposure;\nvec3 SRGBToLinear(vec3 gamma)\n{\n return pow(gamma, vec3(2.2));\n}\nvec3 LinearToSRGB(vec3 linear)\n{\n return pow(linear, vec3(0.454545));\n}\nvec4 CCFragOutput(vec4 color) {\n #if CC_USE_HDR\n color.rgb = mix(color.rgb, SRGBToLinear(color.rgb) * cc_exposure.w, vec3(cc_exposure.z));\n #endif\n return color;\n}\nvarying vec4 wposition;\nuniform vec4 BrushPos;\nuniform vec4 BrushParams;\nvec4 frag () {\n float Radius = BrushParams.x;\n float Falloff = BrushParams.y;\n float Distance = length(wposition.xz - BrushPos.xz);\n float k = 0.0;\n #if LINEAR\n if (Distance <= Radius) {\n k = 1.0;\n }\n else if (Distance > Radius + Falloff) {\n k = 0.0;\n }\n else {\n k = max(0.0, 1.0 - (Distance - Radius) / Falloff);\n }\n #elif SMOOTH\n if (Distance <= Radius) {\n k = 1.0;\n }\n else if (Distance > Radius + Falloff) {\n k = 0.0;\n }\n else {\n float y = (Distance - Radius) / Falloff;\n k = sqrt(1.0 - y * y);\n }\n #elif SPHERICAL\n if (Distance <= Radius) {\n k = 1.0;\n }\n else if (Distance > Radius + Falloff) {\n k = 0.0;\n }\n else {\n k = max(0.0, 1.0 - (Distance - Radius) / Falloff);\n }\n k = k*k*(3 - 2 * k);\n #elif TIP\n if (Distance <= Radius) {\n k = 1.0;\n }\n else if (Distance > Radius + Falloff) {\n k = 0.0;\n }\n else {\n float y = (Falloff + Radius - Distance) / Falloff;\n k = 1.0 - sqrt(1.0 - y * y);\n }\n #endif\n vec4 color = vec4(0, 0, 0, 0);\n color.rgb = vec3(100, 100, 135) / 255.0;\n color.a = 0.85 * k;\n return CCFragOutput(color);\n}\nvoid main() { gl_FragColor = frag(); }\n`
},
"builtins": {"globals":{"blocks":[{"name":"CCGlobal", "defines":[]}], "samplers":[]}, "locals":{"blocks":[{"name":"CCLocal", "defines":[]}], "samplers":[]}},
"defines": [
{"name":"CC_USE_HDR", "type":"boolean", "defines":[]},
{"name":"LINEAR", "type":"boolean", "defines":[]},
{"name":"SMOOTH", "type":"boolean", "defines":[]},
{"name":"SPHERICAL", "type":"boolean", "defines":[]},
{"name":"TIP", "type":"boolean", "defines":[]}
],
"blocks": [
{"name": "TexCoords", "defines": [], "binding": 0, "members": [
{"name":"BrushPos", "type":16, "count":1},
{"name":"BrushParams", "type":16, "count":1}
]}
],
"samplers": [],
"dependencies": {}
}
]
},
{
"name": "pipeline/planar-shadow",
"techniques": [
+1
View File
@@ -14,6 +14,7 @@ export { samplerLib };
export { Light } from './scene/light';
export { Camera } from './scene/camera';
export { Model } from './scene/model';
export { TerrainSample } from './terrain/terrain-sample';
export { default as ParticleBatchModel } from './models/particle-batch-model';
export { SkinningModel } from './models/skinning-model';
+70
View File
@@ -0,0 +1,70 @@
import { clamp } from '../../core/vmath';
export class HeightField
{
data: Uint16Array = new Uint16Array;
w: number = 0;
h: number = 0;
constructor(w: number, h: number) {
this.w = w;
this.h = h;
this.data = new Uint16Array(w * h);
for (let i = 0; i < w * h; ++i) {
this.data[i] = 0;
}
}
set(i: number, j: number, value: number) {
this.data[j * this.w + i] = value;
}
get(i: number, j: number) {
return this.data[j * this.w + i];
}
getClamp(i: number, j: number) {
i = clamp(i, 0, this.w - 1);
j = clamp(j, 0, this.h - 1);
return this.get(i, j);
}
getAt(x: number, y: number) {
let fx = x / this.w;
let fy = y / this.h;
let ix0 = Math.floor(fx);
let iz0 = Math.floor(fy);
let ix1 = ix0 + 1;
let iz1 = iz0 + 1;
let dx = fx - ix0;
let dz = fy - iz0;
ix0 = clamp(ix0, 0, this.w - 1);
iz0 = clamp(iz0, 0, this.h - 1);
ix1 = clamp(ix1, 0, this.w - 1);
iz1 = clamp(iz1, 0, this.h - 1);
let a = this.get(ix0, iz0);
let b = this.get(ix1, iz0);
let c = this.get(ix0, iz1);
let d = this.get(ix1, iz1);
let m = (b + c) * 0.5;
if (dx + dz <= 1.0) {
d = m + (m - a);
}
else {
a = m + (m - d);
}
let h1 = a * (1.0 - dx) + b * dx;
let h2 = c * (1.0 - dx) + d * dx;
let h = h1 * (1.0 - dz) + h2 * dz;
return h;
}
}
+182
View File
@@ -0,0 +1,182 @@
import { Vec3, Vec4 } from "../../core";
import { max } from "../../core/vmath/bits";
import { Material } from '../../3d/assets/material';
import { IDefineMap } from '../core/pass';
// TerrainBrush Interface
//
export class TerrainBrush
{
material: Material|null = null;
position: Vec3 = new Vec3(0, 0, 0);
radius: number = 5;
strength: number = 1;
getDelta(x: number, z: number) {
return 0;
}
update(pos: Vec3) {
this.position = pos;
}
}
// TerrainBrushData Interface
//
export class TerrainBrushData
{
bmin: number[] = [0, 0];
bmax: number[] = [0, 0];
width() {
return this.bmax[0] - this.bmax[0] + 1;
}
height() {
return this.bmax[1] - this.bmax[1] + 1;
}
}
//
export enum eTerrainCircleBrushType {
Linear,
Smooth,
Spherical,
Tip,
}
export class TerrainCircleBrush extends TerrainBrush
{
protected type: eTerrainCircleBrushType = eTerrainCircleBrushType.Linear;
protected falloff: number = 0.5;
constructor() {
super();
this._updateMaterial();
}
setType(type: eTerrainCircleBrushType) {
if (this.type != type) {
this.type = type;
this._updateMaterial();
}
}
getType() {
return this.type;
}
_updateMaterial() {
let effect = cc.EffectAsset.get('editor/terrain-circle-brush');
if (effect != null) {
this.material = new Material;
this.material.initialize({
effectAsset: effect,
defines: this._getTypeDefine()
});
}
}
_getTypeDefine(): IDefineMap {
switch (this.type) {
case eTerrainCircleBrushType.Linear:
return { LINEAR: 1 };
case eTerrainCircleBrushType.Smooth:
return { SMOOTH: 1 };
case eTerrainCircleBrushType.Spherical:
return { SPHERICAL: 1 };
case eTerrainCircleBrushType.Tip:
return { TIP: 1 };
}
return { LINEAR: 1 };
}
_calculateFalloff_Linear(Distance: number, Radius: number, Falloff: number) {
if (Distance <= Radius) {
return 1.0;
}
if (Distance > Radius + Falloff) {
return 0.0;
}
return max(0.0, 1.0 - (Distance - Radius) / Falloff);
};
_calculateFalloff_Spherical(Distance: number, Radius: number, Falloff: number) {
let y = this._calculateFalloff_Linear(Distance, Radius, Falloff);
return y*y*(3 - 2 * y);
};
_calculateFalloff_Smooth(Distance: number, Radius: number, Falloff: number) {
if (Distance <= Radius) {
return 1.0;
}
if (Distance > Radius + Falloff) {
return 0.0;
}
let y = (Distance - Radius) / Falloff;
return Math.sqrt(1.0 - y * y);
}
_calculateFalloff_Tip(Distance: number, Radius: number, Falloff: number) {
if (Distance <= Radius) {
return 1.0;
}
if (Distance > Radius + Falloff) {
return 0.0;
}
let y = (Falloff + Radius - Distance) / Falloff;
return 1.0 - Math.sqrt(1.0 - y * y);
}
getDelta(x: number, z: number) {
let DetlaX = x - this.position.x, DeltaZ = z - this.position.z;
let Distance = Math.sqrt(DetlaX * DetlaX + DeltaZ * DeltaZ);
let Radius = (1.0 - this.falloff) * this.radius;
let Falloff = this.falloff * this.radius;
let k = 0;
switch (this.type) {
case eTerrainCircleBrushType.Linear:
k = this._calculateFalloff_Linear(Distance, Radius, Falloff);
break;
case eTerrainCircleBrushType.Smooth:
k = this._calculateFalloff_Smooth(Distance, Radius, Falloff);
break;
case eTerrainCircleBrushType.Spherical:
k = this._calculateFalloff_Spherical(Distance, Radius, Falloff);
break;
case eTerrainCircleBrushType.Tip:
k = this._calculateFalloff_Tip(Distance, Radius, Falloff);
break;
}
return k * this.strength;
}
update(pos: Vec3) {
super.update(pos);
if (this.material != null) {
let Radius = (1.0 - this.falloff) * this.radius;
let Falloff = this.falloff * this.radius;
let BrushPos = pos;
let BrushParams = new Vec4;
BrushParams.x = Radius;
BrushParams.y = Falloff;
this.material.setProperty("BrushPos", BrushPos);
this.material.setProperty("BrushParams", BrushParams);
}
}
}
@@ -0,0 +1,7 @@
import { TerrainEditor_Mode } from './terrain-editor-mode'
// 管理模式
//
export class TerrainEditor_Manage extends TerrainEditor_Mode
{
}
@@ -0,0 +1,15 @@
import { Terrain, TERRAIN_BLOCK_TILE_COMPLEXITY } from './terrain'
export enum eTerrainEditorMode
{
MANAGE,
SCULPT,
PAINT,
}
export class TerrainEditor_Mode
{
onUpdate(terrain: Terrain, dtime: number) {
}
}
@@ -0,0 +1,176 @@
import { TerrainEditor_Mode } from './terrain-editor-mode'
import { Terrain, TERRAIN_BLOCK_TILE_COMPLEXITY } from './terrain'
import { Vec3, Rect } from '../../core/value-types';
import { clamp } from '../../core/vmath';
import { TerrainBrush, TerrainCircleBrush } from './terrain-brush';
import { TerrainWeightOperation, TerrainWeightUndoRedo } from './terrain-operation';
export class TerrainEditor_Paint extends TerrainEditor_Mode
{
_brush: TerrainBrush = new TerrainCircleBrush;
_undo: TerrainWeightUndoRedo|null = null;
_currentLayer: number = -1;
onUpdate(terrain: Terrain, dtime: number) {
if (this._undo != null) {
this._updateWeight(terrain, dtime);
}
}
onUpdateBrushPosition(terrain: Terrain, pos: Vec3) {
this._brush.update(pos);
let blocks = terrain.getBlocks();
for (let i = 0; i < blocks.length; ++i) {
let block = blocks[i];
let index = block.getIndex();
let bound = new Rect;
bound.x = index[0] * terrain.info.tileSize;
bound.y = index[1] * terrain.info.tileSize;
bound.width = TERRAIN_BLOCK_TILE_COMPLEXITY * terrain.info.tileSize;
bound.height = TERRAIN_BLOCK_TILE_COMPLEXITY * terrain.info.tileSize;
let brushRect = new Rect;
brushRect.x = this._brush.position.x - this._brush.radius;
brushRect.y = this._brush.position.z - this._brush.radius;
brushRect.width = this._brush.radius * 2;
brushRect.height = this._brush.radius * 2;
if (bound.intersects(brushRect)) {
block.setBrushMaterial(this._brush.material);
}
else {
block.setBrushMaterial(null);
}
}
}
onMouseDown()
{
if (this._currentLayer == -1) {
return;
}
this._undo = new TerrainWeightUndoRedo;
}
onMouseUp()
{
this._undo = null;
}
_updateWeight(terrain: Terrain, dtime: number)
{
let uWeigthComplexity = terrain.info.weightMapSize * terrain.info.blockCount[0];
let vWeigthComplexity = terrain.info.weightMapSize * terrain.info.blockCount[1];
if (uWeigthComplexity == 0 || vWeigthComplexity == 0) {
return ;
}
let x1 = this._brush.position.x - this._brush.radius;
let y1 = this._brush.position.z - this._brush.radius;
let x2 = this._brush.position.x + this._brush.radius;
let y2 = this._brush.position.z + this._brush.radius;
x1 /= terrain.info.size[0];
y1 /= terrain.info.size[1];
x2 /= terrain.info.size[0];
y2 /= terrain.info.size[1];
x1 *= uWeigthComplexity - 1;
y1 *= vWeigthComplexity - 1;
x2 *= uWeigthComplexity - 1;
y2 *= vWeigthComplexity - 1;
x1 = Math.floor(x1);
y1 = Math.floor(y1);
x2 = Math.floor(x2);
y2 = Math.floor(y2);
if (x1 > uWeigthComplexity - 1 || x2 < 0)
return;
if (y1 > vWeigthComplexity - 1 || y2 < 0)
return;
x1 = clamp(x1, 0, uWeigthComplexity - 1);
y1 = clamp(y1, 0, vWeigthComplexity - 1);
x2 = clamp(x2, 0, uWeigthComplexity - 1);
y2 = clamp(y2, 0, vWeigthComplexity - 1);
let op = new TerrainWeightOperation;
for (let y = y1; y <= y2; ++y) {
for (let x = x1; x <= x2; ++x) {
let w = terrain.getWeight(x, y);
let bx = Math.floor(x / terrain.info.weightMapSize);
let by = Math.floor(y / terrain.info.weightMapSize);
let block = terrain.getBlock(bx, by);
let layers = [block.getLayer(0), block.getLayer(1), block.getLayer(2), block.getLayer(3)];
let xpos = x / (uWeigthComplexity - 1) * terrain.info.size[0];
let ypos = y / (vWeigthComplexity - 1) * terrain.info.size[1];
let delta = this._brush.getDelta(xpos, ypos) * dtime;
if (delta == 0) {
continue;
}
if (layers[0] == this._currentLayer) {
w.x += delta;
}
else if (layers[1] == this._currentLayer) {
w.y += delta;
}
else if (layers[2] == this._currentLayer) {
w.z += delta;
}
else if (layers[3] == this._currentLayer) {
w.w += delta;
}
else {
if (layers[0] == -1) {
block.setLayer(0, this._currentLayer);
w.x += delta;
}
else if (layers[1] == -1) {
block.setLayer(1, this._currentLayer);
w.y += delta;
}
else if (layers[3] == -1) {
block.setLayer(2, this._currentLayer);
w.z += delta;
}
else if (layers[4] == -1) {
block.setLayer(3, this._currentLayer);
w.w += delta;
}
else {
continue;
}
}
let sum = w.x + w.y + w.z + w.w;
if (sum > 0) {
w.multiply(1.0 / sum);
}
if (this._undo != null) {
this._undo.push(x, y, terrain.getWeight(x, y));
this._undo.pushBlock(block, layers);
}
op.push(x, y, w);
}
}
op.apply(terrain);
}
setCurrentLayer(layer: number) {
this._currentLayer = layer;
}
getCurrentLayer() {
return this._currentLayer;
}
}
@@ -0,0 +1,112 @@
import { TerrainEditor_Mode } from './terrain-editor-mode'
import { Terrain, TERRAIN_BLOCK_TILE_COMPLEXITY } from './terrain'
import { Vec3, Rect } from '../../core/value-types';
import { clamp } from '../../core/vmath';
import { TerrainBrush, TerrainCircleBrush } from './terrain-brush';
import { TerrainHeightOperation, TerrainHeightUndoRedo } from './terrain-operation';
export class TerrainEditor_Sculpt extends TerrainEditor_Mode
{
_brush: TerrainBrush = new TerrainCircleBrush;
_undo: TerrainHeightUndoRedo|null = null;
onUpdate(terrain: Terrain, dtime: number) {
if (this._undo != null) {
this._updateHeight(terrain, dtime, false);
}
}
onUpdateBrushPosition(terrain: Terrain, pos: Vec3) {
this._brush.update(pos);
let blocks = terrain.getBlocks();
for (let i = 0; i < blocks.length; ++i) {
let block = blocks[i];
let index = block.getIndex();
let bound = new Rect;
bound.x = index[0] * terrain.info.tileSize;
bound.y = index[1] * terrain.info.tileSize;
bound.width = TERRAIN_BLOCK_TILE_COMPLEXITY * terrain.info.tileSize;
bound.height = TERRAIN_BLOCK_TILE_COMPLEXITY * terrain.info.tileSize;
let brushRect = new Rect;
brushRect.x = this._brush.position.x - this._brush.radius;
brushRect.y = this._brush.position.z - this._brush.radius;
brushRect.width = this._brush.radius * 2;
brushRect.height = this._brush.radius * 2;
if (bound.intersects(brushRect)) {
block.setBrushMaterial(this._brush.material);
}
else {
block.setBrushMaterial(null);
}
}
}
onMouseDown()
{
this._undo = new TerrainHeightUndoRedo;
}
onMouseUp()
{
this._undo = null;
}
_updateHeight(terrain: Terrain, dtime: number, shift_pressed: boolean)
{
let x1 = this._brush.position.x - this._brush.radius;
let y1 = this._brush.position.z - this._brush.radius;
let x2 = this._brush.position.x + this._brush.radius;
let y2 = this._brush.position.z + this._brush.radius;
x1 /= terrain.info.tileSize;
y1 /= terrain.info.tileSize;
x2 /= terrain.info.tileSize;
y2 /= terrain.info.tileSize;
x1 = Math.floor(x1);
y1 = Math.floor(y1);
x2 = Math.floor(x2);
y2 = Math.floor(y2);
if (x1 > terrain.info.vertexCount[0] - 1 || x2 < 0)
return;
if (y1 > terrain.info.vertexCount[1] - 1 || y2 < 0)
return;
x1 = clamp(x1, 0, terrain.info.vertexCount[0] - 1);
y1 = clamp(y1, 0, terrain.info.vertexCount[1] - 1);
x2 = clamp(x2, 0, terrain.info.vertexCount[0] - 1);
y2 = clamp(y2, 0, terrain.info.vertexCount[1] - 1);
let op = new TerrainHeightOperation;
for (let y = y1; y <= y2; ++y) {
for (let x = x1; x <= x2; ++x) {
let h = terrain.getHeight(x, y);
if (this._undo != null) {
this._undo.push(x, y, h);
}
let xpos = x * terrain.info.tileSize;
let ypos = y * terrain.info.tileSize;
let delta = this._brush.getDelta(xpos, ypos) * dtime;
if (!shift_pressed) {
h += delta;
}
else {
h -= delta;
}
op.push(x, y, h);
}
}
op.apply(terrain);
}
}
+143
View File
@@ -0,0 +1,143 @@
import { TerrainEditor_Mode, eTerrainEditorMode} from './terrain-editor-mode'
import { TerrainEditor_Manage } from './terrain-editor-manage'
import { TerrainEditor_Sculpt } from './terrain-editor-sculpt'
import { TerrainEditor_Paint } from './terrain-editor-paint'
import { Terrain } from './terrain'
import { Node } from '../../scene-graph/node';
import { CameraComponent } from '../../3d/framework';
export class TerrainEditor
{
static Instance: TerrainEditor;
_terrain: Terrain|null = null;
_modes: Array<TerrainEditor_Mode> = [
new TerrainEditor_Manage,
new TerrainEditor_Sculpt,
new TerrainEditor_Paint,
];
_currentMode: TerrainEditor_Mode | null = null;
constructor() {
TerrainEditor.Instance = this;
this.setMode(eTerrainEditorMode.SCULPT);
}
setEditTerrain(t: Terrain) {
this._terrain = t;
}
setMode(mode: eTerrainEditorMode) {
let m = this._modes[mode];
this._currentMode = m;
}
getMode(mode: eTerrainEditorMode) {
return this._modes[mode];
}
getCurrentMode() {
return this._currentMode;
}
getCurrentModeType() {
if (this._currentMode == this._modes[0]) {
return eTerrainEditorMode.MANAGE;
}
else if (this._currentMode == this._modes[1]) {
return eTerrainEditorMode.SCULPT;
}
else if (this._currentMode == this._modes[2]) {
return eTerrainEditorMode.PAINT;
}
return eTerrainEditorMode.SCULPT;
}
setCurrentLayer(id: number) {
let paintMode = this.getMode(eTerrainEditorMode.PAINT) as TerrainEditor_Paint;
paintMode.setCurrentLayer(id);
}
update(dtime: number) {
if (this._currentMode == null || this._terrain == null) {
return;
}
this._currentMode.onUpdate(this._terrain, dtime);
}
onMouseDown(id: number, x: number, y: number) {
if (this._terrain == null) {
return;
}
let sculptMode = this.getMode(eTerrainEditorMode.SCULPT) as TerrainEditor_Sculpt;
let paintMode = this.getMode(eTerrainEditorMode.PAINT) as TerrainEditor_Paint;
if (this._currentMode == sculptMode) {
sculptMode.onMouseDown();
}
else if (this._currentMode == paintMode) {
paintMode.onMouseDown();
}
}
onMouseUp(id: number, x: number, y: number) {
if (this._terrain == null) {
return;
}
let sculptMode = this.getMode(eTerrainEditorMode.SCULPT) as TerrainEditor_Sculpt;
let paintMode = this.getMode(eTerrainEditorMode.PAINT) as TerrainEditor_Paint;
if (this._currentMode == sculptMode) {
sculptMode.onMouseUp();
}
else if (this._currentMode == paintMode) {
paintMode.onMouseUp();
}
}
onMouseMove(camera: Node, x: number, y: number) {
if (this._terrain == null) {
return;
}
const scene = this._terrain.node.scene;
if (camera != null) {
const cameraComponent = camera.getComponent(CameraComponent);
if (cameraComponent == null) {
return;
}
const from = camera.getPosition();
const to = new cc.vmath.vec3();
cameraComponent.screenToWorld(new cc.vmath.vec3(x, y, 0), to);
const dir = new cc.Vec3();
cc.vmath.vec3.subtract(dir, to, from);
cc.vmath.vec3.normalize(dir, dir);
let vhit = this._terrain.rayCheck(from, dir, 0.1);
if (vhit == null) {
return ;
}
let sculptMode = this.getMode(eTerrainEditorMode.SCULPT) as TerrainEditor_Sculpt;
let paintMode = this.getMode(eTerrainEditorMode.PAINT) as TerrainEditor_Paint;
if (this._currentMode == sculptMode) {
sculptMode.onUpdateBrushPosition(this._terrain, vhit);
}
else if (this._currentMode == paintMode) {
paintMode.onUpdateBrushPosition(this._terrain, vhit);
}
}
}
}
+193
View File
@@ -0,0 +1,193 @@
import { TerrainBlock, Terrain } from './terrain'
import { Vec2, Vec4, Rect } from '../../core/value-types';
export class TerrainHeightData
{
x: number = 0;
y: number = 0;
value: number = 0;
}
export class TerrainHeightOperation
{
data: Array<TerrainHeightData> = new Array<TerrainHeightData>();
static addChangeList(changelist: Array<TerrainBlock>, block: TerrainBlock) {
for (let i = 0; i < changelist.length; ++i) {
if (changelist[i] == block) {
return;
}
}
changelist.push(block);
}
push(x: number, y: number, value: number) {
for (let i = 0; i < this.data.length; ++i) {
if (this.data[i].x == x && this.data[i].y == y) {
return ;
}
}
let point = new TerrainHeightData;
point.x = x;
point.y = y;
point.value = value;
this.data.push(point);
}
apply(terrain: Terrain) {
// update data
for (let i = 0; i < this.data.length; ++i) {
let p = this.data[i];
terrain.setHeight(p.x, p.y, p.value);
}
// update normal
for (let i = 0; i < this.data.length; ++i) {
let p = this.data[i];
let n = terrain._calcuNormal(p.x, p.y);
terrain._setNormal(p.x, p.y, n);
}
// update block
let changelist = new Array<TerrainBlock>();
for (let i = 0; i < terrain.getBlocks().length; ++i) {
let block = terrain.getBlocks()[i];
let rect = block.getRect();
for (let j = 0; j < this.data.length; ++j) {
let p = this.data[j];
if (!rect.contains(new Vec2(p.x, p.y)))
continue;
TerrainHeightOperation.addChangeList(changelist, block);
}
}
for (let i = 0; i < changelist.length; ++i) {
changelist[i]._updateHeight();
}
}
}
//
export class TerrainHeightUndoRedo extends TerrainHeightOperation
{
}
//
export class TerrainWeightData
{
x: number = 0;
y: number = 0;
value: Vec4 = new Vec4;
}
export class TerrainWeightOperation
{
data: Array<TerrainWeightData> = new Array<TerrainWeightData>();
static addChangeList(changelist: Array<TerrainBlock>, block: TerrainBlock) {
for (let i = 0; i < changelist.length; ++i) {
if (changelist[i] == block) {
return;
}
}
changelist.push(block);
}
push(x: number, y: number, value: Vec4) {
for (let i = 0; i < this.data.length; ++i) {
if (this.data[i].x == x && this.data[i].y == y) {
return ;
}
}
let point = new TerrainWeightData;
point.x = x;
point.y = y;
point.value = value;
this.data.push(point);
}
apply(terrain: Terrain) {
// update data
for (let i = 0; i < this.data.length; ++i) {
let p = this.data[i];
terrain.setWeight(p.x, p.y, p.value);
}
// update block
let changelist = new Array<TerrainBlock>();
for (let i = 0; i < terrain.getBlocks().length; ++i) {
let block = terrain.getBlocks()[i];
let rect = new Rect;
rect.x = block.getIndex()[0] * terrain.info.weightMapSize;
rect.y = block.getIndex()[1] * terrain.info.weightMapSize;
rect.width = terrain.info.weightMapSize;
rect.height = terrain.info.weightMapSize;
for (let j = 0; j < this.data.length; ++j) {
let p = this.data[j];
if (!rect.contains(new Vec2(p.x, p.y)))
continue;
TerrainWeightOperation.addChangeList(changelist, block);
}
}
for (let i = 0; i < changelist.length; ++i) {
changelist[i]._updateWeightMap();
}
}
}
export class TerrainBlockLayerData
{
block: TerrainBlock;
layers: number[]
constructor(block: TerrainBlock, layers: number[])
{
this.block = block;
this.layers = layers;
}
}
export class TerrainWeightUndoRedo extends TerrainWeightOperation
{
blockLayers: Array<TerrainBlockLayerData> = new Array<TerrainBlockLayerData>();
pushBlock(block: TerrainBlock, layers: number[])
{
for (let i = 0; i < this.blockLayers.length; ++i) {
if (this.blockLayers[i].block == block) {
return;
}
}
this.blockLayers.push(new TerrainBlockLayerData(block, layers));
}
apply(terrain: Terrain) {
super.apply(terrain);
for (let i = 0; i < this.blockLayers.length; ++i) {
let block = this.blockLayers[i].block;
let layers = this.blockLayers[i].layers;
block.setLayer(0, layers[0]);
block.setLayer(1, layers[1]);
block.setLayer(2, layers[2]);
block.setLayer(3, layers[3]);
}
}
}
+185
View File
@@ -0,0 +1,185 @@
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
import { Terrain, TerrainInfo, TerrainLayer } from './terrain'
import { Scene } from '../../scene-graph/scene';
import { Node } from '../../scene-graph/node';
import { Texture2D } from '../../assets';
import { TerrainEditor } from './terrain-editor';
import { eTerrainEditorMode} from './terrain-editor-mode'
import { TerrainEditor_Manage } from './terrain-editor-manage'
import { TerrainEditor_Sculpt } from './terrain-editor-sculpt'
import { TerrainEditor_Paint } from './terrain-editor-paint'
export class TerrainSample
{
terrain: Terrain|null = null;
editor: TerrainEditor|null = null;
camera: Node|null = null;
onInit(scene: Scene) {
console.log("TerrainSample::onInit1");
let info = new TerrainInfo;
info.tileSize = 1;
info.blockCount[0] = 1;
info.blockCount[1] = 1;
let _node = new Node('Terrain');
_node.setParent(scene);
this.terrain = _node.addComponent(Terrain);
if (this.terrain != null) {
// build terrain
this.terrain.build(info);
this.editor = new TerrainEditor;
this.editor.setEditTerrain(this.terrain as Terrain);
// load layer
cc.loader.loadRes("decal_1/texture", Texture2D, (err, asset) => {
let layer = new TerrainLayer;
layer.detailMap = asset;
layer.tileSize = 8.0;
if (this.terrain != null) {
this.terrain.addLayer(layer);
}
});
cc.loader.loadRes("decal_2/texture", Texture2D, (err, asset) => {
let layer = new TerrainLayer;
layer.detailMap = asset;
layer.tileSize = 8.0;
if (this.terrain != null) {
this.terrain.addLayer(layer);
}
});
let paintMode = this.editor.getMode(eTerrainEditorMode.PAINT) as TerrainEditor_Paint;
paintMode.setCurrentLayer(0);
}
this.camera = scene.getChildByName("Camera");
if (this.camera == null) {
console.log('Camera is null');
}
}
onUpdate(dtime: number) {
if (this.editor != null) {
this.editor.update(dtime);
}
}
onKeyDown(id: number) {
if (this.terrain == null || this.editor == null) {
return;
}
let mode = this.editor.getCurrentModeType();
if (id == cc.macro.KEY.tab) {
// change editor mode
if (mode == eTerrainEditorMode.MANAGE) {
this.editor.setMode(eTerrainEditorMode.SCULPT);
}
else if (mode == eTerrainEditorMode.SCULPT) {
this.editor.setMode(eTerrainEditorMode.PAINT);
}
else if (mode == eTerrainEditorMode.PAINT) {
this.editor.setMode(eTerrainEditorMode.SCULPT);
}
}
else if (mode == eTerrainEditorMode.MANAGE) {
if (id == cc.macro.KEY[1]) {
this.onRebuildHeight();
}
else if (id== cc.macro.KEY[2]) {
this.onRebuildBlock();
}
}
else if (mode == eTerrainEditorMode.SCULPT) {
}
else if (mode == eTerrainEditorMode.PAINT) {
// change current layer
if (id == cc.macro.KEY[1]) {
this.editor.setCurrentLayer(0);
}
else if (id== cc.macro.KEY[2]) {
this.editor.setCurrentLayer(1);
}
}
}
onMouseDown(id: number, x: number, y: number) {
this.editor!.onMouseDown(id, x, y);
}
onMouseUp(id: number, x: number, y: number) {
this.editor!.onMouseUp(id, x, y);
}
onMouseMove(x: number, y: number) {
if (this.editor == null || this.camera == null) {
return;
}
this.editor.onMouseMove(this.camera, x, y);
}
onRebuildHeight() {
if (this.terrain == null || this.editor == null) {
return;
}
let info = new TerrainInfo;
info.tileSize = this.terrain.info.tileSize * 2;
info.blockCount[0] = this.terrain.info.blockCount[0];
info.blockCount[1] = this.terrain.info.blockCount[1];
info.weightMapSize = this.terrain.info.weightMapSize;
info.lightMapSize = this.terrain.info.lightMapSize;
this.terrain!.rebuild(info);
if (this.camera != null) {
let pos = this.camera.getPosition();
pos.x = info.size[0] / 2;
this.camera.setPosition(pos);
}
}
onRebuildBlock() {
if (this.terrain == null || this.editor == null) {
return;
}
let info = new TerrainInfo;
info.tileSize = this.terrain.info.tileSize;
info.blockCount[0] = this.terrain.info.blockCount[0] + 1;
info.blockCount[1] = this.terrain.info.blockCount[1] + 1;
info.weightMapSize = this.terrain.info.weightMapSize;
info.lightMapSize = this.terrain.info.lightMapSize;
this.terrain.rebuild(info);
if (this.camera != null) {
let pos = this.camera.getPosition();
pos.x = info.size[0] / 2;
this.camera.setPosition(pos);
}
}
onRebuildWeight() {
if (this.terrain == null || this.editor == null) {
return;
}
let info = new TerrainInfo;
info.tileSize = this.terrain.info.tileSize;
info.blockCount[0] = this.terrain.info.blockCount[0];
info.blockCount[1] = this.terrain.info.blockCount[1];
info.weightMapSize = 256;
info.lightMapSize = this.terrain.info.lightMapSize;
this.terrain.rebuild(info);
}
}
File diff suppressed because it is too large Load Diff