threejs 灯光投影模型渲染demo

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="http://www.yanhuangxueyuan.com/3D/example/three.min.js"></script>
  <script src="./js/OrbitControls.js"></script>
  <title>sence</title>
  <style>
    *{margin:0;padding:0;}
    body{overflow:hidden;}
  </style>
</head>
<body>
</body>
<script>
  // 创建场景
  let Scene = new THREE.Scene(); 

  // 创建相机
  let camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
  camera.position.x = -50;
  camera.position.y = 30;
  camera.position.z = 50;
  camera.lookAt(Scene.position);  // 视角

  // 创建渲染器
  let renderer = new THREE.WebGLRenderer();
  renderer.setSize(window.innerWidth, window.innerHeight);
  renderer.setClearColor(new THREE.Color(0x000000));
  renderer.shadowMap.enabled = true;
  document.body.appendChild(renderer.domElement);


  // 创建平面
  let planeGeometry = new THREE.PlaneGeometry(60, 60, 1, 1); // 平面网格
  let planeMaterial = new THREE.MeshLambertMaterial({color: 0xcccccc});
  let plane = new THREE.Mesh(planeGeometry, planeMaterial);
  plane.rotation.x = -0.5 * Math.PI;
  plane.position.x = 0;
  plane.position.y = 0;
  plane.position.z = 0;
  plane.receiveShadow = true;
  Scene.add(plane);

  // 创建立方体
  let boxGeometry = new THREE.BoxGeometry(10, 10, 10);
  let boxGeometryMaterial = new THREE.MeshLambertMaterial({color: 0x00ff00});
  let box = new THREE.Mesh(boxGeometry, boxGeometryMaterial);
  box.position.x = 0;
  box.position.y = 5;
  box.position.z = 0;
  box.castShadow = true;
  Scene.add(box);

  // 创建灯光

  // 点光源
  let spotLight = new THREE.SpotLight(0xffffff);
  spotLight.position.set(-60, 40, -20);
  spotLight.castShadow = true;
  Scene.add(spotLight);

  // 平行光
  let directionalLight = new THREE.DirectionalLight(0xffffff, 1);
  // 设置光源的方向:通过光源position属性和目标指向对象的position属性计算
  directionalLight.position.set(-60, 40, -20);
  // 方向光指向对象网格模型mesh2,可以不设置,默认的位置是0,0,0
  directionalLight.target = box;
  Scene.add(directionalLight);

  // 环境光
  let ambient = new THREE.AmbientLight(0x444444);
  Scene.add(ambient);//环境光对象添加到scene场景中


  //创建控件对象
  let controls = new THREE.OrbitControls(camera,renderer.domElement);

  // 添加辅助坐标轴
  let axisHelper = new THREE.AxisHelper(250);
  Scene.add(axisHelper);

  function render () {
    renderer.render(Scene, camera);
    requestAnimationFrame(render);
  }
  render();

</script>
</html>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值