news 2026/4/2 17:21:03

城市仿真软件:CityEngine_(8).建筑生成与编辑

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
城市仿真软件:CityEngine_(8).建筑生成与编辑

建筑生成与编辑

在城市仿真软件中,建筑生成与编辑是核心功能之一。CityEngine 提供了强大的工具,让用户能够从简单的规则和数据中生成复杂的建筑模型,并对其进行编辑和优化。本节将详细介绍如何使用 CityEngine 进行建筑生成与编辑,包括基础规则的编写、高级编辑技巧、以及如何利用外部数据源来增强模型的逼真度。

建筑生成的基本步骤

建筑生成的基本步骤包括定义地块、编写规则、生成建筑模型和优化模型。我们将逐一介绍这些步骤,并提供具体的代码示例和数据样例。

1. 定义地块

地块是建筑生成的基础。在 CityEngine 中,地块可以是一个简单的几何形状,也可以是一个复杂的多边形。地块的定义通常在 CAD 软件中完成,然后导入 CityEngine。

示例:导入地块数据

假设我们有一个地块数据文件plot.cgshape,该文件包含地块的多边形信息。我们可以使用以下代码将其导入 CityEngine:

# 导入地块数据importshapefile# 读取地块数据文件sf=shapefile.Reader("plot.cgshape")# 获取地块的几何形状shapes=sf.shapes()# 将地块添加到 CityEngine 场景中forshapeinshapes:plot=shape.points plot=[cg.Point3(x,y,0)forx,yinplot]new_plot=cg.Polygon(plot)cg.addShape(new_plot,"Plot")

2. 编写规则

CityEngine 使用 CGA(Computer Generated Architecture)语言来定义建筑生成规则。CGA 是一种高级脚本语言,允许用户通过简单的指令生成复杂的建筑模型。

示例:编写简单的建筑生成规则

假设我们有一个简单的地块,我们希望在该地块上生成一个矩形建筑。可以使用以下 CGA 规则:

# 定义地块 attr width = 50 attr depth = 30 # 基础形状 Lot --> extrude(height) # 建筑高度 attr height = 10 # 建筑墙体 Building --> wall(2) # 墙体材质 Wall --> color("#FF0000") # 红色墙体

3. 生成建筑模型

在定义了地块和 CGA 规则后,我们可以使用 CityEngine 的生成工具来生成建筑模型。

示例:生成建筑模型

假设我们已经定义了地块和 CGA 规则,可以使用以下代码生成建筑模型:

# 生成建筑模型cg.generate("Plot","Building.cga")

4. 优化模型

生成的建筑模型可能需要进一步优化,以满足特定的设计要求。优化可以包括调整高度、添加窗户、修改材质等。

示例:调整建筑高度

假设我们希望将建筑高度从 10 米调整到 15 米,可以使用以下代码:

# 调整建筑高度cg.setAttribute("Plot","height",15)cg.generate("Plot","Building.cga")

高级编辑技巧

除了基本的生成和优化,CityEngine 还提供了许多高级编辑技巧,如动态生成、参数化设计和复杂几何形状的处理。

1. 动态生成

动态生成允许用户在生成过程中实时调整参数,以观察模型的变化。

示例:动态生成建筑

假设我们希望在生成过程中动态调整建筑的高度和宽度,可以使用以下 CGA 规则:

# 定义地块 attr width = 50 attr depth = 30 attr height = 10 # 基础形状 Lot --> extrude(height) comp(f) { front: BuildingFront back: BuildingBack left: BuildingLeft right: BuildingRight } # 前立面 BuildingFront --> wall(2) windows(5, 3) # 后立面 BuildingBack --> wall(2) # 左立面 BuildingLeft --> wall(2) # 右立面 BuildingRight --> wall(2) # 墙体材质 Wall --> color("#FF0000") # 红色墙体 # 窗户 attr window_width = 1 attr window_height = 1 attr window_spacing = 1 windows(w, h) --> split(y) { 0 : repeat(w, h) { 0 : window(window_width, window_height) window_spacing : wall(2) } } window(w, h) --> color("#FFFFFF") # 白色窗户 extrude(h) split(x) { 0 : wall(2) w : wall(2) }

