当前位置:   article > 正文

three.js 04-07 之 MeshPhongMaterial 材质_threejs meshphongmaterial

threejs meshphongmaterial

    上篇我们已经介绍了 three.js 中高级材质中的 MeshLambertMaterial 材质。本篇将要介绍的是另一种与之对应的高级材质 MeshPhongMaterial 材质。通过它可以创建一种光亮表面的材质效果。这种材质的属性基本跟 MeshLambertMaterial 暗淡材质一样,除此之外,我们列出 MeshPhongMaterial 材质中比较感兴趣的特有属性,如下表所示:

属性描述
ambient (环境光颜色)同 MeshLambertMaterial 材质
emissive (自发光颜色)同 MeshLambertMaterial 材质
specular (镜面反射光颜色)此属性指定该材质的镜面反射光部分的颜色。如果将它设置成跟 color 属性相同的颜色,则会得到一种更加类似金属的材质如果设置成 gray 灰色,材质将变得像塑料
shininess (镜面反射光强度)此属性指定 specular 镜面反射光部分的亮度。默认值是 30
    同样,为了方便读者试验此材质的一些关键属性的效果,我们给出一个完整的示例,代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>示例 04.07 - MeshPhongMaterial</title>
  5. <script src="../build/three.js"></script>
  6. <script src="../build/js/controls/OrbitControls.js"></script>
  7. <script src="../build/js/libs/stats.min.js"></script>
  8. <script src="../build/js/libs/dat.gui.min.js"></script>
  9. <script src="../jquery/jquery-3.2.1.min.js"></script>
  10. <style>
  11. body {
  12. /* 设置 margin 为 0,并且 overflow 为 hidden,来完成页面样式 */
  13. margin: 0;
  14. overflow: hidden;
  15. }
  16. /* 统计对象的样式 */
  17. #Stats-output {
  18. position: absolute;
  19. left: 0px;
  20. top: 0px;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <!-- 用于 WebGL 输出的 Div -->
  26. <div id="webgl-output"></div>
  27. <!-- 用于统计 FPS 输出的 Div -->
  28. <div id="stats-output"></div>
  29. <!-- 运行 Three.js 示例的 Javascript 代码 -->
  30. <script type="text/javascript">
  31. var scene;
  32. var camera;
  33. var render;
  34. var webglRender;
  35. var canvasRender;
  36. var controls;
  37. var stats;
  38. var guiParams;
  39. var ground;
  40. var cube;
  41. var sphere;
  42. var plane;
  43. var activeMesh;
  44. var meshMaterial;
  45. var ambientLight;
  46. var spotLight;
  47. var cameraHelper;
  48. $(function() {
  49. stats = initStats();
  50. scene = new THREE.Scene();
  51. webglRender = new THREE.WebGLRenderer( {antialias: true, alpha: true} ); // antialias 抗锯齿
  52. webglRender.setSize(window.innerWidth, window.innerHeight);
  53. webglRender.setClearColor(0xeeeeee, 1.0);
  54. webglRender.shadowMap.enabled = true; // 允许阴影投射
  55. render = webglRender;
  56. camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 30, 1000); // 2147483647
  57. camera.position.set(-45.5, 68.2, 90.9);
  58. var target = new THREE.Vector3(10, 0 , 0);
  59. controls = new THREE.OrbitControls(camera, render.domElement);
  60. controls.target = target;
  61. camera.lookAt(target);
  62. $('#webgl-output')[0].appendChild(render.domElement);
  63. window.addEventListener('resize', onWindowResize, false);
  64. ambientLight = new THREE.AmbientLight(0x0c0c0c);
  65. scene.add(ambientLight);
  66. spotLight = new THREE.SpotLight(0xffffff);
  67. spotLight.position.set(0, 30, 60);
  68. spotLight.castShadow = true;
  69. scene.add(spotLight);
  70. cameraHelper = new THREE.CameraHelper(spotLight.shadow.camera);
  71. scene.add(cameraHelper);
  72. // 加入一个平面
  73. var groundGeometry = new THREE.PlaneGeometry(100, 100, 4, 4);
  74. var groundMaterial = new THREE.MeshBasicMaterial({color: 0x777777});
  75. ground = new THREE.Mesh(groundGeometry, groundMaterial);
  76. ground.rotation.set(-0.5 * Math.PI, 0, 0); // 沿着 X轴旋转-90°
  77. scene.add(ground);
  78. // 定义 cube, sphere, plane
  79. var cubeGeometry = new THREE.BoxGeometry(15, 15, 15);
  80. var sphereGeometry = new THREE.SphereGeometry(14, 30, 30);
  81. var planeGeometry = new THREE.PlaneGeometry(14, 14, 4, 4);
  82. // 材质
  83. meshMaterial = new THREE.MeshPhongMaterial({color: 0x7777ff, transparent: false, opacity: 1.0});
  84. cube = new THREE.Mesh(cubeGeometry, meshMaterial);
  85. sphere = new THREE.Mesh(sphereGeometry, meshMaterial);
  86. plane = new THREE.Mesh(planeGeometry, meshMaterial);
  87. cube.castShadow = true;
  88. sphere.castShadow = true;
  89. plane.castShadow = true;
  90. cube.position.set(0, 12, 0);
  91. sphere.position.set(0, 14, 0);
  92. plane.position.set(0, 12, 0);
  93. /** 用来保存那些需要修改的变量 */
  94. guiParams = new function() {
  95. this.rotationSpeed = 0.02;
  96. this.opacity = meshMaterial.opacity;
  97. this.transparent = meshMaterial.transparent;
  98. this.visible = meshMaterial.visible;
  99. this.ambient = '#ffffff';
  100. this.emissive = '#2a2a2a';
  101. this.specular = '#ffffff';
  102. this.shininess = 5;
  103. this.side = 'front';
  104. this.color = '#ff000e';
  105. this.selectedMesh = 'sphere';
  106. this.spotLight = true;
  107. this.addMesh = function(e) {
  108. scene.remove(activeMesh);
  109. switch (e) {
  110. case "cube":
  111. activeMesh = cube;
  112. break;
  113. case "sphere":
  114. activeMesh = sphere;
  115. break;
  116. case "plane":
  117. activeMesh = plane;
  118. break;
  119. }
  120. scene.add(activeMesh);
  121. };
  122. this.updateMaterial = function() {
  123. meshMaterial.opacity = this.opacity;
  124. meshMaterial.transparent = this.transparent;
  125. meshMaterial.visible = this.visible;
  126. meshMaterial.ambient = new THREE.Color(this.ambient);
  127. meshMaterial.emissive = new THREE.Color(this.emissive);
  128. meshMaterial.specular = new THREE.Color(this.specular);
  129. meshMaterial.shininess = this.shininess;
  130. meshMaterial.color = new THREE.Color(this.color);
  131. switch (this.side) {
  132. case "front":
  133. meshMaterial.side = THREE.FrontSide;
  134. break;
  135. case "back":
  136. meshMaterial.side = THREE.BackSide;
  137. break;
  138. case "double":
  139. meshMaterial.side = THREE.DoubleSide;
  140. break;
  141. }
  142. meshMaterial.needsUpdate = true;
  143. }
  144. }
  145. /** 定义 dat.GUI 对象,并绑定 guiParams 的几个属性 */
  146. var gui = new dat.GUI();
  147. var folder = gui.addFolder('Mesh');
  148. folder.open();
  149. folder.add(guiParams, 'opacity', 0.1, 1.0).onChange(function(e) {
  150. guiParams.updateMaterial();
  151. });
  152. folder.add(guiParams, 'transparent').onChange(function(e) {
  153. guiParams.updateMaterial();
  154. });
  155. folder.addColor(guiParams, 'ambient').onChange(function(e) {
  156. guiParams.updateMaterial();
  157. });
  158. folder.addColor(guiParams, 'emissive').onChange(function(e) {
  159. guiParams.updateMaterial();
  160. });
  161. folder.addColor(guiParams, 'specular').onChange(function(e) {
  162. guiParams.updateMaterial();
  163. });
  164. folder.add(guiParams, 'shininess', 1, 200).onChange(function(e) {
  165. guiParams.updateMaterial();
  166. });
  167. folder.add(guiParams, 'visible').onChange(function(e) {
  168. guiParams.updateMaterial();
  169. });
  170. folder.add(guiParams, 'side', ['front', 'back', 'double']).onChange(function(e) {
  171. console.log(e);
  172. guiParams.updateMaterial();
  173. });
  174. folder.addColor(guiParams, 'color').onChange(function(e) {
  175. guiParams.updateMaterial();
  176. });
  177. folder.add(guiParams, 'selectedMesh', ['cube', 'sphere', 'plane']).onChange(function(e) {
  178. guiParams.addMesh(e);
  179. });
  180. gui.add(guiParams, 'spotLight').onChange(function(e) {
  181. scene.remove(spotLight);
  182. scene.remove(cameraHelper);
  183. if (e) {
  184. scene.add(spotLight);
  185. scene.add(cameraHelper);
  186. }
  187. });
  188. guiParams.updateMaterial();
  189. guiParams.addMesh(guiParams.selectedMesh);
  190. renderScene();
  191. });
  192. /** 渲染场景 */
  193. function renderScene() {
  194. stats.update();
  195. rotateMesh(); // 旋转物体
  196. requestAnimationFrame(renderScene);
  197. render.render(scene, camera);
  198. }
  199. /** 初始化 stats 统计对象 */
  200. function initStats() {
  201. stats = new Stats();
  202. stats.setMode(0); // 0 为监测 FPS;1 为监测渲染时间
  203. $('#stats-output').append(stats.domElement);
  204. return stats;
  205. }
  206. /** 当浏览器窗口大小变化时触发 */
  207. function onWindowResize() {
  208. camera.aspect = window.innerWidth / window.innerHeight;
  209. camera.updateProjectionMatrix();
  210. render.setSize(window.innerWidth, window.innerHeight);
  211. }
  212. /** 旋转物体 */
  213. function rotateMesh() {
  214. scene.traverse(function(mesh) {
  215. if (mesh instanceof THREE.Mesh && mesh != ground) {
  216. //mesh.rotation.x += guiParams.rotationSpeed;
  217. mesh.rotation.y += guiParams.rotationSpeed;
  218. //mesh.rotation.z += guiParams.rotationSpeed;
  219. }
  220. });
  221. }
  222. </script>
  223. </body>
  224. </html>
这个示例大体上跟上一篇的基本一样,所以不再过多嗷述,读者可以自行参考上一篇中提到的相关引导建议去试验。

未完待续···

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/96087
推荐阅读
相关标签
  

闽ICP备14008679号