Compare commits
2 Commits
d132e6a015
...
e244664b32
Author | SHA1 | Date | |
---|---|---|---|
e244664b32 | |||
1c801ee605 |
|
@ -1,6 +1,7 @@
|
|||
import bpy
|
||||
|
||||
from .util import escape_name, mat2str, needs_def_mb, needs_xform_mb, ExportCancelled
|
||||
from mathutils import Vector, Matrix
|
||||
|
||||
def make_object_data_cache(render_engine, depsgraph, ob, name):
|
||||
if ob.type == 'MESH':
|
||||
|
@ -18,6 +19,9 @@ class Mesh:
|
|||
"""
|
||||
def __init__(self, render_engine, depsgraph, ob, name):
|
||||
self.name = name
|
||||
self.material_name = None
|
||||
if len(ob.material_slots) >= 1 and ob.material_slots[0].material != None:
|
||||
self.material_name = ob.material_slots[0].material.name
|
||||
self.is_subdiv = ob.data.psychopath.is_subdivision_surface
|
||||
self.needs_mb = needs_def_mb(ob)
|
||||
self.time_meshes = []
|
||||
|
@ -36,13 +40,17 @@ class Mesh:
|
|||
|
||||
if self.is_subdiv == False:
|
||||
# Exporting normal mesh
|
||||
w.write("MeshSurface $%s {\n" % self.name)
|
||||
w.write("MeshSurface $%s {\n" % escape_name(self.name))
|
||||
w.indent()
|
||||
else:
|
||||
# Exporting subdivision surface cage
|
||||
w.write("SubdivisionSurface $%s {\n" % self.name)
|
||||
w.write("SubdivisionSurface $%s {\n" % escape_name(self.name))
|
||||
w.indent()
|
||||
|
||||
# Material bindings.
|
||||
if self.material_name != None:
|
||||
w.write("SurfaceShaderBind [${}]\n".format(escape_name(self.material_name)))
|
||||
|
||||
# Write vertices and (if it's smooth shaded) normals
|
||||
for ti in range(len(self.time_meshes)):
|
||||
w.write("Vertices [")
|
||||
|
@ -94,7 +102,7 @@ class SphereLamp:
|
|||
def export(self, render_engine, w):
|
||||
render_engine.update_stats("", "Psychopath: Exporting %s" % self.name)
|
||||
|
||||
w.write("SphereLight $%s {\n" % self.name)
|
||||
w.write("SphereLight $%s {\n" % escape_name(self.name))
|
||||
w.indent()
|
||||
for col in self.time_col:
|
||||
if col[0] == 'Rec709':
|
||||
|
@ -132,14 +140,14 @@ class RectLamp:
|
|||
self.time_dim += [(ob.data.size, ob.data.size_y)]
|
||||
else:
|
||||
self.time_dim += [(ob.data.size, ob.data.size)]
|
||||
|
||||
|
||||
def cleanup(self):
|
||||
pass
|
||||
|
||||
def export(self, render_engine, w):
|
||||
render_engine.update_stats("", "Psychopath: Exporting %s" % self.ob.name)
|
||||
render_engine.update_stats("", "Psychopath: Exporting %s" % self.name)
|
||||
|
||||
w.write("RectangleLight $%s {\n" % self.name)
|
||||
w.write("RectangleLight $%s {\n" % escape_name(self.name))
|
||||
w.indent()
|
||||
for col in self.time_col:
|
||||
if col[0] == 'Rec709':
|
||||
|
@ -175,8 +183,11 @@ class DistantDiskLamp:
|
|||
|
||||
self.time_rad += [ob.data.shadow_soft_size]
|
||||
|
||||
def cleanup(self):
|
||||
pass
|
||||
|
||||
def export(self, render_engine, w):
|
||||
render_engine.update_stats("", "Psychopath: Exporting %s" % self.ob.name)
|
||||
render_engine.update_stats("", "Psychopath: Exporting %s" % escape_name(self.name))
|
||||
w.write("DistantDiskLight $%s {\n" % self.name)
|
||||
w.indent()
|
||||
for direc in self.time_dir:
|
||||
|
@ -193,35 +204,3 @@ class DistantDiskLamp:
|
|||
|
||||
w.unindent()
|
||||
w.write("}\n")
|
||||
|
||||
|
||||
# class Instance:
|
||||
# def __init__(self, render_engine, depsgraph, ob, data_name):
|
||||
# self.ob = ob
|
||||
# self.data_name = data_name
|
||||
# self.needs_mb = needs_xform_mb(self.ob)
|
||||
# self.time_xforms = []
|
||||
|
||||
# def take_sample(self, render_engine, time, translation_offset):
|
||||
# if len(self.time_xforms) == 0 or self.needs_mb:
|
||||
# render_engine.update_stats("", "Psychopath: Collecting '{}' xforms at time {}".format(self.ob.name, time))
|
||||
# mat = self.ob.matrix_world.copy()
|
||||
# mat[0][3] += translation_offset[0]
|
||||
# mat[1][3] += translation_offset[1]
|
||||
# mat[2][3] += translation_offset[2]
|
||||
# self.time_xforms += [mat]
|
||||
|
||||
# def export(self, render_engine, w):
|
||||
# render_engine.update_stats("", "Psychopath: Exporting %s" % self.ob.name)
|
||||
|
||||
# w.write("Instance {\n")
|
||||
# w.indent()
|
||||
# w.write("Data [$%s]\n" % self.data_name)
|
||||
# for mat in self.time_xforms:
|
||||
# w.write("Transform [%s]\n" % mat2str(mat))
|
||||
# for ms in self.ob.material_slots:
|
||||
# if ms != None:
|
||||
# w.write("SurfaceShaderBind [$%s]\n" % escape_name(ms.material.name))
|
||||
# break
|
||||
# w.unindent()
|
||||
# w.write("}\n")
|
||||
|
|
|
@ -2,6 +2,7 @@ import bpy
|
|||
|
||||
from math import log
|
||||
|
||||
from .material import Material
|
||||
from .objects import make_object_data_cache, Mesh, DistantDiskLamp
|
||||
from .util import escape_name, mat2str, ExportCancelled
|
||||
from .world import World, Camera
|
||||
|
@ -53,6 +54,9 @@ class PsychoExporter:
|
|||
self.sun_lamp_data = {} # name -> cached_data
|
||||
self.sun_lamp_instances = {} # instance_id -> [sun_lamp_data_name, transform_list]
|
||||
|
||||
# For all materials.
|
||||
self.materials = {} # name -> cached_data
|
||||
|
||||
# Motion blur segments are rounded down to a power of two.
|
||||
if self.scene.psychopath.motion_blur_segments > 0:
|
||||
self.time_samples = (2**int(log(self.scene.psychopath.motion_blur_segments, 2))) + 1
|
||||
|
@ -124,6 +128,21 @@ class PsychoExporter:
|
|||
self.w.unindent()
|
||||
self.w.write("}\n")
|
||||
|
||||
#------------------------------------------------------
|
||||
# Collect materials.
|
||||
|
||||
# TODO: handle situations where there are more than one
|
||||
# material with the same name. This can happen through
|
||||
# library linking.
|
||||
|
||||
for inst in self.depsgraph.object_instances:
|
||||
ob = inst.object
|
||||
if ob.type in ['MESH']:
|
||||
for ms in ob.material_slots:
|
||||
if ms.material != None:
|
||||
if ms.material.name not in self.materials:
|
||||
self.materials[ms.material.name] = Material(self.render_engine, self.depsgraph, ms.material)
|
||||
|
||||
#------------------------------------------------------
|
||||
# Collect world and object data.
|
||||
|
||||
|
@ -133,7 +152,10 @@ class PsychoExporter:
|
|||
if self.render_engine.test_break():
|
||||
raise ExportCancelled()
|
||||
|
||||
time = self.fr + self.shutter_start + (self.shutter_diff*i)
|
||||
subframe = self.shutter_start + (self.shutter_diff*i)
|
||||
time = self.fr + subframe
|
||||
self.depsgraph.scene.frame_set(self.fr, subframe=subframe)
|
||||
self.depsgraph.update()
|
||||
|
||||
# Collect camera and world data.
|
||||
self.camera.take_sample(self.render_engine, self.depsgraph, time)
|
||||
|
@ -152,13 +174,22 @@ class PsychoExporter:
|
|||
# We use this a couple of times, so make a shorthand.
|
||||
is_sun_lamp = inst.object.type == 'LIGHT' and inst.object.data.type == 'SUN'
|
||||
|
||||
# TODO: handle situations where there are more than one
|
||||
# object with the same name. This can happen through
|
||||
# library linking.
|
||||
|
||||
# Get a unique id for the instance. This is surprisingly
|
||||
# tricky, because the instance's "persistent_id" property
|
||||
# isn't globally unique, as I would have expected from
|
||||
# the documentation.
|
||||
id = None
|
||||
if inst.is_instance:
|
||||
id = (hash((inst.object.name, inst.parent.name)), inst.persistent_id)
|
||||
id = (
|
||||
hash((inst.object.name, inst.parent.name)),
|
||||
# Has to be turned into a tuple, otherwise it doesn't
|
||||
# work as part of the ID for some reason.
|
||||
tuple(inst.persistent_id),
|
||||
)
|
||||
else:
|
||||
id = inst.object.name
|
||||
|
||||
|
@ -195,6 +226,30 @@ class PsychoExporter:
|
|||
self.w.write("Assembly {\n")
|
||||
self.w.indent()
|
||||
|
||||
# Export materials.
|
||||
for name in self.materials:
|
||||
self.materials[name].export(self.render_engine, self.w)
|
||||
|
||||
# Export objects.
|
||||
for name in self.object_data:
|
||||
self.object_data[name].export(self.render_engine, self.w)
|
||||
|
||||
# Export instances.
|
||||
for id in self.instances:
|
||||
[obj_name, xforms] = self.instances[id]
|
||||
self.render_engine.update_stats("", "Psychopath: Exporting %s instance" % obj_name)
|
||||
|
||||
prefix = str(hex(hash(id)))
|
||||
name = "inst_{}__{}".format(prefix, escape_name(obj_name))
|
||||
|
||||
self.w.write("Instance {\n")
|
||||
self.w.indent()
|
||||
self.w.write("Data [${}]\n".format(escape_name(obj_name)))
|
||||
for mat in xforms:
|
||||
self.w.write("Transform [{}]\n".format(mat2str(mat)))
|
||||
self.w.unindent()
|
||||
self.w.write("}\n")
|
||||
|
||||
self.w.unindent()
|
||||
self.w.write("}\n")
|
||||
finally:
|
||||
|
|
|
@ -65,7 +65,7 @@ class Camera:
|
|||
mat = self.ob.matrix_world.copy()
|
||||
matz = Matrix()
|
||||
matz[2][2] = -1
|
||||
self.xforms += [(mat * matz).inverted()]
|
||||
self.xforms += [(mat @ matz).inverted()]
|
||||
|
||||
def cleanup(self):
|
||||
pass
|
||||
|
|
|
@ -257,7 +257,7 @@ impl<'a> Surface for RectangleLight<'a> {
|
|||
_local_ray: &LocalRay,
|
||||
space: &XformFull,
|
||||
isect: &mut SurfaceIntersection,
|
||||
_shader: &dyn SurfaceShader,
|
||||
_shaders: &[&dyn SurfaceShader],
|
||||
) {
|
||||
let time = ray.time;
|
||||
|
||||
|
|
|
@ -207,7 +207,7 @@ impl<'a> Surface for SphereLight<'a> {
|
|||
local_ray: &LocalRay,
|
||||
space: &XformFull,
|
||||
isect: &mut SurfaceIntersection,
|
||||
_shader: &dyn SurfaceShader,
|
||||
_shaders: &[&dyn SurfaceShader],
|
||||
) {
|
||||
let time = ray.time;
|
||||
|
||||
|
|
|
@ -50,23 +50,6 @@ pub fn parse_assembly<'a>(
|
|||
child.iter_leaf_children_with_type("Data").nth(0).unwrap().1
|
||||
};
|
||||
|
||||
// Get surface shader binding, if any.
|
||||
let surface_shader_name = if child
|
||||
.iter_leaf_children_with_type("SurfaceShaderBind")
|
||||
.count()
|
||||
> 0
|
||||
{
|
||||
Some(
|
||||
child
|
||||
.iter_leaf_children_with_type("SurfaceShaderBind")
|
||||
.nth(0)
|
||||
.unwrap()
|
||||
.1,
|
||||
)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// Get xforms
|
||||
let mut xforms = Vec::new();
|
||||
for (_, contents, _) in child.iter_leaf_children_with_type("Transform") {
|
||||
|
@ -75,7 +58,7 @@ pub fn parse_assembly<'a>(
|
|||
|
||||
// Add instance
|
||||
if builder.name_exists(name) {
|
||||
builder.add_instance(name, surface_shader_name, Some(&xforms));
|
||||
builder.add_instance(name, Some(&xforms));
|
||||
} else {
|
||||
return Err(PsyParseError::InstancedMissingData(
|
||||
child.iter_leaf_children_with_type("Data").nth(0).unwrap().2,
|
||||
|
@ -113,7 +96,11 @@ pub fn parse_assembly<'a>(
|
|||
{
|
||||
builder.add_object(
|
||||
ident,
|
||||
Object::Surface(arena.alloc(parse_mesh_surface(arena, child)?)),
|
||||
Object::Surface(arena.alloc(parse_mesh_surface(
|
||||
arena,
|
||||
child,
|
||||
&builder.surface_shader_map,
|
||||
)?)),
|
||||
);
|
||||
} else {
|
||||
// TODO: error condition of some kind, because no ident
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#![allow(dead_code)]
|
||||
|
||||
use std::result::Result;
|
||||
use std::{collections::HashMap, result::Result};
|
||||
|
||||
use nom::{sequence::tuple, IResult};
|
||||
|
||||
|
@ -27,7 +27,9 @@ use super::{
|
|||
pub fn parse_mesh_surface<'a>(
|
||||
arena: &'a Arena,
|
||||
tree: &'a DataTree,
|
||||
surface_shader_map: &HashMap<String, usize>,
|
||||
) -> Result<TriangleMesh<'a>, PsyParseError> {
|
||||
let mut shader_idx = None;
|
||||
let mut verts = Vec::new(); // Vec of vecs, one for each time sample
|
||||
let mut normals = Vec::new(); // Vec of vecs, on for each time sample
|
||||
let mut face_vert_counts = Vec::new();
|
||||
|
@ -36,6 +38,20 @@ pub fn parse_mesh_surface<'a>(
|
|||
// TODO: make sure there are the right number of various children,
|
||||
// and other validation.
|
||||
|
||||
// Get surface shader binding, if any.
|
||||
if tree
|
||||
.iter_leaf_children_with_type("SurfaceShaderBind")
|
||||
.count()
|
||||
> 0
|
||||
{
|
||||
let name = tree
|
||||
.iter_leaf_children_with_type("SurfaceShaderBind")
|
||||
.nth(0)
|
||||
.unwrap()
|
||||
.1;
|
||||
shader_idx = surface_shader_map.get(name).map(|i| *i);
|
||||
}
|
||||
|
||||
// Get verts
|
||||
for (_, mut text, _) in tree.iter_leaf_children_with_type("Vertices") {
|
||||
// Collect verts for this time sample
|
||||
|
@ -116,6 +132,7 @@ pub fn parse_mesh_surface<'a>(
|
|||
|
||||
Ok(TriangleMesh::from_verts_and_indices(
|
||||
arena,
|
||||
shader_idx,
|
||||
&verts,
|
||||
&if normals.is_empty() {
|
||||
None
|
||||
|
|
|
@ -142,7 +142,7 @@ pub struct AssemblyBuilder<'a> {
|
|||
|
||||
// Shader list
|
||||
surface_shaders: Vec<&'a dyn SurfaceShader>,
|
||||
surface_shader_map: HashMap<String, usize>, // map Name -> Index
|
||||
pub surface_shader_map: HashMap<String, usize>, // map Name -> Index
|
||||
|
||||
// Object list
|
||||
objects: Vec<Object<'a>>,
|
||||
|
@ -206,12 +206,7 @@ impl<'a> AssemblyBuilder<'a> {
|
|||
self.assemblies.push(asmb);
|
||||
}
|
||||
|
||||
pub fn add_instance(
|
||||
&mut self,
|
||||
name: &str,
|
||||
surface_shader_name: Option<&str>,
|
||||
xforms: Option<&[Xform]>,
|
||||
) {
|
||||
pub fn add_instance(&mut self, name: &str, xforms: Option<&[Xform]>) {
|
||||
// Make sure name exists
|
||||
if !self.name_exists(name) {
|
||||
panic!("Attempted to add instance with a name that doesn't exist.");
|
||||
|
@ -233,12 +228,6 @@ impl<'a> AssemblyBuilder<'a> {
|
|||
Instance {
|
||||
instance_type: InstanceType::Object,
|
||||
data_index: self.object_map[name],
|
||||
surface_shader_index: surface_shader_name.map(|name| {
|
||||
*self
|
||||
.surface_shader_map
|
||||
.get(name)
|
||||
.unwrap_or_else(|| panic!("Unknown surface shader '{}'.", name))
|
||||
}),
|
||||
id: self.instances.len(),
|
||||
transform_indices: xforms
|
||||
.map(|xf| (self.xforms.len(), self.xforms.len() + xf.len())),
|
||||
|
@ -247,12 +236,6 @@ impl<'a> AssemblyBuilder<'a> {
|
|||
Instance {
|
||||
instance_type: InstanceType::Assembly,
|
||||
data_index: self.assembly_map[name],
|
||||
surface_shader_index: surface_shader_name.map(|name| {
|
||||
*self
|
||||
.surface_shader_map
|
||||
.get(name)
|
||||
.unwrap_or_else(|| panic!("Unknown surface shader '{}'.", name))
|
||||
}),
|
||||
id: self.instances.len(),
|
||||
transform_indices: xforms
|
||||
.map(|xf| (self.xforms.len(), self.xforms.len() + xf.len())),
|
||||
|
@ -391,7 +374,6 @@ pub enum Object<'a> {
|
|||
pub struct Instance {
|
||||
pub instance_type: InstanceType,
|
||||
pub data_index: usize,
|
||||
pub surface_shader_index: Option<usize>,
|
||||
pub id: usize,
|
||||
pub transform_indices: Option<(usize, usize)>,
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ pub trait Surface: Boundable + Debug + Sync {
|
|||
local_ray: &LocalRay,
|
||||
space: &XformFull,
|
||||
isect: &mut SurfaceIntersection,
|
||||
shader: &dyn SurfaceShader,
|
||||
shaders: &[&dyn SurfaceShader],
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -6,10 +6,11 @@ use crate::{
|
|||
accel::BVH4,
|
||||
bbox::BBox,
|
||||
boundable::Boundable,
|
||||
color::Color,
|
||||
lerp::lerp_slice,
|
||||
math::{cross, dot, Normal, Point, XformFull},
|
||||
ray::{LocalRay, Ray},
|
||||
shading::SurfaceShader,
|
||||
shading::{SimpleSurfaceShader, SurfaceShader},
|
||||
};
|
||||
|
||||
use super::{triangle, Surface, SurfaceIntersection, SurfaceIntersectionData};
|
||||
|
@ -18,6 +19,7 @@ const MAX_LEAF_TRIANGLE_COUNT: usize = 3;
|
|||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub struct TriangleMesh<'a> {
|
||||
pub shader_idx: Option<usize>,
|
||||
time_sample_count: usize,
|
||||
vertices: &'a [Point], // Vertices, with the time samples for each vertex stored contiguously
|
||||
normals: Option<&'a [Normal]>, // Vertex normals, organized the same as `vertices`
|
||||
|
@ -28,6 +30,7 @@ pub struct TriangleMesh<'a> {
|
|||
impl<'a> TriangleMesh<'a> {
|
||||
pub fn from_verts_and_indices<'b>(
|
||||
arena: &'b Arena,
|
||||
shader_idx: Option<usize>,
|
||||
verts: &[Vec<Point>],
|
||||
vert_normals: &Option<Vec<Vec<Normal>>>,
|
||||
tri_indices: &[(usize, usize, usize)],
|
||||
|
@ -106,6 +109,7 @@ impl<'a> TriangleMesh<'a> {
|
|||
});
|
||||
|
||||
TriangleMesh {
|
||||
shader_idx: shader_idx,
|
||||
time_sample_count: time_sample_count,
|
||||
vertices: vertices,
|
||||
normals: normals,
|
||||
|
@ -128,8 +132,18 @@ impl<'a> Surface for TriangleMesh<'a> {
|
|||
local_ray: &LocalRay,
|
||||
space: &XformFull,
|
||||
isect: &mut SurfaceIntersection,
|
||||
shader: &dyn SurfaceShader,
|
||||
shaders: &[&dyn SurfaceShader],
|
||||
) {
|
||||
let unassigned_shader = SimpleSurfaceShader::Emit {
|
||||
color: Color::new_xyz(color::rec709_to_xyz((1.0, 0.0, 1.0))),
|
||||
};
|
||||
|
||||
let shader = if let Some(idx) = self.shader_idx {
|
||||
shaders[idx]
|
||||
} else {
|
||||
&unassigned_shader
|
||||
};
|
||||
|
||||
self.accel.traverse(ray, local_ray, |idx_range, ray| {
|
||||
// Iterate through the triangles and test the ray against them.
|
||||
let mut non_shadow_hit = false;
|
||||
|
|
|
@ -73,12 +73,11 @@ impl<'a> Tracer<'a> {
|
|||
InstanceType::Object => {
|
||||
self.trace_object(
|
||||
&assembly.objects[inst.data_index],
|
||||
inst.surface_shader_index
|
||||
.map(|i| assembly.surface_shaders[i]),
|
||||
ray,
|
||||
&local_ray,
|
||||
&local_space,
|
||||
isect,
|
||||
assembly.surface_shaders,
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -103,29 +102,19 @@ impl<'a> Tracer<'a> {
|
|||
fn trace_object<'b>(
|
||||
&mut self,
|
||||
obj: &Object,
|
||||
surface_shader: Option<&dyn SurfaceShader>,
|
||||
ray: &mut Ray,
|
||||
local_ray: &LocalRay,
|
||||
space: &XformFull,
|
||||
isect: &mut SurfaceIntersection,
|
||||
shaders: &[&dyn SurfaceShader],
|
||||
) {
|
||||
match *obj {
|
||||
Object::Surface(surface) => {
|
||||
let unassigned_shader = SimpleSurfaceShader::Emit {
|
||||
color: Color::new_xyz(color::rec709_to_xyz((1.0, 0.0, 1.0))),
|
||||
};
|
||||
let shader = surface_shader.unwrap_or(&unassigned_shader);
|
||||
|
||||
surface.intersect_ray(ray, local_ray, space, isect, shader);
|
||||
surface.intersect_ray(ray, local_ray, space, isect, shaders);
|
||||
}
|
||||
|
||||
Object::SurfaceLight(surface) => {
|
||||
// Lights don't use shaders
|
||||
let bogus_shader = SimpleSurfaceShader::Emit {
|
||||
color: Color::new_xyz(color::rec709_to_xyz((1.0, 0.0, 1.0))),
|
||||
};
|
||||
|
||||
surface.intersect_ray(ray, local_ray, space, isect, &bogus_shader);
|
||||
surface.intersect_ray(ray, local_ray, space, isect, shaders);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user