-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl_geometry_dynamic.py
108 lines (76 loc) · 2.85 KB
/
webgl_geometry_dynamic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
<title>three.js webgl - geometry - dynamic</title>
"""
import random
import math
import sys
from datetime import datetime
from THREE import *
from THREE.pyOpenGL.pyOpenGL import *
from THREE.controls.FirstPersonControls import *
class Params:
def __init__(self):
self.camera = None
self.scene = None
self.renderer = None
self.container = None
self.mesh = None
self.texture = None
self.geometry = None
self.material = None
self.worldWidth = 128
self.worldDepth = 128
self.worldHalfWidth = self.worldWidth / 2
self.worldHalfDepth = self.worldDepth / 2
self.clock = THREE.Clock()
self.controls = None
def init(p):
p.container = pyOpenGL(p)
p.camera = THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 20000 )
p.camera.position.y = 200
p.controls = FirstPersonControls( p.camera, p.container )
p.controls.movementSpeed = 500
p.controls.lookSpeed = 0.1
p.scene = THREE.Scene()
p.scene.background = THREE.Color( 0xaaccff )
p.scene.fog = THREE.FogExp2( 0xaaccff, 0.0007 )
p.geometry = THREE.PlaneBufferGeometry( 20000, 20000, p.worldWidth - 1, p.worldDepth - 1 )
p.geometry.rotateX( - math.pi / 2 )
positions = p.geometry.attributes.position.array
for i in range(1, len(positions), 3):
positions[i] = 35 * math.sin( i / 2 )
p.texture = THREE.TextureLoader().load( "textures/crate.gif" )
p.texture.wrapS = p.texture.wrapT = THREE.RepeatWrapping
p.texture.repeat.set( 5, 5 )
p.material = THREE.MeshBasicMaterial( { 'color': 0x0044ff, 'map': p.texture, 'wireframe': False} )
p.mesh = THREE.Mesh( p.geometry, p.material )
p.scene.add( p.mesh )
p.renderer = THREE.pyOpenGLRenderer({'antialias': True})
p.renderer.setSize( window.innerWidth, window.innerHeight )
# //
p.container.addEventListener( 'resize', onWindowResize, False )
def onWindowResize(event, p):
p.camera.aspect = window.innerWidth / window.innerHeight
p.camera.updateProjectionMatrix()
p.renderer.setSize( window.innerWidth, window.innerHeight )
p.controls.handleResize(window.innerWidth, window.innerHeight)
def animate(p):
render(p)
def render(p):
delta = p.clock.getDelta()
time = p.clock.getElapsedTime() * 5000
positions = p.geometry.attributes.position.array
k = 0
for i in range(1, len(positions), 3):
positions[i] = 35 * math.sin( k / 5 + ( time + k ) / 7 )
k += 1
p.mesh.geometry.attributes.position.needsUpdate = True
p.controls.update( delta * 500)
p.renderer.render( p.scene, p.camera )
def main(argv=None):
params = Params()
init(params)
params.container.addEventListener( 'animationRequest', animate)
return params.container.loop()
if __name__ == "__main__":
sys.exit(main())