Saturday, March 16, 2013

Using Shaders and Selective Glow Effects in Three.js

When I first started working with Three.js, in addition to the awe-inspiring collection of examples from Mr.doob, I was also dazzled by the "glow examples" of Thibaut Despoulain: the glowing Tron disk, the animated glowing Tron disk (with accompanying blog post), the Tron disk with particles, and more recently, the volumetric light approximation (with accompanying blog post).  At the time, I didn't know anything about shaders (although there were some excellent tutorials at Aerotwist, part 1 and part 2).

Recently, I learned about the Interactive 3D Graphics course being offered (for free!) at Udacity, that teaches participants all about Three.js. This course is taught by Eric Haines (Google+, Blog), who has written the excellent book Real-Time Rendering.  Eric stumbled across my own collection of introductory examples for Three.js, and recommended a really great article about an introduction to pixel shaders in Three.js  (a.k.a. fragment shaders) by Felix Turner at Airtight Interactive. This motivated me to do two things: (1) sign up for the Udacity course (which is really excellent, and highly recommended!), and (2) to update my collection of Three.js examples for the latest release of Three.js (version 56 at the time of writing).

While updating, I noticed that a number of new classes had been created for shaders and post-processing effects, and so I spent a lot of time looking through source code and examples, and eventually created two new demos of my own: a minimal example of using a Three.js pixel shader, and a shader explorer, which lets you see the results of applying a variety of the Three.js shaders (sepia, vignette, dot screen, bloom, and blur) and the effects of changing the values of their parameters.

Inspired, I revisited the glowing Tron disk examples, hoping to use the BlendShader.js now included as part of the Three.js github repo.  Alas, the search term "THREE.BlendShader" is almost a Googlewhack, with only two results appearing (at the time of writing, which will hopefully change soon); using these links as a starting point, I found a nice example of using the BlendShader to create a neat motion blur effect on a spinning cube.

Next came some experimentation to implement the approach described by Thibaut above, which requires the creation of two scenes to achieve the glow effect.  The first is the "base scene"; the second contains copies of the base scene objects with either bright textures (for the objects that will glow) or black textures (for nonglowing objects, so that they will block the glowing effect properly in the combined scene). The secondary scene is blurred and then blended with the base scene.  However, BlendShader.js creates a linear combination of the pixels from each image, and this implementation of the glow effect requires additive blending.  So I wrote AdditiveBlendShader.js, which is one of the simplest shaders to write, as you are literally adding together the two color values at the pixels of the two textures you are combining.  The result (with thoroughly commented code) can be viewed here:


Hopefully other Three.js enthusiasts will find this example helpful!








Friday, October 5, 2012

Using Game Controllers for Browser-Based Games

