threejs-materials.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. 'use strict';
  2. /* global threejsLessonUtils */
  3. {
  4. function smoothOrFlat(flatShading, radius = 7) {
  5. const widthDivisions = 12;
  6. const heightDivisions = 9;
  7. const geometry = new THREE.SphereBufferGeometry(radius, widthDivisions, heightDivisions);
  8. const material = new THREE.MeshPhongMaterial({
  9. flatShading,
  10. color: 'hsl(300,50%,50%)',
  11. });
  12. return new THREE.Mesh(geometry, material);
  13. }
  14. function basicLambertPhongExample(MaterialCtor, lowPoly, params = {}) {
  15. const radius = 7;
  16. const widthDivisions = lowPoly ? 8 : 100;
  17. const heightDivisions = lowPoly ? 5 : 50;
  18. const geometry = new THREE.SphereBufferGeometry(radius, widthDivisions, heightDivisions);
  19. const material = new MaterialCtor(Object.assign({
  20. color: 'hsl(210,50%,50%)',
  21. }, params));
  22. return new THREE.Mesh(geometry, material);
  23. }
  24. function sideExample(side) {
  25. const base = new THREE.Object3D();
  26. const size = 6;
  27. const geometry = new THREE.PlaneBufferGeometry(size, size);
  28. [
  29. { position: [ -1, 0, 0], up: [0, 1, 0], },
  30. { position: [ 1, 0, 0], up: [0, -1, 0], },
  31. { position: [ 0, -1, 0], up: [0, 0, -1], },
  32. { position: [ 0, 1, 0], up: [0, 0, 1], },
  33. { position: [ 0, 0, -1], up: [ 1, 0, 0], },
  34. { position: [ 0, 0, 1], up: [-1, 0, 0], },
  35. ].forEach((settings, ndx) => {
  36. const material = new THREE.MeshBasicMaterial({side});
  37. material.color.setHSL(ndx / 6, .5, .5);
  38. const mesh = new THREE.Mesh(geometry, material);
  39. mesh.up.set(...settings.up);
  40. mesh.lookAt(...settings.position);
  41. mesh.position.set(...settings.position).multiplyScalar(size * .75);
  42. base.add(mesh);
  43. });
  44. return base;
  45. }
  46. threejsLessonUtils.addDiagrams({
  47. smoothShading: {
  48. create() {
  49. return smoothOrFlat(false);
  50. },
  51. },
  52. flatShading: {
  53. create() {
  54. return smoothOrFlat(true);
  55. },
  56. },
  57. MeshBasicMaterial: {
  58. create() {
  59. return basicLambertPhongExample(THREE.MeshBasicMaterial);
  60. },
  61. },
  62. MeshLambertMaterial: {
  63. create() {
  64. return basicLambertPhongExample(THREE.MeshLambertMaterial);
  65. },
  66. },
  67. MeshPhongMaterial: {
  68. create() {
  69. return basicLambertPhongExample(THREE.MeshPhongMaterial);
  70. },
  71. },
  72. MeshBasicMaterialLowPoly: {
  73. create() {
  74. return basicLambertPhongExample(THREE.MeshBasicMaterial, true);
  75. },
  76. },
  77. MeshLambertMaterialLowPoly: {
  78. create() {
  79. return basicLambertPhongExample(THREE.MeshLambertMaterial, true);
  80. },
  81. },
  82. MeshPhongMaterialLowPoly: {
  83. create() {
  84. return basicLambertPhongExample(THREE.MeshPhongMaterial, true);
  85. },
  86. },
  87. MeshPhongMaterialShininess0: {
  88. create() {
  89. return basicLambertPhongExample(THREE.MeshPhongMaterial, false, {
  90. color: 'red',
  91. shininess: 0,
  92. });
  93. },
  94. },
  95. MeshPhongMaterialShininess30: {
  96. create() {
  97. return basicLambertPhongExample(THREE.MeshPhongMaterial, false, {
  98. color: 'red',
  99. shininess: 30,
  100. });
  101. },
  102. },
  103. MeshPhongMaterialShininess150: {
  104. create() {
  105. return basicLambertPhongExample(THREE.MeshPhongMaterial, false, {
  106. color: 'red',
  107. shininess: 150,
  108. });
  109. },
  110. },
  111. MeshBasicMaterialCompare: {
  112. create() {
  113. return basicLambertPhongExample(THREE.MeshBasicMaterial, false, {
  114. color: 'purple',
  115. });
  116. },
  117. },
  118. MeshLambertMaterialCompare: {
  119. create() {
  120. return basicLambertPhongExample(THREE.MeshLambertMaterial, false, {
  121. color: 'black',
  122. emissive: 'purple',
  123. });
  124. },
  125. },
  126. MeshPhongMaterialCompare: {
  127. create() {
  128. return basicLambertPhongExample(THREE.MeshPhongMaterial, false, {
  129. color: 'black',
  130. emissive: 'purple',
  131. shininess: 0,
  132. });
  133. },
  134. },
  135. MeshToonMaterial: {
  136. create() {
  137. return basicLambertPhongExample(THREE.MeshToonMaterial);
  138. },
  139. },
  140. MeshDepthMaterial: {
  141. create(props) {
  142. const {camera} = props;
  143. const radius = 4;
  144. const tube = 1.5;
  145. const radialSegments = 8;
  146. const tubularSegments = 64;
  147. const p = 2;
  148. const q = 3;
  149. const geometry = new THREE.TorusKnotBufferGeometry(radius, tube, tubularSegments, radialSegments, p, q);
  150. const material = new THREE.MeshDepthMaterial();
  151. camera.near = 7;
  152. camera.far = 20;
  153. return new THREE.Mesh(geometry, material);
  154. },
  155. },
  156. MeshNormalMaterial: {
  157. create() {
  158. const radius = 4;
  159. const tube = 1.5;
  160. const radialSegments = 8;
  161. const tubularSegments = 64;
  162. const p = 2;
  163. const q = 3;
  164. const geometry = new THREE.TorusKnotBufferGeometry(radius, tube, tubularSegments, radialSegments, p, q);
  165. const material = new THREE.MeshNormalMaterial();
  166. return new THREE.Mesh(geometry, material);
  167. },
  168. },
  169. sideDefault: {
  170. create() {
  171. return sideExample(THREE.FrontSide);
  172. },
  173. },
  174. sideDouble: {
  175. create() {
  176. return sideExample(THREE.DoubleSide);
  177. },
  178. },
  179. });
  180. }