@woosh/meep-engine 2.33.65 → 2.33.70
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/core/geom/2d/quad-tree/QuadTreeNode.js +3 -2
- package/core/geom/3d/triangle/computeTriangleRayIntersection.js +3 -9
- package/engine/asset/loaders/GLTFAssetLoader.js +0 -4
- package/engine/ecs/fow/FogOfWarRevealerSystem.js +8 -0
- package/engine/ecs/fow/shader/screenSpaceFogOfWarShader.js +2 -10
- package/engine/ecs/grid/NormalMap2AOMap.js +10 -13
- package/engine/ecs/terrain/ecs/BuildLightTexture.js +106 -0
- package/engine/ecs/terrain/ecs/PromiseSamplerHeight.js +59 -0
- package/engine/ecs/terrain/ecs/Terrain.js +14 -153
- package/engine/graphics/FULL_SCREEN_QUAD_VERTEX_SHADER.js +10 -0
- package/engine/graphics/filter/ImageFilter.js +6 -2
- package/engine/graphics/geometry/buffered/deinterleaveBufferAttribute.js +3 -2
- package/engine/graphics/geometry/buffered/query/ClippingPlaneContainmentComputingVisitor.js +28 -27
- package/engine/graphics/geometry/buffered/query/GeometryVisitor.js +29 -2
- package/engine/graphics/geometry/buffered/query/RaycastNearestHitComputingVisitor.js +28 -27
- package/engine/graphics/micron/convert_three_object_to_micron.js +14 -3
- package/engine/graphics/micron/plugin/MicronRenderPlugin.js +3 -0
- package/engine/graphics/shaders/AmbientOcclusionShader.js +57 -77
- package/engine/graphics/shaders/DenoiseShader.js +119 -0
- package/engine/graphics/texture/noise_octaves.js +33 -0
- package/engine/graphics/texture/sampler/Sampler2D2Texture.js +7 -18
- package/engine/graphics/texture/sampler/debug/paintSamplerOnHTML.js +8 -5
- package/engine/graphics/texture/sampler/sampler2DToFloat32Texture.js +10 -3
- package/generation/example/SampleGenerator0.js +7 -11
- package/generation/example/filters/SampleGroundMoistureFilter.js +2 -1
- package/generation/example/main.js +215 -0
- package/generation/example/themes/SampleTheme0.js +22 -9
- package/generation/filtering/numeric/complex/CellFilterGaussianBlur.js +29 -13
- package/generation/grid/GridData.js +17 -2
- package/generation/placement/GridCellPlacementRule.js +3 -5
- package/package.json +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { IndexedBinaryBVHVisitor } from "../../../../../core/bvh2/binary/IndexedBinaryBVHVisitor.js";
|
|
2
2
|
|
|
3
|
-
export class GeometrySpatialAcceleratorVisitor extends IndexedBinaryBVHVisitor{
|
|
3
|
+
export class GeometrySpatialAcceleratorVisitor extends IndexedBinaryBVHVisitor {
|
|
4
4
|
constructor() {
|
|
5
5
|
super();
|
|
6
6
|
|
|
@@ -25,6 +25,20 @@ export class GeometrySpatialAcceleratorVisitor extends IndexedBinaryBVHVisitor{
|
|
|
25
25
|
*/
|
|
26
26
|
this.__buffer_vertices = null;
|
|
27
27
|
|
|
28
|
+
/**
|
|
29
|
+
*
|
|
30
|
+
* @type {number}
|
|
31
|
+
* @protected
|
|
32
|
+
*/
|
|
33
|
+
this.__buffer_vertex_stride = 3;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
*
|
|
37
|
+
* @type {number}
|
|
38
|
+
* @protected
|
|
39
|
+
*/
|
|
40
|
+
this.__buffer_position_offset = 0;
|
|
41
|
+
|
|
28
42
|
/**
|
|
29
43
|
* @protected
|
|
30
44
|
* @type {IndexedBinaryBVH}
|
|
@@ -47,7 +61,20 @@ export class GeometrySpatialAcceleratorVisitor extends IndexedBinaryBVHVisitor{
|
|
|
47
61
|
this.geometry = geometry;
|
|
48
62
|
|
|
49
63
|
this.__buffer_indices = geometry.getIndex().array;
|
|
50
|
-
|
|
64
|
+
|
|
65
|
+
const position_attribute = geometry.getAttribute('position');
|
|
66
|
+
|
|
67
|
+
if (position_attribute.isInterleavedBufferAttribute) {
|
|
68
|
+
const data = position_attribute.data;
|
|
69
|
+
|
|
70
|
+
this.__buffer_vertices = data.array;
|
|
71
|
+
this.__buffer_vertex_stride = data.stride;
|
|
72
|
+
this.__buffer_position_offset = position_attribute.offset;
|
|
73
|
+
} else {
|
|
74
|
+
this.__buffer_vertices = position_attribute.array;
|
|
75
|
+
this.__buffer_vertex_stride = 3;
|
|
76
|
+
this.__buffer_position_offset = 0;
|
|
77
|
+
}
|
|
51
78
|
}
|
|
52
79
|
|
|
53
80
|
}
|
|
@@ -96,21 +96,32 @@ export class RaycastNearestHitComputingVisitor extends GeometrySpatialAccelerato
|
|
|
96
96
|
|
|
97
97
|
const index3 = index * 3;
|
|
98
98
|
|
|
99
|
-
const
|
|
100
|
-
const b = this.__buffer_indices[index3 + 1] * 3;
|
|
101
|
-
const c = this.__buffer_indices[index3 + 2] * 3;
|
|
99
|
+
const indices = this.__buffer_indices;
|
|
102
100
|
|
|
103
|
-
const
|
|
104
|
-
const
|
|
105
|
-
const
|
|
101
|
+
const a = indices[index3];
|
|
102
|
+
const b = indices[index3 + 1];
|
|
103
|
+
const c = indices[index3 + 2];
|
|
106
104
|
|
|
107
|
-
const
|
|
108
|
-
const by = this.__buffer_vertices[b + 1];
|
|
109
|
-
const bz = this.__buffer_vertices[b + 2];
|
|
105
|
+
const vertices = this.__buffer_vertices;
|
|
110
106
|
|
|
111
|
-
const
|
|
112
|
-
const
|
|
113
|
-
|
|
107
|
+
const stride = this.__buffer_vertex_stride;
|
|
108
|
+
const offset = this.__buffer_position_offset;
|
|
109
|
+
|
|
110
|
+
const a_address = a * stride + offset;
|
|
111
|
+
const b_address = b * stride + offset;
|
|
112
|
+
const c_address = c * stride + offset;
|
|
113
|
+
|
|
114
|
+
const ax = vertices[a_address];
|
|
115
|
+
const ay = vertices[a_address + 1];
|
|
116
|
+
const az = vertices[a_address + 2];
|
|
117
|
+
|
|
118
|
+
const bx = vertices[b_address];
|
|
119
|
+
const by = vertices[b_address + 1];
|
|
120
|
+
const bz = vertices[b_address + 2];
|
|
121
|
+
|
|
122
|
+
const cx = vertices[c_address];
|
|
123
|
+
const cy = vertices[c_address + 1];
|
|
124
|
+
const cz = vertices[c_address + 2];
|
|
114
125
|
|
|
115
126
|
if (computeTriangleRayIntersection(
|
|
116
127
|
this.__temp_hit,
|
|
@@ -120,15 +131,9 @@ export class RaycastNearestHitComputingVisitor extends GeometrySpatialAccelerato
|
|
|
120
131
|
rayDirection.x,
|
|
121
132
|
rayDirection.y,
|
|
122
133
|
rayDirection.z,
|
|
123
|
-
ax,
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
bx,
|
|
127
|
-
by,
|
|
128
|
-
bz,
|
|
129
|
-
cx,
|
|
130
|
-
cy,
|
|
131
|
-
cz
|
|
134
|
+
ax, ay, az,
|
|
135
|
+
bx, by, bz,
|
|
136
|
+
cx, cy, cz
|
|
132
137
|
)) {
|
|
133
138
|
const d = this.__temp_hit.position.distanceSqrTo(rayOrigin);
|
|
134
139
|
|
|
@@ -154,12 +159,8 @@ export class RaycastNearestHitComputingVisitor extends GeometrySpatialAccelerato
|
|
|
154
159
|
const z1 = data[address + 5];
|
|
155
160
|
|
|
156
161
|
return intersectRay(
|
|
157
|
-
x0,
|
|
158
|
-
|
|
159
|
-
z0,
|
|
160
|
-
x1,
|
|
161
|
-
y1,
|
|
162
|
-
z1,
|
|
162
|
+
x0, y0, z0,
|
|
163
|
+
x1, y1, z1,
|
|
163
164
|
|
|
164
165
|
rayOrigin.x,
|
|
165
166
|
rayOrigin.y,
|
|
@@ -43,10 +43,15 @@ export async function convert_three_object_to_micron(object, micron_cache = defa
|
|
|
43
43
|
return attached_micron;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
const
|
|
46
|
+
const cached_micron = await micron_cache.get(g);
|
|
47
47
|
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
|
|
49
|
+
if (cached_micron !== undefined && cached_micron !== null) {
|
|
50
|
+
|
|
51
|
+
// set proxy
|
|
52
|
+
cached_micron.proxy = g;
|
|
53
|
+
|
|
54
|
+
return cached_micron;
|
|
50
55
|
}
|
|
51
56
|
|
|
52
57
|
const pending = pending_cache.get(g);
|
|
@@ -61,8 +66,14 @@ export async function convert_three_object_to_micron(object, micron_cache = defa
|
|
|
61
66
|
|
|
62
67
|
promises.push(micron_geometry_promise);
|
|
63
68
|
|
|
69
|
+
/**
|
|
70
|
+
*
|
|
71
|
+
* @type {MicronGeometry}
|
|
72
|
+
*/
|
|
64
73
|
const micron_geometry = await micron_geometry_promise;
|
|
65
74
|
|
|
75
|
+
micron_geometry.proxy = g;
|
|
76
|
+
|
|
66
77
|
await micron_cache.set(g, micron_geometry);
|
|
67
78
|
|
|
68
79
|
return micron_geometry;
|
|
@@ -106,6 +106,9 @@ class GLTFAssetTransformer extends AssetTransformer {
|
|
|
106
106
|
|
|
107
107
|
this.__serde.deserialize(buffer, micron_geometry);
|
|
108
108
|
|
|
109
|
+
// set proxy for raycasting
|
|
110
|
+
micron_geometry.proxy = geometry;
|
|
111
|
+
|
|
109
112
|
geometry[MICRON_GEOMETRY_FIELD] = micron_geometry;
|
|
110
113
|
}
|
|
111
114
|
|
|
@@ -3,9 +3,6 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { Vector2 } from "three";
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
const samplesPerPixel = 37;
|
|
8
|
-
|
|
9
6
|
const AmbientOcclusionShader = function () {
|
|
10
7
|
return {
|
|
11
8
|
|
|
@@ -13,11 +10,16 @@ const AmbientOcclusionShader = function () {
|
|
|
13
10
|
|
|
14
11
|
"normalMap": { type: "t", value: null },
|
|
15
12
|
"heightMap": { type: "t", value: null },
|
|
16
|
-
"
|
|
13
|
+
"world_size": { type: "v2", value: new Vector2(512, 512) },
|
|
17
14
|
"rayLength": { type: 'f', value: 17 }
|
|
18
15
|
|
|
19
16
|
},
|
|
20
17
|
|
|
18
|
+
defines: {
|
|
19
|
+
'NUM_SAMPLES': 64,
|
|
20
|
+
'NUM_RINGS': 7,
|
|
21
|
+
},
|
|
22
|
+
|
|
21
23
|
vertexShader: [
|
|
22
24
|
|
|
23
25
|
"varying vec2 vUv;",
|
|
@@ -33,17 +35,17 @@ const AmbientOcclusionShader = function () {
|
|
|
33
35
|
|
|
34
36
|
fragmentShader: [
|
|
35
37
|
`
|
|
36
|
-
uniform vec2 resolution;
|
|
37
38
|
uniform sampler2D normalMap;
|
|
38
39
|
uniform sampler2D heightMap;
|
|
39
40
|
uniform float rayLength;
|
|
41
|
+
uniform vec2 world_size;
|
|
40
42
|
|
|
41
43
|
varying vec2 vUv;
|
|
42
44
|
|
|
43
45
|
vec3 get(float x, float y){
|
|
44
|
-
vec2 _uv = vUv.xy + vec2(x,y)/
|
|
46
|
+
vec2 _uv = vUv.xy + vec2(x,y) / world_size;
|
|
45
47
|
float h = texture2D(heightMap, _uv).x;
|
|
46
|
-
return vec3(_uv.x, h, _uv.y);
|
|
48
|
+
return vec3( _uv.x * world_size.x, h, _uv.y * world_size.y );
|
|
47
49
|
}
|
|
48
50
|
|
|
49
51
|
float hash1( float n )
|
|
@@ -56,92 +58,70 @@ const AmbientOcclusionShader = function () {
|
|
|
56
58
|
p = 50.0*fract( p*0.3183099 );
|
|
57
59
|
return fract( p.x*p.y*(p.x+p.y) );
|
|
58
60
|
}
|
|
59
|
-
|
|
60
61
|
|
|
61
|
-
|
|
62
|
-
{
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
float a = hash1(p+vec2(0,0));
|
|
68
|
-
float b = hash1(p+vec2(1,0));
|
|
69
|
-
float c = hash1(p+vec2(0,1));
|
|
70
|
-
float d = hash1(p+vec2(1,1));
|
|
71
|
-
|
|
72
|
-
return -1.0+2.0*( a + (b-a)*u.x + (c-a)*u.y + (a - b - c + d)*u.x*u.y );
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
float noise_01(in vec2 x){
|
|
76
|
-
return (noise(x) + 1.0)*0.5;
|
|
62
|
+
// Non-sin based hash function, fast and has good randomness
|
|
63
|
+
float hash12(vec2 p){
|
|
64
|
+
vec3 p3 = fract(vec3(p.xyx) * .1031);
|
|
65
|
+
p3 += dot(p3, p3.yzx + 33.33);
|
|
66
|
+
return fract((p3.x + p3.y) * p3.z);
|
|
77
67
|
}
|
|
78
68
|
|
|
79
|
-
|
|
80
|
-
float random_0 = noise_01( seed + vec2(1.123, 3.1) );
|
|
81
|
-
float random_1 = noise_01( seed + vec2(3.123, 7.8913) );
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
float angleCosine = cos(angle);
|
|
85
|
-
|
|
86
|
-
float z = random_0 * (1.0 - angleCosine) + angleCosine;
|
|
69
|
+
const float bias = 0.001;
|
|
87
70
|
|
|
88
|
-
|
|
71
|
+
float pow2(float x){
|
|
72
|
+
return x*x;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
float getOcclusion(vec3 origin, vec3 normal, vec3 hit_position){
|
|
76
|
+
vec3 viewDelta = hit_position - origin;
|
|
89
77
|
|
|
90
|
-
float
|
|
78
|
+
float viewDistance = length( viewDelta );
|
|
91
79
|
|
|
92
|
-
float
|
|
80
|
+
float vn = dot( normal, viewDelta );
|
|
81
|
+
float a2 = (vn) / viewDistance - bias;
|
|
82
|
+
float a1 = (1.0 + pow2( viewDistance ) );
|
|
93
83
|
|
|
94
|
-
|
|
95
|
-
|
|
84
|
+
return max(0.0, a2) / a1;
|
|
85
|
+
}
|
|
96
86
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
87
|
+
const float PI2 = 6.28318530717958;
|
|
88
|
+
const float kernelRadius = 100.0;
|
|
89
|
+
|
|
90
|
+
const float ANGLE_STEP = PI2 * float( NUM_RINGS ) / float( NUM_SAMPLES );
|
|
91
|
+
const float INV_NUM_SAMPLES = 1.0 / float( NUM_SAMPLES );
|
|
92
|
+
|
|
93
|
+
float getAmbientOcclusion(vec3 world_position, vec3 world_normal){
|
|
94
|
+
// jsfiddle that shows sample pattern: https://jsfiddle.net/a16ff1p7/
|
|
95
|
+
float angle = hash12( vUv ) * PI2;
|
|
96
|
+
vec2 radius = vec2( rayLength * INV_NUM_SAMPLES );
|
|
100
97
|
|
|
101
|
-
float
|
|
102
|
-
|
|
103
|
-
float c = dZ;
|
|
104
|
-
float s = sin(rot);
|
|
105
|
-
float t = 1.0 - c;
|
|
106
|
-
float tx = -t * dY;
|
|
107
|
-
float ty = t * dX;
|
|
98
|
+
float occlusionSum = 0.0;
|
|
99
|
+
float weightSum = 0.0;
|
|
108
100
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
101
|
+
for( int i = 0; i < NUM_SAMPLES; i ++ ) {
|
|
102
|
+
vec2 sampleUv = vec2( cos( angle ), sin( angle ) ) * radius * float(i+1);
|
|
103
|
+
|
|
104
|
+
angle += ANGLE_STEP;
|
|
105
|
+
|
|
106
|
+
vec3 sample_pos = get(sampleUv.x, sampleUv.y);
|
|
114
107
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
108
|
+
occlusionSum += getOcclusion(world_position, world_normal, sample_pos);
|
|
109
|
+
weightSum += 1.0;
|
|
110
|
+
}
|
|
118
111
|
|
|
119
|
-
return
|
|
112
|
+
return occlusionSum/weightSum;
|
|
120
113
|
}
|
|
121
|
-
|
|
114
|
+
|
|
122
115
|
void main() {
|
|
123
|
-
float occlusion = 0.0;
|
|
124
116
|
vec3 pos = get(0.0, 0.0);
|
|
125
|
-
vec3 normal = texture2D( normalMap, vUv ).xyz;
|
|
126
|
-
|
|
127
|
-
for(int i=1; i< ${samplesPerPixel}; i++){
|
|
128
|
-
float s = float(i)/${samplesPerPixel - 1}.0;
|
|
129
|
-
float a = sqrt(s*512.0);
|
|
130
|
-
float b = sqrt(s)*rayLength;
|
|
131
|
-
float x = sin(a)*b;
|
|
132
|
-
float y = cos(a)*b;
|
|
133
|
-
|
|
134
|
-
vec3 sample_pos = get(x, y);
|
|
135
|
-
|
|
136
|
-
vec3 sample_dir = normalize(sample_pos - pos);
|
|
137
|
-
float vDot = dot(normal, sample_dir);
|
|
138
|
-
float lambert = clamp(vDot, 0.0, 1.0);
|
|
139
|
-
float dist_factor = 0.23/sqrt(length(sample_pos - pos));
|
|
140
|
-
occlusion += dist_factor*lambert;
|
|
141
|
-
}
|
|
142
117
|
|
|
143
|
-
|
|
144
|
-
|
|
118
|
+
vec3 normal = texture2D( normalMap, vUv ).xzy;
|
|
119
|
+
|
|
120
|
+
float occlusion = getAmbientOcclusion(pos, normal);
|
|
121
|
+
|
|
122
|
+
float incident = 1.0 - occlusion;
|
|
123
|
+
|
|
124
|
+
gl_FragColor = vec4(pow(incident,10.0), 0.0, 0.0, 1.0);
|
|
145
125
|
}
|
|
146
126
|
`
|
|
147
127
|
].join('\n')
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { Vector2 } from "three";
|
|
2
|
+
|
|
3
|
+
const KERNEL_SIZE = 15;
|
|
4
|
+
|
|
5
|
+
const fragment = `
|
|
6
|
+
// bilateral filter, based on https://www.shadertoy.com/view/4dfGDH# and
|
|
7
|
+
// http://people.csail.mit.edu/sparis/bf_course/course_notes.pdf
|
|
8
|
+
// A bilateral filter is a non-linear, edge-preserving, and noise-reducing smoothing filter for images.
|
|
9
|
+
// It replaces the intensity of each pixel with a weighted average of intensity values from nearby pixels.
|
|
10
|
+
// This weight can be based on a Gaussian distribution. Crucially, the weights depend not only on
|
|
11
|
+
// Euclidean distance of pixels, but also on the radiometric differences (e.g., range differences, such
|
|
12
|
+
// as color intensity, depth distance, etc.). This preserves sharp edges.
|
|
13
|
+
|
|
14
|
+
float normpdf3(in vec3 v, in float sigma) {
|
|
15
|
+
return 0.39894 * exp(-0.5 * dot(v, v) / (sigma * sigma)) / sigma;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// filter size
|
|
19
|
+
#define MSIZE ${KERNEL_SIZE}
|
|
20
|
+
varying vec2 vUv;
|
|
21
|
+
uniform sampler2D source;
|
|
22
|
+
uniform vec2 pixelOffset;
|
|
23
|
+
uniform vec2 sigmas;
|
|
24
|
+
uniform float bZnorm;
|
|
25
|
+
uniform float kernel[MSIZE];
|
|
26
|
+
|
|
27
|
+
void main(void) {
|
|
28
|
+
|
|
29
|
+
// range sigma - controls blurriness based on a pixel distance
|
|
30
|
+
float sigma = sigmas.x;
|
|
31
|
+
|
|
32
|
+
// domain sigma - controls blurriness based on a pixel similarity (to preserve edges)
|
|
33
|
+
float bSigma = sigmas.y;
|
|
34
|
+
vec4 pixel = texture2D(source, vUv);
|
|
35
|
+
vec4 accumulatedColor = vec4(0.0);
|
|
36
|
+
float accumulatedFactor = 0.0;
|
|
37
|
+
|
|
38
|
+
// read out the texels
|
|
39
|
+
const int kSize = (MSIZE-1)/2;
|
|
40
|
+
|
|
41
|
+
for (int i = -kSize; i <= kSize; ++i) {
|
|
42
|
+
for (int j = -kSize; j <= kSize; ++j) {
|
|
43
|
+
|
|
44
|
+
// sample the pixel with offset
|
|
45
|
+
vec2 coord = vUv + vec2(float(i), float(j)) * pixelOffset;
|
|
46
|
+
vec4 rgba = texture2D(source, coord);
|
|
47
|
+
|
|
48
|
+
// bilateral factors
|
|
49
|
+
float factor = kernel[kSize + j] * kernel[kSize + i];
|
|
50
|
+
factor *= normpdf3(rgba.rgb - pixel.rgb, bSigma) * bZnorm;
|
|
51
|
+
|
|
52
|
+
// accumulate
|
|
53
|
+
accumulatedColor += factor * rgba;
|
|
54
|
+
accumulatedFactor += factor;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
gl_FragColor = accumulatedColor / accumulatedFactor;
|
|
59
|
+
}
|
|
60
|
+
`;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
*
|
|
64
|
+
* @param {number} x
|
|
65
|
+
* @param {number} sigma
|
|
66
|
+
* @return {number}
|
|
67
|
+
*/
|
|
68
|
+
function normpdf(x, sigma) {
|
|
69
|
+
return 0.39894 * Math.exp(-0.5 * x * x / (sigma * sigma)) / sigma;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
*
|
|
74
|
+
* @param {number} sigma
|
|
75
|
+
* @param {number} filterSmoothness
|
|
76
|
+
* @constructor
|
|
77
|
+
*/
|
|
78
|
+
export function DenoiseShader(sigma = 10, filterSmoothness = 0.2) {
|
|
79
|
+
// compute zNorm
|
|
80
|
+
const zNorm = 1 / normpdf(0, filterSmoothness);
|
|
81
|
+
|
|
82
|
+
// build kernel
|
|
83
|
+
const kernel = new Float32Array(KERNEL_SIZE);
|
|
84
|
+
const kSize = Math.floor((KERNEL_SIZE - 1) / 2);
|
|
85
|
+
for (let i = 0; i <= kSize; i++) {
|
|
86
|
+
const v = normpdf(i, sigma);
|
|
87
|
+
|
|
88
|
+
kernel[kSize + i] = v;
|
|
89
|
+
kernel[kSize - i] = v;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
uniforms: {
|
|
94
|
+
source: {
|
|
95
|
+
type: 't',
|
|
96
|
+
value: null
|
|
97
|
+
},
|
|
98
|
+
pixelOffset: {
|
|
99
|
+
type: 'v2',
|
|
100
|
+
/**
|
|
101
|
+
* Holds single pixel size in UV space ( 1 / texture_size )
|
|
102
|
+
*/
|
|
103
|
+
value: new Vector2(1, 1)
|
|
104
|
+
},
|
|
105
|
+
sigmas: {
|
|
106
|
+
type: "v2", value: new Vector2(sigma, sigma)
|
|
107
|
+
},
|
|
108
|
+
bZnorm: {
|
|
109
|
+
type: 'f',
|
|
110
|
+
value: zNorm
|
|
111
|
+
},
|
|
112
|
+
kernel: {
|
|
113
|
+
type: 'f',
|
|
114
|
+
value: kernel
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
fragmentShader: fragment
|
|
118
|
+
};
|
|
119
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export function noise_octaves(
|
|
2
|
+
noise,
|
|
3
|
+
noise_ctx,
|
|
4
|
+
x, y, z,
|
|
5
|
+
octaves,
|
|
6
|
+
persistence = 0.5,
|
|
7
|
+
lacunarity = 2
|
|
8
|
+
) {
|
|
9
|
+
let total = 0;
|
|
10
|
+
let frequency = 1;
|
|
11
|
+
let amplitude = 1;
|
|
12
|
+
|
|
13
|
+
let totalAmplitude = 0; // Used for normalizing result to 0.0 - 1.0
|
|
14
|
+
let i = 0;
|
|
15
|
+
|
|
16
|
+
for (; i < octaves; i++) {
|
|
17
|
+
|
|
18
|
+
const noise_value = noise.call(noise_ctx,
|
|
19
|
+
x * frequency,
|
|
20
|
+
y * frequency,
|
|
21
|
+
z * frequency
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
total += noise_value * amplitude;
|
|
25
|
+
|
|
26
|
+
totalAmplitude += amplitude;
|
|
27
|
+
amplitude *= persistence;
|
|
28
|
+
frequency *= lacunarity;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return total / totalAmplitude;
|
|
32
|
+
}
|
|
33
|
+
|
|
@@ -3,43 +3,32 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { ClampToEdgeWrapping, DataTexture, RGBAFormat, UnsignedByteType } from 'three';
|
|
5
5
|
import { assert } from "../../../../core/assert.js";
|
|
6
|
+
import { copy_Sampler2D_channel_data } from "./copy_Sampler2D_channel_data.js";
|
|
7
|
+
import { Sampler2D } from "./Sampler2D.js";
|
|
6
8
|
|
|
7
9
|
|
|
8
10
|
/**
|
|
9
11
|
*
|
|
10
12
|
* @param {Sampler2D} sampler
|
|
11
|
-
* @param {Number} scale
|
|
12
|
-
* @param {Number} offset
|
|
13
13
|
* @return {DataTexture}
|
|
14
14
|
*/
|
|
15
|
-
function sampler2D2Texture(sampler
|
|
15
|
+
function sampler2D2Texture(sampler) {
|
|
16
16
|
assert.notEqual(sampler, undefined, 'sampler is undefined');
|
|
17
|
-
assert.equal(typeof scale, "number", `scale must be a number, instead was '${typeof scale}'`);
|
|
18
|
-
assert.equal(typeof offset, "number", `scale must be a number, instead was '${typeof offset}'`);
|
|
19
17
|
|
|
20
18
|
const result = new DataTexture();
|
|
21
19
|
result.format = RGBAFormat;
|
|
22
20
|
|
|
23
21
|
let data;
|
|
24
22
|
|
|
25
|
-
if (
|
|
23
|
+
if (sampler.itemSize === 4) {
|
|
26
24
|
//no transformation, use a direct reference
|
|
27
25
|
data = sampler.data;
|
|
28
26
|
|
|
29
27
|
} else {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const arrayFiller = sampler.makeArrayFiller(scale, offset);
|
|
28
|
+
const dest = Sampler2D.uint8(4, sampler.width, sampler.height);
|
|
29
|
+
copy_Sampler2D_channel_data(sampler, dest);
|
|
33
30
|
|
|
34
|
-
|
|
35
|
-
let x, y;
|
|
36
|
-
|
|
37
|
-
for (y = 0; y < sampler.height; y++) {
|
|
38
|
-
for (x = 0; x < sampler.width; x++) {
|
|
39
|
-
arrayFiller(index, data, x, y);
|
|
40
|
-
index += 4;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
31
|
+
data = dest.data;
|
|
43
32
|
}
|
|
44
33
|
|
|
45
34
|
result.type = UnsignedByteType;
|
|
@@ -10,20 +10,23 @@ import { typedArrayConstructorByInstance } from "../typedArrayConstructorByInsta
|
|
|
10
10
|
* @param {number} y
|
|
11
11
|
* @param scale
|
|
12
12
|
* @param offset
|
|
13
|
+
* @param {number} resolution
|
|
13
14
|
*/
|
|
14
|
-
export function paintSamplerOnHTML(s, x, y, scale, offset) {
|
|
15
|
+
export function paintSamplerOnHTML(s, x, y, scale, offset, resolution=256) {
|
|
15
16
|
|
|
16
|
-
const d =
|
|
17
|
+
const d = resolution;
|
|
17
18
|
|
|
18
|
-
const
|
|
19
|
+
const TA = typedArrayConstructorByInstance(s.data);
|
|
20
|
+
|
|
21
|
+
const target = new Sampler2D(new TA(d * d * s.itemSize), s.itemSize, d, d);
|
|
19
22
|
|
|
20
23
|
scaleSampler2D(s, target);
|
|
21
24
|
|
|
22
25
|
const c = convertSampler2D2Canvas(target, scale, offset);
|
|
23
26
|
c.style.zIndex = 1000;
|
|
24
27
|
c.style.position = "absolute";
|
|
25
|
-
c.style.left = `${x
|
|
26
|
-
c.style.top = `${y
|
|
28
|
+
c.style.left = `${x}px`;
|
|
29
|
+
c.style.top = `${y}px`;
|
|
27
30
|
|
|
28
31
|
document.body.appendChild(c);
|
|
29
32
|
}
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import { ClampToEdgeWrapping, DataTexture, FloatType, LinearFilter, NearestFilter
|
|
1
|
+
import { ClampToEdgeWrapping, DataTexture, FloatType, LinearFilter, NearestFilter } from "three";
|
|
2
2
|
import { assert } from "../../../../core/assert.js";
|
|
3
|
+
import { channelCountToThreeTextureFormat } from "../channelCountToThreeTextureFormat.js";
|
|
4
|
+
import { computeThreeTextureInternalFormatFromDataType } from "../../render/forward_plus/data/computeThreeTextureInternalFormatFromDataType.js";
|
|
5
|
+
import { DataType } from "../../../../core/collection/table/DataType.js";
|
|
3
6
|
|
|
4
7
|
/**
|
|
5
8
|
*
|
|
@@ -12,7 +15,7 @@ export function sampler2DtoFloat32Texture(sampler) {
|
|
|
12
15
|
const width = sampler.width;
|
|
13
16
|
const height = sampler.height;
|
|
14
17
|
|
|
15
|
-
const texture = new DataTexture(sampler.data, width, height
|
|
18
|
+
const texture = new DataTexture(sampler.data, width, height);
|
|
16
19
|
|
|
17
20
|
texture.wrapS = ClampToEdgeWrapping;
|
|
18
21
|
texture.wrapT = ClampToEdgeWrapping;
|
|
@@ -24,7 +27,11 @@ export function sampler2DtoFloat32Texture(sampler) {
|
|
|
24
27
|
|
|
25
28
|
texture.flipY = false;
|
|
26
29
|
|
|
27
|
-
texture.
|
|
30
|
+
texture.type = FloatType;
|
|
31
|
+
texture.format = channelCountToThreeTextureFormat(sampler.itemSize);
|
|
32
|
+
texture.internalFormat = computeThreeTextureInternalFormatFromDataType(DataType.Float32, sampler.itemSize);
|
|
33
|
+
|
|
34
|
+
texture.needsUpdate = true;
|
|
28
35
|
|
|
29
36
|
return texture;
|
|
30
37
|
}
|