Giter Site home page Giter Site logo

gitter-badger / simple-raytracer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from daviddias/simple-raytracer

0.0 1.0 0.0 5.51 MB

simple-raytracer works as a standalone version of the JS raytracer found in distracer.io, which was inspired by jsRayTracer. It's purpose is to offer a simple raytracing interface that can be run locally in, in a distributed fashion on a server or even in browsers

JavaScript 100.00%

simple-raytracer's Introduction

simple-raytracer

simple-raytracer works as a standalone version of the JS raytracer found in distracer.io, which was inspired by jsRayTracer. It's purpose is to offer a simple raytracing interface that can be run locally in, in a distributed fashion on a server or even in browsers

Badgers

Description

API

Example local

var fs = require('fs');
var srt = require('simple-raytracer');
var Png = require('png').Png;

var N_SPLIT = 50; // The number of tasks will be N_SPLIT * N_SPLIT
var SCENE_PATH = './example-scenes/pokeball.rt'; // The scene configuration to be processed

// Parse and load the scene configuration
var scene = srt.prepareScene(SCENE_PATH);

// Create the tasks that are going to be executed, depending on the scene size and N_SPLIT
var tasks = srt.prepareTasks({
  split: N_SPLIT, /* Number of tasks the job is going to be divided into */
  width: scene.global.width,
  height: scene.global.height
});


// Execute a RayTrace for every single Task
var results = tasks.map(function(task) {
  return {
    begin_x: task.begin_x,
    end_x: task.end_x,
    begin_y: task.begin_y,
    end_y: task.end_y,
    animation: task.animation,
    data: srt.runTask(scene, task).data
  };
});

// Construct the image into a buffer 
var rgb = new Buffer(scene.global.width * scene.global.height * 3);

results.map(function (el) {
  var i = 0;
  for(var y = el.begin_y; y < el.end_y; y++) {
    for(var x = el.begin_x; x < el.end_x; x++) {
      var z = (x * scene.global.width + y) * 3;
      rgb[z] = el.data[i++];
      rgb[z+1] = el.data[i++];
      rgb[z+2] = el.data[i++];
    }
  }
});

// Print out an PNG from the RayTraced Image
var png = new Png(rgb, scene.global.width, scene.global.height, 'rgb');
fs.writeFileSync('./out.png', png.encodeSync());

Example browser with browserify

Acknowledgements

Thank you to Diogo Cunha and Pierre Ozoux for creating distracer.io that lead to the creation of this module and Igor Soarez for reviewing and helping me solve a gnarly bug :)

simple-raytracer's People

Contributors

daviddias avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.