从Unity到Three.js(画线组件line)

JavaScript 0基础,只是照着官方文档临摹了下,之后有时间再进行细节学习和功能封装。

import * as THREE from 'three'; //引入threejs

const renderer = new THREE.WebGLRenderer();//创建渲染器
//设置渲染范围,当前撑满全屏,屏幕左上角是(0,0)
let width = window.innerWidth;
let height = window.innerHeight;
renderer.setSize(width, height);

document.body.appendChild(renderer.domElement);
//配置相机,对应unity中 camer组件的相关设置
const camera = new THREE.PerspectiveCamera(45,width/height,1,500);
camera.position.set(0,0,100);//设置相机位置,对应unity中配置camer坐标
camera.lookAt(0,0,0);//设置相机一直朝向的坐标点,对应unity中的相机观察中心点
//创建一个材质球
//const material = new THREE.LineBasicMaterial({color:new THREE.Color("rgb(0, 100, 150)")});//({color:0x0000ff});
const material = new THREE.LineBasicMaterial( {
   
	color: new THREE.Color("rgb(0, 100, 150)"),
	linewidth: 10,//官方文档告知 由于OpenGL Core Profile与 大多数平台上WebGL渲染器的限制,无论如何设置该值,线宽始终为1。
	linecap: 'bevel', //ignored by WebGLRenderer
	linejoin:  'bevel' //ignored by WebGLRenderer
} );
//配置画线经过的点,对应unity中的lineRenderer组件
//屏幕正中间是(0,0,0)
const points = [];
points.push(new THREE.Vector3(-10,0,0));
points.push(new THREE.Vector3(0,10,0));
points.push(new THREE.Vector3(10,0,0));
points.push(new THREE.Vector3(0,-10,0));
points.push(new THREE.Vector3(-10,0,0));
//传入点 创建几何体
const geometry = new THREE.BufferGeometry().setFromPoints(points);    
//设置直线类型,其他还没研究      
const line = new THREE.Line(geometry,material);

//创建场景
const scene = new THREE.Scene();
scene.add(line);//把线加入场景
renderer.render(scene,camera);//设置要渲染的场景和相机

相关推荐

  1. UnityThree.js(线组件line

    2024-02-12 04:40:02       36 阅读
  2. UnityThree.js(计时器、Transform)

    2024-02-12 04:40:02       29 阅读
  3. UnityThree.js(模型文件加载)

    2024-02-12 04:40:02       26 阅读
  4. UnityThree.js(动态创建mesh)

    2024-02-12 04:40:02       37 阅读
  5. 如何在three.js中3D圆弧及半圆弧组成

    2024-02-12 04:40:02       15 阅读
  6. Unity Mirror 入门入神(二)

    2024-02-12 04:40:02       16 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-12 04:40:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-12 04:40:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-12 04:40:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-12 04:40:02       20 阅读

热门阅读

  1. 1103: 地盘划分(New Online Judge)

    2024-02-12 04:40:02       24 阅读
  2. kubeadm部署k8s集群

    2024-02-12 04:40:02       30 阅读
  3. Codeforces Round 924 (Div. 2)

    2024-02-12 04:40:02       32 阅读
  4. linux赋予普通用户权限

    2024-02-12 04:40:02       35 阅读
  5. Linux 禁用/启用 交换分区

    2024-02-12 04:40:02       35 阅读
  6. vue3-应用规模化-单文件组件

    2024-02-12 04:40:02       30 阅读
  7. Dockerfile命令

    2024-02-12 04:40:02       28 阅读
  8. 解锁 SpringBoot 强大配置功能

    2024-02-12 04:40:02       27 阅读
  9. vector基本用法(可变长数组)

    2024-02-12 04:40:02       26 阅读
  10. Docker-CE 国内源国内镜像

    2024-02-12 04:40:02       39 阅读
  11. Debezium发布历史121

    2024-02-12 04:40:02       32 阅读