-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
361 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
core/src/commonMain/kotlin/info/laht/threekt/objects/InstancedMesh.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package info.laht.threekt.objects | ||
|
||
import info.laht.threekt.core.* | ||
import info.laht.threekt.materials.Material | ||
import info.laht.threekt.math.Color | ||
import info.laht.threekt.math.Matrix4 | ||
|
||
class InstancedMesh( | ||
geometry: BufferGeometry? = null, | ||
materials: MutableList<Material>? = null, | ||
var count: Int | ||
) : Mesh(geometry, materials) { | ||
|
||
val instanceMatrix: FloatBufferAttribute = FloatBufferAttribute(FloatArray(count * 16), 16) | ||
var instanceColor: FloatBufferAttribute? = null | ||
override var frustumCulled = false | ||
|
||
fun copy(source: InstancedMesh): InstancedMesh { | ||
super.copy(source) | ||
instanceMatrix.copy(source.instanceMatrix) | ||
instanceColor = source.instanceColor?.clone() | ||
count = source.count | ||
return this | ||
} | ||
|
||
fun getColorAt(index: Int, color: Color) { | ||
val instanceColor = this.instanceColor | ||
checkNotNull(instanceColor) { "Instance color is null." } | ||
color.fromArray(instanceColor.buffer, index * 3) | ||
} | ||
|
||
fun getMatrixAt(index: Int, matrix: Matrix4) { | ||
matrix.fromArray(instanceMatrix.buffer, index * 16) | ||
} | ||
|
||
fun setColorAt(index: Int, color: Color) { | ||
if (instanceColor === null) { | ||
instanceColor = FloatBufferAttribute(FloatArray(count * 3), 3) | ||
} | ||
color.toArray(instanceColor!!.buffer, index * 3) | ||
} | ||
|
||
fun setMatrixAt(index: Int, matrix: Matrix4) { | ||
matrix.toArray(instanceMatrix.buffer, index * 16) | ||
} | ||
|
||
override fun raycast(raycaster: Raycaster, intersects: MutableList<Intersection>) { | ||
val matrixWorld = matrixWorld | ||
val raycastTimes = count | ||
|
||
_mesh.geometry = geometry | ||
_mesh.material = material | ||
|
||
if (_mesh.materials.isEmpty()) { | ||
return | ||
} | ||
|
||
for (instanceId in 0 until raycastTimes) { | ||
|
||
// calculate the world matrix for each instance | ||
|
||
getMatrixAt(instanceId, _instanceLocalMatrix) | ||
|
||
_instanceWorldMatrix.multiplyMatrices(matrixWorld, _instanceLocalMatrix) | ||
|
||
// the mesh represents this single instance | ||
|
||
_mesh.matrixWorld.copy(_instanceWorldMatrix) | ||
|
||
_mesh.raycast(raycaster, _instanceIntersects) | ||
|
||
// process the result of raycast | ||
|
||
for (intersect in _instanceIntersects) { | ||
intersect.instanceId = instanceId | ||
intersect.`object` = this | ||
intersects.add(intersect) | ||
} | ||
|
||
_instanceIntersects.clear() | ||
} | ||
} | ||
|
||
fun dispose() { | ||
dispatchEvent("dispose", this) | ||
} | ||
|
||
companion object { | ||
private val _instanceLocalMatrix = Matrix4() | ||
private val _instanceWorldMatrix = Matrix4() | ||
private val _instanceIntersects = mutableListOf<Intersection>() | ||
private val _mesh = Mesh() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 10 additions & 2 deletions
12
core/src/commonMain/kotlin/info/laht/threekt/renderers/shaders/chunk/worldpos_vertex.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.