|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
6 r9 ?% \) v T5 P0 C
7 N2 ?) V* x- b* Z8 Q7 M2 N1 ]废话不多说,官网自己百度下, threejs.org ,入门很简单。
" @9 |# F* B+ j( a O' ^# A7 h2 f% A* o- p5 U* h
创建的javascript脚本如下2 S3 t6 F- \9 E3 f
7 h6 \2 ]) J1 w4 H
, z) n7 R; J+ K4 r6 ]
[mw_shl_code=javascript,true]
$ n( `% e4 x8 x/ x+ tvar scene = new THREE.Scene();/ `; l, x$ N6 d6 z
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
5 p3 T X* E5 Y# [: W3 x Ivar renderer = new THREE.WebGLRenderer();
- s, q) _# h0 q% jrenderer.setSize(window.innerWidth, window.innerHeight);9 L2 T2 D9 g z) a" `! T H
document.body.appendChild(renderer.domElement);! S' G$ k2 H$ B, @: s: E& J$ |
$ v4 _& i! J$ J6 w
// Refresh Issue
2 @0 ?- E6 g1 x5 C, r5 K8 a& vwindow.addEventListener('resize', function () {
% O- w3 ~( p2 w+ s6 V- `# ? var width = this.window.innerWidth;
+ v5 M' m( D& x) m+ x. D, B var height = this.window.innerHeight;
0 e* I3 j1 A! y$ \/ s2 T: k render.setSize(width, height);+ C* f2 r; d Q" V* U
camera.aspect = width / height;
+ a9 z& H; G: R9 W) Q* | camera.updateProjectionMatrix();* I o" X: `3 P
})
8 h [# [. J; ^' x! E, |8 N6 k: e1 u% k+ E3 S+ z( T% ~4 z+ Y7 g
//orbit contorls
0 C% z2 n' k3 n c$ b3 hcontrols = new THREE.OrbiTControls(camera,renderer.domElement);1 f5 G4 e! |: t' a, \9 E
1 _ _8 e3 A" u6 o( {1 g
//create the shape4 V+ {4 N/ n: i/ U F. z! a
var geometry = new THREE.TorusGeometry(2, 1, 16, 100);
( r( `3 y- `) @6 M* u) P# g//create a meterial7 l5 y9 y3 ?. h; L; U
var material = new THREE.MeshBasicMaterial({ color: 0xffff00, wireframe: true, });
# r/ ^' H( F9 V0 \6 x//var myMaterial = [new THREE.MeshBasicMaterial({map:new THREE.TextureLoader().load('image/plmhome.png'),side:THREE.DoubleSide}),];) S' d& t; d5 H p+ w
//var material = new THREE.MeshFaceMaterial(myMaterial);5 h, x% ~% |3 Y( a3 D% X" G; z
0 k' J/ }2 D* J, A4 g7 M5 Hvar cube = new THREE.Mesh(geometry, material);, ^! E( H6 ]: g; ?$ O6 N6 S P. I
scene.add(cube); h! u) }2 j. i3 U
camera.position.z = 5;" }( L- t; q) l( d; f
% \' o6 N+ e5 _/ ?3 _* m
& R1 [4 d" p# m
7 q' G- R* R2 H# K& L
$ B" Q& X( T3 `: G% _ R9 m# N1 l1 M' P+ U7 N
//rotate + `8 r" q7 x& e( v+ k1 A
var update = function () {2 _; y( w4 k. o. w' h9 h
cube.rotation.x += .01;
. {5 k$ X2 @4 {% y/ e( G4 O6 V cube.rotation.y += .02;
! V$ q6 M' z: n6 ^0 P* I cube.rotation.z += .02;
. o% _) p, Z6 ^! x1 x- i5 d- Q0 c}) K. W5 r/ M- @+ U! W6 n0 c3 p& z( ?
# w' m, p7 H: @9 B1 M S! S& |
var render = function () {( T0 X' J) _& ]" U
renderer.render(scene, camera);% c# l- @" H+ L# A1 M& M( G- K3 r; O
}
0 U7 g, h. x4 H3 b q8 @" e
' A% _0 |: E+ M3 o! v* h `4 J
2 A. l& j5 c. F% ^$ o6 k
8 s0 l, U- b7 gvar Gameloop = function () {
: S- I7 o- f) ?9 m requestAnimationFrame(Gameloop);
1 {2 o4 b$ ?- y" ~: b update();
6 h3 ~; G$ V8 l) b9 f$ E render();2 C! p. ]' v) r
}+ V" x& U% ?$ ~
2 v$ i+ s: {7 z
Gameloop();- V2 v, r- [9 N8 {' H- ^6 F
[/mw_shl_code]. N- l1 O# h9 l
) p0 V+ g0 H1 p! Y
1 y. Z' b- I1 [4 c1 J0 n# w! [5 O$ A引入争取的html,
6 A" N" L2 W8 m+ i5 Z0 `
, {9 S. @3 b7 x! ?3 G: q# K[mw_shl_code=html,true]<!DOCTYPE html>
! H7 k4 x: y2 |/ Z" B3 p1 P2 w<html>9 ~8 ?/ O% v, ]
1 O# {1 o. T, H. ?<head>
1 @3 W8 N% s) G& q% i <title>hello ThreeJS</title>
7 J% e7 O' [8 f/ v g <style>. X# s- J. T# k) P. [' Z1 Q4 u% [
body {7 ?) d6 m2 {7 t7 B6 J( c
margin: 0;6 L; L5 ^: _; ^
}/ a$ G8 a8 G3 t' I/ L. u3 k
4 w! y2 h) `6 Q/ C, h5 P canvas {
. s( `8 M T7 G+ ?- }( ~! ` width: 100%;$ O+ ?9 k0 O: q9 L
height: 100%;
8 K: i0 a0 N( Z; A5 Z }# c$ W( v' y. ]+ {9 ?5 Y3 ?
/ x: Y! i8 L" s* c9 @ i h1 {
) h8 O3 W% X: U J; z% e color: aqua;
( R8 Z2 G' R& h" i- v }, _1 ~. V4 ~/ k" D
</style>
B0 ] j. S5 X0 w& B</head>
0 V# m: i. j$ F4 {! }$ a' ^& F; m* n7 c) @% ~2 ?
<body>, {. ^* i) C3 e1 U! m. n
<script src="js/three.js"></script>
7 ]( N6 C) g2 e: t% { <script src="js/OrbitControls.js"></script>
' K" V W/ o2 B% X. h <script src="myjs.js"></script>% I/ }1 r0 [1 [
6 Z7 ?* d6 V5 W3 t" C! f X9 D
- V* g6 A. _ y0 `# j6 l+ }; K' H2 n1 I( q' E' v
+ a: D3 g+ U* i, M9 ]- ]2 W1 f</body>
& r3 A5 W+ w; F$ k8 [( y6 j
6 ~$ s7 s8 X/ k0 _</html>[/mw_shl_code]- l& p; H4 s7 y1 I
; K2 m: m; j; m/ J1 R" v. d
效果点击这里:0 J, _4 B2 k9 g0 N' z+ ]
6 s5 E& j4 b: I+ s* `% Y
http://plmhome.com/doteam/lesson1.html
+ c" c. s$ L Z$ J6 v" {: H( R* R8 `. S
( l& ~) ^- q e- S
|
|