Skip to content
This repository has been archived by the owner on Dec 24, 2021. It is now read-only.

Commit

Permalink
Merge 'level-normals' branch into master
Browse files Browse the repository at this point in the history
  • Loading branch information
PavelBlend committed Jun 28, 2019
2 parents 2175e7e + 8b5c806 commit 0de9d64
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
9 changes: 8 additions & 1 deletion stalker_tools/level/geom/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from . import format_
from .. import read


try:
from io_scene_xray import xray_io
except ImportError:
Expand Down Expand Up @@ -76,7 +77,13 @@ def vertex_buffers(data, level, fastpath=False):
coord_x, coord_y, coord_z = packed_reader.getf('3f')
vertex_buffer.position.append((coord_x, coord_z, coord_y))
elif format_.usage[usage] == format_.NORMAL:
norm_x, norm_y, norm_z, norm_HZ = packed_reader.getf('4B')
norm_x, norm_y, norm_z, hemi = packed_reader.getf('4B')
vertex_buffer.normal.append((
(2.0 * norm_z / 255.0 - 1.0),
(2.0 * norm_x / 255.0 - 1.0),
(2.0 * norm_y / 255.0 - 1.0)
))
vertex_buffer.colors_hemi.append(hemi / 255)
elif format_.usage[usage] == format_.TEXCOORD:
if format_.types[type] == format_.FLOAT2:
if texcoord == 0:
Expand Down
48 changes: 48 additions & 0 deletions stalker_tools/level/importer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import os
import math

import bpy
import bmesh
Expand Down Expand Up @@ -308,10 +309,12 @@ def import_visuals(level):

vertex_buffer = level.vertex_buffers[visual.gcontainer.vb_index]
vertices = vertex_buffer.position[visual.gcontainer.vb_offset : visual.gcontainer.vb_offset + visual.gcontainer.vb_size]
normals = vertex_buffer.normal[visual.gcontainer.vb_offset : visual.gcontainer.vb_offset + visual.gcontainer.vb_size]
uvs = vertex_buffer.uv[visual.gcontainer.vb_offset : visual.gcontainer.vb_offset + visual.gcontainer.vb_size]
uvs_lmap = vertex_buffer.uv_lmap[visual.gcontainer.vb_offset : visual.gcontainer.vb_offset + visual.gcontainer.vb_size]
colors_light = vertex_buffer.colors_light[visual.gcontainer.vb_offset : visual.gcontainer.vb_offset + visual.gcontainer.vb_size]
colors_sun = vertex_buffer.colors_sun[visual.gcontainer.vb_offset : visual.gcontainer.vb_offset + visual.gcontainer.vb_size]
colors_hemi = vertex_buffer.colors_hemi[visual.gcontainer.vb_offset : visual.gcontainer.vb_offset + visual.gcontainer.vb_size]
for vertex in vertices:
b_mesh.verts.new((vertex[0], vertex[1], vertex[2]))
b_mesh.verts.ensure_lookup_table()
Expand Down Expand Up @@ -369,6 +372,16 @@ def import_visuals(level):
bmesh_color.g = color
bmesh_color.b = color

if colors_hemi:
color_layer = b_mesh.loops.layers.color.new('Hemi')
for face in b_mesh.faces:
for loop in face.loops:
bmesh_color = loop[color_layer]
color = colors_hemi[loop.vert.index]
bmesh_color.r = color
bmesh_color.g = color
bmesh_color.b = color

if colors_light and not bpy_mat.use_nodes:

bpy_mat.use_nodes = True
Expand Down Expand Up @@ -451,6 +464,41 @@ def import_visuals(level):
b_mesh.to_mesh(bpy_mesh)
loaded_visuals[visual_key] = bpy_mesh.name

load_custom_normals = False

if load_custom_normals:
bpy_mesh.normals_split_custom_set_from_vertices(normals=normals)
bpy_mesh.use_auto_smooth = True
bpy_mesh.auto_smooth_angle = math.pi

debug = False

if debug:
v_coords = []
v_norms = []
for v in bpy_mesh.vertices:
v_coords.append(v.co)
v_norms.append(normals[v.index])

locations = []
for co, n in zip(v_coords, v_norms):
location = (
co[0] + n[0] * 2.0,
co[1] + n[1] * 2.0,
co[2] + n[2] * 2.0,
)
locations.append(location)
locations.append(co)

edges = []
for i in range(0, len(locations), 2):
edges.append((i, i + 1))

debug_me = bpy.data.meshes.new('debug')
debug_me.from_pydata(locations, edges, ())
debug_ob = bpy.data.objects.new('debug', debug_me)
bpy.context.scene.objects.link(debug_ob)

else:
bpy_mesh = bpy.data.meshes[bpy_mesh_name]

Expand Down
2 changes: 2 additions & 0 deletions stalker_tools/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ def __init__(self):
class VertexBuffer:
def __init__(self):
self.position = []
self.normal = []
self.uv = []
self.uv_lmap = []
self.colors_light = []
self.colors_sun = []
self.colors_hemi = []


class GeometryContainer:
Expand Down

0 comments on commit 0de9d64

Please sign in to comment.