小言_互联网的博客

微信小程序集成three.js--1.创建各种光源的场景

318人阅读  评论(0)

1.实例演示

微信小程序集成Three.js,各种光源效果演示

2.源码

(1)引入three.js库文件


  
  1. import * as THREE from '../../libs/three.weapp.js'
  2. import {
  3. OrbitControls
  4. } from '../../jsm/controls/OrbitControls'
  5. const app = getApp()

库文件下载及配置看这里https://blog.csdn.net/weixin_39318421/article/details/128468409?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22128468409%22%2C%22source%22%3A%22weixin_39318421%22%7D

 (2)默认环境光场景

这个场景只加入了简单的环境光,AmbientLight的光线没有特定的来源,而且这个光源也不会影响阴影的生成。在使用其他光源的同时使用AmbientLight,目的是弱化阴影或添加一些颜色。


  
  1. initScene1( ) {
  2. wx. createSelectorQuery()
  3. . select( '#webgl')
  4. . node()
  5. . exec( (res) => {
  6. let canvasId = String(res[ 0]. node. id)
  7. const canvas = THREE. global. registerCanvas(canvasId, res[ 0]. node)
  8. this. setData({
  9. canvasId: canvasId
  10. })
  11. //相机
  12. const camera = new THREE. PerspectiveCamera( 70, canvas. width / canvas. height, 1, 1000);
  13. //场景
  14. const scene = new THREE. Scene();
  15. scene. background = new THREE. Color( 0xffffff);
  16. const renderer = new THREE. WebGLRenderer({
  17. antialias: true
  18. });
  19. camera. position. set( 30, 40, 50);
  20. //控制器
  21. const controls = new OrbitControls(camera, renderer. domElement);
  22. controls. enableDamping = true;
  23. controls. update();
  24. //加入环境光
  25. let ambiColor = "#cccccc";
  26. let ambientLight = new THREE. AmbientLight(ambiColor);
  27. scene. add(ambientLight)
  28. //球体
  29. const sphereGeometry = new THREE. SphereGeometry( 4, 20, 20)
  30. const sphereMaterial = new THREE. MeshLambertMaterial({
  31. color: 0x7777ff
  32. })
  33. const sphere = new THREE. Mesh(sphereGeometry, sphereMaterial)
  34. sphere. position. y = 10
  35. sphere. position. z = 10
  36. //设置球体投射阴影
  37. sphere. castShadow = true
  38. scene. add(sphere)
  39. //正方体1
  40. const geometry = new THREE. BoxBufferGeometry( 10, 10, 10);
  41. const material = new THREE. MeshLambertMaterial({
  42. color: 0x2a5a5
  43. });
  44. const mesh1 = new THREE. Mesh(geometry, material);
  45. mesh1. position. x = 15
  46. mesh1. name = 'ms1'
  47. //设置正方体投射阴影
  48. mesh1. castShadow = true;
  49. scene. add(mesh1);
  50. //平面
  51. const planeGeometry = new THREE. PlaneGeometry( 200, 150, 5, 5);
  52. const planeMaterial = new THREE. MeshBasicMaterial({
  53. color: 0xa8a8a8,
  54. });
  55. planeMaterial. side = THREE. DoubleSide;
  56. const plane = new THREE. Mesh(planeGeometry, planeMaterial)
  57. plane. rotation. x = Math. PI / 2
  58. plane. position. y = - 5
  59. //设置平面接收阴影
  60. plane. receiveShadow = true;
  61. scene. add(plane)
  62. //辅助线
  63. const axesHelper = new THREE. AxesHelper( 500);
  64. scene. add(axesHelper)
  65. renderer. setPixelRatio(wx. getSystemInfoSync(). pixelRatio);
  66. //渲染器使能阴影渲染
  67. renderer. shadowMap. enabled = true
  68. renderer. shadowMap. type = THREE. PCFSoftShadowMap;
  69. renderer. setSize(canvas. width, canvas. height);
  70. function render( ) {
  71. canvas. requestAnimationFrame(render);
  72. //更新控制器
  73. controls. update();
  74. renderer. render(scene, camera);
  75. }
  76. render()
  77. })
  78. },

