cellShading.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. var CreateCellShadingScene = function (engine) {
  2. var CellShadingMaterial = function (name, scene, light) {
  3. this.name = name;
  4. this.id = name;
  5. this.light = light;
  6. this._scene = scene;
  7. scene.materials.push(this);
  8. this.texture = null;
  9. this.toonThresholds = [0.95, 0.5, 0.2, 0.03];
  10. this.toonBrightnessLevels = [1.0, 0.8, 0.6, 0.35, 0.01];
  11. };
  12. CellShadingMaterial.prototype = Object.create(BABYLON.Material.prototype);
  13. // Properties
  14. CellShadingMaterial.prototype.needAlphaBlending = function () {
  15. return false;
  16. };
  17. CellShadingMaterial.prototype.needAlphaTesting = function () {
  18. return false;
  19. };
  20. // Methods
  21. CellShadingMaterial.prototype.isReady = function (mesh) {
  22. var engine = this._scene.getEngine();
  23. if (this.texture && !this.texture.isReady) {
  24. return false;
  25. }
  26. this._effect = engine.createEffect("./Scenes/Customs/shaders/cellShading",
  27. ["position", "normal", "uv"],
  28. ["worldViewProjection", "world", "view", "vLightPosition", "vLightColor", "ToonBrightnessLevels", "ToonThresholds"],
  29. ["textureSampler"],
  30. "");
  31. if (!this._effect.isReady()) {
  32. return false;
  33. }
  34. return true;
  35. };
  36. CellShadingMaterial.prototype.bind = function (world, mesh) {
  37. this._effect.setMatrix("world", world);
  38. this._effect.setMatrix("worldViewProjection", world.multiply(this._scene.getTransformMatrix()));
  39. this._effect.setVector3("vLightPosition", this.light.position);
  40. this._effect.setColor3("vLightColor", this.light.diffuse);
  41. this._effect.setArray("ToonThresholds", this.toonThresholds);
  42. this._effect.setArray("ToonBrightnessLevels", this.toonBrightnessLevels);
  43. // Textures
  44. this._effect.setTexture("textureSampler", this.texture);
  45. };
  46. CellShadingMaterial.prototype.dispose = function () {
  47. if (this.texture) {
  48. this.texture.dispose();
  49. }
  50. this.baseDispose();
  51. };
  52. var scene = new BABYLON.Scene(engine);
  53. var camera = new BABYLON.ArcRotateCamera("Camera", 0, Math.PI / 4, 40, BABYLON.Vector3.Zero(), scene);
  54. var light = new BABYLON.PointLight("Omni", new BABYLON.Vector3(20, 100, 2), scene);
  55. var sphere = BABYLON.Mesh.CreateSphere("Sphere0", 32, 3, scene);
  56. var cylinder = BABYLON.Mesh.CreateCylinder("Sphere1", 5, 3, 2, 32, scene);
  57. var torus = BABYLON.Mesh.CreateTorus("Sphere2", 3, 1, 32, scene);
  58. var cellShadingMaterial = new CellShadingMaterial("mat0", scene, light);
  59. cellShadingMaterial.texture = new BABYLON.Texture("Scenes/Customs/Ground.jpg", scene);
  60. sphere.material = cellShadingMaterial;
  61. sphere.position = new BABYLON.Vector3(-10, 0, 0);
  62. cylinder.material = cellShadingMaterial;
  63. torus.material = cellShadingMaterial;
  64. torus.position = new BABYLON.Vector3(10, 0, 0);
  65. // Animations
  66. var alpha = 0;
  67. scene.registerBeforeRender(function () {
  68. sphere.rotation.y = alpha;
  69. sphere.rotation.x = alpha;
  70. cylinder.rotation.y = alpha;
  71. cylinder.rotation.x = alpha;
  72. torus.rotation.y = alpha;
  73. torus.rotation.x = alpha;
  74. alpha += 0.05;
  75. });
  76. return scene;
  77. };