@zephyr3d/scene 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/asset/assetmanager.js +124 -67
- package/dist/asset/assetmanager.js.map +1 -1
- package/dist/asset/builtin.js +2 -2
- package/dist/asset/loaders/gltf/gltf_loader.js +105 -59
- package/dist/asset/loaders/gltf/gltf_loader.js.map +1 -1
- package/dist/asset/loaders/hdr/hdr.js +1 -1
- package/dist/blitter/blitter.js +0 -1
- package/dist/blitter/blitter.js.map +1 -1
- package/dist/blitter/depthlimitedgaussion.js +0 -1
- package/dist/blitter/depthlimitedgaussion.js.map +1 -1
- package/dist/blitter/gaussianblur.js +0 -1
- package/dist/blitter/gaussianblur.js.map +1 -1
- package/dist/camera/camera.js +10 -8
- package/dist/camera/camera.js.map +1 -1
- package/dist/camera/orbit.js +24 -7
- package/dist/camera/orbit.js.map +1 -1
- package/dist/index.d.ts +1842 -2484
- package/dist/index.js +21 -10
- package/dist/index.js.map +1 -1
- package/dist/material/blinn.js +47 -57
- package/dist/material/blinn.js.map +1 -1
- package/dist/material/grassmat.js +127 -0
- package/dist/material/grassmat.js.map +1 -0
- package/dist/material/grassmaterial.js +66 -194
- package/dist/material/grassmaterial.js.map +1 -1
- package/dist/material/lambert.js +48 -22
- package/dist/material/lambert.js.map +1 -1
- package/dist/material/lightmodel.js +4 -4
- package/dist/material/material.js +100 -59
- package/dist/material/material.js.map +1 -1
- package/dist/material/meshmaterial.js +227 -186
- package/dist/material/meshmaterial.js.map +1 -1
- package/dist/material/mixins/albedocolor.js +29 -100
- package/dist/material/mixins/albedocolor.js.map +1 -1
- package/dist/material/mixins/foliage.js +47 -0
- package/dist/material/mixins/foliage.js.map +1 -0
- package/dist/material/mixins/ggxlut.js +213 -0
- package/dist/material/mixins/ggxlut.js.map +1 -0
- package/dist/material/mixins/lightmodel/blinnphong.js +89 -0
- package/dist/material/mixins/lightmodel/blinnphong.js.map +1 -0
- package/dist/material/mixins/lightmodel/lambert.js +58 -0
- package/dist/material/mixins/lightmodel/lambert.js.map +1 -0
- package/dist/material/mixins/lightmodel/pbrmetallicroughness.js +132 -0
- package/dist/material/mixins/lightmodel/pbrmetallicroughness.js.map +1 -0
- package/dist/material/mixins/lightmodel/pbrspecularglossness.js +105 -0
- package/dist/material/mixins/lightmodel/pbrspecularglossness.js.map +1 -0
- package/dist/material/mixins/lit.js +464 -0
- package/dist/material/mixins/lit.js.map +1 -0
- package/dist/material/mixins/pbr/common.js +451 -0
- package/dist/material/mixins/pbr/common.js.map +1 -0
- package/dist/material/mixins/pbr/metallicroughness.js +126 -0
- package/dist/material/mixins/pbr/metallicroughness.js.map +1 -0
- package/dist/material/mixins/pbr/specularglossness.js +104 -0
- package/dist/material/mixins/pbr/specularglossness.js.map +1 -0
- package/dist/material/mixins/texture.js +157 -0
- package/dist/material/mixins/texture.js.map +1 -0
- package/dist/material/mixins/vertexcolor.js +16 -11
- package/dist/material/mixins/vertexcolor.js.map +1 -1
- package/dist/material/pbrmr.js +65 -0
- package/dist/material/pbrmr.js.map +1 -0
- package/dist/material/pbrsg.js +64 -0
- package/dist/material/pbrsg.js.map +1 -0
- package/dist/material/shader/helper.js +903 -0
- package/dist/material/shader/helper.js.map +1 -0
- package/dist/material/terrainmat.js +357 -0
- package/dist/material/terrainmat.js.map +1 -0
- package/dist/material/terrainmaterial.js +311 -103
- package/dist/material/terrainmaterial.js.map +1 -1
- package/dist/material/unlit.js +12 -9
- package/dist/material/unlit.js.map +1 -1
- package/dist/posteffect/bloom.js +8 -6
- package/dist/posteffect/bloom.js.map +1 -1
- package/dist/posteffect/compositor.js +14 -6
- package/dist/posteffect/compositor.js.map +1 -1
- package/dist/posteffect/posteffect.js +1 -1
- package/dist/posteffect/sao.js +8 -5
- package/dist/posteffect/sao.js.map +1 -1
- package/dist/posteffect/tonemap.js +2 -2
- package/dist/posteffect/water.js +10 -8
- package/dist/posteffect/water.js.map +1 -1
- package/dist/render/cluster_light.js +6 -5
- package/dist/render/cluster_light.js.map +1 -1
- package/dist/render/depthpass.js +46 -0
- package/dist/render/depthpass.js.map +1 -0
- package/dist/render/envlight.js +26 -24
- package/dist/render/envlight.js.map +1 -1
- package/dist/render/fullscreenquad.js +38 -0
- package/dist/render/fullscreenquad.js.map +1 -0
- package/dist/render/lightpass.js +98 -0
- package/dist/render/lightpass.js.map +1 -0
- package/dist/render/render_queue.js +2 -1
- package/dist/render/render_queue.js.map +1 -1
- package/dist/render/renderer.js +191 -0
- package/dist/render/renderer.js.map +1 -0
- package/dist/render/renderpass.js +10 -8
- package/dist/render/renderpass.js.map +1 -1
- package/dist/render/shadowmap_pass.js +3 -4
- package/dist/render/shadowmap_pass.js.map +1 -1
- package/dist/render/sky.js +31 -15
- package/dist/render/sky.js.map +1 -1
- package/dist/scene/environment.js +8 -6
- package/dist/scene/environment.js.map +1 -1
- package/dist/scene/graph_node.js +3 -0
- package/dist/scene/graph_node.js.map +1 -1
- package/dist/scene/mesh.js +13 -12
- package/dist/scene/mesh.js.map +1 -1
- package/dist/scene/scene.js +11 -15
- package/dist/scene/scene.js.map +1 -1
- package/dist/scene/scene_node.js +10 -16
- package/dist/scene/scene_node.js.map +1 -1
- package/dist/scene/terrain/grass.js +4 -14
- package/dist/scene/terrain/grass.js.map +1 -1
- package/dist/scene/terrain/patch.js +3 -3
- package/dist/scene/terrain/terrain.js +4 -9
- package/dist/scene/terrain/terrain.js.map +1 -1
- package/dist/shaders/framework.js +17 -3
- package/dist/shaders/framework.js.map +1 -1
- package/dist/shaders/misc.js +13 -161
- package/dist/shaders/misc.js.map +1 -1
- package/dist/shaders/noise.js +7 -7
- package/dist/shaders/pbr.js +1 -82
- package/dist/shaders/pbr.js.map +1 -1
- package/dist/shaders/shadow.js +33 -31
- package/dist/shaders/shadow.js.map +1 -1
- package/dist/shaders/water.js +3 -9
- package/dist/shaders/water.js.map +1 -1
- package/dist/shadow/esm.js +11 -9
- package/dist/shadow/esm.js.map +1 -1
- package/dist/shadow/pcf_opt.js +15 -15
- package/dist/shadow/pcf_pd.js +15 -15
- package/dist/shadow/shadowmapper.js +13 -15
- package/dist/shadow/shadowmapper.js.map +1 -1
- package/dist/shadow/ssm.js +21 -55
- package/dist/shadow/ssm.js.map +1 -1
- package/dist/shadow/vsm.js +15 -13
- package/dist/shadow/vsm.js.map +1 -1
- package/dist/shapes/torus.js +2 -2
- package/dist/utility/bounding_volume.js +27 -27
- package/dist/utility/pmrem.js +4 -4
- package/dist/utility/sheenlut.js +196 -0
- package/dist/utility/sheenlut.js.map +1 -0
- package/dist/utility/shprojection.js +0 -1
- package/dist/utility/shprojection.js.map +1 -1
- package/dist/values.js +11 -8
- package/dist/values.js.map +1 -1
- package/package.json +3 -3
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helper.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
import { applyMaterialMixins, MeshMaterial } from './meshmaterial.js';
|
|
2
|
+
import { mixinPBRMetallicRoughness } from './mixins/pbr/metallicroughness.js';
|
|
3
|
+
import { mixinLight } from './mixins/lit.js';
|
|
4
|
+
import { Application } from '../app.js';
|
|
5
|
+
import { Vector4 } from '@zephyr3d/base';
|
|
6
|
+
import { drawFullscreenQuad } from '../render/helper.js';
|
|
7
|
+
|
|
8
|
+
class NewTerrainMaterial extends applyMaterialMixins(MeshMaterial, mixinLight, mixinPBRMetallicRoughness) {
|
|
9
|
+
static _metallicRoughnessGenerationProgram = null;
|
|
10
|
+
static _metallicRoughnessGenerationBindGroup = null;
|
|
11
|
+
_options;
|
|
12
|
+
_uvScales;
|
|
13
|
+
_numDetailMaps;
|
|
14
|
+
_terrainInfo;
|
|
15
|
+
constructor(options){
|
|
16
|
+
super();
|
|
17
|
+
this.normalMapMode = 'object-space';
|
|
18
|
+
this._options = null;
|
|
19
|
+
this._numDetailMaps = 0;
|
|
20
|
+
this._uvScales = null;
|
|
21
|
+
this._terrainInfo = null;
|
|
22
|
+
if (options && options.splatMap && options.detailMaps && options.detailMaps.albedoTextures) {
|
|
23
|
+
this._options = Object.assign({}, options);
|
|
24
|
+
const albedoTextures = this._options.detailMaps.albedoTextures;
|
|
25
|
+
this._numDetailMaps = Array.isArray(albedoTextures) ? albedoTextures.length : albedoTextures.depth;
|
|
26
|
+
if (!this._numDetailMaps) {
|
|
27
|
+
throw new Error(`TerrainLightModel(): Invalid detail textures`);
|
|
28
|
+
}
|
|
29
|
+
if (this._numDetailMaps > 4) {
|
|
30
|
+
throw new Error(`TerrainLightModel(): The maximum detail levels is 4`);
|
|
31
|
+
}
|
|
32
|
+
if (!this._options.detailMaps.uvScale || this._options.detailMaps.uvScale.length !== this._numDetailMaps) {
|
|
33
|
+
throw new Error(`TerrainLightModel(): Invalid uv scale`);
|
|
34
|
+
}
|
|
35
|
+
this._uvScales = new Float32Array(this._numDetailMaps * 4);
|
|
36
|
+
for(let i = 0; i < this._numDetailMaps; i++){
|
|
37
|
+
this._uvScales[i * 4] = this._options.detailMaps.uvScale[i];
|
|
38
|
+
this._uvScales[i * 4 + 1] = 1;
|
|
39
|
+
this._uvScales[i * 4 + 2] = 0.01;
|
|
40
|
+
this._uvScales[i * 4 + 3] = 0.99;
|
|
41
|
+
}
|
|
42
|
+
if (this._options.detailMaps.metallic) {
|
|
43
|
+
if (this._options.detailMaps.metallic.length !== this._numDetailMaps) {
|
|
44
|
+
throw new Error(`TerrainLightModel(): Invalid metallic values`);
|
|
45
|
+
}
|
|
46
|
+
for(let i = 0; i < this._numDetailMaps; i++){
|
|
47
|
+
this._uvScales[i * 4 + 2] = this._options.detailMaps.metallic[i];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (this._options.detailMaps.roughness) {
|
|
51
|
+
if (this._options.detailMaps.roughness.length !== this._numDetailMaps) {
|
|
52
|
+
throw new Error(`TerrainLightModel(): Invalid roughness values`);
|
|
53
|
+
}
|
|
54
|
+
for(let i = 0; i < this._numDetailMaps; i++){
|
|
55
|
+
this._uvScales[i * 4 + 3] = this._options.detailMaps.roughness[i];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const normalTextures = options.detailMaps.normalTextures;
|
|
59
|
+
if (normalTextures) {
|
|
60
|
+
const m = Array.isArray(normalTextures) ? normalTextures.length : normalTextures.depth;
|
|
61
|
+
if (m !== this._numDetailMaps) {
|
|
62
|
+
throw new Error(`TerrainLightModel(): The number of normal textures not match the number of albedo textures`);
|
|
63
|
+
}
|
|
64
|
+
if (options.detailMaps.normalScale) {
|
|
65
|
+
if (options.detailMaps.normalScale.length !== this._numDetailMaps) {
|
|
66
|
+
throw new Error(`TerrainLightModel(): Invalid normal scale`);
|
|
67
|
+
}
|
|
68
|
+
for(let i = 0; i < this._numDetailMaps; i++){
|
|
69
|
+
this._uvScales[i * 4 + 1] = options.detailMaps.normalScale[i];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
this._options = Object.assign({}, options);
|
|
74
|
+
if (Array.isArray(albedoTextures)) {
|
|
75
|
+
for(let i = 0; i < albedoTextures.length; i++){
|
|
76
|
+
if (!albedoTextures[i]) {
|
|
77
|
+
throw new Error(`TerrainLightModel(): Invalid detail albedo texture`);
|
|
78
|
+
}
|
|
79
|
+
albedoTextures[i].samplerOptions = {
|
|
80
|
+
addressU: 'repeat',
|
|
81
|
+
addressV: 'repeat'
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
} else {
|
|
85
|
+
albedoTextures.samplerOptions = {
|
|
86
|
+
addressU: 'repeat',
|
|
87
|
+
addressV: 'repeat'
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
if (Array.isArray(normalTextures)) {
|
|
91
|
+
for(let i = 0; i < normalTextures.length; i++){
|
|
92
|
+
if (!normalTextures[i]) {
|
|
93
|
+
throw new Error(`TerrainLightModel(): Invalid detail normal texture`);
|
|
94
|
+
}
|
|
95
|
+
normalTextures[i].samplerOptions = {
|
|
96
|
+
addressU: 'repeat',
|
|
97
|
+
addressV: 'repeat'
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
} else if (normalTextures) {
|
|
101
|
+
normalTextures.samplerOptions = {
|
|
102
|
+
addressU: 'repeat',
|
|
103
|
+
addressV: 'repeat'
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
this.vertexNormal = true;
|
|
108
|
+
this.metallicRoughnessTexture = this.generateMetallicRoughnessMap();
|
|
109
|
+
this.metallicRoughnessTexCoordIndex = -1;
|
|
110
|
+
this.albedoTexCoordIndex = -1;
|
|
111
|
+
this.normalTexCoordIndex = -1;
|
|
112
|
+
}
|
|
113
|
+
get terrainInfo() {
|
|
114
|
+
return this._terrainInfo;
|
|
115
|
+
}
|
|
116
|
+
set terrainInfo(val) {
|
|
117
|
+
this._terrainInfo = val;
|
|
118
|
+
this.optionChanged(false);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* {@inheritDoc Material.isTransparent}
|
|
122
|
+
* @override
|
|
123
|
+
*/ isTransparent() {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* {@inheritDoc Material.supportLighting}
|
|
128
|
+
* @override
|
|
129
|
+
*/ supportLighting() {
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
applyUniformValues(bindGroup, ctx) {
|
|
133
|
+
super.applyUniformValues(bindGroup, ctx);
|
|
134
|
+
if (this.needFragmentColor(ctx)) {
|
|
135
|
+
bindGroup.setValue('terrainInfo', this._terrainInfo);
|
|
136
|
+
if (this._options) {
|
|
137
|
+
bindGroup.setValue('kkDetailScales', this._uvScales);
|
|
138
|
+
bindGroup.setTexture('kkSplatMap', this._options.splatMap);
|
|
139
|
+
if (Array.isArray(this._options.detailMaps.albedoTextures)) {
|
|
140
|
+
for(let i = 0; i < this._numDetailMaps; i++){
|
|
141
|
+
bindGroup.setTexture(`kkDetailAlbedoMap${i}`, this._options.detailMaps.albedoTextures[i]);
|
|
142
|
+
}
|
|
143
|
+
} else {
|
|
144
|
+
bindGroup.setTexture('kkDetailAlbedoMap', this._options.detailMaps.albedoTextures);
|
|
145
|
+
}
|
|
146
|
+
if (Array.isArray(this._options.detailMaps.normalTextures)) {
|
|
147
|
+
for(let i = 0; i < this._numDetailMaps; i++){
|
|
148
|
+
bindGroup.setTexture(`kkDetailNormalMap${i}`, this._options.detailMaps.normalTextures[i]);
|
|
149
|
+
}
|
|
150
|
+
} else {
|
|
151
|
+
bindGroup.setTexture('kkDetailNormalMap', this._options.detailMaps.normalTextures);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/** @ts-ignore */ getMetallicRoughnessTexCoord(scope) {
|
|
157
|
+
return scope.$inputs.mapUV;
|
|
158
|
+
}
|
|
159
|
+
/** @ts-ignore */ getNormalTexCoord(scope) {
|
|
160
|
+
return scope.$inputs.mapUV;
|
|
161
|
+
}
|
|
162
|
+
/** @ts-ignore */ getAlbedoTexCoord(scope) {
|
|
163
|
+
return scope.$inputs.mapUV;
|
|
164
|
+
}
|
|
165
|
+
calculateAlbedoColor(scope, ctx) {
|
|
166
|
+
if (!this._options) {
|
|
167
|
+
return super.calculateAlbedoColor(scope, ctx);
|
|
168
|
+
}
|
|
169
|
+
const that = this;
|
|
170
|
+
const pb = scope.$builder;
|
|
171
|
+
const funcName = 'getTerrainAlbedo';
|
|
172
|
+
pb.func(funcName, [], function() {
|
|
173
|
+
this.$l.mask = pb.textureSample(this.kkSplatMap, this.$inputs.mapUV);
|
|
174
|
+
this.$l.color = pb.vec3(0);
|
|
175
|
+
const useTextureArray = !Array.isArray(that._options.detailMaps.albedoTextures);
|
|
176
|
+
for(let i = 0; i < that._numDetailMaps; i++){
|
|
177
|
+
const uv = pb.mul(this.$inputs.mapUV, this.kkDetailScales.at(i).x);
|
|
178
|
+
const sample = useTextureArray ? pb.textureArraySample(this.kkDetailAlbedoMap, uv, i).rgb : pb.textureSample(this[`kkDetailAlbedoMap${i}`], uv).rgb;
|
|
179
|
+
this.color = pb.add(this.color, pb.mul(sample, this.mask[i]));
|
|
180
|
+
}
|
|
181
|
+
this.$return(pb.vec4(this.color, 1));
|
|
182
|
+
});
|
|
183
|
+
return pb.getGlobalScope()[funcName]();
|
|
184
|
+
}
|
|
185
|
+
sampleNormalMapWithTBN(scope, tex, texCoord, normalScale, TBN) {
|
|
186
|
+
const pb = scope.$builder;
|
|
187
|
+
const pixel = pb.sub(pb.mul(pb.textureSample(tex, texCoord).rgb, 2), pb.vec3(1));
|
|
188
|
+
const normalTex = pb.mul(pixel, pb.vec3(pb.vec3(normalScale).xx, 1));
|
|
189
|
+
return pb.normalize(pb.mul(TBN, normalTex));
|
|
190
|
+
}
|
|
191
|
+
calculateNormalAndTBN(scope, ctx) {
|
|
192
|
+
scope.$l.normalInfo = super.calculateNormalAndTBN(scope, ctx);
|
|
193
|
+
const pb = scope.$builder;
|
|
194
|
+
let calcNormal = false;
|
|
195
|
+
if (this._options && this._options.detailMaps.normalTextures) {
|
|
196
|
+
scope.$l.detailMask = pb.textureSample(scope.kkSplatMap, scope.$inputs.mapUV);
|
|
197
|
+
if (Array.isArray(this._options.detailMaps.normalTextures)) {
|
|
198
|
+
for(let i = 0; i < this._options.detailMaps.normalTextures.length; i++){
|
|
199
|
+
const tex = scope[`kkDetailNormalMap${i}`];
|
|
200
|
+
const scale = scope.kkDetailScales.at(i).y;
|
|
201
|
+
const texCoord = pb.mul(scope.$inputs.mapUV, scope.kkDetailScales.at(i).x);
|
|
202
|
+
scope.normalInfo.normal = pb.add(scope.normalInfo.normal, pb.mul(this.sampleNormalMapWithTBN(scope, tex, texCoord, scale, scope.normalInfo.TBN), scope.detailMask[i]));
|
|
203
|
+
calcNormal = true;
|
|
204
|
+
}
|
|
205
|
+
} else {
|
|
206
|
+
const tex = scope.kkDetailNormalMap;
|
|
207
|
+
for(let i = 0; i < this._numDetailMaps; i++){
|
|
208
|
+
const scale = scope.kkDetailScales.at(i).y;
|
|
209
|
+
const texCoord = pb.mul(scope.$inputs.mapUV, scope.kkDetailScales.at(i).x);
|
|
210
|
+
const pixel = pb.sub(pb.mul(pb.textureArraySample(tex, texCoord, i).rgb, 2), pb.vec3(1));
|
|
211
|
+
const normalTex = pb.mul(pixel, pb.vec3(pb.vec3(scale).xx, 1));
|
|
212
|
+
const detailNormal = pb.normalize(pb.mul(scope.normalInfo.TBN, normalTex));
|
|
213
|
+
scope.normalInfo.normal = pb.add(scope.normalInfo.normal, pb.mul(detailNormal, scope.detailMask[i]));
|
|
214
|
+
calcNormal = true;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
if (calcNormal) {
|
|
219
|
+
scope.normalInfo.normal = pb.normalize(scope.normalInfo.normal);
|
|
220
|
+
}
|
|
221
|
+
return scope.normalInfo;
|
|
222
|
+
}
|
|
223
|
+
vertexShader(scope, ctx) {
|
|
224
|
+
super.vertexShader(scope, ctx);
|
|
225
|
+
const pb = scope.$builder;
|
|
226
|
+
scope.$inputs.zPos = pb.vec3().attrib('position');
|
|
227
|
+
if (this.needFragmentColor(ctx)) {
|
|
228
|
+
scope.$g.terrainInfo = pb.vec4().uniform(2);
|
|
229
|
+
scope.$outputs.mapUV = pb.div(scope.$inputs.zPos.xz, scope.terrainInfo.xy);
|
|
230
|
+
}
|
|
231
|
+
this.transformVertexAndNormal(scope);
|
|
232
|
+
}
|
|
233
|
+
fragmentShader(scope, ctx) {
|
|
234
|
+
super.fragmentShader(scope, ctx);
|
|
235
|
+
const pb = scope.$builder;
|
|
236
|
+
const that = this;
|
|
237
|
+
if (this.needFragmentColor(ctx)) {
|
|
238
|
+
if (this._options) {
|
|
239
|
+
scope.$g.kkDetailScales = pb.vec4[this._numDetailMaps]().uniform(2);
|
|
240
|
+
scope.$g.kkSplatMap = pb.tex2D().uniform(2);
|
|
241
|
+
const useAlbedoTextureArray = !Array.isArray(that._options.detailMaps.albedoTextures);
|
|
242
|
+
if (useAlbedoTextureArray) {
|
|
243
|
+
scope.$g.kkDetailAlbedoMap = pb.tex2DArray().uniform(2);
|
|
244
|
+
} else {
|
|
245
|
+
for(let i = 0; i < that._numDetailMaps; i++){
|
|
246
|
+
scope.$g[`kkDetailAlbedoMap${i}`] = pb.tex2D().uniform(2);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
const useNormalTextureArray = !Array.isArray(that._options.detailMaps.normalTextures);
|
|
250
|
+
if (useNormalTextureArray) {
|
|
251
|
+
scope.$g.kkDetailNormalMap = pb.tex2DArray().uniform(2);
|
|
252
|
+
} else {
|
|
253
|
+
for(let i = 0; i < that._numDetailMaps; i++){
|
|
254
|
+
scope.$g[`kkDetailNormalMap${i}`] = pb.tex2D().uniform(2);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
scope.$l.albedo = this.calculateAlbedoColor(scope, ctx);
|
|
259
|
+
scope.$l.normalInfo = this.calculateNormalAndTBN(scope, ctx);
|
|
260
|
+
scope.$l.normal = scope.normalInfo.normal;
|
|
261
|
+
scope.$l.viewVec = this.calculateViewVector(scope);
|
|
262
|
+
scope.$l.pbrData = this.getCommonData(scope, scope.albedo, scope.viewVec, scope.normalInfo.TBN);
|
|
263
|
+
scope.$l.lightingColor = pb.vec3(0);
|
|
264
|
+
scope.$l.emissiveColor = this.calculateEmissiveColor(scope);
|
|
265
|
+
this.indirectLighting(scope, scope.normal, scope.viewVec, scope.pbrData, scope.lightingColor, ctx);
|
|
266
|
+
this.forEachLight(scope, ctx, function(type, posRange, dirCutoff, colorIntensity, shadow) {
|
|
267
|
+
this.$l.diffuse = pb.vec3();
|
|
268
|
+
this.$l.specular = pb.vec3();
|
|
269
|
+
this.$l.lightAtten = that.calculateLightAttenuation(this, type, posRange, dirCutoff);
|
|
270
|
+
this.$l.lightDir = that.calculateLightDirection(this, type, posRange, dirCutoff);
|
|
271
|
+
this.$l.NoL = pb.clamp(pb.dot(this.normal, this.lightDir), 0, 1);
|
|
272
|
+
this.$l.lightColor = pb.mul(colorIntensity.rgb, colorIntensity.a, this.lightAtten, this.NoL);
|
|
273
|
+
if (shadow) {
|
|
274
|
+
this.lightColor = pb.mul(this.lightColor, that.calculateShadow(this, this.NoL, ctx));
|
|
275
|
+
}
|
|
276
|
+
that.directLighting(this, this.lightDir, this.lightColor, this.normal, this.viewVec, this.pbrData, this.lightingColor);
|
|
277
|
+
});
|
|
278
|
+
scope.$l.litColor = pb.add(scope.lightingColor, scope.emissiveColor);
|
|
279
|
+
this.outputFragmentColor(scope, pb.vec4(scope.litColor, scope.albedo.a), ctx);
|
|
280
|
+
} else {
|
|
281
|
+
this.outputFragmentColor(scope, null, ctx);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
generateMetallicRoughnessMap() {
|
|
285
|
+
const device = Application.instance.device;
|
|
286
|
+
if (!this._options) {
|
|
287
|
+
const tex = device.createTexture2D('rgba8unorm', 1, 1, {
|
|
288
|
+
samplerOptions: {
|
|
289
|
+
mipFilter: 'none'
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
tex.update(new Uint8Array([
|
|
293
|
+
0,
|
|
294
|
+
1,
|
|
295
|
+
0,
|
|
296
|
+
0
|
|
297
|
+
]), 0, 0, 1, 1);
|
|
298
|
+
tex.name = 'TerrainMetallicRoughnessMap';
|
|
299
|
+
return tex;
|
|
300
|
+
}
|
|
301
|
+
if (!NewTerrainMaterial._metallicRoughnessGenerationProgram) {
|
|
302
|
+
NewTerrainMaterial._metallicRoughnessGenerationProgram = device.buildRenderProgram({
|
|
303
|
+
vertex (pb) {
|
|
304
|
+
this.$inputs.pos = pb.vec2().attrib('position');
|
|
305
|
+
this.$outputs.uv = pb.vec2();
|
|
306
|
+
pb.main(function() {
|
|
307
|
+
this.$builtins.position = pb.vec4(this.$inputs.pos, 0, 1);
|
|
308
|
+
this.$outputs.uv = pb.add(pb.mul(this.$inputs.pos.xy, 0.5), pb.vec2(0.5));
|
|
309
|
+
if (device.type === 'webgpu') {
|
|
310
|
+
this.$builtins.position.y = pb.neg(this.$builtins.position.y);
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
},
|
|
314
|
+
fragment (pb) {
|
|
315
|
+
this.$outputs.outColor = pb.vec4();
|
|
316
|
+
this.roughness = pb.vec4().uniform(0);
|
|
317
|
+
this.metallic = pb.vec4().uniform(0);
|
|
318
|
+
this.splatMap = pb.tex2D().uniform(0);
|
|
319
|
+
pb.main(function() {
|
|
320
|
+
this.weights = pb.textureSample(this.splatMap, this.$inputs.uv);
|
|
321
|
+
this.roughnessValue = pb.dot(this.weights, this.roughness);
|
|
322
|
+
this.metallicValue = pb.dot(this.weights, this.metallic);
|
|
323
|
+
this.$outputs.outColor = pb.vec4(0, this.roughnessValue, this.metallicValue, 1);
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
NewTerrainMaterial._metallicRoughnessGenerationBindGroup = device.createBindGroup(NewTerrainMaterial._metallicRoughnessGenerationProgram.bindGroupLayouts[0]);
|
|
328
|
+
}
|
|
329
|
+
const roughnessValues = Vector4.one();
|
|
330
|
+
const metallicValues = Vector4.zero();
|
|
331
|
+
for(let i = 0; i < this._numDetailMaps; i++){
|
|
332
|
+
metallicValues[i] = this._uvScales[i * 4 + 2];
|
|
333
|
+
roughnessValues[i] = this._uvScales[i * 4 + 3];
|
|
334
|
+
}
|
|
335
|
+
const tex = device.createTexture2D('rgba8unorm', this._options.splatMap.width, this._options.splatMap.height);
|
|
336
|
+
tex.name = 'TerrainMetallicRoughnessMap';
|
|
337
|
+
const program = NewTerrainMaterial._metallicRoughnessGenerationProgram;
|
|
338
|
+
const bindgroup = NewTerrainMaterial._metallicRoughnessGenerationBindGroup;
|
|
339
|
+
bindgroup.setValue('roughness', roughnessValues);
|
|
340
|
+
bindgroup.setValue('metallic', metallicValues);
|
|
341
|
+
bindgroup.setTexture('splatMap', this._options.splatMap);
|
|
342
|
+
const fb = device.createFrameBuffer([
|
|
343
|
+
tex
|
|
344
|
+
], null);
|
|
345
|
+
device.pushDeviceStates();
|
|
346
|
+
device.setFramebuffer(fb);
|
|
347
|
+
device.setProgram(program);
|
|
348
|
+
device.setBindGroup(0, bindgroup);
|
|
349
|
+
drawFullscreenQuad();
|
|
350
|
+
device.popDeviceStates();
|
|
351
|
+
fb.dispose();
|
|
352
|
+
return tex;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
export { NewTerrainMaterial };
|
|
357
|
+
//# sourceMappingURL=terrainmat.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"terrainmat.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|