|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
9 l4 x! o4 k! D
$ m" L- |3 g: L
废话不多说,官网自己百度下, threejs.org ,入门很简单。
/ B7 S# e A4 l4 D
& x' N9 L5 W+ z) u% p6 z创建的javascript脚本如下
M' G0 }* q. K$ E* @4 x2 `2 }. t' V% ^; t/ @7 ]
0 {$ u/ C( q. t5 Q7 u6 Y
[mw_shl_code=javascript,true], D; s0 e# l- K: U) C% w1 P2 W
var scene = new THREE.Scene();6 u* R0 C5 V7 U, B# R
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
* H: s! \1 B7 fvar renderer = new THREE.WebGLRenderer();
! E* e1 R# k8 S! b; T3 Mrenderer.setSize(window.innerWidth, window.innerHeight);( Y) x7 M! j* |+ W
document.body.appendChild(renderer.domElement);
" J* N9 k) Q& g( H0 I
7 c+ K# V0 d9 L* e// Refresh Issue : _( B) C: R- g/ C |& e5 ]+ S1 R
window.addEventListener('resize', function () {" i( V0 U" Y, P; y+ r1 v
var width = this.window.innerWidth;
' \* D, H1 m; {+ E! I9 m3 X+ Z' ~* ^$ N var height = this.window.innerHeight;" A2 R6 M: n0 |
render.setSize(width, height);+ x4 G5 | N- _( Z7 \, s
camera.aspect = width / height;
7 j' d5 C* r7 O0 x9 K camera.updateProjectionMatrix();
3 o' a9 `+ w3 K$ P$ Q}) n' t& F1 d- y; d3 c9 Y) D' x
; o# x- p; |. m# X2 ~//orbit contorls
: r7 t4 Z; x7 h+ e/ }) P% acontrols = new THREE.OrbiTControls(camera,renderer.domElement);
) S1 O; A% J5 a6 G3 O6 j: S% [) h; B* R. x" K
//create the shape% P& Z. x+ N5 `, [) V# V& Q8 c
var geometry = new THREE.TorusGeometry(2, 1, 16, 100);( Y8 k6 ~8 ^/ l% W9 j4 A
//create a meterial
j4 Z4 h) G; F; V. ?, c2 I) |8 yvar material = new THREE.MeshBasicMaterial({ color: 0xffff00, wireframe: true, });
1 Z, D6 y' V) o0 M//var myMaterial = [new THREE.MeshBasicMaterial({map:new THREE.TextureLoader().load('image/plmhome.png'),side:THREE.DoubleSide}),];
1 V; J# K }4 X/ s' b8 Q//var material = new THREE.MeshFaceMaterial(myMaterial);8 Q7 A- }: o$ V
; W. v7 J/ O' o3 a1 Jvar cube = new THREE.Mesh(geometry, material);
' K5 V; F: W8 L3 {scene.add(cube);- y8 Q) H# r& O5 c# O4 x
camera.position.z = 5;& q1 Z# {% M: y7 l5 F% S
8 z; e5 g+ I/ h
! A1 S/ q$ x$ v$ z' k, {% i' C; e) c
1 Z+ L& f5 ^( F& j" V7 E" m& k3 y# b( O/ p: |- t
//rotate
( ~6 R) k2 z0 u- d( hvar update = function () {8 J \% n. F/ E% }. K6 z
cube.rotation.x += .01;6 G j% M2 t* {! X# K* X
cube.rotation.y += .02;0 d# R- e/ g! s, |4 A
cube.rotation.z += .02;! d2 [: t9 k& x* X" ~, E
}
8 ~# [* [. ?+ o. z0 P% q$ B! O: [2 O" p6 w3 T
var render = function () {
& L: |. m8 q! H& F5 p% N5 d renderer.render(scene, camera);
( Q- ]" z' S, _5 z; [8 W- @}: W, Q+ y1 ?. W. b$ d
1 h: _, ^' F. U7 W
) N- N, j$ [2 ^* h: K+ c5 o7 o0 y5 v: |. y( @0 l7 l3 ~
var Gameloop = function () {
" P: w1 T* Y7 F8 G2 } T requestAnimationFrame(Gameloop);
& m% K; m3 O: T7 ] update();. E+ ?2 o9 K5 O7 F3 b! i8 U1 \
render(); _8 [) P8 x$ X
} U* f8 C+ c+ T, ` X* X
0 u, a- I1 b9 O# K8 y# s* a; ]
Gameloop();( @! }/ O6 c2 D5 m) Z7 w3 W$ K
[/mw_shl_code]5 L" z4 G% g3 V( J# Q! y2 p3 Y
% Q6 @1 L6 d" b
* Q4 r3 C* u8 m2 H n
引入争取的html,% H3 c: A$ B# ]: H; J- A( F" ?
( e+ h. `3 q+ h2 e& h% y[mw_shl_code=html,true]<!DOCTYPE html>
0 j! v; I8 g) c& Q' I<html>
) v% ~( I+ L4 }3 j, [5 g
7 F: n" T6 t+ O/ k) j5 f) j* e<head># {3 l, u, D0 V2 L
<title>hello ThreeJS</title>% z+ P% ~; ^$ Z% n/ R5 ~
<style>
* W. k& |( I5 k' n- t+ g( h body {
$ A* d" Z2 m" W9 ]" L- h margin: 0;
0 D: g9 I0 u+ h- [) @ }
( l3 k! s, G! U6 _
* } P, A$ ~4 E; k0 z4 n canvas {
( M( B7 b/ b5 X5 g7 ]- q1 h width: 100%;+ x8 V* u* M* s7 J
height: 100%;4 S& D$ j8 d: { y' R
}4 A! X! R) h. b' o0 Y! j
4 d) u# |3 [/ k7 ?4 j) k3 r h1 {. y$ h$ [) F# a7 x
color: aqua;
$ c) G1 ~' e5 f2 H }9 z, y) Q, ^) R* t/ b' b
</style>! @; Y; V' ?& G7 X+ E0 e) _
</head>
, H% j# W! i$ }( B/ J& |+ {; T: P) k* X& x% c0 S0 i
<body>
1 Y0 c" w B4 T8 x7 V+ d% b <script src="js/three.js"></script>
. D, K& ?5 v5 u8 t9 t) e2 u0 F* L5 P <script src="js/OrbitControls.js"></script>$ E$ u' g1 J, _9 F9 P
<script src="myjs.js"></script>
3 j4 e2 X; Y. Q5 }2 x. c & ^# }1 M6 j+ t0 |5 y7 l2 E! z! {3 L* i
. F: K! P8 z2 C' N0 j' u# n3 t) G+ z+ G. [, ~8 T! @& y& O
+ s; v5 O' V1 F* L- k) `& I' j9 b8 b
</body>4 n2 T: w J: S" Y8 N: ^
# F7 E& b" C: R2 _! ^4 x</html>[/mw_shl_code]
2 |. M$ C% w! P) O* i4 U
, F- X! J( r H8 Y; q; e# a效果点击这里:
7 |4 ?" S2 @7 Q4 w# H% o$ H3 Z2 x8 o/ A9 [
http://plmhome.com/doteam/lesson1.html M9 e. X, T! z$ T
; I( N7 T6 p, V& @* Z$ F
& D2 G5 K' p1 w8 F: ^0 c6 t0 s+ y
|
|