Coder Social home page Coder Social logo

sparkthreepbr's Introduction

SparkThreePBR

PBR Car This is a port of the THREE.JS standard shader to Spark AR. I created this as a proof of concept on how you could build fully custom shaders just like you would with THREE. I don't intend to maintain or support this but hope it inspires others in the community to create custom shaders via scripting. Currently it only supports image based lighting with a PMREM texture and models with tangent space normal maps. The shader inside of THREE supports a huge amount of different configurations so you can reference the original code if you'd like to extend this.

The 3d model in the project is Pony Cartoon by Slava Z. from Sketchfab

The environment texture is Ninomaru Teien by Greg Zaal from HDRIHaven

Usage

It's relatively straight forward if you look at the project, but you attach this shader to the diffuse channel of a flat material. In this project there are two scripts:

  • threeShader.js - This script is the port of the shader from three. It exports a buildShader function that returns a shader signal.
  • createMaterials.js - This script imports the buildShader function from threeShader.js and uses it to create and attach the shaders to the objects in the scene.

Create a custom PMREM texture

The texture in this repo was created with the PMREMGenerator class that is included in the THREE library. Here is the basic gist of how to do that:

import {
  WebGLRenderer,
  TextureLoader,
  PMREMGenerator
} from  'three';

const  renderer = new  WebGLRenderer({ antialias:  true });

function  createPMREM ( path ) {
  new  TextureLoader().load( path, ( texture ) => {
    // read data from WebGL Texture
    const  pmremGenerator = new  PMREMGenerator( renderer );
    pmremGenerator.compileEquirectangularShader();
    const  pmrem = pmremGenerator.fromEquirectangular( texture );
      
    // read data from WebGL Texture
    const  data = new  Uint8ClampedArray(pmrem.width*pmrem.height*4);    
    renderer.readRenderTargetPixels(pmrem, 0, 0, pmrem.width, pmrem.height, data); 
       
    // set the alpha to full
    for(let  i=0; i<data.length; i += 4)
      data[i+3] = 255;	
    
    // create canvas
    const  canvas = document.createElement('canvas');
    canvas.width = pmrem.width;
    canvas.height = pmrem.height;
    document.body.appendChild(canvas);
    
    // draw data to canvas
    var  iData = new  ImageData(data, pmrem.width, pmrem.height);
    canvas.getContext('2d').putImageData(iData, 0, 0);		    
    
    pmremGenerator.dispose();
  });
};

createPMREM('assets/forest.png');

sparkthreepbr's People

Contributors

jonathanhooker avatar

Watchers

James Cloos 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.