-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathjsonloader.py
175 lines (170 loc) · 7.5 KB
/
jsonloader.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
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
173
174
175
from __future__ import print_function
from panda3d.core import *
import json
#import os
import sys
from direct.stdpy.file import exists, open
from direct.actor.Actor import Actor
from vfx_loader import createEffect
# Python 3
if sys.version_info >= (3, 0):
unicode = str
def loadModel(file, collision=None, animation=None):
model=None
if animation:
model=Actor(file, animation)
#default anim
if 'default' in animation:
model.loop('default')
elif 'idle' in animation:
model.loop('idle')
else: #some random anim
model.loop(list(animation.items())[0])
else:
model=loader.loadModel(file)
model.setPythonTag('model_file', file)
#load shaders
for geom in model.findAllMatches('**/+GeomNode'):
if geom.hasTag('light'):
model.setPythonTag('hasLight', True)
if geom.hasTag('particle'):
file='particle/'+geom.getTag('particle')
if exists(file):
with open(file) as f:
values=json.load(f)
p=createEffect(values)
model.setPythonTag('particle', p)
p.start(parent=model, renderParent=render)
if geom.hasTag('cg_shader'):
geom.setShader(loader.loadShader("shaders/"+geom.getTag('cg_shader')))
elif geom.hasTag('glsl_shader'):
glsl_shader=geom.getTag('glsl_shader')
geom.setShader(Shader.load(Shader.SLGLSL, "shaders/{0}_v.glsl".format(glsl_shader),"shaders/{0}_f.glsl".format(glsl_shader)))
else:
#geom.setShader(loader.loadShader("shaders/default.cg"))
geom.setShader(Shader.load(Shader.SLGLSL, "shaders/default_v.glsl","shaders/default_f.glsl"))
#collisions
model.setCollideMask(BitMask32.allOff())
if collision:
coll=loader.loadModel(collision)
coll.reparentTo(model)
coll.find('**/collision').setCollideMask(BitMask32.bit(2))
coll.find('**/collision').setPythonTag('object', model)
if animation:
model.setPythonTag('actor_files', [file,animation,collision])
else:
try:
model.find('**/collision').setCollideMask(BitMask32.bit(2))
model.find('**/collision').setPythonTag('object', model)
except:
print("WARNING: Model {0} has no collision geometry!\nGenerating collision sphere...".format(file))
bounds=model.getBounds()
radi=bounds.getRadius()
cent=bounds.getCenter()
coll_sphere=model.attachNewNode(CollisionNode('collision'))
coll_sphere.node().addSolid(CollisionSphere(cent[0],cent[1],cent[2], radi))
coll_sphere.setCollideMask(BitMask32.bit(2))
coll_sphere.setPythonTag('object', model)
#coll_sphere.show()
if animation:
model.setPythonTag('actor_files', [file,animation,None])
return model
def LoadScene(file, quad_tree, actors, terrain, textures, current_textures, grass, grass_tex, current_grass_tex, flatten=False):
json_data=None
if not exists(file+'.json'):
return None
with open(file+'.json') as f:
json_data=json.load(f)
return_data=[]
for object in json_data:
print(".", end=' ')
model=None
if 'textures' in object:
i=0
for tex in object['textures']:
if tex in textures:
if current_textures:
id=textures.index(tex)
current_textures[i]=id
terrain.setTexture(terrain.findTextureStage('tex{0}'.format(i+1)), loader.loadTexture(tex), 1)
#normal texture should have the same name but should be in '/normal/' directory
normal_tex=tex.replace('/diffuse/','/normal/')
terrain.setTexture(terrain.findTextureStage('tex{0}n'.format(i+1)), loader.loadTexture(normal_tex), 1)
else:
print("WARNING: texture '{0}' not found!".format(tex))
i+=1
continue
elif 'grass' in object:
i=0
for tex in object['grass']:
if tex in grass_tex:
if current_grass_tex:
id=grass_tex.index(tex)
current_grass_tex[i]=id
grs_tex=loader.loadTexture(tex)
grs_tex.setWrapU(Texture.WMClamp)
grs_tex.setWrapV(Texture.WMClamp)
grass.setTexture(grass.findTextureStage('tex{0}'.format(i+1)), grs_tex, 1)
else:
print("WARNING: grass texture '{0}' not found!".format(tex))
i+=1
continue
elif 'model' in object:
model=loadModel(object['model'])
elif 'actor' in object:
model=loadModel(object['actor'],object['actor_collision'],object['actor_anims'])
actors.append(model)
else:
return_data.append(object)
if model:
model.reparentTo(quad_tree[object['parent_index']])
model.setPythonTag('props', object['props'])
model.setHpr(render,object['rotation_h'],object['rotation_p'],object['rotation_r'])
model.setPos(render,object['position_x'],object['position_y'],object['position_z'])
if 'color_r' in object:
model.setPythonTag('light_color', [object['color_r'],object['color_g'],object['color_b']])
model.setScale(object['scale'])
if flatten:
for node in quad_tree:
flat=render.attachNewNode('flatten')
for child in node.getChildren():
if child.getPythonTag('props')=='': #objects with SOME properties should be keept alone
child.clearPythonTag('model_file')
child.clearPythonTag('props')
child.clearModelNodes()
child.wrtReparentTo(flat)
flat.flattenStrong()
flat.wrtReparentTo(node)
return return_data
def SaveScene(file, quad_tree, extra_data=None):
export_data=[]
if extra_data:
for item in extra_data:
export_data.append(item)
for node in quad_tree:
for child in node.getChildren():
temp={}
if child.hasPythonTag('actor_files'):
temp['actor']=unicode(child.getPythonTag('actor_files')[0])
temp['actor_anims']=child.getPythonTag('actor_files')[1]
temp['actor_collision']=child.getPythonTag('actor_files')[2]
elif child.hasPythonTag('model_file'):
temp['model']=unicode(child.getPythonTag('model_file'))
if child.hasPythonTag('light_color'):
c=child.getPythonTag('light_color')
temp['color_r']=c[0]
temp['color_g']=c[1]
temp['color_b']=c[2]
temp['rotation_h']=child.getH(render)
temp['rotation_p']=child.getP(render)
temp['rotation_r']=child.getR(render)
temp['position_x']=child.getX(render)
temp['position_y']=child.getY(render)
temp['position_z']=child.getZ(render)
temp['scale']=child.getScale()[0]
temp['parent_name']=node.getName()
temp['parent_index']=quad_tree.index(node)
temp['props']=unicode(child.getPythonTag('props'))
export_data.append(temp)
with open(file, 'w') as outfile:
json.dump(export_data, outfile, indent=4, separators=(',', ': '), sort_keys=True)