Я пробовал следующее:
- добавление UV1
- custom0
- изменение масштаба фигуры
- изменение VertexBuffer
material {
name : terrain,
shadingModel : unlit,
vertexDomain : object,
parameters : [
{ type : float4, name : baseColor },
{ type : float4, name : peakColor },
{ type : float2, name : heightRange }
],
variables : [
{ name : heightFactor, type : float, interpolation : smooth }
],
requires : [ uv0 ]
}
vertex {
void materialVertex(inout MaterialVertexInputs tp) {
float h = getPosition().y;
float minH = materialParams.heightRange.x;
float maxH = materialParams.heightRange.y;
// Normalización del factor de altura
variable_heightFactor.y = clamp((h - minH) / (maxH - minH), 0.0, 1.0);
}
}
fragment {
void material(inout MaterialInputs material) {
prepareMaterial(material);
float t = variable_heightFactor.y;
material.baseColor = mix(materialParams.baseColor, materialParams.peakColor, t);
}
}
class FilamentSurface @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null
) : SurfaceView(context, attrs), SurfaceHolder.Callback {
private var engine: Engine? = null
private var renderer: Renderer? = null
private var swapChain: SwapChain? = null
private var scene: Scene? = null
private var camera: Camera? = null
private var view: View? = null
private var renderThread: HandlerThread? = null
private var renderHandler: Handler? = null
private val pending = ConcurrentLinkedQueue Unit>()
private val entities = mutableListOf()
private val materials = mutableListOf()
private val materialInstances = mutableListOf()
private var terrainMaterial: Material? = null
private var terrainMI: MaterialInstance? = null
private var terrainEntity: Int = 0
var onFilamentReady: ((Camera) -> Unit)? = null
init {
holder.addCallback(this)
}
private fun createTerrainMesh(): Pair {
val engine = engine ?: throw IllegalStateException("Engine not initialized")
val gridSide = 30
val vertexSide = gridSide + 1
val vertexCount = vertexSide * vertexSide
val offset = gridSide / 2f
val vertices = FloatArray(vertexCount * 5)
var vIdx = 0
for (y in 0 until vertexSide) {
for (x in 0 until vertexSide) {
vertices[vIdx++] = x.toFloat() - offset
vertices[vIdx++] = (Math.random() * 15.0).toFloat()
vertices[vIdx++] = y.toFloat() - offset
vertices[vIdx++] = x.toFloat() / gridSide.toFloat()
vertices[vIdx++] = y.toFloat() / gridSide.toFloat()
}
}
val vb = VertexBuffer.Builder()
.vertexCount(vertexCount)
.bufferCount(1)
.attribute(VertexBuffer.VertexAttribute.POSITION, 0, VertexBuffer.AttributeType.FLOAT3, 0, 20)
.attribute(VertexBuffer.VertexAttribute.UV0, 0, VertexBuffer.AttributeType.FLOAT2, 12, 20)
.build(engine)
val vBuffer = ByteBuffer.allocateDirect(vertices.size * 4).order(ByteOrder.nativeOrder())
vBuffer.asFloatBuffer().put(vertices)
vBuffer.rewind()
vb.setBufferAt(engine, 0, vBuffer)
val indices = ShortArray(gridSide * gridSide * 6)
var iIdx = 0
for (y in 0 until gridSide) {
for (x in 0 until gridSide) {
val quad = y * vertexSide + x
indices[iIdx++] = quad.toShort()
indices[iIdx++] = (quad + vertexSide).toShort()
indices[iIdx++] = (quad + vertexSide + 1).toShort()
indices[iIdx++] = quad.toShort()
indices[iIdx++] = (quad + vertexSide + 1).toShort()
indices[iIdx++] = (quad + 1).toShort()
}
}
val ib = IndexBuffer.Builder()
.indexCount(indices.size)
.bufferType(IndexBuffer.Builder.IndexType.USHORT)
.build(engine)
val iBuffer = ByteBuffer.allocateDirect(indices.size * 2).order(ByteOrder.nativeOrder())
iBuffer.asShortBuffer().put(indices)
iBuffer.rewind()
ib.setBuffer(engine, iBuffer)
return Pair(vb, ib)
}
Подробнее здесь: https://stackoverflow.com/questions/798 ... only-green
Мобильная версия