效果:
代码:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>Three.js tutorial - Lesson 02</title> <style>body{background: #eeeeee;overflow: hidden;}</style> <script src="js/r69/three.js"></script> <script src="js/r69/Detector.js"></script> <script src="js/r69/CanvasRenderer.js"></script> <script src="js/r69/Projector.js"></script> </head> <body> <div id="WebGLCanvas"></div> <script> var scene = new THREE.Scene(); var canvasWidth = window.innerWidth; var canvasHeight = window.innerHeight; var camera = new THREE.PerspectiveCamera(45, canvasWidth / canvasHeight, 1, 100); camera.position.set(0, 0, 10); camera.lookAt(scene.position); scene.add(camera); var renderer; if(Detector.webgl){ renderer = new THREE.WebGLRenderer({antialias:true}); //创建一个抗锯齿效果的渲染器 } else { renderer = new THREE.CanvasRenderer(); } renderer.setClearColor(0xeeeeee, 1); renderer.setSize(canvasWidth, canvasHeight); document.getElementById("WebGLCanvas").appendChild(renderer.domElement); var triangleGeometry = new THREE.Geometry(); triangleGeometry.vertices.push(new THREE.Vector3( 0.0, 1.0, 0.0)); triangleGeometry.vertices.push(new THREE.Vector3(-1.0, -1.0, 0.0)); triangleGeometry.vertices.push(new THREE.Vector3( 1.0, -1.0, 0.0)); triangleGeometry.faces.push(new THREE.Face3(0, 1, 2)); // To color the surface, a material has to be created. If all vertices shall have // different colors, we need to set the vertex colors of each face. The colors in // between will be interpolated as color gradients. // Unfortunately, the canvas renderer doesn't support vertex colors (yet [Three.js, // release 69]). Either you accept a white face color, or set another face color. /*给顶点赋予颜色*/ triangleGeometry.faces[0].vertexColors[0] = new THREE.Color(0xFF0000); triangleGeometry.faces[0].vertexColors[1] = new THREE.Color(0x00FF00); triangleGeometry.faces[0].vertexColors[2] = new THREE.Color(0x0000FF); // To activate the vertex color, we have to set 'vertexColors' attribute to // 'THREE.VertexColors'. Otherwise they won't be displayed. // Create a basic material, supporting vertex colors. Activate the 'doubleSided' // attribute to force the rendering of both sides of each face (front and back). // This prevents the so called 'backface culling'. Usually, only the side is // rendered, whose normal vector points towards the camera. The other side is not // rendered (backface culling). But this performance optimization sometimes leads // to wholes in the surface. When this happens in your surface, simply set // 'doubleSided' to 'true'. var triangleMaterial = new THREE.MeshBasicMaterial({ //color:0xFFFFFF, vertexColors:THREE.VertexColors, //将顶点颜色放入材质中 side:THREE.DoubleSide }); var triangleMesh = new THREE.Mesh(triangleGeometry, triangleMaterial); triangleMesh.position.set(-1.5, 0.0, 4.0); scene.add(triangleMesh); var squareGeometry = new THREE.Geometry(); squareGeometry.vertices.push(new THREE.Vector3(-1.0, 1.0, 0.0)); squareGeometry.vertices.push(new THREE.Vector3( 1.0, 1.0, 0.0)); squareGeometry.vertices.push(new THREE.Vector3( 1.0, -1.0, 0.0)); squareGeometry.vertices.push(new THREE.Vector3(-1.0, -1.0, 0.0)); squareGeometry.faces.push(new THREE.Face3(0, 1, 2)); squareGeometry.faces.push(new THREE.Face3(0, 2, 3)); var squareMaterial = new THREE.MeshBasicMaterial({ color:0x8080FF/*, side:THREE.DoubleSide */ //不设置side属性为DoubleSide,当正方形旋转到背面就看不到了 }); var squareMesh = new THREE.Mesh(squareGeometry, squareMaterial); squareMesh.position.set(1.5, 0.0, 4.0); scene.add(squareMesh); function render(){ triangleMesh.rotation.y += 0.1; //绕y轴旋转,旋转速率为0.1 squareMesh.rotation.x -= 0.075; //绕x轴旋转,旋转速率为0.075 renderer.render(scene, camera); requestAnimationFrame(render); //帧动画 } render(); </script> </body> </html>
附注:当前笔者使用的three.js版本是r69
相关推荐
opengl简单教程前五课pdf,
【二年级英语上册 unit1 Lesson6-Lesson7教案】主要涵盖了小学阶段英语教学的一些核心知识点,包括情感目标、知识目标、能力目标、教学重点和难点,以及教学过程中的具体活动设计。以下是对这些内容的详细说明: 1....
OpenGL是计算机图形学中的一个强大库,用于在各种...这个系列教程对于任何想要进入游戏开发、科学可视化或图形设计领域的初学者来说,都是一个宝贵的资源。在观看视频的同时,配合实践编程,将能更好地巩固所学知识。
opengl简单视频教程Lesson10-Lesson11,网站地址: http://www.videotutorialsrock.com
比特鹏哥C语言学习--Lesson 2 板书
软件测试-Lesson2 黑盒测试.pptx
郭天祥-51单片机-LESSON2_流水灯pdf,
这个简单的视频教程“Lesson6-Lesson9”旨在帮助初学者理解并掌握OpenGL的基础概念和核心功能。通过观看这些课程,学习者将能够逐步建立起对OpenGL的理解,并能够创建自己的基本图形应用程序。 在Lesson6中,主要...
NeHe OpenGL教程(中英文版附带VC++源码)中英文系列 Lesson 01-lesson 02 创建一个OpenGL窗口: 如何创建三角形和四边形 http://ieee.org.cn/dispbbs.asp?boardID=61&ID=53679 Lesson 03-lesson 04 添加颜色 旋转 ...
软件测试-Lesson2 软件测试实践.ppt
OpenGL是计算机图形学中的一种广泛应用的编程接口...通过这个系列的教程,学习者将获得足够的技能来开发自己的OpenGL应用程序,并能够理解和应用许多现代图形编程技术。建议结合实际编程实践,逐步掌握并巩固所学知识。
next.js-ssr课程 在egghead.io上回购课程 指示 git clone https://github.com/tgrecojs/next.js-ssr-lesson && cd next.js-ssr-lesson npm install npm run dev
NeHe OpenGL教程(中英文版附带VC++源码)中英文系列 Lesson 01-lesson 02 创建一个OpenGL窗口: 如何创建三角形和四边形 http://ieee.org.cn/dispbbs.asp?boardID=61&ID=53679 Lesson 03-lesson 04 添加颜色 旋转 ...
新概念青少版a---lesson5.ppt
plots-lesson16
[2.1.1]--Lesson1Four-StepTranslationProce.srt
中国民航飞行人员英语--lesson-4.doc
在"js-fifth-lesson"这个主题中,我们可以深入探讨JavaScript的一些核心概念和关键知识点。 1. 变量与数据类型: JavaScript支持变量的动态类型,使用`var`, `let`, 或 `const` 关键字声明。基本数据类型包括字符...