对象、属性、构造参数
对象、属性与构造参数
你可以使用
<mesh
visible
userData={{ hello: "world" }}
position={new THREE.Vector3(1, 2, 3)}
rotation={new THREE.Euler(Math.PI / 2, 0, 0)}
geometry={new THREE.SphereGeometry(1, 16, 16)}
material={
new THREE.MeshBasicMaterial({
color: new THREE.Color("hotpink"),
transparent: true,
})
}
/>
问题是,所有这些属性将总是被重新创建。相反,你应该声明性地定义属性。
<mesh
visible
userData={{ hello: "world" }}
position={[1, 2, 3]}
rotation={[Math.PI / 2, 0, 0]}
>
<sphereGeometry args={[1, 16, 16]} />
<meshStandardMaterial color="hotpink" transparent />
</mesh>
构造函数参数
在new THREE.SphereGeometry(1, 32)
someObject.visible = true
<sphereGeometry args={[1, 32]} />
所有属性的底层对象都有一个
<mesh position={[1, 2, 3]} />
<meshStandardMaterial color="hotpink" />
有
// Translates to <mesh scale={[1, 1, 1]} />
<mesh scale={1} />
如果你想深入到嵌套属性(例如:Mesh.rotation.x
<mesh rotation-x={1} material-uniforms-resolution-value={[512, 512]} />
处理非场景对象
你也可以将非
使用 attach
将对象绑定到它们的父级。如果你取消了附加的对象,它将自动从它的父对象上取下。以下是将一个材质附加到网格的 material
属性,将一个几何体附加到几何体属性。
<mesh>
<meshBasicMaterial attach="material">
<boxGeometry attach="geometry">
// Attach bar to foo.a
<foo>
<bar attach="a" />
// Attach bar to foo.a.b and foo.a.b.c (nested object attach)
<foo>
<bar attach="a-b" />
<bar attach="a-b-c" />
// Attach bar to foo.a[0] and foo.a[1] (array attach is just object attach)
<foo>
<bar attach="a-0" />
<bar attach="a-1" />
// Attach bar to foo via explicit add/remove functions
<foo>
<bar attach={(parent, self) => {
parent.add(self)
return () => parent.remove(self)
}} />
// The same as a one liner
<foo>
<bar attach={(parent, self) => (parent.add(self), () => parent.remove(self))} />
实际场景中可以如下使用:
- <directionalLight
- castShadow
- position={[2.5, 8, 5]}
- shadow-mapSize={[1024, 1024]}
- shadow-camera-far={50}
- shadow-camera-left={-10}
- shadow-camera-right={10}
- shadow-camera-top={10}
- shadow-camera-bottom={-10}
- />
+ <directionalLight castShadow position={[2.5, 8, 5]} shadow-mapSize={[1024, 1024]}>
+ <orthographicCamera attach="shadow-camera" args={[-10, 10, 10, -10]} />
+ </directionalLight>