2. 参数化设计

参数化设计允许用户通过调整参数来生成不同风格的建筑模型。

示例:参数化设计建筑

假设我们希望生成不同风格的建筑,可以使用以下 CGA 规则:

# 定义地块 attr width = 50 attr depth = 30 attr height = 10 attr style = "Modern" # 可以是 "Modern", "Historical", "Futuristic" # 基础形状 Lot --> extrude(height) comp(f) { front: BuildingFront(style) back: BuildingBack(style) left: BuildingLeft(style) right: BuildingRight(style) } # 前立面 BuildingFront(style) --> case style == "Modern" : wall(2) case style == "Historical" : historicalWall(2) case style == "Futuristic" : futuristicWall(2) else : wall(2) # 后立面 BuildingBack(style) --> case style == "Modern" : wall(2) case style == "Historical" : historicalWall(2) case style == "Futuristic" : futuristicWall(2) else : wall(2) # 左立面 BuildingLeft(style) --> case style == "Modern" : wall(2) case style == "Historical" : historicalWall(2) case style == "Futuristic" : futuristicWall(2) else : wall(2) # 右立面 BuildingRight(style) --> case style == "Modern" : wall(2) case style == "Historical" : historicalWall(2) case style == "Futuristic" : futuristicWall(2) else : wall(2) # 现代风格墙体 ModernWall --> color("#FF0000") # 红色墙体 # 历史风格墙体 HistoricalWall --> color("#8B4513") # 棕色墙体 # 未来风格墙体 FuturisticWall --> color("#00FF00") # 绿色墙体

3. 复杂几何形状的处理

CityEngine 可以处理复杂的几何形状,如曲线和不规则多边形。

示例:处理复杂几何形状

假设我们有一个不规则多边形的地块,可以使用以下 CGA 规则生成建筑:

# 定义不规则地块 attr points = [cg.Point3(0, 0, 0), cg.Point3(50, 0, 0), cg.Point3(50, 30, 0), cg.Point3(25, 40, 0), cg.Point3(0, 30, 0)] # 基础形状 Lot --> polygon(points) extrude(height) comp(f) { front: BuildingFront back: BuildingBack left: BuildingLeft right: BuildingRight } # 前立面 BuildingFront --> wall(2) windows(5, 3) # 后立面 BuildingBack --> wall(2) # 左立面 BuildingLeft --> wall(2) # 右立面 BuildingRight --> wall(2) # 墙体材质 Wall --> color("#FF0000") # 红色墙体 # 窗户 attr window_width = 1 attr window_height = 1 attr window_spacing = 1 windows(w, h) --> split(y) { 0 : repeat(w, h) { 0 : window(window_width, window_height) window_spacing : wall(2) } } window(w, h) --> color("#FFFFFF") # 白色窗户 extrude(h) split(x) { 0 : wall(2) w : wall(2) }

利用外部数据源

CityEngine 可以利用外部数据源来增强建筑模型的逼真度。常见的外部数据源包括 GIS 数据、建筑数据库和传感器数据。

1. 导入 GIS 数据

GIS 数据通常包含地理信息和属性信息,可以用于生成更真实的建筑模型。

示例:导入 GIS 数据

假设我们有一个包含地块信息的 GIS 数据文件plot.shp,可以使用以下代码将其导入 CityEngine:

# 导入 GIS 数据importshapefile# 读取 GIS 数据文件sf=shapefile.Reader("plot.shp")# 获取地块的几何形状和属性shapes=sf.shapes()records=sf.records()# 将地块添加到 CityEngine 场景中forshape,recordinzip(shapes,records):plot=shape.points plot=[cg.Point3(x,y,0)forx,yinplot]new_plot=cg.Polygon(plot)cg.addShape(new_plot,"Plot")cg.setAttribute(new_plot,"plot_id",record[0])cg.setAttribute(new_plot,"plot_area",record[1])cg.setAttribute(new_plot,"plot_height",record[2])

2. 利用建筑数据库

建筑数据库通常包含建筑的设计参数和材质信息,可以用于生成更详细的建筑模型。

示例:利用建筑数据库

