-
Notifications
You must be signed in to change notification settings - Fork 2
/
basic.html
172 lines (137 loc) · 5.55 KB
/
basic.html
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8" />
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<link href="./styles.css" rel="stylesheet" type="text/css">
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.js"></script>
<script src="./examples/js/controls/OrbitControls.js"></script>
</head>
<body>
<h1>three.js with pyscript </h1>
<div id="container">
</div>
<py-script>
# inspired by https://github.com/simondevyoutube/ThreeJS_Tutorial_BasicWorld/blob/main/main.js
from pyodide import create_proxy, to_js
from js import THREE, window, document, Array
import math
container = document.querySelector('#container')
class BasicWorld:
def __init__(self, container):
self.container = container
self.renderer = self.setup_renderer()
self.container.append(self.renderer.domElement)
self.camera = self.setup_camera()
self.scene = THREE.Scene.new()
self.scene.add(self.setup_directional_light())
self.scene.add(THREE.AmbientLight.new(0x101010))
self.scene.add(self.setup_plane())
self.scene.add(self.setup_box())
self.setup_background_single()
self.controls = self.setup_controls()
window.addEventListener('resize',create_proxy(self.onWindowResize) , False)
self.animate()
def setup_renderer(self):
renderer = THREE.WebGLRenderer.new(to_js({
"antialias": True
}))
renderer.shadowMap.enabled = True
renderer.shadowMap.type = THREE.PCFSoftShadowMap
renderer.setSize(self.container.clientWidth, self.container.clientHeight)
renderer.setPixelRatio(window.devicePixelRatio)
return renderer
def setup_camera(self):
fov = 60
aspect = self.container.clientWidth/self.container.clientHeight
near = 1.0
far = 10000.0
camera = THREE.PerspectiveCamera.new(fov, aspect, near, far)
camera.position.set(75,20,0)
return camera
def setup_directional_light(self):
light = THREE.DirectionalLight.new(0xffffff,1.0)
light.position.set(20,100,10)
light.target.position.set(0,0,0)
light.castShadow = True
light.shadow.bias = -0.001
light.shadow.mapSize.width = 2048
light.shadow.mapSize.height = 2048
light.shadow.camera.near = 0.1
light.shadow.camera.far = 500.0
light.shadow.camera.near = 0.5
light.shadow.camera.far = 500.0
light.shadow.camera.left = 100
light.shadow.camera.right = -100
light.shadow.camera.top = 100
light.shadow.camera.bottom = -100
return light
def setup_plane(self):
plane = THREE.Mesh.new(
THREE.PlaneGeometry.new(100, 100, 10, 10),
THREE.MeshStandardMaterial.new(to_js({
"color": 0xFFFFFF
})))
plane.castShadow = False
plane.receiveShadow = True
plane.rotation.x = -math.pi / 2
return plane
def setup_box(self):
box = THREE.Mesh.new(
THREE.BoxGeometry.new(8,8,8),
THREE.MeshStandardMaterial.new(
to_js(
{"color": "blue"}
)
)
)
box.position.set(0,10,0)
box.castShadow = True
box.receiveShadow = True
return box
def setup_background(self):
loader = THREE.CubeTextureLoader.new()
texture = loader.load(to_js([
"./sky_image/skybox_front.png",
"./sky_image/skybox_back.png",
"./sky_image/skybox_up.png",
"./sky_image/skybox_down.png",
"./sky_image/skybox_right.png",
"./sky_image/skybox_left.png"
]))
self.scene.background = texture
def setup_background_single(self):
ft = THREE.TextureLoader.new().load("./sky_image/skybox_front.png")
bk = THREE.TextureLoader.new().load("./sky_image/skybox_back.png")
up = THREE.TextureLoader.new().load("./sky_image/skybox_up.png")
dn = THREE.TextureLoader.new().load("./sky_image/skybox_down.png")
rt = THREE.TextureLoader.new().load("./sky_image/skybox_right.png")
lf = THREE.TextureLoader.new().load("./sky_image/skybox_left.png")
textures = [ft, bk, up, dn, rt, lf]
materials = Array.new()
for i in textures:
mat = THREE.MeshBasicMaterial.new()
mat.map = i
mat.side = THREE.BackSide
materials.push(mat)
skyboxGeo = THREE.BoxGeometry.new(10000, 10000, 10000)
skybox = THREE.Mesh.new(skyboxGeo, materials)
self.scene.add(skybox)
def setup_controls(self):
controls = THREE.OrbitControls.new(self.camera, self.renderer.domElement )
controls.target.set(0,20,0)
controls.update()
return controls
def onWindowResize(self,event):
self.camera.aspect = self.container.clientWidth/self.container.clientHeight
self.camera.updateProjectionMatrix()
self.renderer.setSize(self.container.clientWidth, self.container.clientHeight)
def animate(self,*args):
window.requestAnimationFrame(create_proxy(self.animate) )
self.controls.update()
self.renderer.render( self.scene, self.camera )
app = BasicWorld(container)
</py-script>
</body>
</html>