Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including three-software-renderer with all npm packages installed. Try it out:

var threeSoftwareRenderer = require("three-software-renderer")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

three-software-renderer v1.2.0

Universal Three.js in-memory renderer

three-software-renderer

##Universal Three.js in-memory renderer for node.js and the browser

What's this for?

The SoftwareRenderer is a drop-in renderer for Three.js that can be used on the server and the client, if you need to be GPU independent.

What's the drawback?

Primarily performance. As this renderer does not have access to GPU acceleration and GPU shader units, it's much much slower than the CanvasRenderer or WebGLRenderer. In addition, you'll have to use THREE.DataTexture instead of a normal THREE.Texture for textures when you're in a headless environment (i.e. node.js).

What are possible use cases?

  • Rendering in a web worker, e.g. for static renderings that should happen in the background
  • Rendering on the server, e.g. for preview images of 3D models
  • Tell me, if you have other ideas

Which Three.js version is this compatible with?

So far I have tested 0.69 and 0.71. Please tell me if you tried it with other versions as well :-)

What is this work based on?

First and foremost this is just a modified copy of the SoftwareRenderer found in the Three.js example sources. It also uses the modified THREE.Projector from the examples.

How to use

const THREE = require("three");
const SoftwareRenderer = require("three-software-renderer");
const PNG = require("pngjs").PNG;
const fs = require("fs");

// Build scene with cube
const width = 1024;
const height = 768;

const camera = new THREE.PerspectiveCamera(75, width / height, 1, 10000);
camera.position.z = 500;

// Create a simple red cube
const geometry = new THREE.BoxGeometry(200, 200, 200);
const material = new THREE.MeshBasicMaterial({color: 0xff0000});
const mesh = new THREE.Mesh(geometry, material);

// Rotate the cube a bit
mesh.rotation.x += 0.5;
mesh.rotation.y += 0.6;

const scene = new THREE.Scene();
scene.add(mesh);

// Render into an Uint8ClampedArray (RGBA).
const renderer = new SoftwareRenderer({
  alpha: false
});
renderer.setSize(width, height);
var imagedata = renderer.render(scene, camera);

// Create a PNG from the array
const png = new PNG({
  width: width,
  height: height,
  filterType: -1
});

// Copy byte array to png object  
for(var i=0;i<imagedata.data.length;i++) {
  png.data[i] = imagedata.data[i];
}

// Write PNG to disk
if (!fs.existsSync("temp")) {
  fs.mkdirSync("temp");
}
png.pack().pipe(fs.createWriteStream("temp/example.png"));

I found a bug, how can I help?

Please help me improve this module by opening an issue on Github. If you have an idea what might cause the problem, I'd be very grateful if you'd try to investigate the bug and make a pull request to this repository. If you have any questions or need help doing so, let me know via the issues in this project!

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free