|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
3 s% n- i6 |5 f4 M
8 Q4 [3 q* X+ N9 Q E6 o7 M8 a# E废话不多说,官网自己百度下, threejs.org ,入门很简单。
8 ?$ u5 F! {+ y& a: @5 E+ P9 M
( B) i6 s& e$ Q1 z创建的javascript脚本如下$ |1 X8 y0 A+ `' p( x
2 F3 D8 Y! f7 n N- N
5 S& i# f; s- |9 M' y3 _[mw_shl_code=javascript,true]8 @: }$ i- E% p
var scene = new THREE.Scene();
7 a, U1 q" w0 y5 P- N, }var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
; A7 o) {1 f# ~1 a' w0 X$ m2 [var renderer = new THREE.WebGLRenderer();
7 G! ~5 t7 m3 ?3 o4 K3 A: V2 A/ grenderer.setSize(window.innerWidth, window.innerHeight);
( [* w; u5 j9 B5 O+ Q+ Bdocument.body.appendChild(renderer.domElement);; M8 i! H1 p$ J! a# Z- M1 E0 D
f6 e" r- L2 `// Refresh Issue - P* X |& c$ y& R l
window.addEventListener('resize', function () {
% y: b: R( C' B* w var width = this.window.innerWidth;. p3 d5 Q0 r, n' w
var height = this.window.innerHeight;
7 z. u4 V. f$ _( s5 D$ m/ g* M& D render.setSize(width, height);
. I( W& u& l/ G8 K1 ~ camera.aspect = width / height;
- R. b% s) X6 c6 u) Y camera.updateProjectionMatrix();
D! T: _7 }' C5 X. q0 x; l})
$ c, W# ?; f8 g2 O; y4 O6 w5 h0 r' Z: f1 {
//orbit contorls
. [/ a A# y, K! x: Y$ y) e0 Jcontrols = new THREE.OrbiTControls(camera,renderer.domElement);
4 d! D3 b# j" K7 N! i4 }1 l- S; K4 a F
//create the shape
W. }: c% h& |4 p1 Mvar geometry = new THREE.TorusGeometry(2, 1, 16, 100);) r% f. C/ v! u1 K
//create a meterial
: L: t% t2 c- P9 t) lvar material = new THREE.MeshBasicMaterial({ color: 0xffff00, wireframe: true, });3 P- e" f* T- O
//var myMaterial = [new THREE.MeshBasicMaterial({map:new THREE.TextureLoader().load('image/plmhome.png'),side:THREE.DoubleSide}),];! D2 ?( i# w6 w' @) {9 s7 ]
//var material = new THREE.MeshFaceMaterial(myMaterial);9 E/ k! q% D( B% C) n) I
+ \ K* |; W+ K$ x" R* O5 V9 }3 cvar cube = new THREE.Mesh(geometry, material);
w: s0 d+ z; V ~scene.add(cube);! {' a, Z8 P% ^" u( M
camera.position.z = 5;
: Y( b0 n" `$ T8 x
, F/ q2 B; ] f6 J7 @- T1 l$ \7 s4 T2 ?) y/ X r
; Q g' r5 O1 U( L' w
+ E7 J0 h& G& w$ e W; _$ @) \/ e6 V$ I! X
//rotate
2 [8 h, t- K. C" R9 g8 V0 u5 Z, Ovar update = function () {3 r2 d. d% _. n$ h
cube.rotation.x += .01;
( G/ x- ^- U/ { `+ W cube.rotation.y += .02;
; f8 e I" d" d% \1 Z cube.rotation.z += .02;
5 Z: t; ~ a3 D4 G}9 y8 k% l( ~& p- G$ _7 t
4 n3 h, S% B. A: t" G; M) D
var render = function () {/ k4 y, b( j. t+ U. c) ^4 E
renderer.render(scene, camera);
& m- e& k \. {- t1 S: M1 r& r}
l' u- T8 q3 s; B7 B ?/ H4 L5 t: d
# n# z, y+ w1 Q3 w/ F' r1 ~) Y$ Q: w, u
var Gameloop = function () {9 |' W5 u0 t+ ^) C. ~9 z
requestAnimationFrame(Gameloop);# x7 d/ h, v4 v. S4 b# V
update();+ p- X" ~' [6 z9 {2 L* j* ^
render();
V2 y, b) H( h% `" Q4 T1 K}: [: w& S) T9 b* G$ L
& u# j" G% d# Z% \& [# b* AGameloop();
$ k+ i8 n5 ?. v[/mw_shl_code]
# E9 H5 Z. \' Z$ v9 g( b+ @4 g) K/ }1 ~7 F6 Z, P0 |
" }7 ^; Z) o! ?( [
引入争取的html,9 V) T$ e& i8 Z4 a
8 j! N: W* n& k
[mw_shl_code=html,true]<!DOCTYPE html>/ j$ v8 B+ s9 e) N4 Q
<html>2 M6 d: ~0 O: h0 ]+ ]
% w' H* n3 \% P8 r8 V7 i- t
<head>! b$ m1 K& O* N& l# {5 T0 {
<title>hello ThreeJS</title> ^5 ~1 m. x& L- x$ J s( S
<style>2 D8 g$ K4 b( [8 u9 _: k5 Q* N
body {
" X( u0 r. K) {- Y" q margin: 0;
" x0 h5 A; P+ v- K }
0 l1 o9 v& F- S7 a5 I L# Z! q* {, a# w, M; }/ d
canvas {9 |+ ~1 k# v4 _9 u7 x( ~
width: 100%;3 _- y# e/ q; }3 w+ A; ]
height: 100%;2 m9 d4 J, ]9 N4 K: H
}
- o# E+ _/ Y0 i9 l- J" G
& ^- G4 {4 }. t) i+ k% O D; a, V h1 {
* c9 H: n( J3 \3 J. w color: aqua;
' X g ]9 q! N, V }
: n' i# N3 g6 m. |" ^0 H' U8 i </style>9 s5 ?4 ?8 m3 ]/ a5 M* \
</head>
# ]( h5 F5 m3 _4 U6 t2 w* t, ~6 k0 Y& p2 _8 R7 ^: b" I( `) W
<body>) N) M0 g% B$ Q: F: @; Q0 p$ n7 {! p
<script src="js/three.js"></script>
9 g) W* o' _ Q8 ~# |# g1 f <script src="js/OrbitControls.js"></script>
9 B0 x! W- ~ M) N! `) Q <script src="myjs.js"></script># L" u, O1 U$ m! _) N
* T7 n7 C: @$ r# i
) o7 Y, F5 Y' }- i# D* z& s
& h( b ]5 k. u; W: j5 n
/ q% s4 v" Y! b& q) |$ @+ i7 u</body>8 l) P1 I, |6 @4 T
- A7 e4 A: f2 _+ c; c; n% ` J( i
</html>[/mw_shl_code]
/ ~$ h! {( l( l+ u
7 a+ e' P2 T3 S6 j( S效果点击这里:
/ d1 y) r8 b' y2 z) r8 W. }& Y2 ~
http://plmhome.com/doteam/lesson1.html- V1 \7 q7 p% r
+ [! V/ ]2 b0 X1 }( O- f
# y: _$ d, f9 _, O
|
|