效果:
代码:
<!DOCTYPE html> <!-- The previous line tells the browser, that the page uses the HTML5 standard. --> <html> <head> <meta charset="utf-8"/> <title>Three.js tutorial - Lesson 01</title> <!-- The following meta line optimizes the site for mobile devices. It sets the viewport size to the screen size, so it will be displayed maximized, but unscaled. --> <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1"> <style type="text/css"> body { /* Set the background color of the HTML page to black */ background: #eeeeee; /* Hide oversized content. This prevents the scroll bars. */ overflow: hidden; } </style> <!-- Include Three.js libraries --> <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> <!-- This is the DIV element which will contain the WebGL canvas. To be identifiable lateron, the id 'WebGLCanvas' is applied to it. --> <div id="WebGLCanvas"></div> <!-- This JavaScript block encloses the Three.js commands --> <script> // Create the scene, in which all objects are stored (e. g. camera, lights, // geometries, ...) var scene = new THREE.Scene(); // Get the size of the inner window (content area) to create a full size renderer var canvasWidth = window.innerWidth; var canvasHeight = window.innerHeight; // Now that we have a scene, we want to look into it. Therefore we need a camera. // Three.js offers three camera types: // - PerspectiveCamera (perspective projection) // - OrthographicCamera (parallel projection) // - CombinedCamera (allows to switch between perspective / parallel projection // during runtime) // In this example we create a perspective camera. Parameters for the perspective // camera are ... // ... field of view (FOV), // ... aspect ratio (usually set to the quotient of canvas width to canvas height) // ... near and // ... far. // Near and far define the cliping planes of the view frustum. Three.js provides an // example (http://mrdoob.github.com/three.js/examples/ // -> canvas_camera_orthographic2.html), which allows to play around with these // parameters. // The camera is moved 10 units towards the z axis to allow looking to the center of // the scene. // After definition, the camera has to be added to the scene. var camera = new THREE.PerspectiveCamera(45, canvasWidth / canvasHeight, 1, 100); camera.position.set(0, 0, 10); camera.lookAt(scene.position); //相机聚焦于场景中心 scene.add(camera); // Global renderer object var renderer; if(Detector.webgl){ renderer = new THREE.WebGLRenderer({antialias:true}); //创建一个渲染器(具有抗锯齿效果) // If its not supported, instantiate the canvas renderer to support all non WebGL browsers } else { renderer = new THREE.CanvasRenderer(); } // Set the background color of the renderer to black, with full opacity renderer.setClearColor(0xeeeeee, 1); // Set the renderers size to the content areas size renderer.setSize(canvasWidth, canvasHeight); // Get the DIV element from the HTML document by its ID and append the renderers DOM // object to it document.getElementById("WebGLCanvas").appendChild(renderer.domElement); // Create the triangle (or any arbitrary geometry). // 1. Instantiate the geometry object // 2. Add the vertices // 3. Define the faces by setting the vertices indices 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)); // Create a white basic material and 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, side:THREE.DoubleSide }); // Create a mesh and insert the geometry and the material. Translate the whole mesh // by -1.5 on the x axis and by 4 on the z axis. Finally add the mesh to the scene. var triangleMesh = new THREE.Mesh(triangleGeometry, triangleMaterial); triangleMesh.position.set(-1.5, 0.0, 4.0); scene.add(triangleMesh); // The creation of the square is done in the same way as the triangle, except of the // face definition. Instead of using one THREE.Face3, we have to define two // THREE.Face3 objects. // 1. Instantiate the geometry object // 2. Add the vertices // 3. Define the faces by setting the vertices indices 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)); // Create a white basic material and activate the 'doubleSided' attribute. var squareMaterial = new THREE.MeshBasicMaterial({ color:0xFFFFFF, side:THREE.DoubleSide }); // Create a mesh and insert the geometry and the material. Translate the whole mesh // by 1.5 on the x axis and by 4 on the z axis and add the mesh to the scene. var squareMesh = new THREE.Mesh(squareGeometry, squareMaterial); squareMesh.position.set(1.5, 0.0, 4.0); scene.add(squareMesh); /** * Render the scene. Map the 3D world to the 2D screen. */ function render(){ renderer.render(scene, camera); } render(); </script> </body> </html>
附注:当前笔者使用的three.js版本是r69
相关推荐
opengl简单教程前五课pdf,
本视频教程“OpenGL简单视频教程Lesson1-Lesson5”涵盖了初学者必须掌握的基本概念和技术,帮助他们踏入这个充满可能性的世界。在这个教程中,我们将深入探讨以下几个关键知识点: 1. **OpenGL安装与环境配置**: ...
【二年级英语上册 unit1 Lesson6-Lesson7教案】主要涵盖了小学阶段英语教学的一些核心知识点,包括情感目标、知识目标、能力目标、教学重点和难点,以及教学过程中的具体活动设计。以下是对这些内容的详细说明: 1....
[2.1.1]--Lesson1Four-StepTranslationProce.srt
opengl简单视频教程Lesson10-Lesson11,网站地址: http://www.videotutorialsrock.com
比特鹏哥C语言学习--Lesson 1 板书
在这个“DSC-lesson1”资料中,我们很可能会学习到LabVIEW DSC的基础知识,这对于初学者来说是一份宝贵的学习资源。 首先,LabVIEW DSC的核心是其数据采集和控制能力。通过使用G语言(LabVIEW的图形编程语言),...
这个简单的视频教程“Lesson6-Lesson9”旨在帮助初学者理解并掌握OpenGL的基础概念和核心功能。通过观看这些课程,学习者将能够逐步建立起对OpenGL的理解,并能够创建自己的基本图形应用程序。 在Lesson6中,主要...
本教程“OpenGL简单教程Lesson1-Lesson5(src)”提供了前五课的源代码,帮助初学者深入理解并实践OpenGL编程。在这个教程中,我们将探讨以下几个关键知识点: 1. **OpenGL基本概念**:OpenGL是一个跨语言、跨平台的...
NeHe OpenGL教程(中英文版附带VC++源码)中英文系列 Lesson 01-lesson 02 创建一个OpenGL窗口: 如何创建三角形和四边形 http://ieee.org.cn/dispbbs.asp?boardID=61&ID=53679 Lesson 03-lesson 04 添加颜色 旋转 ...
OpenGL是计算机图形学中的一种广泛应用的编程接口...通过这个系列的教程,学习者将获得足够的技能来开发自己的OpenGL应用程序,并能够理解和应用许多现代图形编程技术。建议结合实际编程实践,逐步掌握并巩固所学知识。
人教版新起点三年级上册英语教案Unit-1-Myself-lesson-1.doc
unit5-lesson1Is-this-your-classroomPPT教案.pptx
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
unit5-lesson1Is-this-your-classroom学习教案.pptx
unit5-lesson1Is-this-your-classroom学习课程.pptx
新概念青少版a---lesson5.ppt
NeHe OpenGL教程(中英文版附带VC++源码)中英文系列 Lesson 01-lesson 02 创建一个OpenGL窗口: 如何创建三角形和四边形 http://ieee.org.cn/dispbbs.asp?boardID=61&ID=53679 Lesson 03-lesson 04 添加颜色 旋转 ...
plots-lesson16
中国民航飞行人员英语--lesson-4.doc