|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
% J$ j( U, |2 J, h4 N7 J
' L! _% m% q0 x: B$ K0 R废话不多说,官网自己百度下, threejs.org ,入门很简单。5 n% `7 F3 N- Z0 \+ ~
: D9 E4 [& G7 U创建的javascript脚本如下
% ?) F* P9 ?1 Z. ?/ \- R- [' z/ ?/ \$ T# F6 m
' `# L0 E9 }8 i/ E% {8 M" N
[mw_shl_code=javascript,true]9 R% c( c" V5 Z z4 C: M' g
var scene = new THREE.Scene();2 @2 f. V& }: m t9 q
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);$ t- g9 ^) ^4 C! L) y
var renderer = new THREE.WebGLRenderer();
* b4 a. D* l* \) Lrenderer.setSize(window.innerWidth, window.innerHeight);/ R2 |, o: u' X( W, |
document.body.appendChild(renderer.domElement);
$ Z' k8 u& ^; X* G
. e' x/ {: L5 L$ ]6 o// Refresh Issue
# N7 Y4 S; k! _. @window.addEventListener('resize', function () {
; A" e' X% ^8 ^, B( h7 ^+ \ var width = this.window.innerWidth;
! E% c: k/ b5 [ var height = this.window.innerHeight;' J7 N$ c" y/ d6 G7 T1 ~' @+ V2 S9 _
render.setSize(width, height);0 ?0 N4 p$ _* }, G* q# R( Q
camera.aspect = width / height;
# z* `6 F% R, \4 b7 s1 j; @ camera.updateProjectionMatrix();$ d( w5 a8 M$ @
})
, [8 \( e: l8 @1 ?6 X7 v4 g" S- a4 F
//orbit contorls
5 c: E9 R' y0 z+ E& hcontrols = new THREE.OrbiTControls(camera,renderer.domElement);* C0 ]& J& F' `9 N
' d: P" C& r+ @" v3 H//create the shape& p$ N5 `! B" z* t
var geometry = new THREE.TorusGeometry(2, 1, 16, 100);
7 c4 U3 J: O% K% w+ Q' a//create a meterial1 H$ y Y- r) _3 v; E" L
var material = new THREE.MeshBasicMaterial({ color: 0xffff00, wireframe: true, });1 @2 k% @. L1 r
//var myMaterial = [new THREE.MeshBasicMaterial({map:new THREE.TextureLoader().load('image/plmhome.png'),side:THREE.DoubleSide}),];# v: d7 c* f& T1 P1 m+ |
//var material = new THREE.MeshFaceMaterial(myMaterial);
7 [% s1 c2 F% L4 `: o" f t3 P/ n8 I0 Q' Y4 F8 h( _* Z: k1 \
var cube = new THREE.Mesh(geometry, material);+ z3 S$ y0 {/ h/ I! t j t
scene.add(cube);
/ x4 }* w8 M/ y; V! {camera.position.z = 5;7 \$ T; R$ |: f9 C8 b. p
% P" @$ P1 z2 O3 U1 x
9 L' c( l( W) @+ I6 U' \6 Z, q' q1 ?4 I* r+ t' j
; ?/ Y: u7 W+ p" a5 E
, W5 R7 ]" |9 i( ]6 y
//rotate
/ v: V3 J4 I! gvar update = function () {
3 f& }% V" C0 Z cube.rotation.x += .01;
) ^3 u' z0 F1 f4 E% W F- _ cube.rotation.y += .02; w: R8 [7 R: w
cube.rotation.z += .02;, M. g% L% g% I" Z, z
}, Y) B! u5 Z: C- Q9 z
+ m: b: K) m7 R/ x2 h% i+ P: i
var render = function () {8 b% ]( I: G6 P3 w+ f" y4 c% r
renderer.render(scene, camera);* s# b, M* C) Y5 n3 Q* L8 b! D' _, ^
}5 L$ ^. P0 M; l0 ]
4 _) }$ i1 e6 N* A6 V$ X8 `" o1 L$ s, ]9 V
6 w) o" {& g3 T' bvar Gameloop = function () {
7 X' m. H1 J/ x, A9 A requestAnimationFrame(Gameloop);
" O2 V% X L( s8 j% r update();
7 l7 s5 B& @" K! T! D1 X render();
$ U3 n9 @* n" _3 Y9 _# E}
( f! O* d/ f8 ]: k6 n5 K0 W3 E# s, K8 D: W, {: r' [! m3 T( b4 w
Gameloop(); D6 \: b2 z0 A* y" d0 o
[/mw_shl_code]$ u4 x2 S; x8 f
& J0 o& g3 ^* ]" k& p5 ^; ?* Y0 D% @& o( h" O- t
引入争取的html,
# s5 W* y. Z! g3 \7 ?4 {1 b1 S1 F4 f2 |/ D3 S Z$ Y, c
[mw_shl_code=html,true]<!DOCTYPE html>( t5 o8 D: ^7 X; Z; N- G1 f8 a1 P
<html>2 I+ q( U* h6 c9 H9 X: @7 e1 ]
' `4 T- O" k: n1 T0 K. M" y
<head>; m6 k# d: K+ D: T: B
<title>hello ThreeJS</title>
. D* Q5 z4 M! k* W, u9 B <style>- O8 R' Z2 O4 C4 ] v
body {1 | r4 y1 U' A" V) g" H; o4 I
margin: 0;; E" l' A' G9 H# f! e
}
" Y# W" f/ c% E% A
4 Z2 y1 Z8 R' w canvas {. i1 k' j! O( I. H% ?
width: 100%;
2 m/ j9 y& ]4 d6 ] height: 100%;& w5 _ a/ [4 M+ o
}/ `- K. z$ Y. I# ^/ J" Q* |! `
! K( S, j9 O; P. a4 Z6 Z& S
h1 {* @" B7 U# f" n& K$ P, T3 c
color: aqua;. I8 o2 ]4 Y6 C7 ~7 Y
}
2 R! E9 H, k- F* e w) j, i </style>
" j. F6 \3 U4 N</head>0 G& x1 h o) w1 E1 {# \7 [8 m! H }
5 M9 i- d( N n+ J<body>- |4 D# j0 n2 C m
<script src="js/three.js"></script>
2 F4 U, R$ {8 h1 a f4 a$ r; e5 p <script src="js/OrbitControls.js"></script>
( n2 h) d: k$ g* g# n <script src="myjs.js"></script>
$ b7 V' Q! S9 ^: k4 A& R) q
, p: M1 Q$ j+ t. N( e6 c' ~# s1 ~% d# O+ y0 k$ Z" r
1 S- ^( i @& N3 v# ]) D: W# m# `6 t# L3 d: b. Q/ |
</body>
% Y; h. ]: e3 V& q% F
1 J/ N* N5 q3 I$ x0 |5 i</html>[/mw_shl_code]
0 ~- R3 `" E1 S f1 N
4 n, O" U, H2 [8 g. b效果点击这里:2 P6 Q/ {+ E6 L" I `: f7 S
9 j1 `( l8 V; Ihttp://plmhome.com/doteam/lesson1.html
0 M/ F# }" K$ s' p6 x, D
5 Z* E- O7 t+ f. s3 w
( x$ [- K0 s+ y7 ^, E7 ^& z/ X |
|