|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
* R6 ?# I! X; ]( O$ j1 \* H
" ^+ r) F- `- v4 h/ G9 r. A, I废话不多说,官网自己百度下, threejs.org ,入门很简单。. O1 d" t3 q/ D* I' d6 g! j, c0 w
, {4 v' H! X: `3 P( H6 Q5 ^( q
创建的javascript脚本如下
: g0 b$ _# M) g9 T0 ^' S+ ^* I% @
, j! d' b$ p6 Z) L, e4 n
[mw_shl_code=javascript,true]( n' E, g; }. z3 u+ s m9 M
var scene = new THREE.Scene();
, q, S/ a3 E2 @0 z. F0 e$ U" Qvar camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
( L; w* {* h6 L: ]# s) |8 Bvar renderer = new THREE.WebGLRenderer();
& L0 U3 ?8 v% O# ^* K3 q; w# d# Qrenderer.setSize(window.innerWidth, window.innerHeight);# F/ _( ~4 U7 j- t. `
document.body.appendChild(renderer.domElement);
( _- M5 V3 K/ u( b6 x2 o: e: p; K6 { s4 c2 W. h/ o# c$ {
// Refresh Issue
8 e4 R% ^9 O' Z, @window.addEventListener('resize', function () {
6 F0 i$ P% t7 V var width = this.window.innerWidth;
) T {6 j: j# A var height = this.window.innerHeight;
$ o# Q7 `9 e+ H: | render.setSize(width, height);0 C/ m; |' g5 z! P' Q8 |
camera.aspect = width / height;# j7 I$ n6 L! R! v6 U% e; a
camera.updateProjectionMatrix();) L7 {+ c+ k2 Z+ R4 Z
})
$ H& m) u4 A3 W' l: x0 d
& _( Q7 j/ O e# j1 _; N F2 r//orbit contorls! Q9 N& T" |0 t$ F% N5 C
controls = new THREE.OrbiTControls(camera,renderer.domElement);4 h+ G$ u9 a! m, ?1 H3 _
[+ h O& N8 K/ T [1 W) k0 V8 ?//create the shape* e2 o* x" J) e9 X7 \/ k* q u
var geometry = new THREE.TorusGeometry(2, 1, 16, 100);
+ l% o3 v) t2 T! `//create a meterial
, X2 x$ m2 V5 rvar material = new THREE.MeshBasicMaterial({ color: 0xffff00, wireframe: true, });1 m7 ~- ~! W- u& C' I+ `" t- }. m; M
//var myMaterial = [new THREE.MeshBasicMaterial({map:new THREE.TextureLoader().load('image/plmhome.png'),side:THREE.DoubleSide}),];
, D. w- ~) E; r. u9 I6 ~6 t//var material = new THREE.MeshFaceMaterial(myMaterial);6 { ^* V8 `3 u" x3 P: X# Z
Z; I1 i7 P: ?5 m0 x1 T
var cube = new THREE.Mesh(geometry, material);5 c% {/ ^6 U7 z+ k4 X
scene.add(cube);; n$ x' N) |' M, G+ g. S
camera.position.z = 5;7 l& J6 K$ f- |4 M! i
( G4 j- G9 L1 E0 n5 }7 O$ g
! V' l1 \9 ]3 A0 U
x6 R# ]) }" Z9 S/ z" q* o# e/ ~/ R5 M$ t+ w
. s$ c$ M4 w) M& K4 W//rotate
b' w4 x7 X& j- j6 A8 ivar update = function () {* g- x& M% ^1 Q9 K$ r
cube.rotation.x += .01;
4 n# i4 V0 ~* h cube.rotation.y += .02;9 i! W6 x. H+ s) J
cube.rotation.z += .02;
6 S$ U/ y1 t4 E7 h7 U% }}& D6 b) E" [) n, k
- n7 Z! e* |" t- j7 [% ovar render = function () {3 e- U5 a( b S u
renderer.render(scene, camera);
1 L0 R) b) O! H" t1 }}
: Z; Q6 T) u# C' t0 s7 [0 ^$ \4 N+ p, M
5 x6 j7 O8 V5 d% ?
4 ~8 n9 p( {5 T; X5 R: _& U# Fvar Gameloop = function () {
7 b5 K9 g" N/ z$ M* o requestAnimationFrame(Gameloop);% N) `1 Z8 |! p% l+ _- P3 F( N
update();* ] Q7 [6 h# t( z0 U
render();$ g6 x( h- Y. e
}
) z% d' Z' h' l6 h# ]% @
W: c8 Y3 G7 L8 X- B3 AGameloop();
& x' {+ h! p6 ^[/mw_shl_code]
1 t2 k- i- C6 U0 _& N! {6 ~! L6 L; S r7 G
$ r. Q7 N0 Y' b2 b, m$ s引入争取的html,
' B0 S& ?6 ~5 C. P3 G# [ p/ c* H: @) g& s* `
[mw_shl_code=html,true]<!DOCTYPE html>' e5 }0 _0 e9 i! z, f- A/ }, b
<html>
* h4 D, Q7 l" L* o; m3 g0 f8 O, U( \
<head>' N- J$ k9 i! P% Z
<title>hello ThreeJS</title>' ^) y2 Q1 u# b2 A
<style>
* { Z K. e# {# ]- g0 j" O body {; Q$ }3 L1 W: n' O0 L- E2 s
margin: 0;( k6 f! s0 J' N9 C9 ^
}
6 E1 I5 D5 o! U7 B- d, F; f) ]* b, D7 @( m+ v
canvas {
# j, I: B4 N/ F& o* R2 q. D width: 100%;9 Z2 L- Z; Y2 u2 }) F# {* \; t
height: 100%;) `+ j: F: E# H" }% p2 A% x
}
+ C# S, b; H& g/ r, f8 T6 W2 q& x
; _1 {2 w2 i0 R3 ` h1 {
! \: l7 C4 ?& \& P/ b color: aqua;
/ w9 r: O+ D2 | \) E: z }3 S) v5 a( T$ p
</style>
9 z3 {) M2 Y- y' X' X- s$ l# X</head>5 N3 ]" h( R5 Y3 t5 ^
5 m" M* |+ j5 U7 g<body>% t6 s3 a4 n5 S8 R6 c
<script src="js/three.js"></script>' |* A) D! D. h
<script src="js/OrbitControls.js"></script>/ r( ]) a$ q, M0 f0 c7 q) `
<script src="myjs.js"></script>
# Z8 ]9 `* l) ^% L
9 w& t6 i4 b; _" y1 S; q" ~2 t5 @, a9 t$ z$ @* z2 Q* n9 x4 m
- p- m- M$ u0 d7 m i5 p2 [6 F7 L0 z
</body>, f) _- I( }) l4 x8 I2 J1 M
' T4 T' r5 o; r
</html>[/mw_shl_code]* `5 k; |$ F, Z- X$ N
/ r: y# M9 C- {
效果点击这里:) i# I. m4 s+ Z' d5 P
4 j0 y' P6 n6 e4 q, j& Fhttp://plmhome.com/doteam/lesson1.html
( p5 R" ?1 Z& ~2 I: h$ N3 J
6 h! i, K0 l; z4 A% q; |# w o5 q. _
$ k: Q' q8 ]$ m) ?4 h3 m! H( i |
|