Skip to content

Commit

Permalink
Nodes: Add PixelationNode (#1110)
Browse files Browse the repository at this point in the history
* Nodes: Add PixelationNode

* Update three.js

* Add examples

* Update patch and delete examples

* Add examples

* Update patch and delete examples

* Format
  • Loading branch information
Methuselah96 committed Jul 22, 2024
1 parent 102cd9a commit 124f03a
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 1 deletion.
86 changes: 86 additions & 0 deletions examples-testing/changes.patch
Original file line number Diff line number Diff line change
Expand Up @@ -14720,6 +14720,92 @@ index 5e75fa7a..dfebe406 100644

init();

diff --git a/examples-testing/examples/webgpu_postprocessing_pixel.ts b/examples-testing/examples/webgpu_postprocessing_pixel.ts
index 731c8253..eb8bd63d 100644
--- a/examples-testing/examples/webgpu_postprocessing_pixel.ts
+++ b/examples-testing/examples/webgpu_postprocessing_pixel.ts
@@ -1,12 +1,23 @@
-import * as THREE from 'three';
+import * as THREE from 'three/webgpu';

import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';

-import { uniform, pixelationPass } from 'three/tsl';
-
-let camera, scene, renderer, postProcessing, crystalMesh, clock;
-let gui, effectController;
+import { uniform, pixelationPass, UniformNode } from 'three/tsl';
+
+let camera: THREE.OrthographicCamera,
+ scene: THREE.Scene,
+ renderer: THREE.WebGPURenderer,
+ postProcessing: THREE.PostProcessing,
+ crystalMesh: THREE.Mesh<THREE.IcosahedronGeometry, THREE.MeshPhongMaterial>,
+ clock: THREE.Clock;
+let gui: GUI,
+ effectController: {
+ pixelSize: UniformNode<number>;
+ normalEdgeStrength: UniformNode<number>;
+ depthEdgeStrength: UniformNode<number>;
+ pixelAlignedPanning: boolean;
+ };

init();

@@ -34,7 +45,7 @@ function init() {

const boxMaterial = new THREE.MeshPhongMaterial({ map: texChecker2 });

- function addBox(boxSideLength, x, z, rotation) {
+ function addBox(boxSideLength: number, x: number, z: number, rotation: number) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(boxSideLength, boxSideLength, boxSideLength), boxMaterial);
mesh.castShadow = true;
//mesh.receiveShadow = true;
@@ -170,7 +181,7 @@ function animate() {

// Helper functions

-function pixelTexture(texture) {
+function pixelTexture(texture: THREE.Texture) {
texture.minFilter = THREE.NearestFilter;
texture.magFilter = THREE.NearestFilter;
texture.generateMipmaps = false;
@@ -180,25 +191,30 @@ function pixelTexture(texture) {
return texture;
}

-function easeInOutCubic(x) {
+function easeInOutCubic(x: number) {
return x ** 2 * 3 - x ** 3 * 2;
}

-function linearStep(x, edge0, edge1) {
+function linearStep(x: number, edge0: number, edge1: number) {
const w = edge1 - edge0;
const m = 1 / w;
const y0 = -m * edge0;
return THREE.MathUtils.clamp(y0 + m * x, 0, 1);
}

-function stopGoEased(x, downtime, period) {
+function stopGoEased(x: number, downtime: number, period: number) {
const cycle = (x / period) | 0;
const tween = x - cycle * period;
const linStep = easeInOutCubic(linearStep(tween, downtime, period));
return cycle + linStep;
}

-function pixelAlignFrustum(camera, aspectRatio, pixelsPerScreenWidth, pixelsPerScreenHeight) {
+function pixelAlignFrustum(
+ camera: THREE.OrthographicCamera,
+ aspectRatio: number,
+ pixelsPerScreenWidth: number,
+ pixelsPerScreenHeight: number,
+) {
// 0. Get Pixel Grid Units
const worldScreenWidth = (camera.right - camera.left) / camera.zoom;
const worldScreenHeight = (camera.top - camera.bottom) / camera.zoom;
diff --git a/examples-testing/examples/webgpu_postprocessing_sobel.ts b/examples-testing/examples/webgpu_postprocessing_sobel.ts
index cbc5a96b..66ec682e 100644
--- a/examples-testing/examples/webgpu_postprocessing_sobel.ts
Expand Down
2 changes: 1 addition & 1 deletion three.js
Submodule three.js updated 46 files
+5 −5 .github/workflows/ci.yml
+3 −3 .github/workflows/codeql-code-scanning.yml
+1 −1 .github/workflows/read-size.yml
+1 −1 .github/workflows/report-size.yml
+1 −1 build/three.cjs
+1 −1 build/three.module.js
+1 −1 build/three.module.min.js
+330 −6 build/three.webgpu.js
+1 −1 build/three.webgpu.min.js
+0 −3 docs/api/ar/objects/LOD.html
+0 −3 docs/api/ar/objects/Line.html
+0 −3 docs/api/ar/objects/Mesh.html
+0 −3 docs/api/ar/objects/Points.html
+1 −4 docs/api/ar/objects/Sprite.html
+0 −3 docs/api/en/objects/LOD.html
+0 −3 docs/api/en/objects/Line.html
+0 −3 docs/api/en/objects/Mesh.html
+0 −3 docs/api/en/objects/Points.html
+0 −3 docs/api/en/objects/Sprite.html
+0 −6 docs/api/it/objects/LOD.html
+0 −5 docs/api/it/objects/Line.html
+0 −3 docs/api/it/objects/Mesh.html
+0 −5 docs/api/it/objects/Points.html
+0 −5 docs/api/it/objects/Sprite.html
+0 −5 docs/api/zh/objects/LOD.html
+0 −5 docs/api/zh/objects/Line.html
+0 −3 docs/api/zh/objects/Mesh.html
+0 −5 docs/api/zh/objects/Points.html
+0 −5 docs/api/zh/objects/Sprite.html
+1 −0 examples/files.json
+0 −2 examples/jsm/shaders/FXAAShader.js
+1 −1 examples/jsm/shaders/GTAOShader.js
+ examples/screenshots/webgpu_postprocessing_pixel.jpg
+277 −0 examples/webgpu_postprocessing_pixel.html
+152 −107 package-lock.json
+1 −1 package.json
+4 −4 src/math/Box2.js
+6 −6 src/math/Box3.js
+1 −0 src/nodes/Nodes.js
+1 −1 src/nodes/display/ColorAdjustmentNode.js
+201 −0 src/nodes/display/PixelationPassNode.js
+7 −1 src/nodes/lighting/AnalyticLightNode.js
+1 −1 src/objects/InstancedMesh.js
+2 −2 src/renderers/webgpu/WebGPUBackend.js
+4 −1 src/renderers/webgpu/nodes/WGSLNodeBuilder.js
+1 −0 test/e2e/puppeteer.js
1 change: 1 addition & 0 deletions types/three/src/nodes/Nodes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ export { default as GaussianBlurNode, gaussianBlur } from "./display/GaussianBlu
export { ao, default as GTAONode } from "./display/GTAONode.js";
export { default as Lut3DNode, lut3D } from "./display/Lut3DNode.js";
export { default as NormalMapNode, normalMap } from "./display/NormalMapNode.js";
export { default as PixelationPassNode, pixelationPass } from "./display/PixelationPassNode.js";
export { default as PosterizeNode, posterize } from "./display/PosterizeNode.js";
export { default as RenderOutputNode, renderOutput } from "./display/RenderOutputNode.js";
export { default as RGBShiftNode, rgbShift } from "./display/RGBShiftNode.js";
Expand Down
67 changes: 67 additions & 0 deletions types/three/src/nodes/display/PixelationPassNode.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Camera } from "../../cameras/Camera.js";
import { Scene } from "../../scenes/Scene.js";
import Node from "../core/Node.js";
import TempNode from "../core/TempNode.js";
import UniformNode from "../core/UniformNode.js";
import { NodeRepresentation, ShaderNodeObject } from "../shadernode/ShaderNode.js";
import PassNode from "./PassNode.js";

declare class PixelationNode extends TempNode {
textureNode: Node;
depthNode: Node;
normalNode: Node;

pixelSize: Node;
normalEdgeStrength: Node;
depthEdgeStrength: Node;

constructor(
textureNode: Node,
depthNode: Node,
normalNode: Node,
pixelSize: Node,
normalEdgeStrength: Node,
depthEdgeStrength: Node,
);
}

declare const pixelation: (
node: NodeRepresentation,
depthNode: NodeRepresentation,
normalNode: NodeRepresentation,
pixelSize?: number,
normalEdgeStrength?: number,
depthEdgeStrength?: number,
) => ShaderNodeObject<PixelationNode>;

declare module "../shadernode/ShaderNode.js" {
interface NodeElements {
pixelation: typeof pixelation;
}
}

declare class PixelationPassNode extends PassNode {
pixelSize: UniformNode<number>;
normalEdgeStrength: UniformNode<number>;
depthEdgeStrength: UniformNode<number>;

readonly isPixelationPassNode: true;

constructor(
scene: Scene,
camera: Camera,
pixelSize: number,
normalEdgeStrength: number,
depthEdgeStrength: number,
);
}

export const pixelationPass: (
scene: Scene,
camera: Camera,
pixelSize: UniformNode<number>,
normalEdgeStrength: UniformNode<number>,
depthEdgeStrength: UniformNode<number>,
) => ShaderNodeObject<PixelationPassNode>;

export default PixelationPassNode;

0 comments on commit 124f03a

Please sign in to comment.