假设我们有一个包含建筑参数的数据库文件buildings.db,可以使用以下代码将其导入 CityEngine:

# 导入建筑数据库importsqlite3# 连接数据库conn=sqlite3.connect("buildings.db")cursor=conn.cursor()# 查询建筑参数cursor.execute("SELECT building_id, width, depth, height, style FROM buildings")buildings=cursor.fetchall()# 将建筑参数应用到地块forbuildinginbuildings:building_id,width,depth,height,style=building plot=cg.getShape("Plot",building_id)cg.setAttribute(plot,"width",width)cg.setAttribute(plot,"depth",depth)cg.setAttribute(plot,"height",height)cg.setAttribute(plot,"style",style)cg.generate(plot,"Building.cga")

3. 利用传感器数据

传感器数据可以用于实时调整建筑模型的生成参数,如光照、风速等。

示例:利用传感器数据

假设我们有一个包含光照数据的传感器文件light_data.json,可以使用以下代码将其导入 CityEngine:

# 导入传感器数据importjson# 读取光照数据文件withopen("light_data.json","r")asfile:light_data=json.load(file)# 将光照数据应用到地块forplot_id,lightinlight_data.items():plot=cg.getShape("Plot",plot_id)cg.setAttribute(plot,"light",light)cg.generate(plot,"Building.cga")

建筑生成的高级应用

在实际应用中,建筑生成和编辑可以结合多种工具和技术,实现更复杂和逼真的城市仿真。

1. 生成建筑群

生成建筑群需要考虑地块之间的关系和城市规划的要求。

示例:生成建筑群

假设我们有一个包含多个地块的 GIS 数据文件plots.shp,可以使用以下代码生成建筑群:

# 导入 GIS 数据importshapefile# 读取 GIS 数据文件sf=shapefile.Reader("plots.shp")# 获取地块的几何形状和属性shapes=sf.shapes()records=sf.records()# 生成建筑群forshape,recordinzip(shapes,records):plot=shape.points plot=[cg.Point3(x,y,0)forx,yinplot]new_plot=cg.Polygon(plot)cg.addShape(new_plot,"Plot")cg.setAttribute(new_plot,"plot_id",record[0])cg.setAttribute(new_plot,"plot_area",record[1])cg.setAttribute(new_plot,"plot_height",record[2])cg.generate(new_plot,"Building.cga")

2. 生成动态城市

生成动态城市需要考虑时间的变化和环境的影响。

示例:生成动态城市

假设我们有一个包含时间戳和光照数据的传感器文件dynamic_light_data.json,可以使用以下代码生成动态城市:

# 导入传感器数据importjson# 读取光照数据文件withopen("dynamic_light_data.json","r")asfile:light_data=json.load(file)# 生成动态城市fortimestamp,datainlight_data.items():forplot_id,lightindata.items():plot=cg.getShape("Plot",plot_id)cg.setAttribute(plot,"light",light)cg.generate(plot,"Building.cga")cg.createSnapshot("snapshot_{}.png".format(timestamp))

3. 生成虚拟城市

生成虚拟城市需要考虑虚拟环境的要求和用户交互的体验。

示例:生成虚拟城市

假设我们有一个包含虚拟环境参数的配置文件virtual_city_config.json,可以使用以下代码生成虚拟城市:

# 导入虚拟城市配置importjson# 读取虚拟城市配置文件withopen("virtual_city_config.json","r")asfile:config=json.load(file)# 生成虚拟城市forplot_id,parametersinconfig["plots"].items():plot=cg.getShape("Plot",plot_id)forkey,valueinparameters.items():cg.setAttribute(plot,key,value)cg.generate(plot,"Building.cga")# 添加虚拟环境元素forelementinconfig["environment"]:cg.addShape(element["geometry"],element["type"])forkey,valueinelement["attributes"].items():cg.setAttribute(element["geometry"],key,value)

建筑生成的优化技巧

在生成和编辑建筑模型时,优化技巧可以帮助提高模型的性能和逼真度。

1. 优化模型性能

优化模型性能包括减少多边形数量、优化材质和提高渲染速度。

示例:减少多边形数量

假设我们希望减少生成的建筑模型的多边形数量,可以使用以下 CGA 规则:

