正如标题中提到的,我想知道如何在Godo3.1中使用gdscript创建一个3d对象。我是戈多的新手。我已经搜索并学习了一些教程,这真的很有帮助。我想知道如何
用GDScript。我只知道这一点
var cube1 = MeshInstance.new()
我对现场处理有点了解,但如果可能的话,我想遵循这个方法。事先非常感谢
发布于 2020-03-31 14:56:23
米孜,
最初,您的脚本将需要附加到父节点。我正在使用一个普通的Node对象。将其更改为父节点的任何类型。
据我所知,对代码进行了注释,解释了实现您的要点所需的每一步。我已经更改了步骤1和3的执行顺序,因为在将其节点定义为父节点的子节点之前,需要设置脚本。否则,它将遵循您所说的相同的顺序。
在创建网格之前,您需要一个Physics Object。你用的是你自己。
extends Node # or whatever object type it's attached to
# Preloads script to be attached
const my_script = preload("res://Scripts/your_script.gd")
func _ready(): # Runs when scene is initialized
# STEP 1: add a cube to the scene
# Step 1.1, create a Physics body.
# I'm using a static body but this can be any
# other type of Physics Body
var cube = StaticBody.new()
cube.transform.origin = Vector3(0, 0, 0) # change initial pos here
# STEP 3: attach script
# It is actually required to have the script
# attached before a node is defined as a child node
# to the parent. So your step 3 goes here
cube.set_script(my_script)
self.add_child(cube) # Add as a child node to self
# Step 1.2, add a collision shape to the
# Physics body, defining its shape to a Box (cube)
var coll = CollisionShape.new()
coll.shape = BoxShape.new()
cube.add_child(coll) # Add as a child node to cube
# Step 1.3, add a mesh, so that it's visible
var mesh = MeshInstance.new()
mesh.mesh = CubeMesh.new()
cube.add_child(mesh) # Add as a child to cube
# STEP 2: change texture
# Step 2.1, load your texture from pc
var new_texture = ImageTexture.new()
new_texture.load("res://path/to/new_texture.png")
# Step 2.2, get material from cube
var cube_material = mesh.get_surface_material(0)
# Step 2.3, change texture from material to your new texture
cube_material.albedo_texture = new_texture希望这能有所帮助。如果有任何问题,请告诉我。
https://stackoverflow.com/questions/60783998
复制相似问题