(3)采用点光源的场景

点光源,PointLight,照射所有方向的光源。


  
  1. initScene2( ) {
  2. wx. createSelectorQuery()
  3. . select( '#webgl')
  4. . node()
  5. . exec( (res) => {
  6. let canvasId = String(res[ 0]. node. id)
  7. const canvas = THREE. global. registerCanvas(canvasId, res[ 0]. node)
  8. this. setData({
  9. canvasId: canvasId
  10. })
  11. //相机
  12. const camera = new THREE. PerspectiveCamera( 70, canvas. width / canvas. height, 1, 1000);
  13. //场景
  14. const scene = new THREE. Scene();
  15. scene. background = new THREE. Color( 0xffffff);
  16. const renderer = new THREE. WebGLRenderer({
  17. antialias: true
  18. });
  19. camera. position. set( 30, 40, 50);
  20. //控制器
  21. const controls = new OrbitControls(camera, renderer. domElement);
  22. controls. enableDamping = true;
  23. controls. update();
  24. //加入点光源
  25. let pointColor = "#ccffcc";
  26. let pointLight = new THREE. PointLight(pointColor);
  27. //设置点光源的位置
  28. pointLight. position. set( 0, 30, 0)
  29. scene. add(pointLight)
  30. //球体
  31. const sphereGeometry = new THREE. SphereGeometry( 4, 20, 20)
  32. const sphereMaterial = new THREE. MeshLambertMaterial({
  33. color: 0x7777ff
  34. })
  35. const sphere = new THREE. Mesh(sphereGeometry, sphereMaterial)
  36. sphere. position. y = 10
  37. sphere. position. z = 10
  38. //设置球体投射阴影
  39. sphere. castShadow = true
  40. scene. add(sphere)
  41. //正方体1
  42. const geometry = new THREE. BoxBufferGeometry( 10, 10, 10);
  43. const material = new THREE. MeshLambertMaterial({
  44. color: 0x2a5a5
  45. });
  46. const mesh1 = new THREE. Mesh(geometry, material);
  47. mesh1. position. x = 15
  48. mesh1. name = 'ms1'
  49. //设置正方体投射阴影
  50. mesh1. castShadow = true;
  51. scene. add(mesh1);
  52. //平面
  53. const planeGeometry = new THREE. PlaneGeometry( 200, 150, 20, 20);
  54. const planeMaterial = new THREE. MeshLambertMaterial({
  55. color: 0x7777ff
  56. });
  57. planeMaterial. side = THREE. DoubleSide;
  58. const plane = new THREE. Mesh(planeGeometry, planeMaterial)
  59. plane. rotation. x = Math. PI / 2
  60. plane. position. y = - 5
  61. //设置平面接收阴影
  62. plane. receiveShadow = true;
  63. scene. add(plane)
  64. //辅助线
  65. const axesHelper = new THREE. AxesHelper( 500);
  66. scene. add(axesHelper)
  67. renderer. setPixelRatio(wx. getSystemInfoSync(). pixelRatio);
  68. //渲染器使能阴影渲染
  69. renderer. shadowMap. enabled = true
  70. renderer. shadowMap. type = THREE. PCFSoftShadowMap;
  71. renderer. setSize(canvas. width, canvas. height);
  72. function render( ) {
  73. canvas. requestAnimationFrame(render);
  74. //更新控制器
  75. controls. update();
  76. renderer. render(scene, camera);
  77. }
  78. render()
  79. })
  80. },

(4)采用锥形光源场景

