Coder Social home page Coder Social logo

wuyifan0203 / three-wboit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from stevinz/three-wboit

0.0 0.0 0.0 5.36 MB

Weighted, blended order independent transparency pass for use with three.js.

Home Page: https://stevinz.github.io/three-wboit

License: MIT License

JavaScript 53.37% CSS 1.06% HTML 45.57%

three-wboit's Introduction

Three Wboit

Weighted, Blended Order Independent Transparency (paper, blog) for use with three.js. This implementation is designed as a rendering Pass. It can be used as a stand-alone replacement for a traditional render pass (i.e. renderer.render), or used as part of a larger rendering stack within Effect Composer.

Examples

More Info

There are several common techniques available for order independent transparency. This implementation uses Weighted, Blended Order-Indepent Transparency (WBOIT), both for it's high performance and also compatibility on slower hardware. This implementation is WebGL 1 compatible and mobile friendly.

One of the biggest advantages of order independent transparency is for the rendering of detailed transparent models. Typically when rendering such a model, it is common for some faces to be depth culled. When rendering with WBOIT, all faces will be visible. WBOIT is approximate, though, and while it provides good results it may not be appropriate for all use cases.

There are a variety of weight functions available when rendering with WBOIT. This is partially due to inconsistencies in rendering overlapping pixels at varying depths. Some weight functions are better at incorporating camera near / far planes, some are better at handling larger groups of overlapping triangles. This implementation includes a weight modifier within MeshWboitMaterial that attempts to adjust the weight function for both opacity and color depending on the depth of the fragments.

The biggest downside of this method is that due to the blending method used, as opacity approaches 1.0 objects still retain an artifically high amount of transparency. WBOIT enabled materials can be made opaque during the render pass by setting the material property transparent to false.

Install

  • Option 1: Copy files from src directory into project, import from files...
import { MeshWboitMaterial } from './materials/MeshWboitMaterial.js';
import { WboitPass } from './WboitPass.js';
  • Option 2: Install from npm, import from 'three-wboit'...
npm install three-wboit
import { MeshWboitMaterial, WboitPass } from 'three-wboit';
  • Option 3: Import directly from CDN...
import { MeshWboitMaterial, WboitPass } from 'https://unpkg.com/three-wboit/build/index.module.js';

Usage

To setup your scene to use WBOIT, create an instance of WboitPass.

When creating Mesh objects intended to be transparent, use the included material MeshWboitMaterial. Objects using this material have WBOIT enabled by default. WBOIT can be turned on / off on each object by setting the material's transparent property. The material is functionaly equivalent to MeshBasicMaterial, and supports all it's methods and properties.

When rendering your scene, instead of calling renderer.render(), call wboitPass.render( renderer ). Enjoy.

import * as THREE from 'three';

import { MeshWboitMaterial, WboitPass } from 'three-wboit';

const renderer = new THREE.WebGLRenderer( { preserveDrawingBuffer: true } );

const camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.10, 100 );

const scene = new THREE.Scene();
scene.add( new THREE.BoxGeometry(), new MeshWboitMaterial( { opacity: 0.5 } ) );

const wboitPass = new WboitPass( renderer, scene, camera, 0 /* optional clear color */, 1.0 /* optional clear alpha */);

...

render() {

    // OLD:
    //  renderer.render( scene, camera );

    // NEW:
    wboitPass.render( renderer );

}

Patching Materials

To use WboitPass with any existing material, use the included utility function WboitUtils.patch()

import * as THREE from 'three';

import { WboitUtils } from 'three-wboit';

const material = new THREE.MeshStandardMaterial();
WboitUtils.patch( material );

const myMesh = new THREE.Mesh( new THREE.BoxGeometry(), material );

NOTE: Three.js has made some changes to the renderer from versions v148 through v152, including how it renders transparent objects and how it deals with color space. For versions prior to 152 it is recommended to use the version of this library v1.0.13.

Acknowledgements

  • Weighted, Blended Order-Independent Transparency by Morgan McGuire and Louis Bavoil - Paper
  • Weighted, Blended WebGL 2 Example by Tarek Sherif @tsherif - Repo
  • Weighted, Blended Three.js Example by Alexander Rose @arose - Issue
  • Depth Peel Three.js Example by Dusan Bosnjak @pailhead - Pull

three-wboit's People

Contributors

stevinz 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.