three-stdlib 2.8.12 → 2.10.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("three"),e=require("./Pass.cjs.js");require("@babel/runtime/helpers/defineProperty");class n extends e.Pass{constructor(n,i,a,o,l={}){var s,d;super(),this.pixelSize=i,this.resolution=new t.Vector2,this.renderResolution=new t.Vector2,this.setSize(n.x,n.y),this.fsQuad=new e.FullScreenQuad(this.material()),this.scene=a,this.camera=o,this.normalEdgeStrength=null!=(s=l.normalEdgeStrength)?s:.3,this.depthEdgeStrength=null!=(d=l.depthEdgeStrength)?d:.4,this.rgbRenderTarget=r(this.renderResolution,t.RGBAFormat,!0),this.normalRenderTarget=r(this.renderResolution,t.RGBAFormat,!1),this.normalMaterial=new t.MeshNormalMaterial}dispose(){this.rgbRenderTarget.dispose(),this.normalRenderTarget.dispose(),this.fsQuad.dispose()}setSize(t,e){var n,r,i;this.resolution.set(t,e),this.renderResolution.set(t/this.pixelSize|0,e/this.pixelSize|0);const{x:a,y:o}=this.renderResolution;null===(n=this.rgbRenderTarget)||void 0===n||n.setSize(a,o),null===(r=this.normalRenderTarget)||void 0===r||r.setSize(a,o),null===(i=this.fsQuad)||void 0===i||i.material.uniforms.resolution.value.set(a,o,1/a,1/o)}setPixelSize(t){this.pixelSize=t,this.setSize(this.resolution.x,this.resolution.y)}render(t,e){const n=this.fsQuad.material.uniforms;n.normalEdgeStrength.value=this.normalEdgeStrength,n.depthEdgeStrength.value=this.depthEdgeStrength,t.setRenderTarget(this.rgbRenderTarget),t.render(this.scene,this.camera);const r=this.scene.overrideMaterial;t.setRenderTarget(this.normalRenderTarget),this.scene.overrideMaterial=this.normalMaterial,t.render(this.scene,this.camera),this.scene.overrideMaterial=r,n.tDiffuse.value=this.rgbRenderTarget.texture,n.tDepth.value=this.rgbRenderTarget.depthTexture,n.tNormal.value=this.normalRenderTarget.texture,this.renderToScreen?t.setRenderTarget(null):(t.setRenderTarget(e),this.clear&&t.clear()),this.fsQuad.render(t)}material(){return new t.ShaderMaterial({uniforms:{tDiffuse:{value:null},tDepth:{value:null},tNormal:{value:null},resolution:{value:new t.Vector4(this.renderResolution.x,this.renderResolution.y,1/this.renderResolution.x,1/this.renderResolution.y)},normalEdgeStrength:{value:0},depthEdgeStrength:{value:0}},vertexShader:"\n\t\t\t\tvarying vec2 vUv;\n\n\t\t\t\tvoid main() {\n\n\t\t\t\t\tvUv = uv;\n\t\t\t\t\tgl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\n\t\t\t\t}\n\t\t\t\t",fragmentShader:"\n\t\t\t\tuniform sampler2D tDiffuse;\n\t\t\t\tuniform sampler2D tDepth;\n\t\t\t\tuniform sampler2D tNormal;\n\t\t\t\tuniform vec4 resolution;\n\t\t\t\tuniform float normalEdgeStrength;\n\t\t\t\tuniform float depthEdgeStrength;\n\t\t\t\tvarying vec2 vUv;\n\n\t\t\t\tfloat getDepth(int x, int y) {\n\n\t\t\t\t\treturn texture2D( tDepth, vUv + vec2(x, y) * resolution.zw ).r;\n\n\t\t\t\t}\n\n\t\t\t\tvec3 getNormal(int x, int y) {\n\n\t\t\t\t\treturn texture2D( tNormal, vUv + vec2(x, y) * resolution.zw ).rgb * 2.0 - 1.0;\n\n\t\t\t\t}\n\n\t\t\t\tfloat depthEdgeIndicator(float depth, vec3 normal) {\n\n\t\t\t\t\tfloat diff = 0.0;\n\t\t\t\t\tdiff += clamp(getDepth(1, 0) - depth, 0.0, 1.0);\n\t\t\t\t\tdiff += clamp(getDepth(-1, 0) - depth, 0.0, 1.0);\n\t\t\t\t\tdiff += clamp(getDepth(0, 1) - depth, 0.0, 1.0);\n\t\t\t\t\tdiff += clamp(getDepth(0, -1) - depth, 0.0, 1.0);\n\t\t\t\t\treturn floor(smoothstep(0.01, 0.02, diff) * 2.) / 2.;\n\n\t\t\t\t}\n\n\t\t\t\tfloat neighborNormalEdgeIndicator(int x, int y, float depth, vec3 normal) {\n\n\t\t\t\t\tfloat depthDiff = getDepth(x, y) - depth;\n\t\t\t\t\tvec3 neighborNormal = getNormal(x, y);\n\t\t\t\t\t\n\t\t\t\t\t// Edge pixels should yield to faces who's normals are closer to the bias normal.\n\t\t\t\t\tvec3 normalEdgeBias = vec3(1., 1., 1.); // This should probably be a parameter.\n\t\t\t\t\tfloat normalDiff = dot(normal - neighborNormal, normalEdgeBias);\n\t\t\t\t\tfloat normalIndicator = clamp(smoothstep(-.01, .01, normalDiff), 0.0, 1.0);\n\t\t\t\t\t\n\t\t\t\t\t// Only the shallower pixel should detect the normal edge.\n\t\t\t\t\tfloat depthIndicator = clamp(sign(depthDiff * .25 + .0025), 0.0, 1.0);\n\n\t\t\t\t\treturn (1.0 - dot(normal, neighborNormal)) * depthIndicator * normalIndicator;\n\n\t\t\t\t}\n\n\t\t\t\tfloat normalEdgeIndicator(float depth, vec3 normal) {\n\t\t\t\t\t\n\t\t\t\t\tfloat indicator = 0.0;\n\n\t\t\t\t\tindicator += neighborNormalEdgeIndicator(0, -1, depth, normal);\n\t\t\t\t\tindicator += neighborNormalEdgeIndicator(0, 1, depth, normal);\n\t\t\t\t\tindicator += neighborNormalEdgeIndicator(-1, 0, depth, normal);\n\t\t\t\t\tindicator += neighborNormalEdgeIndicator(1, 0, depth, normal);\n\n\t\t\t\t\treturn step(0.1, indicator);\n\n\t\t\t\t}\n\n\t\t\t\tvoid main() {\n\n\t\t\t\t\tvec4 texel = texture2D( tDiffuse, vUv );\n\n\t\t\t\t\tfloat depth = 0.0;\n\t\t\t\t\tvec3 normal = vec3(0.0);\n\n\t\t\t\t\tif (depthEdgeStrength > 0.0 || normalEdgeStrength > 0.0) {\n\n\t\t\t\t\t\tdepth = getDepth(0, 0);\n\t\t\t\t\t\tnormal = getNormal(0, 0);\n\n\t\t\t\t\t}\n\n\t\t\t\t\tfloat dei = 0.0;\n\t\t\t\t\tif (depthEdgeStrength > 0.0) \n\t\t\t\t\t\tdei = depthEdgeIndicator(depth, normal);\n\n\t\t\t\t\tfloat nei = 0.0; \n\t\t\t\t\tif (normalEdgeStrength > 0.0) \n\t\t\t\t\t\tnei = normalEdgeIndicator(depth, normal);\n\n\t\t\t\t\tfloat Strength = dei > 0.0 ? (1.0 - depthEdgeStrength * dei) : (1.0 + normalEdgeStrength * nei);\n\n\t\t\t\t\tgl_FragColor = texel * Strength;\n\n\t\t\t\t}\n\t\t\t\t"})}}function r(e,n,r){const i=new t.WebGLRenderTarget(e.x,e.y,r?{depthTexture:new t.DepthTexture(e.x,e.y),depthBuffer:!0}:void 0);return i.texture.format=n,i.texture.minFilter=t.NearestFilter,i.texture.magFilter=t.NearestFilter,i.texture.generateMipmaps=!1,i.stencilBuffer=!1,i}exports.RenderPixelatedPass=n;
@@ -0,0 +1,213 @@
1
+ import { Vector2, MeshNormalMaterial, ShaderMaterial, Vector4, WebGLRenderTarget, DepthTexture, NearestFilter, RGBAFormat } from 'three';
2
+ import { Pass, FullScreenQuad } from './Pass.js';
3
+
4
+ class RenderPixelatedPass extends Pass {
5
+ constructor(resolution, pixelSize, scene, camera, options = {}) {
6
+ var _options$normalEdgeSt, _options$depthEdgeStr;
7
+
8
+ super();
9
+ this.pixelSize = pixelSize;
10
+ this.resolution = new Vector2();
11
+ this.renderResolution = new Vector2();
12
+ this.setSize(resolution.x, resolution.y);
13
+ this.fsQuad = new FullScreenQuad(this.material());
14
+ this.scene = scene;
15
+ this.camera = camera;
16
+ this.normalEdgeStrength = (_options$normalEdgeSt = options.normalEdgeStrength) != null ? _options$normalEdgeSt : 0.3;
17
+ this.depthEdgeStrength = (_options$depthEdgeStr = options.depthEdgeStrength) != null ? _options$depthEdgeStr : 0.4;
18
+ this.rgbRenderTarget = pixelRenderTarget(this.renderResolution, RGBAFormat, true);
19
+ this.normalRenderTarget = pixelRenderTarget(this.renderResolution, RGBAFormat, false);
20
+ this.normalMaterial = new MeshNormalMaterial();
21
+ }
22
+
23
+ dispose() {
24
+ this.rgbRenderTarget.dispose();
25
+ this.normalRenderTarget.dispose();
26
+ this.fsQuad.dispose();
27
+ }
28
+
29
+ setSize(width, height) {
30
+ var _this$rgbRenderTarget, _this$normalRenderTar, _this$fsQuad;
31
+
32
+ this.resolution.set(width, height);
33
+ this.renderResolution.set(width / this.pixelSize | 0, height / this.pixelSize | 0);
34
+ const {
35
+ x,
36
+ y
37
+ } = this.renderResolution;
38
+ (_this$rgbRenderTarget = this.rgbRenderTarget) === null || _this$rgbRenderTarget === void 0 ? void 0 : _this$rgbRenderTarget.setSize(x, y);
39
+ (_this$normalRenderTar = this.normalRenderTarget) === null || _this$normalRenderTar === void 0 ? void 0 : _this$normalRenderTar.setSize(x, y);
40
+ (_this$fsQuad = this.fsQuad) === null || _this$fsQuad === void 0 ? void 0 : _this$fsQuad.material.uniforms.resolution.value.set(x, y, 1 / x, 1 / y);
41
+ }
42
+
43
+ setPixelSize(pixelSize) {
44
+ this.pixelSize = pixelSize;
45
+ this.setSize(this.resolution.x, this.resolution.y);
46
+ }
47
+
48
+ render(renderer, writeBuffer) {
49
+ const uniforms = this.fsQuad.material.uniforms;
50
+ uniforms.normalEdgeStrength.value = this.normalEdgeStrength;
51
+ uniforms.depthEdgeStrength.value = this.depthEdgeStrength;
52
+ renderer.setRenderTarget(this.rgbRenderTarget);
53
+ renderer.render(this.scene, this.camera);
54
+ const overrideMaterial_old = this.scene.overrideMaterial;
55
+ renderer.setRenderTarget(this.normalRenderTarget);
56
+ this.scene.overrideMaterial = this.normalMaterial;
57
+ renderer.render(this.scene, this.camera);
58
+ this.scene.overrideMaterial = overrideMaterial_old;
59
+ uniforms.tDiffuse.value = this.rgbRenderTarget.texture;
60
+ uniforms.tDepth.value = this.rgbRenderTarget.depthTexture;
61
+ uniforms.tNormal.value = this.normalRenderTarget.texture;
62
+
63
+ if (this.renderToScreen) {
64
+ renderer.setRenderTarget(null);
65
+ } else {
66
+ renderer.setRenderTarget(writeBuffer);
67
+ if (this.clear) renderer.clear();
68
+ }
69
+
70
+ this.fsQuad.render(renderer);
71
+ }
72
+
73
+ material() {
74
+ return new ShaderMaterial({
75
+ uniforms: {
76
+ tDiffuse: {
77
+ value: null
78
+ },
79
+ tDepth: {
80
+ value: null
81
+ },
82
+ tNormal: {
83
+ value: null
84
+ },
85
+ resolution: {
86
+ value: new Vector4(this.renderResolution.x, this.renderResolution.y, 1 / this.renderResolution.x, 1 / this.renderResolution.y)
87
+ },
88
+ normalEdgeStrength: {
89
+ value: 0
90
+ },
91
+ depthEdgeStrength: {
92
+ value: 0
93
+ }
94
+ },
95
+ vertexShader: `
96
+ varying vec2 vUv;
97
+
98
+ void main() {
99
+
100
+ vUv = uv;
101
+ gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
102
+
103
+ }
104
+ `,
105
+ fragmentShader: `
106
+ uniform sampler2D tDiffuse;
107
+ uniform sampler2D tDepth;
108
+ uniform sampler2D tNormal;
109
+ uniform vec4 resolution;
110
+ uniform float normalEdgeStrength;
111
+ uniform float depthEdgeStrength;
112
+ varying vec2 vUv;
113
+
114
+ float getDepth(int x, int y) {
115
+
116
+ return texture2D( tDepth, vUv + vec2(x, y) * resolution.zw ).r;
117
+
118
+ }
119
+
120
+ vec3 getNormal(int x, int y) {
121
+
122
+ return texture2D( tNormal, vUv + vec2(x, y) * resolution.zw ).rgb * 2.0 - 1.0;
123
+
124
+ }
125
+
126
+ float depthEdgeIndicator(float depth, vec3 normal) {
127
+
128
+ float diff = 0.0;
129
+ diff += clamp(getDepth(1, 0) - depth, 0.0, 1.0);
130
+ diff += clamp(getDepth(-1, 0) - depth, 0.0, 1.0);
131
+ diff += clamp(getDepth(0, 1) - depth, 0.0, 1.0);
132
+ diff += clamp(getDepth(0, -1) - depth, 0.0, 1.0);
133
+ return floor(smoothstep(0.01, 0.02, diff) * 2.) / 2.;
134
+
135
+ }
136
+
137
+ float neighborNormalEdgeIndicator(int x, int y, float depth, vec3 normal) {
138
+
139
+ float depthDiff = getDepth(x, y) - depth;
140
+ vec3 neighborNormal = getNormal(x, y);
141
+
142
+ // Edge pixels should yield to faces who's normals are closer to the bias normal.
143
+ vec3 normalEdgeBias = vec3(1., 1., 1.); // This should probably be a parameter.
144
+ float normalDiff = dot(normal - neighborNormal, normalEdgeBias);
145
+ float normalIndicator = clamp(smoothstep(-.01, .01, normalDiff), 0.0, 1.0);
146
+
147
+ // Only the shallower pixel should detect the normal edge.
148
+ float depthIndicator = clamp(sign(depthDiff * .25 + .0025), 0.0, 1.0);
149
+
150
+ return (1.0 - dot(normal, neighborNormal)) * depthIndicator * normalIndicator;
151
+
152
+ }
153
+
154
+ float normalEdgeIndicator(float depth, vec3 normal) {
155
+
156
+ float indicator = 0.0;
157
+
158
+ indicator += neighborNormalEdgeIndicator(0, -1, depth, normal);
159
+ indicator += neighborNormalEdgeIndicator(0, 1, depth, normal);
160
+ indicator += neighborNormalEdgeIndicator(-1, 0, depth, normal);
161
+ indicator += neighborNormalEdgeIndicator(1, 0, depth, normal);
162
+
163
+ return step(0.1, indicator);
164
+
165
+ }
166
+
167
+ void main() {
168
+
169
+ vec4 texel = texture2D( tDiffuse, vUv );
170
+
171
+ float depth = 0.0;
172
+ vec3 normal = vec3(0.0);
173
+
174
+ if (depthEdgeStrength > 0.0 || normalEdgeStrength > 0.0) {
175
+
176
+ depth = getDepth(0, 0);
177
+ normal = getNormal(0, 0);
178
+
179
+ }
180
+
181
+ float dei = 0.0;
182
+ if (depthEdgeStrength > 0.0)
183
+ dei = depthEdgeIndicator(depth, normal);
184
+
185
+ float nei = 0.0;
186
+ if (normalEdgeStrength > 0.0)
187
+ nei = normalEdgeIndicator(depth, normal);
188
+
189
+ float Strength = dei > 0.0 ? (1.0 - depthEdgeStrength * dei) : (1.0 + normalEdgeStrength * nei);
190
+
191
+ gl_FragColor = texel * Strength;
192
+
193
+ }
194
+ `
195
+ });
196
+ }
197
+
198
+ }
199
+
200
+ function pixelRenderTarget(resolution, pixelFormat, useDepthTexture) {
201
+ const renderTarget = new WebGLRenderTarget(resolution.x, resolution.y, !useDepthTexture ? undefined : {
202
+ depthTexture: new DepthTexture(resolution.x, resolution.y),
203
+ depthBuffer: true
204
+ });
205
+ renderTarget.texture.format = pixelFormat;
206
+ renderTarget.texture.minFilter = NearestFilter;
207
+ renderTarget.texture.magFilter = NearestFilter;
208
+ renderTarget.texture.generateMipmaps = false;
209
+ renderTarget.stencilBuffer = false;
210
+ return renderTarget;
211
+ }
212
+
213
+ export { RenderPixelatedPass };