1.0.3 • Published 2 years ago

space-scene-2d v1.0.3

Weekly downloads
-
License
Unlicense
Repository
github
Last release
2 years ago

Space Scene 2D

Procedural generator for 2D space scenes. Try it out live here.

Install

npm i space-scene-2d

Example

import { Space2D } from "space-scene-2d";

const space2d = new Space2D();

const canvas = space2d.render(512, 512, {
  nebulaAlbedoLow: [0.1, 0.25, 0.5],
  nebulaAlbedoHigh: [0.5, 0.25, 0.1],
  nebulaFalloff: 512,
  nebulaAbsorption: 1.0,
  nebulaDensity: 0.1,
  nebulaAlbedoScale: 5,
  stars: [
    {
      position: [256, 256, 350],
      color: [0.1375, 0.1375, 0.25],
      falloff: 256,
      diffractionSpikeFalloff: 1024,
      diffractionSpikeScale: 8,
    },
  ],
});

document.body.appendChild(canvas);

API

Constructor

const space2d = new Space2D();

Methods

space2d.render(width: number, height: number, parameters: RenderParameters): HTMLCanvasElement

Renders the space scene. Returns an HTMLCanvasElement with the rendered scene.

ParameterTypeDescription
widthnumberThe width of the rendered scene in pixels.
heightnumberThe height of the rendered scene in pixels.
parameters?RenderParametersOptional. See definition of RenderParameters

Types

RenderParameters

FieldTypeDefaultDescription
scale?number0.002How zoomed out the camera is. Larger value is zoomed out more.
offset?number, number0, 0The location of the lower-left corner of the scene.
backgroundColor?number, number, number0.125, 0.125, 0.25The color of the background noise pattern.
backgroundDepth?number137.0How far in the z-direction the background noise pattern is sliced out of 3D noise.
backgroundLacunarity?number2.0The frequency multiplier for each octave of background noise. Numbers closer to one result in smoother noise.
backgroundGain?number0.5The magnitude multiplier for each octave of background noise. Numbers closer to one result in smoother noise.
backgroundOctaves?number8The number of octaves of background noise.
backgroundDensity?number2.0A scaling factor applied after the background noise is calculated. Larger numbers result in greater density.
backgroundFalloff?number8.0An exponential factor applied after the background noise is calculated. Larger numbers result in a more rapid falloff in density.
backgroundScale?number0.003How zoomed out the camera is for the background noise.
backgroundStarDensity?number0.05The density of the background stars.
backgroundStarBrightness?number0.125How bright the background stars are.
nebulaNear?number0.0The near plane of the nebula noise layers.
nebulaFar?number500.0The far plane of the nebula noise layers.
nebulaLayers?number1000The number of nebula noise layers.
nebulaAbsorption?number1.0How much light is absorbed by each layer as a function of the density at each point.
nebulaLacunarity?number2.0The frequency multiplier for each octave of nebula noise. Numbers closer to one result in smoother noise.
nebulaGain?number0.5The magnitude multiplier for each octave of nebula noise. Numbers closer to one result in smoother noise.
nebulaOctaves?number7The number of octaves of nebula noise.
nebulaDensity?number0.1A scaling factor applied after the nebula noise is calculated. Larger numbers result in greater density.
nebulaFalloff?number4.0An exponential factor applied after the nebula noise is calculated. Larger numbers result in a more rapid falloff in density.
nebulaEmissiveLow?number, number, number0, 0, 0One of two colors mixed to calculate a final emissive color for the nebula. All zeros disables emissivity.
nebulaEmissiveHigh?number, number, number0, 0, 0One of two colors mixed to calculate a final emissive color for the nebula. All zeros disables emissivity.
nebulaEmissiveOffset?number, number, number0, 0, 0The offset into the perlin noise function used to determine a mixing coefficient for the emissive color.
nebulaEmissiveScale?number1.0The frequency multiplier applied to the perlin noise function used to determine a mixing coefficient for the emissive color.
nebulaAlbedoLow?number, number, number1, 1, 1One of two colors mixed to calculate a final albedo color for the nebula.
nebulaAlbedoHigh?number, number, number1, 1, 1One of two colors mixed to calculate a final albedo color for the nebula.
nebulaAlbedoOffset?number, number, number0, 0, 0The offset into the perlin noise function used to determine a mixing coefficient for the albedo color.
nebulaAlbedoScale?number1.0The frequency multiplier applied to the perlin noise function used to determine a mixing coefficient for the albedo color.
stars?Star[][]A list of Star objects to use during this render. See definition of Star.

Star

FieldTypeDescription
position?number, number, numberThe x, y, and z coordinates of the star. These values are relative to the offset, nebulaNear and nebulaFar values of RenderParameters.
color?number, number, numberThe red, green, and blue components of the star color.
falloff?numberAn exponential factor applied to the body of the star. Larger values result in smaller star radii.
diffractionSpikeFalloff?numberAn exponential factor applied to the width of the star diffraction spikes. Larger values result in narrower spikes.
diffractionSpikeScale?numberA scaling factor applied to the length of the star diffraction spikes. Larger values result in longer spikes.
1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago