private void Update() { float spinAngleDelta = 0.125f * PI * Time.deltaTime; FractalPart rootPart = parts[0][0]; rootPart.spinAngle += spinAngleDelta; rootPart.worldRotation = mul(transform.rotation, mul(rootPart.rotation, quaternion.RotateY(rootPart.spinAngle)) ); rootPart.worldPosition = transform.position; parts[0][0] = rootPart; float objectScale = transform.lossyScale.x; float3x3 r = float3x3(rootPart.worldRotation) * objectScale; matrices[0][0] = float3x4(r.c0, r.c1, r.c2, rootPart.worldPosition); float scale = objectScale; JobHandle jobHandle = default; for (int li = 1; li < parts.Length; li++) { scale *= 0.5f; jobHandle = new UpdateFractalLevelJob { spinAngleDelta = spinAngleDelta, scale = scale, parents = parts[li - 1], parts = parts[li], matrices = matrices[li] }.ScheduleParallel(parts[li].Length, 5, jobHandle); // job을 계속 등록. } jobHandle.Complete(); // 등록된 job을 일괄 계산. var bounds = new Bounds(rootPart.worldPosition, 3f * objectScale * Vector3.one); bounds.extents = Vector3.one * 0.5f; for (int i = 0; i < matricesBuffers.Length; i++) { ComputeBuffer buffer = matricesBuffers[i]; buffer.SetData(matrices[i]); propertyBlock.SetBuffer(matricesId, buffer); Graphics.DrawMeshInstancedProcedural( mesh, 0, material, bounds, buffer.count, propertyBlock ); } }
void Update() { float spinAngleDelta = 0.125f * PI * Time.deltaTime; FractalPart rootPart = parts[0][0]; rootPart.spinAngle += spinAngleDelta; rootPart.worldRotation = mul(transform.rotation, mul(rootPart.rotation, quaternion.RotateY(rootPart.spinAngle)) ); parts[0][0] = rootPart; float scale = ObjectScale; JobHandle jobHandle = default; for (int li = 1; li < parts.Length; li++) { scale *= 0.5f; jobHandle = new UpdateFractalLevelJob { spinAngleDelta = spinAngleDelta, scale = scale, parents = parts[li - 1], parts = parts[li], matrices = matrices[li] }.ScheduleParallel(parts[li].Length, 1, jobHandle); } jobHandle.Complete(); var bounds = new Bounds(Vector3.zero, 3f * ObjectScale * Vector3.one); for (int i = 0; i < matricesBuffers.Length; i++) { ComputeBuffer buffer = matricesBuffers[i]; buffer.SetData(matrices[i]); propertyBlock.SetBuffer(matricesId, buffer); material.SetBuffer(matricesId, buffer); Graphics.DrawMeshInstancedProcedural(mesh, 0, material, bounds, buffer.count, propertyBlock); } }
private void Update() { float spinAngleDelta = 0.125f * PI * Time.deltaTime; FractalPart rootPart = parts[0][0]; rootPart.spinAngle += spinAngleDelta; rootPart.worldRotation = mul(transform.rotation, mul(rootPart.rotation, quaternion.RotateY(rootPart.spinAngle))); parts[0][0] = rootPart; // FractalPart is struct, thus have to reassign to copy the value float objectScale = transform.localScale.x; float3x3 r = float3x3(rootPart.worldRotation) * objectScale; matrices[0][0] = float3x4(r.c0, r.c1, r.c2, rootPart.worldPosition); float scale = objectScale; JobHandle jobHandle = default; for (int li = 1; li != depth; ++li) { scale *= 0.5f; jobHandle = new UpdateFractalLevelJob() { spinAngleDelta = spinAngleDelta, scale = scale, parents = parts[li - 1], parts = parts[li], matrices = matrices[li] }.ScheduleParallel(parts[li].Length, 10, jobHandle); } jobHandle.Complete(); Bounds bounds = new Bounds(rootPart.worldPosition, 3f * objectScale * Vector3.one); for (int i = 0; i < matricesBuffers.Length; ++i) { ComputeBuffer buffer = matricesBuffers[i]; buffer.SetData(matrices[i]); propertyBlock.SetBuffer(matricesId, buffer); Graphics.DrawMeshInstancedProcedural(mesh, 0, material, bounds, buffer.count, propertyBlock); } }
private void Update() { float spinAngleDelta = 0.125f * PI * Time.deltaTime; FractalPart rootPart = parts[0][0]; rootPart.spinAngle += spinAngleDelta; rootPart.worldRotation = mul(transform.rotation, mul(rootPart.rotation, quaternion.RotateY(rootPart.spinAngle))); parts[0][0] = rootPart; // FractalPart is struct, thus have to reassign to copy the value float objectScale = transform.localScale.x; float3x3 r = float3x3(rootPart.worldRotation) * objectScale; matrices[0][0] = float3x4(r.c0, r.c1, r.c2, rootPart.worldPosition); float scale = objectScale; JobHandle jobHandle = default; for (int li = 1; li != depth; ++li) { scale *= 0.5f; jobHandle = new UpdateFractalLevelJob() { spinAngleDelta = spinAngleDelta, scale = scale, parents = parts[li - 1], parts = parts[li], matrices = matrices[li] }.ScheduleParallel(parts[li].Length, 10, jobHandle); } jobHandle.Complete(); int leafIndex = matricesBuffers.Length - 1; Bounds bounds = new Bounds(rootPart.worldPosition, 3f * objectScale * Vector3.one); for (int i = 0; i < matricesBuffers.Length; ++i) { Color colorA, colorB = default; Mesh instanceMesh = null; if (i == leafIndex) { colorA = leafColorA; colorB = leafColorB; instanceMesh = leafMesh; } else { float gradientInterpolate = i / (matricesBuffers.Length - 1.0f); colorA = gradientA.Evaluate(gradientInterpolate); colorB = gradientB.Evaluate(gradientInterpolate); instanceMesh = mesh; } ComputeBuffer buffer = matricesBuffers[i]; buffer.SetData(matrices[i]); propertyBlock.SetColor(colorAID, colorA); propertyBlock.SetColor(colorBID, colorB); propertyBlock.SetBuffer(matricesId, buffer); propertyBlock.SetVector(sequenceNumbersId, sequenceNumbers[i]); Graphics.DrawMeshInstancedProcedural(instanceMesh, 0, material, bounds, buffer.count, propertyBlock); } }
void Update() { var deltaTime = Time.deltaTime; var level = 0; var root = children[level][0]; root.spinAngle += root.spinSpeed * deltaTime; var rootTransform = transform; root.worldRotation = math.mul(rootTransform.rotation, math.mul(root.rotation, quaternion.RotateY(root.spinAngle))); root.worldPosition = rootTransform.position; children[level][0] = root; var scale = rootTransform.lossyScale.x; matrices[level][0] = UpdateFractalLevelJob.Matrix(root, scale); level++; JobHandle jobHandle = default; for (; level < children.Length; level++) { scale *= CHILD_SCALE; var job = new UpdateFractalLevelJob { childCount = CHILD_ROTATION.Length, deltaTime = deltaTime, scale = scale, parents = children[level - 1], children = children[level], matrices = matrices[level] }; jobHandle = job.ScheduleParallel(children[level].Length, 5, jobHandle); } jobHandle.Complete(); var bounds = new Bounds(root.worldPosition, 3 * scale * Vector3.one); var leafIndex = matricesBuffers.Length - 1; for (var index = 0; index < matricesBuffers.Length; index++) { var buffer = matricesBuffers[index]; buffer.SetData(matrices[index]); propertyBlock.SetBuffer(MATRICES, buffer); Mesh instanceMesh; Color color1, color2; if (index == leafIndex) { color1 = leafColor1; color2 = leafColor2; instanceMesh = leafMesh; } else { var gradientInterpolator = index / (matricesBuffers.Length - 2f); color1 = gradient1.Evaluate(gradientInterpolator); color2 = gradient2.Evaluate(gradientInterpolator); instanceMesh = mesh; } propertyBlock.SetColor(COLOR_1, color1); propertyBlock.SetColor(COLOR_2, color2); propertyBlock.SetVector(SEQUENCE_NUMBERS, sequenceNumbers[index]); Graphics.DrawMeshInstancedProcedural(instanceMesh, 0, material, bounds, buffer.count, propertyBlock); } }
void Update() { float deltaTime = Time.deltaTime; FractalPart rootPart = parts[0][0]; rootPart.spinAngle += rootPart.spinVelocity * deltaTime; rootPart.worldRotation = mul(transform.rotation, mul(rootPart.rotation, quaternion.RotateY(rootPart.spinAngle))); rootPart.worldPosition = transform.position; parts[0][0] = rootPart; float objectScale = transform.lossyScale.x; float3x3 r = float3x3(rootPart.worldRotation) * objectScale; matrices[0][0] = float3x4(r.c0, r.c1, r.c2, rootPart.worldPosition); float scale = objectScale; JobHandle jobHandle = default; for (int li = 1; li < parts.Length; li++) { scale *= 0.5f; jobHandle = new UpdateFractalLevelJob { deltaTime = deltaTime, scale = scale, parents = parts[li - 1], parts = parts[li], matrices = matrices[li] }.ScheduleParallel(parts[li].Length, 5, jobHandle); } jobHandle.Complete(); var bounds = new Bounds(Vector3.zero, 3f * objectScale * Vector3.one); int leafIndex = matricesBuffers.Length - 1; for (int i = 0; i < matricesBuffers.Length; i++) { ComputeBuffer buffer = matricesBuffers[i]; buffer.SetData(matrices[i]); Color colorA, colorB; Mesh instanceMesh; if (i == leafIndex) { colorA = leafColorA; colorB = leafColorB; instanceMesh = leafMesh; } else { float gradientInterpolator = i / (matricesBuffers.Length - 2f); colorA = gradientA.Evaluate(gradientInterpolator); colorB = gradientB.Evaluate(gradientInterpolator); instanceMesh = mesh; } propertyBlock.SetColor(colorAId, colorA); propertyBlock.SetColor(colorBId, colorB); propertyBlock.SetBuffer(matricesId, buffer); propertyBlock.SetVector(sequenceNumbersId, sequenceNumbers[i]); Graphics.DrawMeshInstancedProcedural( instanceMesh, 0, material, bounds, buffer.count, propertyBlock); } }