Skip to content

Commit b21ddc1

Browse files
committed
Shadows WIP
1 parent c03eb4c commit b21ddc1

File tree

13 files changed

+673
-170
lines changed

13 files changed

+673
-170
lines changed

examples/Shadows.ipynb

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"from pythreejs import *\n",
10+
"import ipywidgets\n",
11+
"from IPython.display import display"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": null,
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"view_width = 800\n",
21+
"view_height = 600"
22+
]
23+
},
24+
{
25+
"cell_type": "code",
26+
"execution_count": null,
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"sphere = Mesh(\n",
31+
" SphereBufferGeometry(1, 32, 16),\n",
32+
" MeshStandardMaterial(color='red')\n",
33+
")"
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": null,
39+
"metadata": {},
40+
"outputs": [],
41+
"source": [
42+
"cube = Mesh(\n",
43+
" BoxBufferGeometry(1, 1, 1),\n",
44+
" MeshPhysicalMaterial(color='green'),\n",
45+
" position=[2, 0, 4]\n",
46+
")"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": null,
52+
"metadata": {},
53+
"outputs": [],
54+
"source": [
55+
"plane = Mesh(\n",
56+
" PlaneBufferGeometry(10, 10),\n",
57+
" MeshPhysicalMaterial(color='gray'),\n",
58+
" position=[0, -2, 0],)\n",
59+
"plane.rotation = (-3.14/2, 0, 0, 'XYZ')"
60+
]
61+
},
62+
{
63+
"cell_type": "code",
64+
"execution_count": null,
65+
"metadata": {},
66+
"outputs": [],
67+
"source": [
68+
"camera = PerspectiveCamera( position=[10, 6, 10], aspect=view_width/view_height)\n",
69+
"key_light = SpotLight(position=[0, 10, 10], angle = 0.3, penumbra = 0.1)\n",
70+
"key_light.target = cube\n",
71+
"ambient_light = AmbientLight()"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": null,
77+
"metadata": {},
78+
"outputs": [],
79+
"source": [
80+
"scene = Scene(children=[sphere, cube, plane, camera, key_light, ambient_light])\n",
81+
"controller = OrbitControls(controlling=camera)\n",
82+
"renderer = Renderer(camera=camera, scene=scene, controls=[controller],\n",
83+
" width=view_width, height=view_height, antialias=True)"
84+
]
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": null,
89+
"metadata": {},
90+
"outputs": [],
91+
"source": [
92+
"renderer"
93+
]
94+
},
95+
{
96+
"cell_type": "code",
97+
"execution_count": null,
98+
"metadata": {},
99+
"outputs": [],
100+
"source": [
101+
"# Turn on shadows in the renderer\n",
102+
"renderer.shadowMap.enabled = True\n",
103+
"renderer.shadowMap.type = 'PCFSoftShadowMap' # default PCFShadowMap"
104+
]
105+
},
106+
{
107+
"cell_type": "code",
108+
"execution_count": null,
109+
"metadata": {},
110+
"outputs": [],
111+
"source": [
112+
"# Enable shadows for the light\n",
113+
"key_light.castShadow = True\n",
114+
"# Enable objects for casting/receiving shadows:\n",
115+
"sphere.castShadow = True\n",
116+
"cube.castShadow = True\n",
117+
"plane.receiveShadow = True"
118+
]
119+
},
120+
{
121+
"cell_type": "code",
122+
"execution_count": null,
123+
"metadata": {},
124+
"outputs": [],
125+
"source": [
126+
"cube.position = (0, 1, 2)"
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"execution_count": null,
132+
"metadata": {},
133+
"outputs": [],
134+
"source": [
135+
"# Also enable sphere to receive shadow:\n",
136+
"sphere.receiveShadow = True\n",
137+
"sphere.material.needsUpdate = True"
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": null,
143+
"metadata": {},
144+
"outputs": [],
145+
"source": [
146+
"key_light.shadow.mapSize = (2048, 2048)"
147+
]
148+
},
149+
{
150+
"cell_type": "code",
151+
"execution_count": null,
152+
"metadata": {},
153+
"outputs": [],
154+
"source": [
155+
"repr(key_light.shadow.camera)"
156+
]
157+
},
158+
{
159+
"cell_type": "code",
160+
"execution_count": null,
161+
"metadata": {},
162+
"outputs": [],
163+
"source": []
164+
}
165+
],
166+
"metadata": {
167+
"kernelspec": {
168+
"display_name": "Python 3",
169+
"language": "python",
170+
"name": "python3"
171+
},
172+
"language_info": {
173+
"codemirror_mode": {
174+
"name": "ipython",
175+
"version": 3
176+
},
177+
"file_extension": ".py",
178+
"mimetype": "text/x-python",
179+
"name": "python",
180+
"nbconvert_exporter": "python",
181+
"pygments_lexer": "ipython3",
182+
"version": "3.5.4"
183+
}
184+
},
185+
"nbformat": 4,
186+
"nbformat_minor": 2
187+
}

js/scripts/generate-wrappers.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,18 @@ var IGNORE_FILES = [
5959
'**/materials/MeshDistanceMaterial.js', // TODO: Undocumented as of yet
6060
'**/renderers/WebGLRenderer.js', // For now, the internals of the webgl
6161
'**/renderers/WebGL2Renderer.js', // render is not exposed.
62-
'**/renderers/webgl/**',
62+
//'**/renderers/webgl/**',
63+
'**/renderers/webgl/WebGLAttributes.js',
64+
'**/renderers/webgl/WebGLBackground.js',
65+
'**/renderers/webgl/WebGLClipping.js',
66+
'**/renderers/webgl/WebGLFlareRenderer.js',
67+
'**/renderers/webgl/WebGLMorphtargets.js',
68+
'**/renderers/webgl/WebGLRenderLists.js',
69+
'**/renderers/webgl/WebGLSpriteRenderer.js',
70+
'**/renderers/webgl/WebGLTextures.js',
71+
'**/renderers/webgl/WebGLUniforms.js',
72+
'**/renderers/webgl/WebGLUtils.js',
73+
'**/renderers/webvr/**',
6374
'**/renderers/shaders/**',
6475
'**/extras/core/Interpolations.js', // Only functions, nothing to export
6576
'**/extras/core/PathPrototype.js', // Sub-part of one object, ignore

js/scripts/three-class-config.js

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -470,16 +470,19 @@ module.exports = {
470470
relativePath: './lights/DirectionalLight',
471471
superClass: 'Light',
472472
properties: {
473-
target: new Types.InitializedThreeType('Object3D', {args: '()', nullable: false}),
474-
// TODO: shadows
475-
// shadow: new Types.ThreeType('LightShadow'),
476-
castsShadow: new Types.Bool(false),
473+
target: new Types.InitializedThreeType('Object3D', null, {args: '()', nullable: false}),
474+
shadow: new Types.InitializedThreeType('DirectionalLightShadow', 'LightShadow', {args: '()', nullable: false}),
477475
},
478476
constructorArgs: [ 'color', 'intensity' ],
479-
propsDefinedByThree: [ 'target' ]
477+
propsDefinedByThree: [ 'target', 'shadow' ]
480478
},
481479
DirectionalLightShadow: {
482480
relativePath: './lights/DirectionalLightShadow',
481+
superClass: 'LightShadow',
482+
properties: {
483+
// TODO: Fix this
484+
camera: new Types.InitializedThreeType('OrthographicCamera', 'Camera', {args: '()'}),
485+
},
483486
},
484487
HemisphereLight: {
485488
relativePath: './lights/HemisphereLight',
@@ -500,6 +503,15 @@ module.exports = {
500503
},
501504
LightShadow: {
502505
relativePath: './lights/LightShadow',
506+
properties: {
507+
// TODO: Fix this
508+
camera: new Types.ThreeType('Camera', {args: '()'}),
509+
bias: new Types.Float(0),
510+
mapSize: new Types.Vector2(512, 512),
511+
radius: new Types.Float(1)
512+
},
513+
constructorArgs: [ 'camera' ],
514+
propsDefinedByThree: [ 'camera' ],
503515
},
504516
PointLight: {
505517
relativePath: './lights/PointLight',
@@ -508,10 +520,10 @@ module.exports = {
508520
power: new Types.Float(4.0 * Math.PI),
509521
distance: new Types.Float(0.0),
510522
decay: new Types.Float(1.0),
511-
// TODO: shadows
512-
// shadow: new Types.ThreeType('LightShadow'),
523+
shadow: new Types.InitializedThreeType('LightShadow', null, {args: '()'}),
513524
},
514525
constructorArgs: [ 'color', 'intensity', 'distance', 'decay' ],
526+
propsDefinedByThree: [ 'shadow' ],
515527
},
516528
RectAreaLight: {
517529
relativePath: './lights/RectAreaLight',
@@ -521,19 +533,23 @@ module.exports = {
521533
relativePath: './lights/SpotLight',
522534
superClass: 'Light',
523535
properties: {
524-
target: new Types.InitializedThreeType('Object3D', {args: '()', nullable: false}),
536+
target: new Types.InitializedThreeType('Object3D', null, {args: '()', nullable: false}),
525537
distance: new Types.Float(0.0),
526538
angle: new Types.Float(Math.PI / 3.0),
527539
penumbra: new Types.Float(0.0),
528540
decay: new Types.Float(1.0),
529-
// TODO: shadows
530-
// shadow: new Types.ThreeType('LightShadow'),
541+
shadow: new Types.InitializedThreeType('SpotLightShadow', 'LightShadow', {args: '()'}),
531542
},
532543
constructorArgs: [ 'color', 'intensity', 'distance', 'angle', 'penumbra', 'decay' ],
533-
propsDefinedByThree: [ 'target' ]
544+
propsDefinedByThree: [ 'target', 'shadow' ]
534545
},
535546
SpotLightShadow: {
536547
relativePath: './lights/SpotLightShadow',
548+
superClass: 'LightShadow',
549+
properties: {
550+
// TODO: Fix this
551+
camera: new Types.InitializedThreeType('PerspectiveCamera', 'Camera', {args: '()'}),
552+
},
537553
},
538554
AnimationLoader: {
539555
relativePath: './loaders/AnimationLoader',
@@ -1160,6 +1176,21 @@ module.exports = {
11601176
},
11611177
WebGLRenderer: {
11621178
relativePath: './renderers/WebGLRenderer',
1179+
properties: {
1180+
clippingPlanes: new Types.ThreeTypeArray('Plane'),
1181+
gammaFactor: new Types.Float(2),
1182+
gammaInput: new Types.Bool(false),
1183+
gammaOutput: new Types.Bool(false),
1184+
localClippingEnabled: new Types.Bool(false),
1185+
physicallyCorrectLights: new Types.Bool(false),
1186+
// Note: shadow map has custom handling in `Renderable` base class
1187+
shadowMap: new Types.ThreeType('WebGLShadowMap'),
1188+
sortObjects: new Types.Bool(false),
1189+
toneMapping: new Types.Enum('ToneMappings', 'LinearToneMapping'),
1190+
toneMappingExposure: new Types.Float(1.0),
1191+
toneMappingWhitepoint: new Types.Float(1.0),
1192+
},
1193+
propsDefinedByThree: ['shadowMap']
11631194
},
11641195
Fog: {
11651196
relativePath: './scenes/Fog',

0 commit comments

Comments
 (0)