锥形光-SpotLight(聚光灯光源),通过设置target属性来确定要照射的对象


  
  1. initScene3( ) {
  2. wx. createSelectorQuery()
  3. . select( '#webgl')
  4. . node()
  5. . exec( (res) => {
  6. let canvasId = String(res[ 0]. node. id)
  7. const canvas = THREE. global. registerCanvas(canvasId, res[ 0]. node)
  8. this. setData({
  9. canvasId: canvasId
  10. })
  11. //相机
  12. const camera = new THREE. PerspectiveCamera( 70, canvas. width / canvas. height, 1, 1000);
  13. //场景
  14. const scene = new THREE. Scene();
  15. scene. background = new THREE. Color( 0xffffff);
  16. const renderer = new THREE. WebGLRenderer({
  17. antialias: true
  18. });
  19. camera. position. set( 30, 40, 50);
  20. //控制器
  21. const controls = new OrbitControls(camera, renderer. domElement);
  22. controls. enableDamping = true;
  23. controls. update();
  24. //添加灯光
  25. const spotLight = new THREE. SpotLight( 0xffffff);
  26. spotLight. position. set(- 40, 60, - 10)
  27. //设置点光源投射阴影
  28. spotLight. castShadow = true;
  29. spotLight. target = plane;
  30. //设置投影显示的像素
  31. spotLight. shadow. mapSize. width = 1024
  32. spotLight. shadow. mapSize. height = 1024
  33. scene. add(spotLight)
  34. //球体
  35. const sphereGeometry = new THREE. SphereGeometry( 4, 20, 20)
  36. const sphereMaterial = new THREE. MeshLambertMaterial({
  37. color: 0x7777ff
  38. })
  39. const sphere = new THREE. Mesh(sphereGeometry, sphereMaterial)
  40. sphere. position. y = 10
  41. sphere. position. z = 10
  42. //设置球体投射阴影
  43. sphere. castShadow = true
  44. scene. add(sphere)
  45. //正方体1
  46. const geometry = new THREE. BoxBufferGeometry( 10, 10, 10);
  47. const material = new THREE. MeshLambertMaterial({
  48. color: 0x2a5a5
  49. });
  50. const mesh1 = new THREE. Mesh(geometry, material);
  51. mesh1. position. x = 15
  52. mesh1. name = 'ms1'
  53. //设置正方体投射阴影
  54. mesh1. castShadow = true;
  55. scene. add(mesh1);
  56. //平面
  57. const planeGeometry = new THREE. PlaneGeometry( 200, 150, 20, 20);
  58. const planeMaterial = new THREE. MeshLambertMaterial({
  59. color: 0x7777ff
  60. });
  61. planeMaterial. side = THREE. DoubleSide;
  62. const plane = new THREE. Mesh(planeGeometry, planeMaterial)
  63. plane. rotation. x = Math. PI / 2
  64. plane. position. y = - 5
  65. //设置平面接收阴影
  66. plane. receiveShadow = true;
  67. scene. add(plane)
  68. //辅助线
  69. const axesHelper = new THREE. AxesHelper( 500);
  70. scene. add(axesHelper)
  71. renderer. setPixelRatio(wx. getSystemInfoSync(). pixelRatio);
  72. //渲染器使能阴影渲染
  73. //renderer.shadowMap.enabled = true
  74. renderer. shadowMap. type = THREE. PCFSoftShadowMap;
  75. renderer. setSize(canvas. width, canvas. height);
  76. function render( ) {
  77. canvas. requestAnimationFrame(render);
  78. //更新控制器
  79. controls. update();
  80. renderer. render(scene, camera);
  81. }
  82. render()
  83. })
  84. }

 (5)区域光场景

