HTML5로 자신만의 3D 엔진을 만들자

[출처] http://sixrevisions.com/web-development/how-to-create-an-html5-3d-engine/

How to Create an HTML5 3D Engine

 

11-01_html5_3d_engine.png

You’re probably aware by now that HTML5 is game-changing. In particular, with the HTML5 canvas, there’s been an outpouring of HTML5 3D JavaScript engines such as three.js released to enhance the rudimentary 2D HTML5 API. After all, 3D graphics is a lot cooler than 2D graphics, right?

It’s probably fair to say that if you were to create a full-blown 3D engine–complete with three dimensional matrix transformations, point objects, plane objects, shading, ray tracing, and other spatial computations–it would be quite difficult.

But what if we wanted to create an optimized, barebones 3D JavaScript engine that supports translations and rotations? How hard would it be?

What if I tell you that we can create our own simple 3D JavaScript engine with just 40 lines of code?

This guide will provide a simple and straightforward JavaScript engine that enables HTML5 3D renderings that can be used in web animation, games, and so forth.

Understanding 3D Projections

Before creating our own 3D engine, we should first understand how the illusion of 3D space is created on a 2D screen. These illusions are called 3D projections. 3D projections map points onto a two-dimensional plane. In our case, the three-dimensional points define an object we wish to render, and the two-dimensional plane is the computer screen.

As you can see in the following image, we can create a three-dimensional projection of a cube on a two-dimensional plane by drawing three irregular quadrilaterals – the top, left, and front quadrilaterals.

11-02_cube.png

Plane Projections with Ellipses

Unfortunately, calculating three-dimensional projections can be quite complex to code. How can we simplify it?

Imagine looking down at a rotating rectangular plane. Its four corners would lie on the outline of a perfect circle, whose radius is equal to half the distance from opposite corners of the rectangle.

Now imagine if we were to tilt the plane in 3D space. What happens? This imaginary circle suddenly becomes an ellipse, whose height is less than its width.

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

This means that we could create a simple 3D projection by creating planes whose corners reside along the edges of an ellipse.

If we are defining the rotation of the plane with an angle, theta, the 3D projection calculations suddenly become quite simple!

Any point on our three-dimensional plane can be defined with the following two equations which describe a point along the edges of an ellipse:

x = A * cos(theta)
y = B * sin(theta)

where A is half of the ellipse width, and B is half of the ellipse height.

A Simple 3D Engine

If our engine simply creates planes in 3D space, we can build multiple cross-sectional planes that frame a 3D object that we wish to render.

Sample 3D Object Rendering

11-03_3d_plane.png

Cross-Sectional Planes Diagram

11-04_3d_cross_section.png

View demo

This particular 3D shape was created by generating three cross-sectional planes – the top, center, and bottom plane. These imaginary planes provide us with all of the points we need to render the 3D object using the HTML5 Canvas API.

By creating a straightforward function that can generate, translate, and rotate these cross-sectional planes, we have effectively created a simple 3D engine.

Here is the code for our HTML5 3D engine:

// This simple 3D engine was provided by www.Html5CanvasTutorials.com
// for the purpose of creating 3D HTML5 renderings
function Plane(centerX,centerY, planeLength, planeWidth, planeTilt, planeTheta) {
   this.centerX = centerX;
   this.centerY = centerY;
   this.planeLength = planeLength;
   this.planeTheta = planeTheta;

   var lastPerspectiveX = null;
   var lastPerspectiveX2 = null;
   var planeNextCornerAngle = 2*Math.asin(planeWidth/planeLength);

   this.rotate = function(newTheta) {
   	planeTheta = newTheta - planeNextCornerAngle/2;
   }

   this.translate = function(newCenterX, newCenterY) {
   	centerX = newCenterX;
   	centerY = newCenterY;
   }

   this.generate = function() {
   	var ovalLength = planeLength;
   	var ovalWidth = ovalLength * planeTilt;

   	var perspectiveX = (ovalLength / 2) * Math.cos(planeTheta);
   	var perspectiveY = (ovalWidth / 2) * Math.sin(planeTheta);            
   	var perspectiveX2 = (ovalLength / 2) * Math.cos(planeTheta + planeNextCornerAngle);
   	var perspectiveY2 = (ovalWidth / 2) * Math.sin(planeTheta + planeNextCornerAngle);

   	this.topLeftX = (perspectiveX *1) + centerX;
   	this.topLeftY = (perspectiveY * -1) + centerY;
  	this.bottomRightX = (perspectiveX*-1) + centerX;
   	this.bottomRightY = (perspectiveY*1) + centerY
   	this.topRightX = (perspectiveX2 *1) + centerX;
  	this.topRightY = (perspectiveY2 *-1) + centerY;
   	this.bottomLeftX = (perspectiveX2 *-1) + centerX;
   	this.bottomLeftY = (perspectiveY2 *1) + centerY;
   }
 }

This simple 3D engine is completely free to use and modify so long as you keep the commented credits in your code. Enjoy!

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
176 그 외 OpenAPI 가을의 곰을... 2012.08.22 4337
175 이통사 OpenAPI file 가을의 곰을... 2012.08.22 4175
174 포털 OpenAPI file 가을의 곰을... 2012.08.22 4099
173 공공기관 OpenAPI 가을의 곰을... 2012.08.22 3356
172 멀티부팅시 우분투 GRUB 복구하기.. file 가을의 곰을... 2012.08.21 6025
171 Hadoop HDFS BASIC Usage Over View : 하둡 사용 개요 가을의 곰을... 2012.08.20 5378
170 HDFS 테스트 file 가을의 곰을... 2012.08.20 4560
169 클라우드 기반의 재해복구 시스템 (DRS system) 구축 분석에 대한 논문 가을의 곰을... 2012.08.20 4576
168 오픈 소스 클라우드 분석 file 가을의 곰을... 2012.08.20 3896
167 Visual studio 2010 에서 소스파일을 UTF-8 로 자동변환 file 가을의 곰을... 2012.08.19 3622
» HTML5로 자신만의 3D 엔진을 만들자 file 가을의 곰을... 2012.07.29 8303
165 SQL 쿼리를 NoSQL인 MongoDB 쿼리로 변경하기 가을의 곰을... 2012.07.29 4249
164 GitHub 소개글 - 류광님 가을의 곰을... 2012.07.29 3523
163 GIT Server 구축 : LINUX UBUNTU, MS WIN file 가을의 곰을... 2012.07.28 5786
162 git 서버 설치 요령 file 가을의 곰을... 2012.07.28 4020
161 mongoDB와 PHP 참고자료 가을의 곰을... 2012.07.15 4845
160 MongoDB와 PHP 연동 가을의 곰을... 2012.07.15 4767
159 phpmyadmin과 비슷하게 mongoDB를 php 서버로 관리하기 위한 phpMoAdmin file 가을의 곰을... 2012.07.15 10935
158 Hadoop을 이용한 분산 데이터 처리, Part 3: 애플리케이션 개발 file 가을의 곰을... 2012.07.15 3577
157 Hadoop을 이용한 분산 데이터 처리, Part 2: 추가 주제 file 가을의 곰을... 2012.07.15 3979
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED