Giter VIP home page Giter VIP logo

three-subdivide's Introduction

Three Subdivide

This modifier uses the Loop (Charles Loop, 1987) subdivision surface algorithm to smooth modern three.js BufferGeometry.

Examples

Screenshot

Loop Subdivision Demo

Background

       At one point, three.js included a subdivision surface modifier in the extended examples, it was removed in r125. This modifier was originally based on the Catmull-Clark algorithm, which works best for geometry with convex coplanar n-gon faces. In three.js r60 the modifier was changed to use the Loop algorithm, which was designed to work better with triangle based meshes.

       The Loop algorithm, however, doesn't always provide uniform results as the vertices are skewed toward the most used vertex positions. A triangle box (like BoxGeometry for example) will favor some corners more than others. To alleviate this issue, this implementation includes an initial pass to split coplanar faces at their shared edges. It starts by splitting along the longest shared edge first, and then from that midpoint it splits to any remaining coplanar shared edges. This can be disabled by passing 'split' as false.

       Also by default, this implementation inserts new UV coordinates, but does not average them using the Loop algorithm. In some cases (often in flat geometries) this will produce undesired results, a noticeable tearing will occur. In such cases, try passing 'uvSmooth' as true to enable UV averaging.

Install

  • Option 1: Copy file LoopSubdivision.js, import from file...
import { LoopSubdivision } from 'LoopSubdivision.js';
  • Option 2: Install from npm, import from 'three-subdivide'...
npm install three-subdivide
import { LoopSubdivision } from 'three-subdivide';
  • Option 3: Import directly from CDN...
import { LoopSubdivision } from 'https://unpkg.com/three-subdivide/build/index.module.js';

Usage

To create subdivided geometry, use the static function modify(). The following code creates a cube with smoothed geometry and adds it to a three.js Scene.

import * as THREE from 'three';
import { LoopSubdivision } from 'LoopSubdivision.js';

const iterations = 1;

const params = {
    split:          true,       // optional, default: true
    uvSmooth:       false,      // optional, default: false
    preserveEdges:  false,      // optional, default: false
    flatOnly:       false,      // optional, default: false
    maxTriangles:   Infinity,   // optional, default: Infinity
};

const geometry = LoopSubdivision.modify(new THREE.BoxGeometry(), iterations, params);

const material = new THREE.MeshNormalMaterial();
const mesh = new THREE.Mesh(geometry, material);

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

Modify

LoopSubdivision.modify(bufferGeometry, iterations = 1, params = {}) {

  • bufferGeometry : BufferGeometry - existing three.js BufferGeometry object to be subdivided
  • iterations : Int (optional) - total passes of subdivision to apply, generally between 1 to 5
  • params : Object (optional) - optional parameters object, see below

Parameters Object ('params')

  • split : Boolean (optional) - split coplanar faces at their shared edges before subdividing?
  • uvSmooth : Boolean (optional) - smooth UV coordinates during subdivision?
  • preserveEdges Boolean (optional) - should edges / breaks in geometry be ignored during subdivision?
  • flatOnly : Boolean (optioanl) - subdivide triangles but do not apply smoothing?
  • maxTriangles : Number (optional) - limits subdivision to meshes with less than this number of triangles

NOTE: This modifier converts geometry to non-indexed before the subdivision algorithm is applied. If desired, you can use BufferGeometryUtils.mergeVertices to re-index geometry.

three-subdivide's People

Contributors

dvlp avatar stevinz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

three-subdivide's Issues

Can't smooth mesh

No matter what I do, I wasn't able to obtain a smooth surface after the modifier was applied. I tried with a new mesh, with computeVertexNormal(). Nothing works

Minimum triangle surface area

Is it possible to implement alternative stopping criteria?

Now the library provides maxTriangles, but for example minimumArea would also be a useful criterion, in order to achieve more "uniform" subdivision.

About application to glTF animation

Hello.
I found it a great project.
But would it be difficult to apply it to glTF animation?

my code:

  // Morphing Animation at the top of the screen
  gltfMesh = gltf.scene.children[ 0 ];
  gltfMesh.scale.set( 1.5, 1.5, 1.5 );
  gltfMesh = gltf.scene;
  originalGeometry = gltfMesh.children[0].geometry;
  scene.add( gltfMesh );
  mixer = new THREE.AnimationMixer( gltfMesh );
  mixer.clipAction( gltf.animations[ 1 ] ).setDuration( 5 ).play();
  
  
  // Mesh with subdivisions applied at the bottom of the screen
  const geometry = LoopSubdivision.modify(originalGeometry);
  const material = new THREE.MeshNormalMaterial();
  const gltfMesh_sub = new THREE.Mesh(geometry, material);
  scene.add( gltfMesh_sub );					
  mixer_sub = new THREE.AnimationMixer( gltfMesh_sub );
  mixer_sub.clipAction( gltf.animations[ 1 ] ).setDuration( 5 ).play();

https://user-images.githubusercontent.com/868060/185723390-98f9d10b-3e14-43cd-a74d-4f8658efa171.mp4
To me it looks like the keyframe animation is disappearing.

What I am trying to do is
https://threejs.org/examples/webgl_morphtargets_horse.html
I am trying to apply a subdivision surface to an animation like this sample

Group preservation

Currently when the input geometry has groups the resulting geometry doesn't have any.

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.