Skip to content

Commit

Permalink
Merge pull request #448 from Thorium-Sim/nebula-improvements
Browse files Browse the repository at this point in the history
fix: Improvements to the nebula renderer. Closes #447
  • Loading branch information
alexanderson1993 authored Jan 7, 2023
2 parents c08c6d0 + 2c2ee59 commit f5092e6
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 24 deletions.
Binary file added client/public/assets/skybox/back.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added client/public/assets/skybox/bottom.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added client/public/assets/skybox/front.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added client/public/assets/skybox/left.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added client/public/assets/skybox/right.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added client/public/assets/skybox/top.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions client/src/components/Starmap/Nebula/generateNebulaMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ function generateTexture(seed, canvases) {
nebulaParams.push({
scale: rand.random() * 0.5 + 0.25,
color: [rand.random(), rand.random(), rand.random()],
intensity: rand.random() * 0.2 + 0.5,
falloff: rand.random() * 3.0 + 2.0,
intensity: rand.random() * 0.2 + 0.9,
falloff: rand.random() * 3.0 + 3.0,
offset: [
rand.random() * 2000 - 1000,
rand.random() * 2000 - 1000,
Expand Down
68 changes: 46 additions & 22 deletions client/src/components/Starmap/Nebula/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,60 @@ import {
sRGBEncoding,
BoxBufferGeometry,
Mesh,
TextureLoader,
} from "three";
import {useGetStarmapStore} from "../starmapStore";
import NebulaWorker from "./generateNebulaMap?worker";

const radius = 1e20;
const CANVAS_WIDTH = 2048;
const nebulaWorker = new NebulaWorker();
let nebulaWorker: Worker | null = null;

const canvas = document.createElement("canvas");
if ("transferControlToOffscreen" in canvas) {
nebulaWorker = new NebulaWorker();
}

const nebulaGeometry = new BoxBufferGeometry(1, 1, 1);

function generateMaterial() {
const sides = ["back", "bottom", "front", "left", "right", "top"];
function generateMaterial(primary: boolean, index: number) {
const canvas = document.createElement("canvas");
document.body.appendChild(canvas);
canvas.width = canvas.height = CANVAS_WIDTH;

// @ts-ignore Built in types don't recognize this property
const offscreenCanvas = canvas.transferControlToOffscreen();
offscreenCanvas.width = offscreenCanvas.height = CANVAS_WIDTH;
if ("transferControlToOffscreen" in canvas) {
// @ts-ignore Built in types don't recognize this property
const offscreenCanvas = canvas.transferControlToOffscreen();
offscreenCanvas.width = offscreenCanvas.height = CANVAS_WIDTH;

const canvasTexture = new CanvasTexture(canvas);
canvasTexture.encoding = sRGBEncoding;
const canvasTexture = new CanvasTexture(canvas);
canvasTexture.encoding = sRGBEncoding;

const material = new MeshBasicMaterial({
side: BackSide,
// transparent: true,
// depthWrite: false,
depthTest: false,
map: canvasTexture,
userData: {
offscreenCanvas,
},
});
return material;
const material = new MeshBasicMaterial({
side: BackSide,
transparent: primary,
depthWrite: !primary,
depthTest: false,
map: canvasTexture,
userData: {
offscreenCanvas,
},
});
return material;
} else {
const texture = new TextureLoader().load(
`/assets/skybox/${sides[index]}.png`
);
const material = new MeshBasicMaterial({
side: BackSide,
transparent: primary,
depthWrite: !primary,
depthTest: false,
map: texture,
});
return material;
}
}

function Nebula() {
Expand All @@ -48,12 +70,14 @@ function Nebula() {

const [primaryMaterials, secondaryMaterials] = React.useMemo(
() => [
Array.from({length: 6}).map(generateMaterial),
Array.from({length: 6}).map(generateMaterial),
Array.from({length: 6}).map((_, index) => generateMaterial(true, index)),
Array.from({length: 6}).map((_, index) => generateMaterial(false, index)),
],
[]
);

useEffect(() => {
if (!nebulaWorker) return;
if (primaryMesh.current && secondaryMesh.current) {
if (
Array.isArray(primaryMesh.current.material) &&
Expand Down Expand Up @@ -87,15 +111,15 @@ function Nebula() {
}, 500);
}
nebulaWorker.addEventListener("message", onMessage);
return () => nebulaWorker.removeEventListener("message", onMessage);
return () => nebulaWorker?.removeEventListener("message", onMessage);
}
}
}, []);
const useStarmapStore = useGetStarmapStore();

const skyboxKey = useStarmapStore(s => s.skyboxKey);
useEffect(() => {
nebulaWorker.postMessage({
nebulaWorker?.postMessage({
type: "render",
seed: skyboxKey,
which: activeMesh.current === "primary" ? "secondary" : "primary",
Expand Down

0 comments on commit f5092e6

Please sign in to comment.