# 定义地块 attr width = 50 attr depth = 30 attr height = 10 attr simplify = 0.5 # 基础形状 Lot --> extrude(height) simplify(simplify) comp(f) { front: BuildingFront back: BuildingBack left: BuildingLeft right: BuildingRight } # 前立面 BuildingFront --> wall(2) windows(5, 3) # 后立面 BuildingBack --> wall(2) # 左立面 BuildingLeft --> wall(2) # 右立面 BuildingRight --> wall(2) # 墙体材质 Wall --> color("#FF0000") # 红色墙体 # 窗户 attr window_width = 1 attr window_height = 1 attr window_spacing = 1 windows(w, h) --> split(y) { 0 : repeat(w, h) { 0 : window(window_width, window_height) window_spacing : wall(2) } } window(w, h) --> color("#FFFFFF") # 白色窗户 extrude(h) split(x) { 0 : wall(2) w : wall(2) }

2. 优化材质

优化材质可以提高模型的视觉效果,使其更加逼真。

示例:优化材质

假设我们希望使用高质量的纹理和反射效果,可以使用以下 CGA 规则:

# 定义地块 attr width = 50 attr depth = 30 attr height = 10 # 基础形状 Lot --> extrude(height) comp(f) { front: BuildingFront back: BuildingBack left: BuildingLeft right: BuildingRight } # 前立面 BuildingFront --> wall(2) windows(5, 3) # 后立面 BuildingBack --> wall(2) # 左立面 BuildingLeft --> wall(2) # 右立面 BuildingRight --> wall(2) # 墙体材质 Wall --> texture("wall_texture.jpg") reflection(0.5) # 窗户 attr window_width = 1 attr window_height = 1 attr window_spacing = 1 windows(w, h) --> split(y) { 0 : repeat(w, h) { 0 : window(window_width, window_height) window_spacing : wall(2) } } window(w, h) --> texture("window_texture.jpg") reflection(0.9) extrude(h) split(x) { 0 : wall(2) w : wall(2) }

3. 优化渲染速度

优化渲染速度可以提高用户的交互体验,使其能够更快地查看和编辑模型。这通常包括使用低分辨率的纹理、减少多边形数量、以及优化渲染设置。

示例:优化渲染速度

假设我们希望使用低分辨率的纹理和减少多边形数量来优化渲染速度,可以使用以下 CGA 规则:

# 定义地块 attr width = 50 attr depth = 30 attr height = 10 attr simplify = 0.5 # 基础形状 Lot --> extrude(height) simplify(simplify) comp(f) { front: BuildingFront back: BuildingBack left: BuildingLeft right: BuildingRight } # 前立面 BuildingFront --> wall(2) windows(5, 3) # 后立面 BuildingBack --> wall(2) # 左立面 BuildingLeft --> wall(2) # 右立面 BuildingRight --> wall(2) # 墙体材质 Wall --> texture("low_res_wall_texture.jpg") # 使用低分辨率纹理 reflection(0.5) # 窗户 attr window_width = 1 attr window_height = 1 attr window_spacing = 1 windows(w, h) --> split(y) { 0 : repeat(w, h) { 0 : window(window_width, window_height) window_spacing : wall(2) } } window(w, h) --> texture("low_res_window_texture.jpg") # 使用低分辨率纹理 reflection(0.9) extrude(h) split(x) { 0 : wall(2) w : wall(2) }

4. 优化模型细节

优化模型细节可以提高模型的精细度和真实感,使其更加符合实际设计要求。这包括添加更多的几何细节、使用更复杂的材质和光影效果等。

示例:优化模型细节

假设我们希望在建筑模型中添加更多的几何细节,如屋顶、阳台和装饰物,可以使用以下 CGA 规则:

