项目作者: nickcoutsos

项目描述 :
three.js material creation/application via stylesheets
高级语言: JavaScript
项目地址: git://github.com/nickcoutsos/threestyle.git
创建时间: 2016-12-15T16:31:49Z
项目社区:https://github.com/nickcoutsos/threestyle

开源协议:

下载


threestyle

What if three.js materials, but CSS?

Introduction

This module seeks to make it possible to use familiar CSS selectors and style
rule syntax for defining and applying three.js materials to scenes.

Example Usage

Set up a simple scene with some objects and a light, then apply a stylesheet.

Any object which includes a geometry can be selected by that geometry’s name.
Additionally, a property className in the object’s userData can further
describe the object and make sharing of style rules easier.

Calling applyStyle with a given node and stylesheet will parse the stylesheet
and traverse the graph to match nodes with rules to dynamically create and apply
materials.

  1. import three from 'three';
  2. import {applyStyle} from 'threestyle';
  3. let camera = new three.PerspectiveCamera(90, window.innerWidth / window.innerHeight, 1, 1000),
  4. scene = new three.Scene()
  5. renderer = new three.WebGLRenderer(),
  6. sphere = new three.Mesh(new three.SphereGeometry(5)),
  7. box = new three.Mesh(new three.BoxGeometry(5, 5, 5)),
  8. light = new three.PointLight();
  9. camera.position.set(10, 10, 10);
  10. camera.lookAt(0, 0, 0);
  11. light.position.set(-10, 10, 10);
  12. sphere.userData.className = 'transparent';
  13. sphere.position.set(-5, 0, 0);
  14. box.position.set(5, 0, 0);
  15. scene.add(camera, sphere, box, light);
  16. applyStyle(
  17. scene,
  18. `
  19. BoxGeometry {
  20. color: red;
  21. }
  22. SphereGeometry {
  23. color: blue;
  24. }
  25. .transparent {
  26. transparent: true;
  27. opacity: 0.5;
  28. }
  29. `
  30. );
  31. function render() {
  32. renderer.render(scene, camera);
  33. requestAnimationFrame(render);
  34. }
  35. render();

Acknowledgements

Inspiration for this project comes from threess which takes steps towards
separating material assignment from the scene graph hierarchy. Additional heavy
lifting will hopefully come from:

  • reworkcss: parsing a stylesheet into an AST
  • cssauron: tree node matching via selectors

Demo texture maps created by soady.