博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
three.js入门(二):三维空间下的直线
阅读量:6221 次
发布时间:2019-06-21

本文共 3343 字,大约阅读时间需要 11 分钟。

hot3.png

1、测试页面:index.html

    
测试

2、代码实现:chapter2.1.js

步骤概述:初始化渲染器 > 初始化相机 > 初始化场景 > 建立几何图形并添加到场景中 > 渲染

function threeStart() {    initThree();     initCamera();    initScene();    initObject();    renderer.clear();    renderer.render(scene, camera);}threeStart();

初始化渲染器

var canvas = document.querySelector("canvas");canvas.width = window.innerWidth;canvas.height = window.innerHeight;var renderer;function initThree() {    renderer = new THREE.WebGLRenderer({        canvas : canvas,        antialias : true    });    renderer.setSize(window.innerWidth, window.innerHeight);    renderer.setClearColor(0xffffff, 1);}

初始化相机

var camera;function initCamera() {    camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);    camera.position.x = 0;    camera.position.y = 0;    camera.position.z = 1000;    camera.up.x = 0;    camera.up.y = 1;    camera.up.z = 0;    camera.lookAt(0, 0, 0);}

初始化场景

var scene;function initScene () {    scene = new THREE.Scene();}

生成几何图行并添加到场景中

var line;function initObject() {    //定义两个顶点    var p1 = new THREE.Vector3(-100, -100, 0);    var p2 = new THREE.Vector3(100, 100, 0);    var geometry = new THREE.Geometry();    var material = new THREE.LineBasicMaterial({vertexColors:THREE.VertexColors });    geometry.vertices.push(p1);    geometry.vertices.push(p2);    var color1 = new THREE.Color( 0x444444 ), color2 = new THREE.Color( 0xFF0000 );    geometry.colors.push(color1, color2);    line = new THREE.Line(geometry, material, THREE.LineSegments);    scene.add(line);}

执行效果:

因为实例化材质的时候,用了vertexColors:THREE.VertexColors参数,因此直线的颜色是根据顶点的颜色渐变的。

3、直线进阶:网格线

var canvas = document.body.querySelector("canvas");canvas.width = window.innerWidth;canvas.height = window.innerHeight;var renderer;function initThree() {    renderer = new THREE.WebGLRenderer({        canvas: canvas,        antialias: true    });    renderer.setSize(window.innerWidth, window.innerHeight);    renderer.setClearColor(0xFFF000, 1.0);}var camera;function initCamera() {    camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100000);    camera.position.x = 0;    camera.position.y = 0;    camera.position.z = 1000;    camera.up.x = 0;    camera.up.y = 1;    camera.up.z = 0;    camera.lookAt(0, 0, 0);}var scene;function initScene() {    scene = new THREE.Scene();}var light;function initLight() {    light = new THREE.DirectionalLight(0xFF0000, 1.0, 0);    light.position.set(10, 10, 20);    scene.add(light);}var cube;function initObject() {    var geometry = new THREE.Geometry();    geometry.vertices.push(new THREE.Vector3(-500, 0, 0));    geometry.vertices.push(new THREE.Vector3(500, 0, 0));    for (var i = 0; i <= 20; i++) {        var line = new THREE.Line(geometry, new THREE.LineBasicMaterial({color: 0x000000, opacity: 0.2}));        line.position.y = (i * 50) - 500;        scene.add(line);        var line2 = new THREE.Line(geometry, new THREE.LineBasicMaterial({color: 0x000000, opacity: 0.2}));        line2.position.x = (i * 50) - 500;        line2.rotation.z = 90 * Math.PI / 180;        scene.add(line2);    }}function threeStart() {    initThree();    initCamera();    initScene();    initLight();    initObject();    renderer.clear();    renderer.render(scene, camera);}threeStart();

效果:

转载于:https://my.oschina.net/u/4108765/blog/3059557

你可能感兴趣的文章
vagrant box add metadata.json报错
查看>>
Swift Currying(柯里化)
查看>>
Java 泛型原理
查看>>
支持cocoapods
查看>>
为什么说传统分布式事务不再适用于微服务架构?
查看>>
【正则表达式】解析numeral的千位分隔符
查看>>
[小团队自动化](二) Drone CI使用Vault作为凭据存储 —— 打造自己的CI/CD工作流...
查看>>
穆宁格:5.8 投资心态很重要,心态决定输赢
查看>>
每日一算 (001)
查看>>
python 闭包与装饰器
查看>>
引入 Tinker 之后如何在 Debug 模式下开启 Instant Run
查看>>
干货:小微个人如何接入免费短信验证码
查看>>
javaScript之全局函数
查看>>
阿里中后台UI解决方案 - Fusion
查看>>
HTTP常用状态码
查看>>
剑指offer 题目4
查看>>
重学前端(9)正则还真要多练
查看>>
MongoDB的复制源oplog
查看>>
五线谱入门(三)
查看>>
原创文章:使用Vuejs实现个人所得税功能兼容移动端
查看>>