# 定义地块 attr width = 50 attr depth = 30 attr height = 10 attr roof_height = 2 attr balcony_width = 2 attr balcony_spacing = 3 # 基础形状 Lot --> extrude(height) roof(roof_height) comp(f) { front: BuildingFront back: BuildingBack left: BuildingLeft right: BuildingRight } # 屋顶 roof(roof_height) --> split(z) { 0 : wall(2) height : extrude(roof_height) height + roof_height : roofCap() } roofCap() --> color("#8B4513") # 棕色屋顶 # 前立面 BuildingFront --> wall(2) balconies(balcony_width, balcony_spacing) windows(5, 3) # 后立面 BuildingBack --> wall(2) # 左立面 BuildingLeft --> wall(2) # 右立面 BuildingRight --> wall(2) # 墙体材质 Wall --> texture("high_res_wall_texture.jpg") reflection(0.5) # 阳台 balconies(width, spacing) --> split(y) { 0 : repeat(width, spacing) { 0 : balcony(width) spacing : wall(2) } } balcony(width) --> color("#8B4513") # 棕色阳台 extrude(0.5) split(x) { 0 : wall(2) width : wall(2) } # 窗户 attr window_width = 1 attr window_height = 1 attr window_spacing = 1 windows(w, h) --> split(y) { 0 : repeat(w, h) { 0 : window(window_width, window_height) window_spacing : wall(2) } } window(w, h) --> texture("high_res_window_texture.jpg") reflection(0.9) extrude(h) split(x) { 0 : wall(2) w : wall(2) }

建筑生成的实际案例

为了更好地理解如何在实际项目中应用 CityEngine 的建筑生成与编辑功能,我们来看一个具体的案例:生成一个包含多种建筑风格的城市街区。

1. 导入地块和建筑参数

首先,我们需要导入地块和建筑参数。假设我们有一个包含多个地块的 GIS 数据文件plots.shp和一个包含建筑参数的数据库文件buildings.db

示例代码:
# 导入 GIS 数据importshapefile# 读取 GIS 数据文件sf=shapefile.Reader("plots.shp")# 获取地块的几何形状和属性shapes=sf.shapes()records=sf.records()# 将地块添加到 CityEngine 场景中forshape,recordinzip(shapes,records):plot=shape.points plot=[cg.Point3(x,y,0)forx,yinplot]new_plot=cg.Polygon(plot)cg.addShape(new_plot,"Plot")cg.setAttribute(new_plot,"plot_id",record[0])cg.setAttribute(new_plot,"plot_area",record[1])cg.setAttribute(new_plot,"plot_height",record[2])# 导入建筑数据库importsqlite3# 连接数据库conn=sqlite3.connect("buildings.db")cursor=conn.cursor()# 查询建筑参数cursor.execute("SELECT plot_id, width, depth, height, style FROM buildings")buildings=cursor.fetchall()# 将建筑参数应用到地块forbuildinginbuildings:plot_id,width,depth,height,style=building plot=cg.getShape("Plot",plot_id)cg.setAttribute(plot,"width",width)cg.setAttribute(plot,"depth",depth)cg.setAttribute(plot,"height",height)cg.setAttribute(plot,"style",style)

2. 编写参数化 CGA 规则

接下来,我们需要编写参数化 CGA 规则,以生成不同风格的建筑。

示例 CGA 规则:
# 定义地块 attr width = 50 attr depth = 30 attr height = 10 attr style = "Modern" # 可以是 "Modern", "Historical", "Futuristic" # 基础形状 Lot --> extrude(height) comp(f) { front: BuildingFront(style) back: BuildingBack(style) left: BuildingLeft(style) right: BuildingRight(style) } # 前立面 BuildingFront(style) --> case style == "Modern" : wall(2) case style == "Historical" : historicalWall(2) case style == "Futuristic" : futuristicWall(2) else : wall(2) # 后立面 BuildingBack(style) --> case style == "Modern" : wall(2) case style == "Historical" : historicalWall(2) case style == "Futuristic" : futuristicWall(2) else : wall(2) # 左立面 BuildingLeft(style) --> case style == "Modern" : wall(2) case style == "Historical" : historicalWall(2) case style == "Futuristic" : futuristicWall(2) else : wall(2) # 右立面 BuildingRight(style) --> case style == "Modern" : wall(2) case style == "Historical" : historicalWall(2) case style == "Futuristic" : futuristicWall(2) else : wall(2) # 现代风格墙体 ModernWall --> color("#FF0000") # 红色墙体 texture("modern_wall_texture.jpg") # 历史风格墙体 HistoricalWall --> color("#8B4513") # 棕色墙体 texture("historical_wall_texture.jpg") # 未来风格墙体 FuturisticWall --> color("#00FF00") # 绿色墙体 texture("futuristic_wall_texture.jpg") # 窗户 attr window_width = 1 attr window_height = 1 attr window_spacing = 1 windows(w, h) --> split(y) { 0 : repeat(w, h) { 0 : window(window_width, window_height) window_spacing : wall(2) } } window(w, h) --> color("#FFFFFF") # 白色窗户 texture("window_texture.jpg") reflection(0.9) extrude(h) split(x) { 0 : wall(2) w : wall(2) }

