feat: track remaining production assets, playtest scenes and references
Close the gaps left open after the LFS migration: 177 files that were sitting untracked now enter version control, with every binary going through Git LFS. - labels: milk and orange-juice wrap artwork (SVG/PNG), Seedream sources, prompts and Dreamina job records - orange juice 2L: bottle, cap, empty state and three label variants (blend + glb each) - cup contents: desktop and mobile-optimised variants plus 3 textures - milk 2L: empty and packaged variants with their glb exports - milk frother: low-poly assembly and split base/jug parts - tamper: model, glb and fit test - juicer: seven assembly-part review scenes and their export scripts - playtests: extraction animation scene and grinding/tamping whitebox - reference: Minex page candidates, user review screenshots, sequence analysis frames and tamper STLs - docs: item design draft, bean research notes, region reference and the model-asset audit report Re-renderable preview/check frames stay untracked on purpose.
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,515 @@
|
||||
import bpy
|
||||
import bmesh
|
||||
import hashlib
|
||||
import json
|
||||
import math
|
||||
from pathlib import Path
|
||||
|
||||
import numpy as np
|
||||
from mathutils import Vector
|
||||
from mathutils.bvhtree import BVHTree
|
||||
|
||||
OUT = Path('/Users/yanchenyu/Documents/WebApps/PerfetCoffee/model_assets/coffee_cup')
|
||||
SOURCE = OUT / 'PerfectCoffee_CoffeeCup.blend'
|
||||
TEXTURES = OUT / 'textures'
|
||||
TEXTURES.mkdir(exist_ok=True)
|
||||
SOURCE_SHA256 = hashlib.sha256(SOURCE.read_bytes()).hexdigest()
|
||||
|
||||
SEGMENTS = 96
|
||||
CUP_SEGMENTS = 24
|
||||
CUP_HEIGHT = 0.110
|
||||
CUP_INNER_BOTTOM_RADIUS = 0.028
|
||||
CUP_INNER_TOP_RADIUS = 0.0405
|
||||
CUP_INNER_FLOOR = 0.002
|
||||
LIQUID_BOTTOM = CUP_INNER_FLOOR + 0.00020
|
||||
LIQUID_TOP = 0.10380
|
||||
FOAM_BOTTOM = LIQUID_TOP + 0.00002
|
||||
FOAM_EDGE_TOP = 0.10570
|
||||
FOAM_DOME = 0.00035
|
||||
FIT_CLEARANCE = 0.00012
|
||||
LIQUID_UV_RADIUS = (CUP_INNER_BOTTOM_RADIUS + (CUP_INNER_TOP_RADIUS - CUP_INNER_BOTTOM_RADIUS) * LIQUID_TOP / CUP_HEIGHT) * math.cos(math.pi / CUP_SEGMENTS) - FIT_CLEARANCE
|
||||
FOAM_UV_RADIUS = (CUP_INNER_BOTTOM_RADIUS + (CUP_INNER_TOP_RADIUS - CUP_INNER_BOTTOM_RADIUS) * FOAM_EDGE_TOP / CUP_HEIGHT) * math.cos(math.pi / CUP_SEGMENTS) - FIT_CLEARANCE
|
||||
|
||||
bpy.ops.wm.open_mainfile(filepath=str(SOURCE))
|
||||
bpy.context.preferences.filepaths.save_version = 0
|
||||
scene = bpy.context.scene
|
||||
scene.unit_settings.system = 'METRIC'
|
||||
scene.unit_settings.length_unit = 'METERS'
|
||||
scene.unit_settings.scale_length = 1.0
|
||||
|
||||
lid = bpy.data.objects.get('CC_Lid')
|
||||
if lid is not None:
|
||||
bpy.data.objects.remove(lid, do_unlink=True)
|
||||
for obj in list(bpy.data.collections['Studio_Exclude_From_Export'].objects):
|
||||
bpy.data.objects.remove(obj, do_unlink=True)
|
||||
source_root = bpy.data.objects.get('CC_CoffeeCup_Assembly')
|
||||
if source_root is not None:
|
||||
bpy.data.objects.remove(source_root, do_unlink=True)
|
||||
|
||||
cup = bpy.data.objects['CC_Cup_Body']
|
||||
cup.parent = None
|
||||
cup.location = (0.0, 0.0, 0.0)
|
||||
cup.rotation_euler = (0.0, 0.0, 0.0)
|
||||
model_collection = bpy.data.collections.get('CC_CoffeeCup_Model')
|
||||
model_collection.name = 'CC_CupContents_Preview'
|
||||
studio = bpy.data.collections['Studio_Exclude_From_Export']
|
||||
|
||||
root = bpy.data.objects.new('CC_CupContents_Assembly', None)
|
||||
model_collection.objects.link(root)
|
||||
root.empty_display_type = 'PLAIN_AXES'
|
||||
root.empty_display_size = 0.02
|
||||
root['units'] = 'meters; Z up; liquid +Y corresponds to UV +V'
|
||||
root['source_cup'] = SOURCE.name
|
||||
root['source_cup_sha256'] = SOURCE_SHA256
|
||||
root['liquid_top_m'] = LIQUID_TOP
|
||||
root['foam_edge_top_m'] = FOAM_EDGE_TOP
|
||||
root['foam_dome_m'] = FOAM_DOME
|
||||
root['foam_thickness_mm'] = (FOAM_EDGE_TOP - FOAM_BOTTOM) * 1000.0
|
||||
root['rim_clearance_mm'] = (CUP_HEIGHT - FOAM_EDGE_TOP - FOAM_DOME) * 1000.0
|
||||
root['liquid_material_slot'] = 'CC_Coffee_Liquid'
|
||||
root['foam_material_slot'] = 'CC_Milk_Foam'
|
||||
root['uv_usage'] = 'LiquidSurfaceUV: planar X/Y top projection, center=(0.5,0.5), full top bounds=(0,0)-(1,1)'
|
||||
root['runtime_hint'] = 'Hide CC_Milk_Foam for Americano; override CC_Coffee_Liquid and CC_Milk_Foam materials independently in Godot'
|
||||
|
||||
|
||||
def inner_radius(z):
|
||||
if z <= 0.022:
|
||||
return 0.028 + (z - CUP_INNER_FLOOR) * (0.0305 - 0.028) / (0.022 - CUP_INNER_FLOOR)
|
||||
return 0.0305 + (z - 0.022) * (0.038 - 0.0305) / (0.088 - 0.022)
|
||||
|
||||
|
||||
def fitted_radius(z):
|
||||
return inner_radius(z) * math.cos(math.pi / CUP_SEGMENTS) - FIT_CLEARANCE
|
||||
|
||||
|
||||
def linear_channel(value):
|
||||
return value / 12.92 if value <= 0.04045 else ((value + 0.055) / 1.055) ** 2.4
|
||||
|
||||
|
||||
def rgba_from_hex(value):
|
||||
return tuple(linear_channel(int(value[index:index + 2], 16) / 255.0) for index in (1, 3, 5)) + (1.0,)
|
||||
|
||||
|
||||
def set_input(node, name, value):
|
||||
socket = node.inputs.get(name)
|
||||
if socket is not None:
|
||||
socket.default_value = value
|
||||
|
||||
|
||||
def new_material(name, hex_color, roughness):
|
||||
mat = bpy.data.materials.new(name)
|
||||
mat.use_nodes = True
|
||||
mat.diffuse_color = rgba_from_hex(hex_color)
|
||||
bsdf = mat.node_tree.nodes.get('Principled BSDF')
|
||||
bsdf.inputs['Base Color'].default_value = rgba_from_hex(hex_color)
|
||||
bsdf.inputs['Roughness'].default_value = roughness
|
||||
return mat, bsdf
|
||||
|
||||
|
||||
def micro_bump(mat, bsdf, scale, distance, strength):
|
||||
nodes = mat.node_tree.nodes
|
||||
links = mat.node_tree.links
|
||||
noise = nodes.new('ShaderNodeTexNoise')
|
||||
noise.name = 'Micro_Surface_Only_In_Blender'
|
||||
noise.label = 'Cycles micro detail; use a Godot shader for equivalent detail'
|
||||
noise.inputs['Scale'].default_value = scale
|
||||
noise.inputs['Detail'].default_value = 2.0
|
||||
noise.inputs['Roughness'].default_value = 0.65
|
||||
bump = nodes.new('ShaderNodeBump')
|
||||
bump.inputs['Strength'].default_value = strength
|
||||
bump.inputs['Distance'].default_value = distance
|
||||
links.new(noise.outputs['Fac'], bump.inputs['Height'])
|
||||
links.new(bump.outputs['Normal'], bsdf.inputs['Normal'])
|
||||
noise.location = (-480, -150)
|
||||
bump.location = (-220, -100)
|
||||
|
||||
|
||||
def make_texture(name, pixels):
|
||||
size = pixels.shape[0]
|
||||
image = bpy.data.images.new(name, width=size, height=size, alpha=True)
|
||||
image.colorspace_settings.name = 'sRGB'
|
||||
rgba = np.ones((size, size, 4), dtype=np.float32)
|
||||
rgba[:, :, :3] = np.clip(pixels, 0.0, 1.0)
|
||||
image.pixels.foreach_set(rgba.ravel())
|
||||
image.filepath_raw = str(TEXTURES / (name + '.png'))
|
||||
image.file_format = 'PNG'
|
||||
image.save()
|
||||
image.pack()
|
||||
return image
|
||||
|
||||
|
||||
def attach_albedo(mat, bsdf, image):
|
||||
nodes = mat.node_tree.nodes
|
||||
links = mat.node_tree.links
|
||||
uv = nodes.new('ShaderNodeUVMap')
|
||||
uv.uv_map = 'LiquidSurfaceUV'
|
||||
uv.location = (-680, 220)
|
||||
texture = nodes.new('ShaderNodeTexImage')
|
||||
texture.image = image
|
||||
texture.interpolation = 'Linear'
|
||||
texture.extension = 'EXTEND'
|
||||
texture.location = (-420, 220)
|
||||
links.new(uv.outputs['UV'], texture.inputs['Vector'])
|
||||
links.new(texture.outputs['Color'], bsdf.inputs['Base Color'])
|
||||
bsdf.location = (0, 160)
|
||||
mat.node_tree.nodes.get('Material Output').location = (310, 160)
|
||||
|
||||
|
||||
SIZE = 1024
|
||||
axis = np.linspace(-1.0, 1.0, SIZE, dtype=np.float32)
|
||||
x, y = np.meshgrid(axis, axis)
|
||||
radius = np.sqrt(x * x + y * y)
|
||||
angle = np.arctan2(y, x)
|
||||
rng = np.random.default_rng(4061)
|
||||
noise = (
|
||||
np.sin(x * 21.0 + y * 13.0) * 0.35
|
||||
+ np.sin(x * 57.0 - y * 43.0) * 0.17
|
||||
+ np.cos(x * 109.0 + y * 83.0) * 0.08
|
||||
+ rng.normal(0.0, 0.10, (SIZE, SIZE))
|
||||
)
|
||||
|
||||
coffee_dark = np.array([39.0, 20.0, 9.0]) / 255.0
|
||||
coffee_edge = np.array([87.0, 45.0, 16.0]) / 255.0
|
||||
edge = np.clip((radius - 0.82) / 0.18, 0.0, 1.0) ** 3
|
||||
swirl = (0.5 + 0.5 * np.sin(angle * 3.0 + radius * 38.0)) * np.exp(-((radius - 0.91) / 0.055) ** 2)
|
||||
coffee_pixels = coffee_dark[None, None, :] * (1.0 - edge[:, :, None] * 0.64) + coffee_edge[None, None, :] * edge[:, :, None] * 0.64
|
||||
coffee_pixels += swirl[:, :, None] * np.array([0.020, 0.010, 0.002])[None, None, :]
|
||||
coffee_pixels += noise[:, :, None] * 0.007
|
||||
coffee_image = make_texture('CC_Coffee_Surface', coffee_pixels)
|
||||
|
||||
latte_base = np.array([163.0, 101.0, 51.0]) / 255.0
|
||||
latte_edge = np.array([119.0, 70.0, 31.0]) / 255.0
|
||||
milk = np.array([255.0, 241.0, 214.0]) / 255.0
|
||||
foam_edge = np.clip((radius - 0.80) / 0.20, 0.0, 1.0) ** 2
|
||||
latte_pixels = latte_base[None, None, :] * (1.0 - foam_edge[:, :, None] * 0.52) + latte_edge[None, None, :] * foam_edge[:, :, None] * 0.52
|
||||
latte_pixels += noise[:, :, None] * 0.010
|
||||
|
||||
curve_size = 2048
|
||||
curve_axis = np.linspace(-1.0, 1.0, curve_size, dtype=np.float32)
|
||||
t = np.linspace(0.0, math.tau, 512, endpoint=False)
|
||||
heart_x = 16.0 * np.sin(t) ** 3 / 17.0 * 0.65
|
||||
heart_y = (13.0 * np.cos(t) - 5.0 * np.cos(2.0 * t) - 2.0 * np.cos(3.0 * t) - np.cos(4.0 * t)) / 17.0 * 0.68 + 0.065
|
||||
next_x = np.roll(heart_x, -1)
|
||||
next_y = np.roll(heart_y, -1)
|
||||
heart_large = np.zeros((curve_size, curve_size), dtype=np.float32)
|
||||
for row, y_value in enumerate(curve_axis):
|
||||
crossing = (heart_y > y_value) != (next_y > y_value)
|
||||
x_crossings = heart_x[crossing] + (y_value - heart_y[crossing]) * (next_x[crossing] - heart_x[crossing]) / (next_y[crossing] - heart_y[crossing])
|
||||
x_crossings.sort()
|
||||
for pair in range(0, len(x_crossings), 2):
|
||||
left = np.searchsorted(curve_axis, x_crossings[pair])
|
||||
right = np.searchsorted(curve_axis, x_crossings[pair + 1])
|
||||
heart_large[row, left:right] = 1.0
|
||||
heart = heart_large.reshape(SIZE, 2, SIZE, 2).mean(axis=(1, 3))
|
||||
latte_pixels = latte_pixels * (1.0 - heart[:, :, None]) + milk[None, None, :] * heart[:, :, None]
|
||||
|
||||
for _ in range(100):
|
||||
bubble_angle = rng.uniform(0.0, math.tau)
|
||||
bubble_radius = rng.uniform(0.82, 0.975)
|
||||
bx = math.cos(bubble_angle) * bubble_radius
|
||||
by = math.sin(bubble_angle) * bubble_radius
|
||||
br = rng.uniform(0.0020, 0.0065)
|
||||
distance = np.sqrt((x - bx) ** 2 + (y - by) ** 2)
|
||||
ring = np.exp(-((distance - br) / 0.0016) ** 2) * 0.26
|
||||
latte_pixels = latte_pixels * (1.0 - ring[:, :, None]) + milk[None, None, :] * ring[:, :, None]
|
||||
latte_image = make_texture('CC_Latte_Surface', latte_pixels)
|
||||
|
||||
coffee_mat, coffee_bsdf = new_material('CC_Coffee_Liquid', '#271409', 0.20)
|
||||
set_input(coffee_bsdf, 'IOR', 1.333)
|
||||
set_input(coffee_bsdf, 'Specular IOR Level', 0.32)
|
||||
set_input(coffee_bsdf, 'Coat Weight', 0.18)
|
||||
set_input(coffee_bsdf, 'Coat Roughness', 0.16)
|
||||
attach_albedo(coffee_mat, coffee_bsdf, coffee_image)
|
||||
micro_bump(coffee_mat, coffee_bsdf, 95.0, 0.000015, 0.08)
|
||||
|
||||
foam_mat, foam_bsdf = new_material('CC_Milk_Foam', '#A36533', 0.62)
|
||||
set_input(foam_bsdf, 'IOR', 1.35)
|
||||
set_input(foam_bsdf, 'Specular IOR Level', 0.20)
|
||||
set_input(foam_bsdf, 'Subsurface Weight', 0.035)
|
||||
set_input(foam_bsdf, 'Subsurface Radius', (0.001, 0.0006, 0.0003))
|
||||
set_input(foam_bsdf, 'Subsurface Scale', 0.001)
|
||||
attach_albedo(foam_mat, foam_bsdf, latte_image)
|
||||
micro_bump(foam_mat, foam_bsdf, 145.0, 0.000055, 0.13)
|
||||
|
||||
|
||||
def recalculate_normals(mesh):
|
||||
bm = bmesh.new()
|
||||
bm.from_mesh(mesh)
|
||||
bmesh.ops.recalc_face_normals(bm, faces=list(bm.faces))
|
||||
bm.to_mesh(mesh)
|
||||
bm.free()
|
||||
mesh.update()
|
||||
|
||||
|
||||
def apply_planar_uv(obj, uv_radius):
|
||||
uv = obj.data.uv_layers['LiquidSurfaceUV']
|
||||
for polygon in obj.data.polygons:
|
||||
polygon.use_smooth = abs(polygon.normal.z) < 0.995
|
||||
for loop_index in polygon.loop_indices:
|
||||
vertex = obj.data.vertices[obj.data.loops[loop_index].vertex_index].co
|
||||
uv.data[loop_index].uv = (0.5 + vertex.x / (2.0 * uv_radius), 0.5 + vertex.y / (2.0 * uv_radius))
|
||||
obj['uv_radius_m'] = uv_radius
|
||||
|
||||
|
||||
def make_object(name, vertices, faces, top_face_start, material, uv_radius):
|
||||
mesh = bpy.data.meshes.new(name + '_Mesh')
|
||||
mesh.from_pydata(vertices, [], faces)
|
||||
recalculate_normals(mesh)
|
||||
obj = bpy.data.objects.new(name, mesh)
|
||||
model_collection.objects.link(obj)
|
||||
obj.parent = root
|
||||
obj.data.materials.append(material)
|
||||
mesh.uv_layers.new(name='LiquidSurfaceUV')
|
||||
mesh.uv_layers.active = mesh.uv_layers['LiquidSurfaceUV']
|
||||
apply_planar_uv(obj, uv_radius)
|
||||
obj['uv_map'] = 'LiquidSurfaceUV'
|
||||
obj['top_uv_faces'] = len(mesh.polygons) - top_face_start
|
||||
obj['material_slot'] = material.name
|
||||
return obj
|
||||
|
||||
|
||||
liquid_rings = [
|
||||
(fitted_radius(LIQUID_BOTTOM), LIQUID_BOTTOM),
|
||||
(fitted_radius(LIQUID_TOP), LIQUID_TOP),
|
||||
]
|
||||
vertices = []
|
||||
for ring_radius, z in liquid_rings:
|
||||
for index in range(SEGMENTS):
|
||||
angle = math.tau * index / SEGMENTS
|
||||
vertices.append((ring_radius * math.cos(angle), ring_radius * math.sin(angle), z))
|
||||
faces = [tuple(reversed(range(SEGMENTS)))]
|
||||
for index in range(SEGMENTS):
|
||||
following = (index + 1) % SEGMENTS
|
||||
faces.append((index, following, SEGMENTS + following, SEGMENTS + index))
|
||||
liquid = make_object('CC_Coffee_Liquid', vertices, faces, len(faces), coffee_mat, LIQUID_UV_RADIUS)
|
||||
liquid_top_start = len(liquid.data.polygons)
|
||||
cap = bmesh.new()
|
||||
cap.from_mesh(liquid.data)
|
||||
outer = [vertex for vertex in cap.verts if abs(vertex.co.z - LIQUID_TOP) < 0.0000001]
|
||||
bmesh.ops.triangle_fill(cap, edges=[edge for edge in cap.edges if all(vertex in outer for vertex in edge.verts)], use_beauty=True)
|
||||
bmesh.ops.recalc_face_normals(cap, faces=list(cap.faces))
|
||||
cap.to_mesh(liquid.data)
|
||||
cap.free()
|
||||
liquid.data.update()
|
||||
apply_planar_uv(liquid, LIQUID_UV_RADIUS)
|
||||
liquid['top_uv_faces'] = len(liquid.data.polygons) - liquid_top_start
|
||||
liquid['fill_height_mm'] = (LIQUID_TOP - LIQUID_BOTTOM) * 1000.0
|
||||
liquid['top_clearance_to_rim_mm'] = (CUP_HEIGHT - LIQUID_TOP) * 1000.0
|
||||
|
||||
foam_rings = [
|
||||
(fitted_radius(FOAM_BOTTOM) - 0.00008, FOAM_BOTTOM),
|
||||
(fitted_radius(FOAM_BOTTOM), FOAM_BOTTOM + 0.00012),
|
||||
(fitted_radius(FOAM_EDGE_TOP) - 0.00008, FOAM_EDGE_TOP - 0.00012),
|
||||
]
|
||||
for fraction in (1.0, 0.975, 0.92, 0.78, 0.55, 0.30, 0.12):
|
||||
z = FOAM_EDGE_TOP + FOAM_DOME * (1.0 - fraction * fraction)
|
||||
foam_rings.append((fitted_radius(FOAM_EDGE_TOP) * fraction, z))
|
||||
vertices = []
|
||||
for ring_radius, z in foam_rings:
|
||||
for index in range(SEGMENTS):
|
||||
angle = math.tau * index / SEGMENTS
|
||||
vertices.append((ring_radius * math.cos(angle), ring_radius * math.sin(angle), z))
|
||||
faces = [tuple(reversed(range(SEGMENTS)))]
|
||||
for ring_index in range(len(foam_rings) - 1):
|
||||
first = ring_index * SEGMENTS
|
||||
second = first + SEGMENTS
|
||||
for index in range(SEGMENTS):
|
||||
following = (index + 1) % SEGMENTS
|
||||
faces.append((first + index, first + following, second + following, second + index))
|
||||
foam_top_start = len(faces) - 7 * SEGMENTS
|
||||
center_index = len(vertices)
|
||||
vertices.append((0.0, 0.0, FOAM_EDGE_TOP + FOAM_DOME))
|
||||
last = (len(foam_rings) - 1) * SEGMENTS
|
||||
for index in range(SEGMENTS):
|
||||
following = (index + 1) % SEGMENTS
|
||||
faces.append((last + index, last + following, center_index))
|
||||
foam = make_object('CC_Milk_Foam', vertices, faces, foam_top_start, foam_mat, FOAM_UV_RADIUS)
|
||||
foam['thickness_at_edge_mm'] = (FOAM_EDGE_TOP - FOAM_BOTTOM) * 1000.0
|
||||
foam['dome_rise_mm'] = FOAM_DOME * 1000.0
|
||||
foam['surface_texture'] = 'CC_Latte_Surface.png'
|
||||
foam['surface_texture_uv'] = 'LiquidSurfaceUV; replace this image for other latte art'
|
||||
|
||||
|
||||
def point_at(obj, target):
|
||||
obj.rotation_euler = (Vector(target) - obj.location).to_track_quat('-Z', 'Y').to_euler()
|
||||
|
||||
|
||||
def camera(name, location, target, ortho_scale):
|
||||
data = bpy.data.cameras.new(name)
|
||||
data.type = 'ORTHO'
|
||||
data.ortho_scale = ortho_scale
|
||||
data.clip_start = 0.001
|
||||
data.clip_end = 100.0
|
||||
obj = bpy.data.objects.new(name, data)
|
||||
studio.objects.link(obj)
|
||||
obj.location = location
|
||||
point_at(obj, target)
|
||||
return obj
|
||||
|
||||
|
||||
def area_light(name, location, energy, size, color, target):
|
||||
data = bpy.data.lights.new(name, 'AREA')
|
||||
data.energy = energy
|
||||
data.shape = 'DISK'
|
||||
data.size = size
|
||||
data.color = color
|
||||
obj = bpy.data.objects.new(name, data)
|
||||
studio.objects.link(obj)
|
||||
obj.location = location
|
||||
point_at(obj, target)
|
||||
return obj
|
||||
|
||||
|
||||
ground_mat, ground_bsdf = new_material('Studio_Warm_Stone', '#A89D8D', 0.78)
|
||||
bpy.ops.mesh.primitive_plane_add(size=2.0, location=(0.0, 0.0, -0.00008))
|
||||
ground = bpy.context.object
|
||||
ground.name = 'Studio_Ground'
|
||||
for collection in list(ground.users_collection):
|
||||
collection.objects.unlink(ground)
|
||||
studio.objects.link(ground)
|
||||
ground.data.materials.append(ground_mat)
|
||||
|
||||
world = bpy.data.worlds.new('CupContents_Studio_World')
|
||||
world.use_nodes = True
|
||||
world.node_tree.nodes['Background'].inputs['Color'].default_value = (0.32, 0.36, 0.42, 1.0)
|
||||
world.node_tree.nodes['Background'].inputs['Strength'].default_value = 0.20
|
||||
scene.world = world
|
||||
area_light('Studio_Key', (0.10, 0.08, 0.25), 0.75, 0.17, (1.0, 0.89, 0.75), (0.0, 0.0, 0.07))
|
||||
area_light('Studio_Fill', (-0.16, 0.04, 0.16), 0.30, 0.14, (0.80, 0.89, 1.0), (0.0, 0.0, 0.065))
|
||||
area_light('Studio_Rim', (0.02, -0.15, 0.23), 0.90, 0.13, (1.0, 0.96, 0.87), (0.0, 0.0, 0.08))
|
||||
hero_camera = camera('Acceptance_Latte_Cycles', (0.145, 0.205, 0.285), (0.0, 0.0, 0.061), 0.165)
|
||||
top_camera = camera('Acceptance_Latte_Top_UV', (0.0, 0.0, 0.31), (0.0, 0.0, 0.0), 0.103)
|
||||
detail_camera = camera('Acceptance_Foam_Detail', (0.095, 0.135, 0.255), (0.0, 0.0, 0.098), 0.107)
|
||||
|
||||
scene.render.engine = 'CYCLES'
|
||||
scene.cycles.samples = 96
|
||||
scene.cycles.use_denoising = True
|
||||
scene.cycles.max_bounces = 8
|
||||
scene.render.resolution_x = 1400
|
||||
scene.render.resolution_y = 1400
|
||||
scene.render.resolution_percentage = 100
|
||||
scene.render.image_settings.file_format = 'PNG'
|
||||
scene.render.image_settings.color_mode = 'RGB'
|
||||
scene.render.film_transparent = False
|
||||
scene.view_settings.view_transform = 'AgX'
|
||||
scene.view_settings.look = 'AgX - Medium High Contrast'
|
||||
scene.view_settings.exposure = -0.5
|
||||
|
||||
|
||||
def non_manifold_count(obj):
|
||||
bm = bmesh.new()
|
||||
bm.from_mesh(obj.data)
|
||||
result = sum(1 for edge in bm.edges if not edge.is_manifold)
|
||||
bm.free()
|
||||
return result
|
||||
|
||||
|
||||
def uv_report(obj, top_z):
|
||||
uv = obj.data.uv_layers['LiquidSurfaceUV']
|
||||
top_loops = [loop_index for polygon in obj.data.polygons if abs(polygon.normal.z) > 0.5 for loop_index in polygon.loop_indices if obj.data.vertices[obj.data.loops[loop_index].vertex_index].co.z >= top_z - 0.000001]
|
||||
center_uv = None
|
||||
if obj == foam:
|
||||
center = obj.data.vertices[center_index]
|
||||
for loop in obj.data.loops:
|
||||
if loop.vertex_index == center_index:
|
||||
center_uv = list(uv.data[loop.index].uv)
|
||||
break
|
||||
return {
|
||||
'uv_map': uv.name,
|
||||
'uv_bounds': [min(uv.data[index].uv[axis] for axis in (0, 1) for index in top_loops), max(uv.data[index].uv[axis] for axis in (0, 1) for index in top_loops)],
|
||||
'center_uv': center_uv,
|
||||
'top_faces': obj['top_uv_faces'],
|
||||
'material_slots': [slot.material.name for slot in obj.material_slots],
|
||||
'non_manifold_edges': non_manifold_count(obj),
|
||||
}
|
||||
|
||||
|
||||
bpy.context.view_layer.update()
|
||||
cup_bvh = BVHTree.FromObject(cup, bpy.context.evaluated_depsgraph_get())
|
||||
liquid_bvh = BVHTree.FromObject(liquid, bpy.context.evaluated_depsgraph_get())
|
||||
foam_bvh = BVHTree.FromObject(foam, bpy.context.evaluated_depsgraph_get())
|
||||
liquid_cup_intersections = len(cup_bvh.overlap(liquid_bvh))
|
||||
foam_cup_intersections = len(cup_bvh.overlap(foam_bvh))
|
||||
foam_liquid_intersections = len(liquid_bvh.overlap(foam_bvh))
|
||||
liquid_uv = uv_report(liquid, LIQUID_TOP)
|
||||
foam_uv = uv_report(foam, FOAM_EDGE_TOP)
|
||||
report = {
|
||||
'source_cup': SOURCE.name,
|
||||
'source_cup_sha256_before_build': SOURCE_SHA256,
|
||||
'source_cup_sha256_after_build': hashlib.sha256(SOURCE.read_bytes()).hexdigest(),
|
||||
'lid_included': False,
|
||||
'liquid_top_mm': LIQUID_TOP * 1000.0,
|
||||
'foam_edge_thickness_mm': (FOAM_EDGE_TOP - FOAM_BOTTOM) * 1000.0,
|
||||
'foam_center_rise_mm': FOAM_DOME * 1000.0,
|
||||
'rim_clearance_mm': (CUP_HEIGHT - FOAM_EDGE_TOP - FOAM_DOME) * 1000.0,
|
||||
'radial_fit_clearance_min_mm': FIT_CLEARANCE * 1000.0,
|
||||
'liquid_cup_triangle_intersections': liquid_cup_intersections,
|
||||
'foam_cup_triangle_intersections': foam_cup_intersections,
|
||||
'foam_liquid_triangle_intersections': foam_liquid_intersections,
|
||||
'coffee': liquid_uv,
|
||||
'foam': foam_uv,
|
||||
'textures_packed': {image.name: bool(image.packed_file) for image in (coffee_image, latte_image)},
|
||||
'render_engine': scene.render.engine,
|
||||
}
|
||||
report['passed'] = (
|
||||
report['source_cup_sha256_before_build'] == report['source_cup_sha256_after_build']
|
||||
and liquid_cup_intersections == 0
|
||||
and foam_cup_intersections == 0
|
||||
and foam_liquid_intersections == 0
|
||||
and liquid.data.uv_layers.active.name == 'LiquidSurfaceUV'
|
||||
and foam.data.uv_layers.active.name == 'LiquidSurfaceUV'
|
||||
and liquid_uv['non_manifold_edges'] == 0
|
||||
and foam_uv['non_manifold_edges'] == 0
|
||||
and len(liquid.material_slots) == 1
|
||||
and len(foam.material_slots) == 1
|
||||
and liquid.material_slots[0].material != foam.material_slots[0].material
|
||||
and all(0.0 <= value <= 1.0 for value in liquid_uv['uv_bounds'] + foam_uv['uv_bounds'])
|
||||
and all(abs(value - 0.5) < 0.000001 for value in foam_uv['center_uv'])
|
||||
and all(report['textures_packed'].values())
|
||||
)
|
||||
(OUT / 'validation_contents.json').write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding='utf-8')
|
||||
print('CUP_CONTENTS_VALIDATION=' + json.dumps(report, ensure_ascii=False), flush=True)
|
||||
assert report['passed'], 'Cup contents validation failed; inspect validation_contents.json'
|
||||
|
||||
bpy.ops.object.select_all(action='DESELECT')
|
||||
root.select_set(True)
|
||||
liquid.select_set(True)
|
||||
foam.select_set(True)
|
||||
bpy.context.view_layer.objects.active = root
|
||||
bpy.ops.export_scene.gltf(
|
||||
filepath=str(OUT / 'PerfectCoffee_CupContents.glb'),
|
||||
export_format='GLB',
|
||||
use_selection=True,
|
||||
export_apply=True,
|
||||
export_animations=False,
|
||||
export_extras=True,
|
||||
export_cameras=False,
|
||||
export_lights=False,
|
||||
)
|
||||
|
||||
scene.camera = hero_camera
|
||||
scene.render.filepath = str(OUT / 'preview_contents_latte.png')
|
||||
bpy.ops.render.render(write_still=True)
|
||||
|
||||
scene.camera = top_camera
|
||||
scene.render.filepath = str(OUT / 'preview_contents_latte_top.png')
|
||||
bpy.ops.render.render(write_still=True)
|
||||
|
||||
foam.hide_render = True
|
||||
scene.camera = detail_camera
|
||||
scene.render.filepath = str(OUT / 'preview_contents_americano.png')
|
||||
bpy.ops.render.render(write_still=True)
|
||||
foam.hide_render = False
|
||||
scene.camera = hero_camera
|
||||
scene.render.filepath = str(OUT / 'preview_contents_latte.png')
|
||||
|
||||
bpy.ops.object.select_all(action='DESELECT')
|
||||
root.select_set(True)
|
||||
bpy.context.view_layer.objects.active = root
|
||||
for screen in bpy.data.screens:
|
||||
for area in screen.areas:
|
||||
if area.type == 'VIEW_3D':
|
||||
area.spaces.active.region_3d.view_perspective = 'CAMERA'
|
||||
area.spaces.active.shading.type = 'MATERIAL'
|
||||
bpy.ops.wm.save_as_mainfile(filepath=str(OUT / 'PerfectCoffee_CupContents.blend'))
|
||||
print('CUP_CONTENTS_BUILD_COMPLETE', flush=True)
|
||||
@@ -0,0 +1,110 @@
|
||||
{
|
||||
"baseline_blend": "PerfectCoffee_CupContents.blend",
|
||||
"baseline_sha256_before_optimization": "62ce59ff695fcfe3d19129fc8735b3a969ab25607b6c9d1ff1b4694b6b725330",
|
||||
"baseline_sha256_after_optimization": "62ce59ff695fcfe3d19129fc8735b3a969ab25607b6c9d1ff1b4694b6b725330",
|
||||
"mobile_blend": "PerfectCoffee_CupContents_Mobile.blend",
|
||||
"mobile_glb": "PerfectCoffee_CupContents_Mobile.glb",
|
||||
"texture": {
|
||||
"before": {
|
||||
"count": 2,
|
||||
"dimensions_each": [
|
||||
1024,
|
||||
1024
|
||||
],
|
||||
"png_bytes_total": 1262110,
|
||||
"rgba8_with_mipmaps_mib": 10.6666666667
|
||||
},
|
||||
"after": {
|
||||
"count": 1,
|
||||
"dimensions": [
|
||||
1024,
|
||||
512
|
||||
],
|
||||
"tile_dimensions_each": [
|
||||
512,
|
||||
512
|
||||
],
|
||||
"format": "RGB PNG, lossless",
|
||||
"png_bytes": 181671,
|
||||
"rgba8_with_mipmaps_mib": 2.6666666667
|
||||
},
|
||||
"file_size_reduction_percent": 85.60577128776414,
|
||||
"gpu_memory_reduction_percent": 75.0
|
||||
},
|
||||
"foam": {
|
||||
"vertices_before": 961,
|
||||
"vertices_after": 145,
|
||||
"triangles_before": 1918,
|
||||
"triangles_after": 286,
|
||||
"triangle_reduction_percent": 85.08863399374349,
|
||||
"segments_before": 96,
|
||||
"segments_after": 48,
|
||||
"thickness_edge_mm": 1.8800000000000066,
|
||||
"dome_rise_mm": 0.35,
|
||||
"outer_envelope_preserved": true,
|
||||
"non_manifold_edges": 0
|
||||
},
|
||||
"complete_contents_triangles_before": 2298,
|
||||
"complete_contents_triangles_after": 666,
|
||||
"intersections": {
|
||||
"liquid_cup": 0,
|
||||
"foam_cup": 0,
|
||||
"foam_liquid": 0
|
||||
},
|
||||
"atlas_uv": {
|
||||
"coffee": {
|
||||
"u": [
|
||||
8.773192838873456e-09,
|
||||
0.5
|
||||
],
|
||||
"v": [
|
||||
1.7546385677746912e-08,
|
||||
1.0
|
||||
]
|
||||
},
|
||||
"foam": {
|
||||
"u": [
|
||||
0.5,
|
||||
1.0
|
||||
],
|
||||
"v": [
|
||||
8.95026985858749e-09,
|
||||
1.0
|
||||
]
|
||||
},
|
||||
"foam_center": [
|
||||
0.75,
|
||||
0.5
|
||||
]
|
||||
},
|
||||
"glb": {
|
||||
"bytes": 218520,
|
||||
"meshes": 2,
|
||||
"vertices_after_export_split": 874,
|
||||
"triangles": 666,
|
||||
"images": 1,
|
||||
"materials": 2,
|
||||
"cameras": 0
|
||||
},
|
||||
"image_metrics_before_vs_after": {
|
||||
"latte": {
|
||||
"ssim_all": 0.999625,
|
||||
"psnr_db": "52.745750"
|
||||
},
|
||||
"latte_top": {
|
||||
"ssim_all": 0.996863,
|
||||
"psnr_db": "40.374979"
|
||||
}
|
||||
},
|
||||
"comparison_convention": "Left=original, right=Mobile; identical camera, lighting, Cycles samples, resolution and color management",
|
||||
"geometry_and_export_passed": true,
|
||||
"visual_review": "passed",
|
||||
"visual_review_notes": "同一 Cycles 机位对比已人工查看;拉花轮廓、奶泡颜色与杯口边缘保持一致,无明显异常暗环、贴图偏移或显著锯齿。",
|
||||
"glb_reimport_validation": {
|
||||
"passed": true,
|
||||
"meshes": 2,
|
||||
"triangles": 666,
|
||||
"embedded_images": 1,
|
||||
"cycles_render_review": "passed"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,340 @@
|
||||
import bpy
|
||||
import bmesh
|
||||
import hashlib
|
||||
import json
|
||||
import math
|
||||
import re
|
||||
import shutil
|
||||
import struct
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
from mathutils.bvhtree import BVHTree
|
||||
|
||||
OUT = Path('/Users/yanchenyu/Documents/WebApps/PerfetCoffee/model_assets/coffee_cup')
|
||||
BASE = OUT / 'PerfectCoffee_CupContents.blend'
|
||||
MOBILE_BLEND = OUT / 'PerfectCoffee_CupContents_Mobile.blend'
|
||||
MOBILE_GLB = OUT / 'PerfectCoffee_CupContents_Mobile.glb'
|
||||
ATLAS = OUT / 'textures' / 'CC_CupContents_Atlas.png'
|
||||
COMPARISONS = OUT / 'optimization'
|
||||
COMPARISONS.mkdir(exist_ok=True)
|
||||
FFMPEG = shutil.which('ffmpeg')
|
||||
assert FFMPEG, 'ffmpeg is required for the existing lossless texture and comparison pipeline'
|
||||
|
||||
BASE_SHA256 = hashlib.sha256(BASE.read_bytes()).hexdigest()
|
||||
ORIGINAL_TEXTURE_BYTES = sum((OUT / 'textures' / name).stat().st_size for name in ('CC_Coffee_Surface.png', 'CC_Latte_Surface.png'))
|
||||
SEGMENTS = 48
|
||||
CUP_SEGMENTS = 24
|
||||
FOAM_BOTTOM = 0.10382
|
||||
FOAM_EDGE_TOP = 0.10570
|
||||
FOAM_DOME = 0.00035
|
||||
FIT_CLEARANCE = 0.00012
|
||||
|
||||
|
||||
def run(command):
|
||||
result = subprocess.run(command, check=True, text=True, capture_output=True)
|
||||
return result.stdout + result.stderr
|
||||
|
||||
|
||||
run([
|
||||
FFMPEG, '-y', '-hide_banner', '-loglevel', 'warning',
|
||||
'-i', str(OUT / 'textures' / 'CC_Coffee_Surface.png'),
|
||||
'-i', str(OUT / 'textures' / 'CC_Latte_Surface.png'),
|
||||
'-filter_complex',
|
||||
'[0:v]scale=512:512:flags=lanczos[a];[1:v]scale=512:512:flags=lanczos[b];[a][b]hstack=inputs=2,format=rgb24[out]',
|
||||
'-map', '[out]', '-frames:v', '1', '-pred', 'mixed', '-compression_level', '9', '-update', '1', str(ATLAS),
|
||||
])
|
||||
|
||||
bpy.ops.wm.open_mainfile(filepath=str(BASE))
|
||||
bpy.context.preferences.filepaths.save_version = 0
|
||||
scene = bpy.context.scene
|
||||
root = bpy.data.objects['CC_CupContents_Assembly']
|
||||
cup = bpy.data.objects['CC_Cup_Body']
|
||||
liquid = bpy.data.objects['CC_Coffee_Liquid']
|
||||
foam = bpy.data.objects['CC_Milk_Foam']
|
||||
coffee_mat = bpy.data.materials['CC_Coffee_Liquid']
|
||||
foam_mat = bpy.data.materials['CC_Milk_Foam']
|
||||
|
||||
original_foam_vertices = len(foam.data.vertices)
|
||||
foam.data.calc_loop_triangles()
|
||||
original_foam_triangles = len(foam.data.loop_triangles)
|
||||
liquid.data.calc_loop_triangles()
|
||||
liquid_triangles = len(liquid.data.loop_triangles)
|
||||
original_foam_bounds = {
|
||||
'min_z': min(vertex.co.z for vertex in foam.data.vertices),
|
||||
'max_z': max(vertex.co.z for vertex in foam.data.vertices),
|
||||
'max_radius': max(math.hypot(vertex.co.x, vertex.co.y) for vertex in foam.data.vertices),
|
||||
}
|
||||
|
||||
scene.cycles.samples = 96
|
||||
scene.cycles.use_denoising = True
|
||||
scene.render.resolution_x = 1400
|
||||
scene.render.resolution_y = 1400
|
||||
scene.render.resolution_percentage = 100
|
||||
for camera_name, filename in [
|
||||
('Acceptance_Latte_Cycles', 'before_latte.png'),
|
||||
('Acceptance_Latte_Top_UV', 'before_latte_top.png'),
|
||||
]:
|
||||
scene.camera = bpy.data.objects[camera_name]
|
||||
scene.render.filepath = str(COMPARISONS / filename)
|
||||
bpy.ops.render.render(write_still=True)
|
||||
|
||||
atlas_image = bpy.data.images.load(str(ATLAS), check_existing=False)
|
||||
atlas_image.name = 'CC_CupContents_Atlas'
|
||||
atlas_image.colorspace_settings.name = 'sRGB'
|
||||
atlas_image.pack()
|
||||
for material in (coffee_mat, foam_mat):
|
||||
for node in material.node_tree.nodes:
|
||||
if node.type == 'TEX_IMAGE':
|
||||
node.image = atlas_image
|
||||
for image_name in ('CC_Coffee_Surface', 'CC_Latte_Surface'):
|
||||
image = bpy.data.images.get(image_name)
|
||||
if image is not None and image.users == 0:
|
||||
bpy.data.images.remove(image)
|
||||
|
||||
for obj, offset in ((liquid, 0.0), (foam, 0.5)):
|
||||
uv = obj.data.uv_layers['LiquidSurfaceUV']
|
||||
for loop_uv in uv.data:
|
||||
loop_uv.uv.x = offset + loop_uv.uv.x * 0.5
|
||||
obj['uv_map'] = 'LiquidSurfaceUV'
|
||||
obj['atlas_rect'] = [offset, 0.0, 0.5, 1.0]
|
||||
|
||||
|
||||
def fitted_radius(z):
|
||||
if z <= 0.022:
|
||||
radius = 0.028 + (z - 0.002) * (0.0305 - 0.028) / 0.020
|
||||
else:
|
||||
radius = 0.0305 + (z - 0.022) * (0.038 - 0.0305) / 0.066
|
||||
return radius * math.cos(math.pi / CUP_SEGMENTS) - FIT_CLEARANCE
|
||||
|
||||
|
||||
outer_radius = fitted_radius(FOAM_EDGE_TOP)
|
||||
rings = [
|
||||
(fitted_radius(FOAM_BOTTOM), FOAM_BOTTOM),
|
||||
(outer_radius, FOAM_EDGE_TOP),
|
||||
(outer_radius * 0.55, FOAM_EDGE_TOP + FOAM_DOME * (1.0 - 0.55 * 0.55)),
|
||||
]
|
||||
vertices = []
|
||||
for radius, z in rings:
|
||||
for index in range(SEGMENTS):
|
||||
angle = math.tau * index / SEGMENTS
|
||||
vertices.append((radius * math.cos(angle), radius * math.sin(angle), z))
|
||||
faces = [tuple(reversed(range(SEGMENTS)))]
|
||||
for ring_index in range(len(rings) - 1):
|
||||
first = ring_index * SEGMENTS
|
||||
second = first + SEGMENTS
|
||||
for index in range(SEGMENTS):
|
||||
following = (index + 1) % SEGMENTS
|
||||
faces.append((first + index, first + following, second + following, second + index))
|
||||
center_index = len(vertices)
|
||||
vertices.append((0.0, 0.0, FOAM_EDGE_TOP + FOAM_DOME))
|
||||
last = (len(rings) - 1) * SEGMENTS
|
||||
for index in range(SEGMENTS):
|
||||
following = (index + 1) % SEGMENTS
|
||||
faces.append((last + index, last + following, center_index))
|
||||
|
||||
mesh = bpy.data.meshes.new('CC_Milk_Foam_Mobile_Mesh')
|
||||
mesh.from_pydata(vertices, [], faces)
|
||||
bm = bmesh.new()
|
||||
bm.from_mesh(mesh)
|
||||
bmesh.ops.recalc_face_normals(bm, faces=list(bm.faces))
|
||||
bm.to_mesh(mesh)
|
||||
bm.free()
|
||||
mesh.update()
|
||||
mesh.materials.append(foam_mat)
|
||||
uv = mesh.uv_layers.new(name='LiquidSurfaceUV')
|
||||
for polygon in mesh.polygons:
|
||||
polygon.use_smooth = abs(polygon.normal.z) < 0.995
|
||||
for loop_index in polygon.loop_indices:
|
||||
vertex = mesh.vertices[mesh.loops[loop_index].vertex_index].co
|
||||
u = 0.5 + vertex.x / (2.0 * outer_radius)
|
||||
v = 0.5 + vertex.y / (2.0 * outer_radius)
|
||||
uv.data[loop_index].uv = (0.5 + u * 0.5, v)
|
||||
mesh.uv_layers.active = uv
|
||||
old_mesh = foam.data
|
||||
foam.data = mesh
|
||||
if old_mesh.users == 0:
|
||||
bpy.data.meshes.remove(old_mesh)
|
||||
foam['radial_segments'] = SEGMENTS
|
||||
foam['radial_surface_rings'] = 2
|
||||
foam['top_uv_faces'] = SEGMENTS * 2
|
||||
foam['atlas_rect'] = [0.5, 0.0, 0.5, 1.0]
|
||||
foam['surface_texture'] = ATLAS.name
|
||||
foam['surface_texture_uv'] = 'LiquidSurfaceUV; right half of atlas; original 0-1 liquid UV is remapped to U=0.5-1.0'
|
||||
foam['optimization'] = '96 to 48 segments; seven concentric surface rings to one intermediate ring; thickness and dome preserved'
|
||||
liquid['surface_texture'] = ATLAS.name
|
||||
liquid['surface_texture_uv'] = 'LiquidSurfaceUV; left half of atlas; original 0-1 liquid UV is remapped to U=0-0.5'
|
||||
root['uv_usage'] = 'LiquidSurfaceUV atlas: coffee U=0-0.5; foam U=0.5-1; shared V=0-1; each tile center U=0.25 or 0.75, V=0.5'
|
||||
root['texture_atlas'] = 'CC_CupContents_Atlas.png; left coffee 512x512, right latte 512x512; RGB PNG lossless'
|
||||
root['mobile_baseline_blend'] = BASE.name
|
||||
root['mobile_baseline_sha256'] = BASE_SHA256
|
||||
root['foam_original_segments'] = 96
|
||||
root['foam_mobile_segments'] = SEGMENTS
|
||||
|
||||
|
||||
def non_manifold_count(obj):
|
||||
bm = bmesh.new()
|
||||
bm.from_mesh(obj.data)
|
||||
count = sum(1 for edge in bm.edges if not edge.is_manifold)
|
||||
bm.free()
|
||||
return count
|
||||
|
||||
|
||||
def uv_bounds(obj):
|
||||
uv = obj.data.uv_layers.active
|
||||
return {
|
||||
'u': [min(loop_uv.uv.x for loop_uv in uv.data), max(loop_uv.uv.x for loop_uv in uv.data)],
|
||||
'v': [min(loop_uv.uv.y for loop_uv in uv.data), max(loop_uv.uv.y for loop_uv in uv.data)],
|
||||
}
|
||||
|
||||
|
||||
def glb_statistics(path):
|
||||
raw = path.read_bytes()
|
||||
json_length, chunk_type = struct.unpack_from('<II', raw, 12)
|
||||
assert chunk_type == 0x4E4F534A
|
||||
asset = json.loads(raw[20:20 + json_length])
|
||||
vertices = 0
|
||||
triangles = 0
|
||||
for mesh in asset['meshes']:
|
||||
for primitive in mesh['primitives']:
|
||||
vertices += asset['accessors'][primitive['attributes']['POSITION']]['count']
|
||||
triangles += asset['accessors'][primitive['indices']]['count'] // 3
|
||||
return {
|
||||
'bytes': len(raw),
|
||||
'meshes': len(asset['meshes']),
|
||||
'vertices_after_export_split': vertices,
|
||||
'triangles': triangles,
|
||||
'images': len(asset.get('images', [])),
|
||||
'materials': len(asset.get('materials', [])),
|
||||
'cameras': len(asset.get('cameras', [])),
|
||||
}
|
||||
|
||||
|
||||
bpy.context.view_layer.update()
|
||||
foam.data.calc_loop_triangles()
|
||||
mobile_foam_triangles = len(foam.data.loop_triangles)
|
||||
cup_bvh = BVHTree.FromObject(cup, bpy.context.evaluated_depsgraph_get())
|
||||
liquid_bvh = BVHTree.FromObject(liquid, bpy.context.evaluated_depsgraph_get())
|
||||
foam_bvh = BVHTree.FromObject(foam, bpy.context.evaluated_depsgraph_get())
|
||||
liquid_uv = uv_bounds(liquid)
|
||||
foam_uv = uv_bounds(foam)
|
||||
center_uv = next(uv.data[loop.index].uv[:] for loop in foam.data.loops if loop.vertex_index == center_index)
|
||||
mobile_foam_bounds = {
|
||||
'min_z': min(vertex.co.z for vertex in foam.data.vertices),
|
||||
'max_z': max(vertex.co.z for vertex in foam.data.vertices),
|
||||
'max_radius': max(math.hypot(vertex.co.x, vertex.co.y) for vertex in foam.data.vertices),
|
||||
}
|
||||
outer_envelope_preserved = all(
|
||||
math.isclose(original_foam_bounds[key], mobile_foam_bounds[key], rel_tol=0.0, abs_tol=0.000000001)
|
||||
for key in original_foam_bounds
|
||||
)
|
||||
geometry_and_export_passed = (
|
||||
non_manifold_count(foam) == 0
|
||||
and len(cup_bvh.overlap(liquid_bvh)) == 0
|
||||
and len(cup_bvh.overlap(foam_bvh)) == 0
|
||||
and len(liquid_bvh.overlap(foam_bvh)) == 0
|
||||
and mobile_foam_triangles == 286
|
||||
and len(foam.data.vertices) == 145
|
||||
and outer_envelope_preserved
|
||||
and abs(liquid_uv['u'][0]) < 0.000001
|
||||
and abs(liquid_uv['u'][1] - 0.5) < 0.000001
|
||||
and abs(foam_uv['u'][0] - 0.5) < 0.000001
|
||||
and abs(foam_uv['u'][1] - 1.0) < 0.000001
|
||||
and all(abs(value - 0.5) < 0.000001 for value in center_uv[1:])
|
||||
and abs(center_uv[0] - 0.75) < 0.000001
|
||||
and atlas_image.packed_file is not None
|
||||
and hashlib.sha256(BASE.read_bytes()).hexdigest() == BASE_SHA256
|
||||
)
|
||||
assert geometry_and_export_passed, 'Mobile geometry, atlas UV or source preservation validation failed'
|
||||
|
||||
bpy.ops.object.select_all(action='DESELECT')
|
||||
root.select_set(True)
|
||||
liquid.select_set(True)
|
||||
foam.select_set(True)
|
||||
bpy.context.view_layer.objects.active = root
|
||||
bpy.ops.export_scene.gltf(
|
||||
filepath=str(MOBILE_GLB),
|
||||
export_format='GLB',
|
||||
use_selection=True,
|
||||
export_apply=True,
|
||||
export_animations=False,
|
||||
export_extras=True,
|
||||
export_cameras=False,
|
||||
export_lights=False,
|
||||
)
|
||||
|
||||
for camera_name, filename in [
|
||||
('Acceptance_Latte_Cycles', 'after_latte.png'),
|
||||
('Acceptance_Latte_Top_UV', 'after_latte_top.png'),
|
||||
]:
|
||||
scene.camera = bpy.data.objects[camera_name]
|
||||
scene.render.filepath = str(COMPARISONS / filename)
|
||||
bpy.ops.render.render(write_still=True)
|
||||
|
||||
metrics = {}
|
||||
for view in ('latte', 'latte_top'):
|
||||
before = COMPARISONS / f'before_{view}.png'
|
||||
after = COMPARISONS / f'after_{view}.png'
|
||||
ssim_log = run([FFMPEG, '-hide_banner', '-nostats', '-i', str(before), '-i', str(after), '-filter_complex', '[0:v][1:v]ssim', '-f', 'null', '-'])
|
||||
psnr_log = run([FFMPEG, '-hide_banner', '-nostats', '-i', str(before), '-i', str(after), '-filter_complex', '[0:v][1:v]psnr', '-f', 'null', '-'])
|
||||
ssim_match = re.search(r'All:([0-9.]+)', ssim_log)
|
||||
psnr_match = re.search(r'average:([0-9.]+|inf)', psnr_log)
|
||||
assert ssim_match and psnr_match, f'Unable to read image metrics for {view}'
|
||||
metrics[view] = {'ssim_all': float(ssim_match.group(1)), 'psnr_db': psnr_match.group(1)}
|
||||
run([
|
||||
FFMPEG, '-y', '-hide_banner', '-loglevel', 'warning', '-i', str(before), '-i', str(after),
|
||||
'-filter_complex',
|
||||
'[0:v]scale=900:900:flags=lanczos[a];[1:v]scale=900:900:flags=lanczos[b];[a]pad=912:900:0:0:color=0x202124[ap];[ap][b]hstack=inputs=2[out]',
|
||||
'-map', '[out]', '-frames:v', '1', '-pred', 'mixed', '-compression_level', '9', '-update', '1',
|
||||
str(COMPARISONS / f'compare_{view}.png'),
|
||||
])
|
||||
|
||||
report = {
|
||||
'baseline_blend': BASE.name,
|
||||
'baseline_sha256_before_optimization': BASE_SHA256,
|
||||
'baseline_sha256_after_optimization': hashlib.sha256(BASE.read_bytes()).hexdigest(),
|
||||
'mobile_blend': MOBILE_BLEND.name,
|
||||
'mobile_glb': MOBILE_GLB.name,
|
||||
'texture': {
|
||||
'before': {'count': 2, 'dimensions_each': [1024, 1024], 'png_bytes_total': ORIGINAL_TEXTURE_BYTES, 'rgba8_with_mipmaps_mib': 10.6666666667},
|
||||
'after': {'count': 1, 'dimensions': [1024, 512], 'tile_dimensions_each': [512, 512], 'format': 'RGB PNG, lossless', 'png_bytes': ATLAS.stat().st_size, 'rgba8_with_mipmaps_mib': 2.6666666667},
|
||||
'file_size_reduction_percent': (1.0 - ATLAS.stat().st_size / ORIGINAL_TEXTURE_BYTES) * 100.0,
|
||||
'gpu_memory_reduction_percent': 75.0,
|
||||
},
|
||||
'foam': {
|
||||
'vertices_before': original_foam_vertices,
|
||||
'vertices_after': len(foam.data.vertices),
|
||||
'triangles_before': original_foam_triangles,
|
||||
'triangles_after': mobile_foam_triangles,
|
||||
'triangle_reduction_percent': (1.0 - mobile_foam_triangles / original_foam_triangles) * 100.0,
|
||||
'segments_before': 96,
|
||||
'segments_after': SEGMENTS,
|
||||
'thickness_edge_mm': (FOAM_EDGE_TOP - FOAM_BOTTOM) * 1000.0,
|
||||
'dome_rise_mm': FOAM_DOME * 1000.0,
|
||||
'outer_envelope_preserved': outer_envelope_preserved,
|
||||
'non_manifold_edges': non_manifold_count(foam),
|
||||
},
|
||||
'complete_contents_triangles_before': liquid_triangles + original_foam_triangles,
|
||||
'complete_contents_triangles_after': liquid_triangles + mobile_foam_triangles,
|
||||
'intersections': {
|
||||
'liquid_cup': len(cup_bvh.overlap(liquid_bvh)),
|
||||
'foam_cup': len(cup_bvh.overlap(foam_bvh)),
|
||||
'foam_liquid': len(liquid_bvh.overlap(foam_bvh)),
|
||||
},
|
||||
'atlas_uv': {'coffee': liquid_uv, 'foam': foam_uv, 'foam_center': list(center_uv)},
|
||||
'glb': glb_statistics(MOBILE_GLB),
|
||||
'image_metrics_before_vs_after': metrics,
|
||||
'comparison_convention': 'Left=original, right=Mobile; identical camera, lighting, Cycles samples, resolution and color management',
|
||||
'geometry_and_export_passed': geometry_and_export_passed,
|
||||
'visual_review': 'pending_manual_comparison',
|
||||
}
|
||||
(COMPARISONS / 'validation_mobile.json').write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding='utf-8')
|
||||
|
||||
scene.camera = bpy.data.objects['Acceptance_Latte_Cycles']
|
||||
scene.render.filepath = str(COMPARISONS / 'after_latte.png')
|
||||
bpy.ops.object.select_all(action='DESELECT')
|
||||
root.select_set(True)
|
||||
bpy.context.view_layer.objects.active = root
|
||||
bpy.ops.wm.save_as_mainfile(filepath=str(MOBILE_BLEND))
|
||||
print('MOBILE_OPTIMIZATION_VALIDATION=' + json.dumps(report, ensure_ascii=False), flush=True)
|
||||
print('MOBILE_OPTIMIZATION_BUILD_COMPLETE', flush=True)
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,49 @@
|
||||
{
|
||||
"source_cup": "PerfectCoffee_CoffeeCup.blend",
|
||||
"source_cup_sha256_before_build": "c0557c2f335e2bf178ff3190f8071e4b74e0b8503937e4d499e349fc304607f5",
|
||||
"source_cup_sha256_after_build": "c0557c2f335e2bf178ff3190f8071e4b74e0b8503937e4d499e349fc304607f5",
|
||||
"lid_included": false,
|
||||
"liquid_top_mm": 103.8,
|
||||
"foam_edge_thickness_mm": 1.8799999999999928,
|
||||
"foam_center_rise_mm": 0.35,
|
||||
"rim_clearance_mm": 3.949999999999999,
|
||||
"radial_fit_clearance_min_mm": 0.12000000000000001,
|
||||
"liquid_cup_triangle_intersections": 0,
|
||||
"foam_cup_triangle_intersections": 0,
|
||||
"foam_liquid_triangle_intersections": 0,
|
||||
"coffee": {
|
||||
"uv_map": "LiquidSurfaceUV",
|
||||
"uv_bounds": [
|
||||
1.7546385677746912e-08,
|
||||
1.0
|
||||
],
|
||||
"center_uv": null,
|
||||
"top_faces": 94,
|
||||
"material_slots": [
|
||||
"CC_Coffee_Liquid"
|
||||
],
|
||||
"non_manifold_edges": 0
|
||||
},
|
||||
"foam": {
|
||||
"uv_map": "LiquidSurfaceUV",
|
||||
"uv_bounds": [
|
||||
8.95026985858749e-09,
|
||||
1.0
|
||||
],
|
||||
"center_uv": [
|
||||
0.5,
|
||||
0.5
|
||||
],
|
||||
"top_faces": 768,
|
||||
"material_slots": [
|
||||
"CC_Milk_Foam"
|
||||
],
|
||||
"non_manifold_edges": 0
|
||||
},
|
||||
"textures_packed": {
|
||||
"CC_Coffee_Surface": true,
|
||||
"CC_Latte_Surface": true
|
||||
},
|
||||
"render_engine": "CYCLES",
|
||||
"passed": true
|
||||
}
|
||||
Reference in New Issue
Block a user