区域光源--RectAreaLight--可定义为一个发光矩形的光源


  
  1. initScene5( ) {
  2. wx. createSelectorQuery()
  3. . select( '#webgl')
  4. . node()
  5. . exec( (res) => {
  6. let canvasId = String(res[ 0]. node. id)
  7. const canvas = THREE. global. registerCanvas(canvasId, res[ 0]. node)
  8. this. setData({
  9. canvasId: canvasId
  10. })
  11. //相机
  12. const camera = new THREE. PerspectiveCamera( 70, canvas. width / canvas. height, 1, 1000);
  13. //场景
  14. const scene = new THREE. Scene();
  15. scene. background = new THREE. Color( 0xffffff);
  16. const renderer = new THREE. WebGLRenderer({
  17. antialias: true
  18. });
  19. camera. position. set( 30, 40, 50);
  20. //控制器
  21. const controls = new OrbitControls(camera, renderer. domElement);
  22. controls. enableDamping = true;
  23. controls. update();
  24. // 地面
  25. var planeGeometry = new THREE. PlaneGeometry( 70, 70, 1, 1);
  26. var planeMaterial = new THREE. MeshStandardMaterial({
  27. roughness: 0.044676705160855, // calculated from shininess = 1000
  28. metalness: 0.0
  29. });
  30. var plane = new THREE. Mesh(planeGeometry, planeMaterial);
  31. // plane.receiveShadow = true;
  32. // rotate and position the plane
  33. plane. rotation. x = - 0.5 * Math. PI;
  34. plane. position. x = 0;
  35. plane. position. y = 0;
  36. plane. position. z = 0;
  37. // add the plane to the scene
  38. scene. add(plane);
  39. //光线
  40. var spotLight0 = new THREE. SpotLight( 0xcccccc);
  41. spotLight0. position. set(- 40, 60, - 10);
  42. spotLight0. intensity = 0.1;
  43. spotLight0. lookAt(plane);
  44. scene. add(spotLight0);
  45. var areaLight1 = new THREE. RectAreaLight( 0xff0000, 500, 4, 10);
  46. areaLight1. position. set(- 10, 10, 35);
  47. scene. add(areaLight1);
  48. var areaLight2 = new THREE. RectAreaLight( 0x00ff00, 500, 4, 10);
  49. areaLight2. position. set( 0, 10, 35);
  50. scene. add(areaLight2);
  51. var areaLight3 = new THREE. RectAreaLight( 0x0000ff, 500, 4, 10);
  52. areaLight3. position. set( 10, 10, 35);
  53. scene. add(areaLight3);
  54. var planeGeometry1 = new THREE. BoxGeometry( 4, 10, 0);
  55. var planeGeometry1Mat = new THREE. MeshBasicMaterial({
  56. color: 0xff0000
  57. });
  58. var plane1 = new THREE. Mesh(planeGeometry1, planeGeometry1Mat);
  59. plane1. position. copy(areaLight1. position);
  60. scene. add(plane1);
  61. var planeGeometry2 = new THREE. BoxGeometry( 4, 10, 0);
  62. var planeGeometry2Mat = new THREE. MeshBasicMaterial({
  63. color: 0x00ff00,
  64. });
  65. var plane2 = new THREE. Mesh(planeGeometry2, planeGeometry2Mat);
  66. plane2. position. copy(areaLight2. position);
  67. scene. add(plane2);
  68. var planeGeometry3 = new THREE. BoxGeometry( 4, 10, 0);
  69. var planeGeometry3Mat = new THREE. MeshBasicMaterial({
  70. color: 0x0000ff
  71. });
  72. var plane3 = new THREE. Mesh(planeGeometry3, planeGeometry3Mat);
  73. plane3. position. copy(areaLight3. position);
  74. scene. add(plane3);
  75. //辅助线
  76. const axesHelper = new THREE. AxesHelper( 500);
  77. scene. add(axesHelper)
  78. renderer. setPixelRatio(wx. getSystemInfoSync(). pixelRatio);
  79. //渲染器使能阴影渲染
  80. renderer. shadowMap. enabled = true
  81. renderer. shadowMap. type = THREE. PCFSoftShadowMap;
  82. renderer. setSize(canvas. width, canvas. height);
  83. function render( ) {
  84. canvas. requestAnimationFrame(render);
  85. //更新控制器
  86. controls. update();
  87. renderer. render(scene, camera);
  88. }
  89. render()
  90. })
  91. },

其他的代码就是按钮演示视频中按钮的点击事件函数,点击按钮后渲染不同的场景,这里不做过多赘述。

3.实例小程序

场景演示->Three.js中的各种光源 


转载:https://blog.csdn.net/weixin_39318421/article/details/128484676
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场