3. 生成建筑模型

在定义了地块和 CGA 规则后,我们使用 CityEngine 的生成工具来生成建筑模型。

示例代码:
# 生成建筑模型forplot_id,buildinginzip(records,buildings):plot=cg.getShape("Plot",plot_id[0])cg.generate(plot,"Building.cga")

4. 优化模型

生成的建筑模型可能需要进一步优化,以满足特定的设计要求。优化可以包括调整高度、添加窗户、修改材质等。

示例代码:
# 调整建筑高度forplot_id,buildinginzip(records,buildings):plot=cg.getShape("Plot",plot_id[0])cg.setAttribute(plot,"height",building[3])cg.generate(plot,"Building.cga")# 添加窗户forplot_id,buildinginzip(records,buildings):plot=cg.getShape("Plot",plot_id[0])cg.setAttribute(plot,"window_width",1.5)cg.setAttribute(plot,"window_height",1.5)cg.setAttribute(plot,"window_spacing",2)cg.generate(plot,"Building.cga")

总结

通过以上内容,我们详细介绍了如何使用 CityEngine 进行建筑生成与编辑。从定义地块、编写 CGA 规则、生成建筑模型到优化模型,每一步都提供了具体的代码示例和数据样例。此外,我们还探讨了高级编辑技巧、利用外部数据源以及实际案例,帮助读者更好地理解和应用这些功能。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/28 16:45:32

装配制造业 - 风机核心产品

通用工业风机六大件(主流版本)中文名称英文名称标准术语 / 常用表达功能简述1. 叶轮ImpellerWheel, Rotor, Runner核心做功部件,将机械能转化为气体动能与压力能2. 机壳(风筒)Housing(Casing, Volute, Scroll)Fan Hous…

作者头像 李华
网站建设 2026/3/28 10:16:29

别再自己飞无人机了!这4个网站,高清航拍素材直接免费用!

瞬间提升大片感,你的“云端无人机”已就位想为你的视频加上震撼的航拍镜头,却没有无人机或时间去拍摄?别担心,专业的视频素材网站就是你随叫随到的“云端机队”。它们提供了海量高清、电影级的航拍画面,能瞬间让你的作…

作者头像 李华
网站建设 2026/4/1 13:07:40

国产操作系统下 Web 部署的实践与深度感悟

一、前言:国产化浪潮下的 Web 部署新命题 随着国产化替代战略的推进,统信 UOS、银河麒麟、深度 Linux 等国产操作系统逐渐成为政企、金融、能源等关键领域的部署首选。作为前端开发者,我在将 VueAxios 开发的 Web 项目迁移至国产 OS 部署时&…

作者头像 李华
网站建设 2026/4/1 23:59:47

全球云基础设施支出增长,人工智能需求驱动市场扩张

来源:维度网 根据研究机构Omdia发布的数据,2025年第三季度,全球云基础设施服务支出达到1026亿美元,较去年同期增长25%。市场已连续五个季度保持超过20%的同比增长,显示出行业的持续增长态势。这一增长得益于人工智能需…

作者头像 李华
网站建设 2026/3/28 4:59:58

CSS Transform 导致 position:fixed 失效的排查与解决

CSS Transform 导致 position:fixed 失效的排查与解决 在 Nuxt 2 项目中,底部导航栏使用了 position: fixed 定位,但始终无法固定在页面底部。经过深入排查,发现罪魁祸首竟然是一个看似不相关的响应式缩放插件。 问题现象 项目是一个基于 Nuxt 2 的保险网站,底部有一个投保…

作者头像 李华