With the recent release of Google Chrome version 22, the Gamepad API is ready for action (no manual adjustment of chrome://flags required).  

Recommended reading:




A great interface to use:
However, due to issues as described in the post:
for the time being, it is better to use the patched version of the code posted at: 
(a GitHub fork of sgraham's code)
at least until the main branch is updated with these changes.

Sunday, September 9, 2012

Digital Humanities

I was recently asked to attend an informal discussion about the "Digital Humanities".  Some quick Google searching revealed a wealth of interesting links, which I'm posting here for my own (and possibly others' ?) future reference:


Wikipedia
http://en.wikipedia.org/wiki/Digital_humanities
"...today digital humanities embrace a variety of topics ranging from curating online collections to data mining large cultural data sets. Digital Humanities currently incorporates both digitized and born-digital materials and combines the methodologies from the traditional humanities disciplines ... with tools provided by computing (such as data visualisation, information retrieval, data mining, statistics, computational analysis) and digital publishing."

Alliance of Digital Humanities Organizations
http://digitalhumanities.org/
http://digitalhumanities.org/dhq/
http://digitalhumanities.org/answers/topic/what-is-digital-humanities
"The Alliance of Digital Humanities Organizations (ADHO) promotes and supports digital research and teaching across all arts and humanities disciplines, acting as a community-based advisory force, and supporting excellence in research, publication, collaboration and training."

CUNY Digital Humanities Resource Guide
http://commons.gc.cuny.edu/wiki/index.php/The_CUNY_Digital_Humanities_Resource_Guide
"...a collaboratively produced introduction to the field of Digital Humanities..."

Digital Humanities Now
http://digitalhumanitiesnow.org/
"Digital Humanities Now showcases the scholarship and news of interest to the digital humanities community through a process of aggregation, discovery, curation, and review. Digital Humanities Now also is an experiment in ways to identify, evaluate, and distribute scholarship on the open web through a weekly publication and the quarterly Journal of Digital Humanities."

Humanities Blast 
http://humanitiesblast.com/publications/
"Engaged Digital Humanities Scholarship"

National Endowment for the Humanities
http://www.neh.gov/divisions/odh
"...supports projects that employ digital technology to improve humanities research, education, preservation, access, and public programming. To that end, ODH works with the scholarly community, and with other funding agencies in the United States and abroad, to encourage collaboration across national and disciplinary boundaries. In addition to sponsoring grant programs, ODH also works collaboratively with the field, participating in conferences and workshops with scholars, librarians, scientists, and other funders to learn more about how to best serve digital scholarship."

Thursday, August 16, 2012

Google Apps Script

Time to prep for the Fall semester classes.  I typically create a spreadsheet of topics on Google Docs Drive, and for student-ease-of-use I would like to use that data in a Google Calendar.  To get these two apps talking, I'm learning Google Apps Script.  It is simple to complete https://developers.google.com/apps-script/your_first_script in a matter of minutes, the syntax is quite readable and easy to understand...

Helpful links:
https://developers.google.com/apps-script/defaultservices

Functions for interacting with spreadsheets:
https://developers.google.com/apps-script/class_spreadsheetapp

Functions for interacting with calendars:
https://developers.google.com/apps-script/class_calendarapp

Here's the function I wrote to export data (lists of dates and topic names for a Calculus 2 course for Fall 2012) to a calendar.

To use it: from Google Spreadsheets, go to Tools -- Script Editor... and enter in the function below (changing variable values as necessary). Save it, then execute the script from Tools -- Script Manager..., accept all the permission requests (only necessary on the first run, as in the tutorial example linked to above), and then execute this script again.

function SpreadsheetToCalendar() 
{
  // This function should be executed from the 
  //  spreadsheet you want to export to the calendar
  var mySpreadsheet = SpreadsheetApp.getActiveSheet();
  
  var myCalendar = CalendarApp.openByName("Calculus 2");
  
  // optional - delete existing events
  var events = myCalendar.getEvents(new Date("January 1, 2011 EST"), 
      new Date("January 1, 2013 EST"));
  for (var i = 0; i < events.length; i++) 
  {
     events[i].deleteEvent();
  }
  
  var dataRange = mySpreadsheet.getRange("B2:C46");
  var data = dataRange.getValues();
  
  // process the data
  for (i in data) 
  {
      var row = data[i];
      // assume that each row contains a date entry and a text entry
      var theDate  = row[0];  // First column of row
      var theTitle = row[1];  // Second column of row
      myCalendar.createAllDayEvent(theTitle, theDate);
  }
 
}
P.S. I've switched to using http://www.stylifyyourblog.com/2012/07/syntax-highlighting-in-blogger-using.html and http://www.tools.stylifyyourblog.com/p/postify.html to format code snippets in this blog... highly recommended!

Saturday, June 9, 2012

Three.js examples on GitHub

In the process of learning Three.js, I am creating my own comprehensive set of examples.  For my own convenience as well as anyone else that may be interested, I'm posting all the code on GitHub at:

http://stemkoski.github.com/Three.js

(As an added bonus, I'm learning how to use GitHub.)

Friday, March 30, 2012

Simple Fast Multimedia Library - setup with Code::Blocks

I love this library.  Compiling and running the basic program was a bit tricky; I had a bunch of "libgcc_s_dw2-1.dll is missing" errors.  What I ended up doing to solve these errors was:
  • First, download the compiler "mingw-with-gcc-4.4.zip" discussed on the webpage http://www.sfml-dev.org/tutorials/1.6/start-cb.php; unzip it into an easy-to-find directory such as C:\MinGW
  • Next, install the Code::Blocks IDE from http://www.codeblocks.org/downloads/26 -- but choose the one that does not include MinGW. When installing, if Code::Blocks doesn't automatically find the MinGW directory you set up earlier, you can configure the compiler afterwards as follows: 
    • In the menu bar, go to "Settings", select "Compiler and Debugger..."
    • In the new window make sure the "Global Compiler Settings" image on the left is highlighted/selected
    • In the "Selected Compiler" dropdown list choose "GNU GCC Compiler"
    • In the tabs underneath the dropdown list, click on "Toolchain Executables". 
    • There will be a textbox where you can type the name of the directory where you unzipped the MinGW compiler
  • In the window described above
    ("Settings -> "Compiler and Debugger..." -> "Global Compiler Settings"), click the "Linker Settings" tab, and in the textbox labelled "Other Linker Options", enter the following five lines of text:
            -static-libgcc
            -static-libstdc++
            -lsfml-graphics
            -lsfml-window
            -lsfml-system
Hope this helps somebody out there (perhaps even my future self?).