• three.js入门 —— 实现第一个3D案例


    前言:

            three.js入门,根据文档实现第一个3D案例

    效果图:

    代码实现:

    1. const scene = new THREE.Scene();
    2. //创建一个长方体几何对象Geometry
    3. const geometry = new THREE.BoxGeometry(100, 100, 100);
    4. //创建一个网络基础材质的材质对象Material (基础网络材质不会收到光照影响)
    5. const material = new THREE.MeshBasicMaterial({
    6. color: 0xff0000, //设置材质颜色
    7. transparent: true, //开启通明
    8. opacity: 0.5,
    9. });
    10. //测试--更换材质 -> 漫反射网络材质MeshLambertMaterial
    11. // const material = new THREE.MeshLambertMaterial();
    12. //创建网络模型 ---- 两个参数分别为“几何体”,“材质”
    13. const mesh = new THREE.Mesh(geometry, material);
    14. //定义网络模型在三维场景中的位置
    15. mesh.position.set(0, 0, 0);
    16. //辅助观察坐标系
    17. const axeHelper = new THREE.AxesHelper(150);
    18. scene.add(axeHelper);
    19. //将网络模型添加至三维场景中
    20. scene.add(mesh);
    21. //定义相机渲染输出的画布尺寸
    22. const width = 800;
    23. const height = 500;
    24. //创建一个透视摄影相机
    25. const camera = new THREE.PerspectiveCamera(30, width / height, 1, 3000);
    26. //定义相机的位置
    27. camera.position.set(300, 300, 300);
    28. //相机观察的目标位置 ---- 可以是坐标点,也可以是指定物体的位置
    29. camera.lookAt(mesh.position);
    30. //创建光源 光源颜色和强度
    31. // const pointLight = new THREE.SpotLight(0xeeeeee, 1,0,0);
    32. // //光源位置
    33. // pointLight.position.set(300, 0, 0);
    34. // //添加光源至三维
    35. // scene.add(pointLight);
    36. //创建渲染器对象
    37. const renderer = new THREE.WebGLRenderer();
    38. //设置画布尺寸
    39. renderer.setSize(width, height);
    40. //渲染器渲染方法 生成一个画布并把三维场景呈现在画布上
    41. renderer.render(scene, camera);
    42. //renderer.domElement获取到方法render()生成的画布
    43. document.body.appendChild(renderer.domElement);

     

  • 相关阅读:
    利用 Gem5 模拟器创建一个简单的配置脚本——翻译自官网
    NetSuite Account Register报表详解
    Pikachu漏洞练习平台----验证码绕过(on server) 的深层次理解
    认知战壳吉桔:认知战战略不是从思潮开始,而在策划!
    STM32简易音乐播放器(HAL库)
    【译】了解17.10 GA 中最新的 Git 工具特性
    LQ0221 逆波兰表达式【程序填空】
    nginx负载均衡配置及常见负载均衡策略
    Zookeeper入门
    Docker入门
  • 原文地址:https://blog.csdn.net/m0_73334325/article/details/133773085