Skip to content

Commit

Permalink
upd: Dispose material preview renderer
Browse files Browse the repository at this point in the history
- Moved material preview image generator to utils
  • Loading branch information
jasonjgardner committed Jun 13, 2024
1 parent ccc9e34 commit cea64f5
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 42 deletions.
1 change: 1 addition & 0 deletions src/pbr_preview/src/lib/actions/materialTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const colorDataUrl = (color: THREE.Color, src?: HTMLCanvasElement) => {

return canvas.toDataURL();
};
import { colorDataUrl, generatePreviewImage } from "../util";

setups.push(() => {
registry.createMaterialTexture = new Action("create_material_texture", {
Expand Down
44 changes: 2 additions & 42 deletions src/pbr_preview/src/lib/tools/materialBrush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,8 @@ import {
debounceApplyPbrMaterial,
} from "../applyPbrMaterial";
import { MaterialBrush } from "../MaterialBrush";
import { vue as Vue, three as THREE } from "../../deps";
import { getSelectedTexture } from "../util";

const generatePreviewImage = (settings: object) => {
const renderer = new THREE.WebGLRenderer({
alpha: true,
antialias: true,
});
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, 96 / 96, 0.1, 1000);

const ambientLight = new THREE.AmbientLight(0xffffff, 0.75);
scene.add(ambientLight);

const pointLight = new THREE.PointLight(0xffffff, 1, 100);
pointLight.position.set(5, 5, 5);
scene.add(pointLight);
const geometry = new THREE.SphereGeometry(1, 32, 32);

const material = new THREE.MeshStandardMaterial({
color: settings.albedo,
metalness: settings.metalness ?? 0,
roughness: settings.roughness ?? 1,
emissive: settings.emissive,
bumpScale: settings.height ?? 0,
envMap: PreviewScene.active?.cubemap ?? null,
envMapIntensity: 0.5,
});

const sphere = new THREE.Mesh(geometry, material);
scene.add(sphere);

camera.position.x = 0;
camera.position.y = 0;
camera.position.z = 2;

renderer.setSize(96, 96);

renderer.render(scene, camera);

return renderer.domElement.toDataURL();
};
import { vue as Vue } from "../../deps";
import { getSelectedTexture, generatePreviewImage } from "../util";

const STORAGE_NAMESPACE = "materialBrushPresets";

Expand Down
77 changes: 77 additions & 0 deletions src/pbr_preview/src/lib/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { three as THREE } from "../deps";
export function getSelectedTexture(): Texture | null {
if (Texture.selected) {
return Texture.selected;
Expand Down Expand Up @@ -87,3 +88,79 @@ export function debounce(func: Function, wait: number) {
timeout = setTimeout(later, wait);
};
}

export function generatePreviewImage(
settings:
| THREE.MeshStandardMaterial
| (THREE.MeshStandardMaterialParameters & {
albedo: THREE.ColorRepresentation;
emissive: THREE.ColorRepresentation;
height?: number;
})
) {
const renderer = new THREE.WebGLRenderer({
alpha: true,
antialias: true,
});
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, 96 / 96, 0.1, 1000);

const ambientLight = new THREE.AmbientLight(0xffffff, 0.75);
scene.add(ambientLight);

const pointLight = new THREE.PointLight(0xffffff, 1, 100);
pointLight.position.set(5, 5, 5);
scene.add(pointLight);
const geometry = new THREE.SphereGeometry(1, 32, 32);

const material =
settings instanceof THREE.MeshStandardMaterial &&
settings.isMeshStandardMaterial
? settings
: new THREE.MeshStandardMaterial({
color: settings.albedo,
metalness: settings.metalness ?? 0,
roughness: settings.roughness ?? 1,
emissive: settings.emissive,
bumpScale: settings.height ?? 0,
envMap: PreviewScene.active?.cubemap ?? null,
envMapIntensity: 0.5,
});

const sphere = new THREE.Mesh(geometry, material);
scene.add(sphere);

camera.position.x = 0;
camera.position.y = 0;
camera.position.z = 2;

renderer.setSize(96, 96);

renderer.render(scene, camera);

const data = renderer.domElement.toDataURL();

renderer.dispose();

return data;
}

export function colorDataUrl(color: THREE.Color, src?: HTMLCanvasElement) {
const canvas = src ?? document.createElement("canvas");
const ctx = canvas.getContext("2d");

if (!ctx) {
return null;
}

const width = Math.max(Project ? Project.texture_width : 16, 16);
const height = Math.max(Project ? Project.texture_height : 16, 16);

canvas.width = width;
canvas.height = height;

ctx.fillStyle = `rgb(${color.r * 255}, ${color.g * 255}, ${color.b * 255})`;
ctx.fillRect(0, 0, width, height);

return canvas.toDataURL();
}

0 comments on commit cea64f5

Please sign in to comment.