Files
Hunter/blender/build_pencil_arena.py
T
2026-11-29 15:49:17 +03:00

408 lines
17 KiB
Python

import bpy
import math
import os
import random
import numpy as np
from mathutils import Vector
# Hunter pencil arena. 1 Blender unit = 1 Roblox stud.
SEED = 20261122
rng = random.Random(SEED)
np_rng = np.random.default_rng(SEED)
ROOT = r"C:\roblox\hunter"
BLEND = ROOT + r"\3D models\game_zone\Hunter_Pencil_Arena.blend"
TEXTURE_DIR = ROOT + r"\3D models\game_zone\textures"
os.makedirs(TEXTURE_DIR, exist_ok=True)
old = bpy.data.scenes.get("Hunter Pencil Arena | layered routes")
if old:
bpy.data.scenes.remove(old)
scene = bpy.data.scenes.new("Hunter Pencil Arena | layered routes")
bpy.context.window.scene = scene
for ob in list(scene.objects):
bpy.data.objects.remove(ob, do_unlink=True)
def collection(name):
c = bpy.data.collections.new(name)
scene.collection.children.link(c)
return c
ground_c = collection("01 | Ground and paths")
wall_c = collection("02 | Pencil tile walls")
deck_c = collection("03 | Connected upper routes")
cover_c = collection("04 | Cover and landmarks")
detail_c = collection("05 | Navigation details")
MODULE = 8 # Roblox studs; all cover footprints and heights use this increment.
# Paint a large seamless sheet: sixteen unique cells per direction, eight studs
# per cell. Fine paper grain and variable pencil pressure remain visible up close.
N = 2048
spacing = 128
line = np.zeros((N, N), dtype=np.float32)
ghost = np.zeros_like(line)
grain = np_rng.normal(0, 1, (N, N)).astype(np.float32)
along = np.arange(N, dtype=np.float32)
indices = np.arange(N)
for axis in range(2):
for index in range(16):
nominal = index * spacing
phase = rng.random() * math.tau
anchors = np_rng.normal(0, 1.9, 110)
wander = np.interp(along, np.linspace(0, N-1, len(anchors)), anchors)
edge_fade = np.clip(np.minimum(along, N-1-along)/45, 0, 1)
jitter = (wander + 1.5*np.sin(along*.022+phase)
+ .65*np.sin(along*.083+phase*1.3))*edge_fade
pressure = np.interp(along, np.linspace(0,N-1,70),
np_rng.uniform(.58, .98, 70))
pressure *= np.clip(np_rng.normal(.98,.12,N),.55,1.16)
width = np.interp(along, np.linspace(0,N-1,65),
np_rng.uniform(2.4,4.2,65))
for _ in range(4):
start = rng.randint(0,N-18)
pressure[start:start+rng.randint(3,12)] *= rng.uniform(.05,.27)
for offset in range(-10,11):
center = nominal + offset
target = center % N
distance = np.abs(offset-jitter)
soft = np.clip((width+.8-distance)/1.7,0,1)
pigment = soft*pressure*np.clip(.88+.15*grain[target,:] if axis==0
else .88+.15*grain[:,target],.45,1.08)
if axis == 0:
line[target,indices] = np.maximum(line[target,indices],pigment)
else:
line[indices,target] = np.maximum(line[indices,target],pigment)
# A lighter, slightly offset tracing gives the hand-drawn double edge.
echo_distance = np.abs(offset-jitter-(3.1 if index%2 else -3.1))
echo = np.clip((1.6-echo_distance)/1.5,0,1)*pressure*.32
if axis == 0:
ghost[target,indices] = np.maximum(ghost[target,indices],echo)
else:
ghost[indices,target] = np.maximum(ghost[indices,target],echo)
def pencil_image(name, base, ink, echo):
base = np.array(base, dtype=np.float32)
ink = np.array(ink, dtype=np.float32)
echo = np.array(echo, dtype=np.float32)
noise = np_rng.normal(0, .007, (N, N, 1)).astype(np.float32)
paper = np.clip(base[None,None,:] + noise + .004*grain[:,:,None], 0, 1)
rgb = paper * (1-line[:,:,None]) + ink[None,None,:] * line[:,:,None]
rgb = rgb * (1-ghost[:,:,None]) + echo[None,None,:] * ghost[:,:,None]
alpha = np.ones((N,N,1), dtype=np.float32)
rgba = np.concatenate((rgb, alpha), axis=2)
image = bpy.data.images.new(name, width=N, height=N, alpha=True)
image.pixels.foreach_set(rgba.ravel())
filename = name.split(" | ")[1].replace(" ", "_").replace("-", "_") + ".png"
image.filepath_raw = os.path.join(TEXTURE_DIR, filename)
image.file_format = "PNG"
image.save()
image.pack()
return image
def material(name, color, image=None):
m = bpy.data.materials.new(name)
m.diffuse_color = (*color, 1)
m.use_nodes = True
nodes = m.node_tree.nodes
bsdf = next(n for n in nodes if n.type == "BSDF_PRINCIPLED")
bsdf.inputs["Base Color"].default_value = (*color,1)
bsdf.inputs["Roughness"].default_value = .88
if image:
tex = nodes.new("ShaderNodeTexImage")
tex.image = image
tex.extension = "REPEAT"
tex.projection = "BOX"
tex.projection_blend = .04
pos = nodes.new("ShaderNodeNewGeometry")
scale = nodes.new("ShaderNodeVectorMath")
scale.operation = "SCALE"
scale.inputs[3].default_value = 1/128
m.node_tree.links.new(pos.outputs["Position"], scale.inputs[0])
m.node_tree.links.new(scale.outputs[0], tex.inputs["Vector"])
m.node_tree.links.new(tex.outputs["Color"], bsdf.inputs["Base Color"])
return m
lav_img = pencil_image("Pencil tiles | uneven lavender 8-stud cells",
(.70,.72,.86), (.43,.47,.64), (.60,.62,.75))
paper_img = pencil_image("Pencil tiles | warm paper",
(.96,.84,.76), (.79,.68,.67), (.88,.77,.73))
orange_img = pencil_image("Pencil tiles | peach orange",
(.97,.59,.39), (.69,.37,.31), (.87,.48,.36))
navy_img = pencil_image("Pencil tiles | deep blue",
(.20,.29,.42), (.42,.45,.47), (.30,.38,.48))
lav = material("WALL | lavender pencil tiles", (.66,.69,.84), lav_img)
paper = material("FLOOR | peach paper tiles", (.95,.82,.73), paper_img)
orange = material("COVER | orange pencil tiles", (.97,.54,.33), orange_img)
navy = material("ROUTE | ink blue tiles", (.17,.25,.37), navy_img)
ink = material("INK | dark outline", (.08,.12,.20))
skyblue = material("MARK | cyan", (.12,.73,.88))
gold = material("MARK | amber", (1,.7,.25))
pale = material("LIGHT | pale cream", (.96,.9,.8))
def move_to(ob, coll):
for c in list(ob.users_collection):
c.objects.unlink(ob)
coll.objects.link(ob)
return ob
def box(name, center, size, mat, coll, bevel=.0, rot=0):
if min(size) < 2:
raise ValueError(f"{name}: mesh thickness must be at least 2 studs")
bpy.ops.mesh.primitive_cube_add(size=1, location=center)
ob = bpy.context.object
ob.name = name
ob.dimensions = size
ob.rotation_euler[2] = math.radians(rot)
bpy.ops.object.transform_apply(location=False, rotation=False, scale=True)
ob.data.materials.append(mat)
move_to(ob, coll)
if bevel:
mod = ob.modifiers.new("Soft ink edges", "BEVEL")
mod.width = bevel
mod.segments = 2
ob.modifiers.new("Weighted face normals", "WEIGHTED_NORMAL")
return ob
def wall(name, cx, cy, sx, sy, height, mat=lav, rot=0):
ob=box(name, (cx,cy,height/2), (sx,sy,height), mat, wall_c, .16, rot)
# A narrow dark cap gives the sketch-like outer edge.
cap=box(name+" | ink cap", (cx,cy,height+1), (sx+2,sy+2,2), ink, detail_c, .12, rot)
return ob
def cover(name, cx, cy, sx, sy, height, mat=orange, base_z=0):
ob=box(name,(cx,cy,base_z+height/2),(sx,sy,height),mat,cover_c,.26)
ob["grid_step_studs"] = MODULE
ob["module_cells"] = (sx//MODULE, sy//MODULE, height//MODULE)
box(name+" | charcoal top",(cx,cy,base_z+height+1),(sx+2,sy+2,2),ink,detail_c,.12)
return ob
def cover_module(name, gx, gy, cells_x, cells_y, cells_z, mat=orange, level=0):
return cover(name, gx*MODULE, gy*MODULE,
cells_x*MODULE, cells_y*MODULE, cells_z*MODULE,
mat, level)
def oriented_deck(name, start, end, width, z, thickness=2, mat=navy):
a=Vector(start); b=Vector(end)
d=b-a
center=(a+b)/2
ob=box(name,(center.x,center.y,z-thickness/2),(d.length,width,thickness),mat,deck_c,.10,math.degrees(math.atan2(d.y,d.x)))
return ob
def ramp(name, start, end, width, top_z, mat=navy):
a=Vector(start); b=Vector(end)
d=(b-a).normalized()
perp=Vector((-d.y,d.x))
low=.03
high=top_z
# A closed triangular prism: horizontal bottom, vertical high end, sloped
# walkable face. Its length/height section is exactly a right triangle.
verts=[]
for p,z in ((a,low),(b,low),(b,high)):
for s in (-1,1):
xy=p+perp*(s*width/2)
verts.append((xy.x,xy.y,z))
faces=[(0,4,5,1),(0,1,3,2),(2,3,5,4),(0,2,4),(1,5,3)]
mesh=bpy.data.meshes.new(name)
mesh.from_pydata(verts,[],faces); mesh.update()
ob=bpy.data.objects.new(name,mesh); deck_c.objects.link(ob)
ob.data.materials.append(mat)
# Raised edge rails trace the slope without obstructing its full walking width.
for s in (-1,1):
for p0,p1 in ((a,b),):
u=p0+perp*(s*(width/2-1))
v=p1+perp*(s*(width/2-1))
rail=oriented_deck(name+" | side rail",u,v,2,0,2,ink)
# Replace the flat rail with a sloped beam by making its mesh vertices rise.
for vert in rail.data.vertices:
local = vert.co
along = local.x / max((v-u).length,.01) + .5
local.z += low + (high-low)*along + 1.8
return ob
def platform(name,cx,cy,sx,sy,z):
box(name,(cx,cy,z-1),(sx,sy,2),navy,deck_c,.12)
for x in (cx-sx/2+4,cx+sx/2-4):
for y in (cy-sy/2+4,cy+sy/2-4):
box(name+" | support",(x,y,(z-2)/2),(3.3,3.3,z-2),lav,deck_c,.08)
# low cover on the deck, deliberately leaving 18+ stud clear corridors
return
def railing(name,p0,p1,z):
a=Vector(p0);b=Vector(p1)
oriented_deck(name,a,b,2,z+3,2,ink)
for t in (0,.25,.5,.75,1):
p=a.lerp(b,t)
box(name+" | post",(p.x,p.y,z+1.5),(2,2,3),ink,detail_c,.12)
# Ground is one continuous slab: no overlapping, coplanar tiled plates.
box("520 x 520 | continuous paper arena floor",(0,0,-1.5),(520,520,3),paper,ground_c,.05)
# Four bounding walls; high enough to visually contain the fighting ground.
wall("North boundary",0,258,520,4,32,navy)
wall("South boundary",0,-258,520,4,32,navy)
wall("East boundary",258,0,4,520,32,navy)
wall("West boundary",-258,0,4,520,32,navy)
# Route hierarchy: three broad south-north lanes with offset links. Obstacles
# frame them instead of sealing them into disconnected rooms.
# Southern split and staggered entry walls.
wall("South | central divider",0,-185,29,86,30)
wall("South | west return",-86,-180,54,8,25)
wall("South | east return",90,-147,48,8,25)
wall("South | left flank",-191,-150,8,94,30)
wall("South | right flank",195,-183,8,104,30)
wall("South | cross screen",-113,-103,8,51,23)
wall("South | angled screen",117,-90,9,65,30,lav,-18)
# North approach: openings alternate side so no corridor is a blind straight tunnel.
wall("North | central divider",15,187,32,82,29)
wall("North | west return",-90,152,56,8,25)
wall("North | east return",100,176,58,8,26)
wall("North | left flank",-194,175,8,86,30)
wall("North | right flank",194,141,8,90,30)
wall("North | cross screen",-111,100,8,56,26)
wall("North | angled screen",123,96,8,74,31,lav,16)
# Middle: four separated corner structures preserve four entrances to the plaza.
wall("Center | NW west",-85,48,8,72,35)
wall("Center | NW north",-61,86,58,8,35)
wall("Center | NE north",72,87,65,8,34)
wall("Center | NE east",107,56,8,64,15)
wall("Center | SW west",-107,-58,8,66,15)
wall("Center | SW south",-76,-89,64,8,31)
wall("Center | SE south",64,-89,55,8,33)
wall("Center | SE east",89,-57,8,63,33)
# West and east bypass pockets. Several screens are diagonal to interrupt a grid.
wall("West | lower shield",-217,-36,9,80,28)
wall("West | mid shield",-166,8,66,8,15)
wall("West | upper shield",-215,75,9,94,31)
wall("West | diagonal",-155,201,8,57,29,lav,23)
wall("East | lower shield",215,-48,9,92,30)
wall("East | mid shield",170,9,69,8,15)
wall("East | upper shield",218,66,9,88,32)
wall("East | diagonal",153,-205,8,68,29,lav,-20)
# Each tuple is grid position, width cells, depth cells and height cells.
# The small, medium and tall variants can be rearranged on the same 8-stud grid.
for name,gx,gy,wx,wy,hz,mat in [
("Hub | orange obelisk",-3,2,2,2,2,orange),
("Hub | low orange shield",4,-3,3,1,1,orange),
("Hub | high slate shield",6,4,2,3,2,lav),
("Hub | slim barricade",-6,-4,1,3,1,lav),
("South | orange cover A",-8,-27,2,2,2,orange),
("South | orange cover B",8,-23,2,2,3,orange),
("South | low shield",18,-26,3,1,1,lav),
("South | long barricade",-6,-17,4,1,2,lav),
("South | slim tower",13,-28,1,1,3,orange),
("West | low crate",-26,-13,3,2,1,orange),
("West | tall crate",-18,8,2,2,3,orange),
("West | far corner block",-28,-28,2,2,2,lav),
("West | north bypass",-28,23,1,3,2,orange),
("East | orange tower",23,-11,2,2,4,orange),
("East | side barricade",28,16,1,3,2,lav),
("East | long low cover",28,26,3,1,1,orange),
("North | orange cover A",-7,26,2,2,2,orange),
("North | orange cover B",9,20,3,1,1,orange),
("North | tall slate",18,25,2,2,3,lav),
("North | tall marker",0,15,2,2,4,orange),
]:
cover_module(name,gx,gy,wx,wy,hz,mat)
# Four connected upper landings. Each has a real supporting structure,
# a walkable ramp at a free side, and attached bridge endpoints.
Z=24
platform("West lower terrace",-167,-78,82,70,Z)
platform("East upper terrace",166,78,82,70,Z)
platform("West north terrace",-167,161,82,70,Z)
platform("Central crossing",0,0,70,70,Z)
ramp("West terrace | south ramp",(-167,-191),(-167,-113),24,Z)
ramp("East terrace | south ramp",(166,-39),(166,43),24,Z)
ramp("North terrace | north ramp",(-167,250),(-167,196),24,Z)
# These wide spans overlap their receiving decks by several studs.
oriented_deck("West to center | diagonal upper crossing",(-129,-69),(-27,-22),19,Z+.12)
oriented_deck("Center to east | diagonal upper crossing",(25,21),(131,68),19,Z+.12)
oriented_deck("West terrace to north terrace | long overlook",(-167,-47),(-167,130),18,Z+.12)
# Secondary ground to upper route prevents one-sided camping.
platform("East south terrace",163,-158,74,57,Z)
ramp("East south terrace | east ramp",(247,-158),(200,-158),24,Z)
oriented_deck("East south to east north | flank crossing",(163,-134),(166,51),18,Z+.12)
# Low, differently shaped cover on landings keeps upper movement tactical.
cover_module("Upper west | short cover",-23,-10,1,1,1,orange,Z)
cover_module("Upper east | broad cover",23,12,2,1,1,orange,Z)
cover_module("Upper north | narrow cover",-23,22,1,2,1,lav,Z)
cover_module("Upper south east | low cover",18,-22,2,1,1,orange,Z)
# Sparse guard edges where the fall is highest, leaving bridge ends and ramps open.
for name,a,b in [
("West long edge",(-176,-37),(-176,125)),
("West diagonal north edge",(-128,-58),(-30,-11)),
("East diagonal south edge",(26,9),(131,56)),
("East flank edge",(172,-129),(174,50)),
]:
railing(name,a,b,Z)
# Upper-route edges and tall cover carry color cues, not player HUD.
for x,y,z in [(-167,-78,Z),(166,78,Z),(-167,161,Z),(163,-158,Z)]:
box("Landing beacon",(x,y,z+1.1),(5,5,2.2),gold,detail_c,.30)
for x,y in [(-245,-240),(245,-240),(-245,240),(245,240)]:
box("Perimeter light mast",(x,y,17),(2.2,2.2,34),ink,detail_c,.12)
box("Perimeter light",(x,y,35),(7,7,3),pale,detail_c,.5)
# Lighting: cool ambient sky plus warm side light emphasize the drawn texture.
world=bpy.data.worlds.new("Open cyan sky")
world.use_nodes=True
wn=world.node_tree.nodes
wn.clear()
bg=wn.new("ShaderNodeBackground")
bg.inputs["Color"].default_value=(.28,.67,.98,1)
bg.inputs["Strength"].default_value=.75
wo=wn.new("ShaderNodeOutputWorld")
world.node_tree.links.new(bg.outputs["Background"],wo.inputs["Surface"])
scene.world=world
def sun(name,rotation,power,color):
data=bpy.data.lights.new(name,"SUN")
ob=bpy.data.objects.new(name,data); scene.collection.objects.link(ob)
ob.rotation_euler=rotation
data.energy=power; data.color=color
data.angle=math.radians(6)
sun("Warm afternoon key",(math.radians(33),math.radians(-18),math.radians(-25)),2.4,(1,.82,.70))
sun("Cool sky fill",(math.radians(48),math.radians(18),math.radians(105)),.9,(.68,.81,1))
def camera(name,position,target,lens):
data=bpy.data.cameras.new(name)
ob=bpy.data.objects.new(name,data); scene.collection.objects.link(ob)
ob.location=position
direction=Vector(target)-ob.location
ob.rotation_euler=direction.to_track_quat("-Z","Y").to_euler()
data.lens=lens
data.clip_end=4000
return ob
overview_cam=camera("Overview | shows looped ground and upper routes",(610,-680,675),(0,0,0),43)
player_cam=camera("Player view | pencil tiles and ramps",(-142,-222,8),(-163,-76,14),31)
ramp_cam=camera("Ramp section | triangular side and module scale",(230,-211,14),(201,-157,12),38)
routes_cam=camera("Route plan | orthographic",(0,0,900),(0,0,0),32)
routes_cam.data.type="ORTHO"
routes_cam.data.ortho_scale=580
scene.render.engine="CYCLES"
scene.cycles.samples=72
scene.render.resolution_x=1440
scene.render.resolution_y=960
scene.render.resolution_percentage=100
scene.render.image_settings.file_format="PNG"
scene.view_settings.view_transform="Standard"
scene.view_settings.look="Medium High Contrast"
scene.render.film_transparent=False
scene.camera=overview_cam
bpy.ops.wm.save_as_mainfile(filepath=BLEND)
result={
"blend":BLEND,
"objects":len(scene.objects),
"size_studs":[520,520],
"upper_route_z":Z
}