private void Render() { foreach (var item in cachedAnimated) { var hash = item.Key; var matrices = matricesData[hash]; var animated = cachedAnimated[hash]; var entityIds = entityIndices[hash]; var clipData = clipDatas[hash]; var overrideFrameData = overrideFrames[hash]; var currentLength = matrices.Count; var start = 0; while (start < currentLength) { var len = Math.Min(1023, currentLength - start); matrices.CopyTo(start, tempMatrices, 0, len); entityIds.CopyTo(start, tempIndices, 0, len); clipData.CopyTo(start, tempClipDatas, 0, len); overrideFrameData.CopyTo(start, overrideFrameDatas, 0, len); block.SetFloatArray("_CurrentAnimation", tempClipDatas); block.SetFloatArray("_OverrideFrame", overrideFrameDatas); block.SetFloatArray("_EntityID", tempIndices); block.SetBuffer("_RunningState", computeData); Graphics.DrawMeshInstanced(animated.Mesh, 0, animated.Material, tempMatrices, len, block, ShadowCastingMode.On, true); start += len; } } }
public static void LoadInto( [NotNull] MaterialPropertyBlock properties, [NotNull] float[] saturation, [NotNull] float[] fadeout, [NotNull] float[] edgeSharpness, LayerArrays?x, LayerArrays?y, LayerArrays?z) { properties.SetFloatArray(SaturationId, saturation); properties.SetFloatArray(FadeoutId, fadeout); properties.SetFloatArray(EdgeSharpnessId, edgeSharpness); if (x.HasValue) { x.Value.LoadInto(properties, XLayerInputStartId, XLayerInputExtentId, XLayerOutputStartId, XLayerOutputEndId); } if (y.HasValue) { y.Value.LoadInto(properties, YLayerInputStartId, YLayerInputExtentId, YLayerOutputStartId, YLayerOutputEndId); } if (z.HasValue) { z.Value.LoadInto(properties, ZLayerInputStartId, ZLayerInputExtentId, ZLayerOutputStartId, ZLayerOutputEndId); } }
public void Convert() { if (ControlArray.Count <= 0) { return; } _block.Clear(); for (int i = 0; i < ControlArray.Count; i++) { var control = ControlArray[i]; _block.SetTexture($"_Control_{i}", control); } _block.SetTexture("_DiffuseArray", DiffuseArray); _block.SetTexture("_NormalArray", NormalArray); _block.SetFloatArray("_DiffuseIndexes", DiffuseIndexes); _block.SetFloatArray("_NormalIndexes", NormalIndexes); _block.SetVectorArray("_Splats_ST", Tillings); _block.SetFloatArray("_NormalScales", NormalScales); var terrain = GetComponent <Terrain>(); terrain.SetSplatMaterialPropertyBlock(_block); }
public void Draw() { var ListEnum = Lod.GetEnumerator(); int n = 0; Mpb.Clear(); while (ListEnum.MoveNext()) { _matrices[n] = ListEnum.Current.LocalToWorldMatrix; _colors[n] = ListEnum.Current.ArmyColor; _wreckage[n] = ListEnum.Current.IsWreckage; n++; if (n == MaxInstancingCount) { Mpb.SetVectorArray("_Color", _colors); Mpb.SetFloatArray("_Wreckage", _wreckage); Graphics.DrawMeshInstanced(BP.LODs[0].Mesh, 0, BP.LODs[0].Mat, _matrices, n, Mpb, ShadowCasting); //Graphics.DrawMeshInstancedIndirect(SharedMesh, 0, SharedMaterial, , null, 0, null, ShadowCasting); n = 0; Mpb.Clear(); } } if (n > 0) { Mpb.SetVectorArray("_Color", _colors); Mpb.SetFloatArray("_Wreckage", _wreckage); Graphics.DrawMeshInstanced(BP.LODs[0].Mesh, 0, BP.LODs[0].Mat, _matrices, n, Mpb, ShadowCasting); } }
public virtual void WriteData2(Material _material) { MaterialPropertyBlock properties = new MaterialPropertyBlock(); properties.SetInt("_GradientCountPoints", sfPoints.Count); List <float> gradientPoints = new List <float>(); List <float> gradientPointsPositions = new List <float>(); List <float> gradientTypePoints = new List <float>(); if (sfPoints.Count > 0) { int curPos = 0; foreach (T point in sfPoints) { gradientPoints = gradientPoints.Merge(point.ToData()); gradientPointsPositions.Add(curPos); curPos += point.GetSize(); gradientTypePoints.Add((float)point.GetTypeGradient()); } } properties.SetFloatArray("_GradientPoints", gradientPoints.ToArray()); properties.SetFloatArray("_GradientPointsPositions", gradientPointsPositions.ToArray()); properties.SetFloatArray("_GradientTypePoints", gradientTypePoints.ToArray()); //_material.re //_material.SetP //_material.SetFloatArray( "_GradientTypePoints", gradientTypePoints.ToArray() ); }
private void ApplyMaterialPropertyBlock() { MaterialPropertyBlock materialPropertyBlock = new MaterialPropertyBlock(); materialPropertyBlock.SetVectorArray(AVATAR_SHADER_COLOR, LocalAvatarConfig.MaterialPropertyBlock.Colors); materialPropertyBlock.SetFloatArray(AVATAR_SHADER_DIFFUSEINTENSITY, LocalAvatarConfig.MaterialPropertyBlock.DiffuseIntensities); materialPropertyBlock.SetFloatArray(AVATAR_SHADER_RIMINTENSITY, LocalAvatarConfig.MaterialPropertyBlock.RimIntensities); materialPropertyBlock.SetFloatArray(AVATAR_SHADER_REFLECTIONINTENSITY, LocalAvatarConfig.MaterialPropertyBlock.ReflectionIntensities); TargetRenderer.GetClosestReflectionProbes(ReflectionProbes); if (ReflectionProbes != null && ReflectionProbes.Count > 0 && ReflectionProbes[0].probe.texture != null) { materialPropertyBlock.SetTexture(AVATAR_SHADER_CUBEMAP, ReflectionProbes[0].probe.texture); } for (int i = 0; i < TextureArrays.Length; i++) { materialPropertyBlock.SetTexture(TextureTypeToShaderProperties[i], TextureArrays[(int)(TextureType)i].TextureArray); } TargetRenderer.SetPropertyBlock(materialPropertyBlock); }
void Start() { properties = new MaterialPropertyBlock(); properties.SetFloatArray("_reflectX", new float[] { 1, -1, 1, -1, 1, -1, 1, -1 }); properties.SetFloatArray("_reflectY", new float[] { 1, 1, -1, -1, 1, 1, -1, -1 }); properties.SetFloatArray("_swapXY", new float[] { 0, 0, 0, 0, 1, 1, 1, 1 }); }
void Update() { if (block == null) { block = new MaterialPropertyBlock(); block.SetVectorArray(baseColorId, baseColors); block.SetFloatArray(metallicId, metallic); block.SetFloatArray(smoothnessId, smoothness); if (!lightProbeVolume) { var positions = new Vector3[1023]; for (int i = 0; i < matrices.Length; i++) { positions[i] = matrices[i].GetColumn(3); } var lightProbes = new SphericalHarmonicsL2[1023]; var occlusionProbes = new Vector4[1023]; LightProbes.CalculateInterpolatedLightAndOcclusionProbes( positions, lightProbes, occlusionProbes ); block.CopySHCoefficientArraysFrom(lightProbes); block.CopyProbeOcclusionArrayFrom(occlusionProbes); } } Graphics.DrawMeshInstanced( mesh, 0, material, matrices, 1023, block, ShadowCastingMode.On, true, 0, null, lightProbeVolume ? LightProbeUsage.UseProxyVolume : LightProbeUsage.CustomProvided, lightProbeVolume ); }
protected override void PopulatePropertyBlockForRendering(ref MaterialPropertyBlock propertyBlock) { propertyBlock.SetFloat("_Intensity", m_SplashItensity); propertyBlock.SetFloatArray("_OverheadDepthU", m_DepthUs); propertyBlock.SetFloatArray("_OverheadDepthV", m_DepthVs); propertyBlock.SetFloatArray("_SplashStartYPosition", m_StartSplashYPositions); propertyBlock.SetFloat("_SplashGroundOffset", m_SplashSurfaceOffset); }
// Start is called before the first frame update void Start() { var materialProperty = new MaterialPropertyBlock(); float[] floatArray = new float[] { 6f, 1f }; List <string> BlobSize = new List <string>(); List <string> BlobNeg = new List <string>(); List <string> BlobPos = new List <string>(); List <string> BlobNeu = new List <string>(); using (var reader = new StreamReader(@"Assets\AtroneerblobData.csv")) { while (!reader.EndOfStream) { var line = reader.ReadLine(); var values = line.Split(','); BlobSize.Add(values[0]); BlobNeg.Add(values[1]); BlobPos.Add(values[2]); BlobNeu.Add(values[3]); } } print(BlobSize.Count); print(BlobSize[1]); materialProperty.SetFloat("blobCount", (float)BlobSize.Count - 1); float[] sizes = convertStringListToFloatArray(BlobSize, false); float[] negatives = convertStringListToFloatArray(BlobNeg, true); float[] positives = convertStringListToFloatArray(BlobPos, true); float[] neutrals = convertStringListToFloatArray(BlobNeu, true); float[] bokiValues = new float[sizes.Length]; for (int i = 0; i < sizes.Length; i++) { if (negatives[i] > positives[i]) { bokiValues[i] = neutrals[i] + negatives[i]; } else { bokiValues[i] = 1 - (neutrals[i] + positives[i]); } } materialProperty.SetFloatArray("blobSize", sizes); materialProperty.SetFloatArray("blobNeg", negatives); materialProperty.SetFloatArray("blobPos", positives); materialProperty.SetFloatArray("blobNeu", neutrals); materialProperty.SetFloatArray("bokivalues", bokiValues); rend.SetPropertyBlock(materialProperty); }
// Update is called once per frame void Update() { if (block == null) { block = new MaterialPropertyBlock(); block.SetVectorArray(baseColorId, baseColors); block.SetFloatArray(metallicId, metallic); block.SetFloatArray(smoothnessId, smoothness); } Graphics.DrawMeshInstanced(mesh, 0, material, matrices, 1023, block); }
void FixedUpdate() { var materialProperty = new MaterialPropertyBlock(); SetArrayData(balls); materialProperty.SetFloat("_Width", (float)balls.Count); materialProperty.SetFloatArray("xPos", xPos); materialProperty.SetFloatArray("yPos", yPos); gameObject.GetComponent <Renderer>().SetPropertyBlock(materialProperty); // if (x == 5 && y == 5) Debug.Log(transform.position + " offset " + transform.lossyScale); }
public void Draw() { if (Lod == null || Lod.Count == 0) { return; } if (BP.LODs[0].Mesh == null) { //Missing mesh or material, skip rendering Debug.LogWarning("Missing mesh for blueprint " + BP.CodeName, gameObject); return; } else if (BP.LODs[0].Mat == null) { Debug.LogWarning("Missing material for blueprint " + BP.CodeName, gameObject); return; } var ListEnum = Lod.GetEnumerator(); int n = 0; Mpb.Clear(); while (ListEnum.MoveNext()) { _matrices[n] = ListEnum.Current.LocalToWorldMatrix; _colors[n] = ListEnum.Current.ArmyColor; _wreckage[n] = ListEnum.Current.IsWreckage; n++; if (n == MaxInstancingCount) { Mpb.SetVectorArray("_Color", _colors); Mpb.SetFloatArray("_Wreckage", _wreckage); Graphics.DrawMeshInstanced(BP.LODs[0].Mesh, 0, BP.LODs[0].Mat, _matrices, n, Mpb, ShadowCasting, true, 0, null, UnityEngine.Rendering.LightProbeUsage.Off, null); //Graphics.DrawMeshInstanced(UnitsInfo.Current.StrategicMesh, 0, BP.strategicMaterial, _matrices, n, Mpb, UnityEngine.Rendering.ShadowCastingMode.Off, false, 0, null, UnityEngine.Rendering.LightProbeUsage.Off, null); //Graphics.DrawMeshInstancedIndirect(SharedMesh, 0, SharedMaterial, , null, 0, null, ShadowCasting); n = 0; Mpb.Clear(); } } if (n > 0) { Mpb.SetVectorArray("_Color", _colors); Mpb.SetFloatArray("_Wreckage", _wreckage); Graphics.DrawMeshInstanced(BP.LODs[0].Mesh, 0, BP.LODs[0].Mat, _matrices, n, Mpb, ShadowCasting, true, 0, null, UnityEngine.Rendering.LightProbeUsage.Off, null); //Graphics.DrawMeshInstanced(UnitsInfo.Current.StrategicMesh, 0, BP.strategicMaterial, _matrices, n, Mpb, UnityEngine.Rendering.ShadowCastingMode.Off, false, 0, null, UnityEngine.Rendering.LightProbeUsage.Off, null); } }
void Update() { if (block == null) { block = new MaterialPropertyBlock(); block.SetVectorArray(baseColorId, baseColors); block.SetFloatArray(metallicId, metallic); block.SetFloatArray(smoothnessId, smoothness); } //mesh, sub-mesh index zero, material, matrices array, //the amount of elements, and property block as arguments Graphics.DrawMeshInstanced(mesh, 0, material, matrices, 1023, block); }
void Update() { if (Block == null) { Block = new MaterialPropertyBlock { }; Block.SetVectorArray(BaseColorID, baseColors); Block.SetFloatArray(MetallicID, metallic); Block.SetFloatArray(MetallicID, metallic); } Graphics.DrawMeshInstanced(Mesh, 0, Material, matrices, 1023, Block); }
private void SetupShaderAnimationCurve(string curveName, MaterialPropertyBlock propertyBlock, NormalizedAnimationCurve curve) { var keys = curve.Keys; var points = keys.Select(_ => _.value).ToArray(); var tangents = keys.Select(_ => new Vector4(_.inTangent, _.outTangent)).ToArray(); var knotVector = keys.Select(_ => _.time).ToArray(); propertyBlock.SetFloatArray(curveName + "Points", points); propertyBlock.SetVectorArray(curveName + "Tangents", tangents); propertyBlock.SetFloatArray(curveName + "KnotVector", knotVector); propertyBlock.SetFloat(curveName + "PointCount", points.Length); propertyBlock.SetFloat(curveName + "Amplitude", curve.GetAmplitude()); propertyBlock.SetFloat(curveName + "Duration", curve.GetDuration()); }
void TranscribeUVFromMeshToWall(int debugindex, MeshRenderer _meshRenderer, MaterialPropertyBlock _propBlock, Material _mat, Vector3 _vertex0, Vector3 _vertex1, Vector3 _vertex2, Vector3 _vertex3, Vector2 _p0, Vector2 _p1, Vector2 _p2, Vector2 _p3) { _meshRenderer.GetPropertyBlock(_propBlock); var p0 = _p0; var p1 = _p1; var p2 = _p2; var p3 = _p3; Vector2 vertex0 = new Vector2(_vertex0.y, _vertex0.z); Vector2 vertex1 = new Vector2(_vertex1.y, _vertex1.z); Vector2 vertex2 = new Vector2(_vertex2.y, _vertex2.z); Vector2 vertex3 = new Vector2(_vertex3.y, _vertex3.z); Vector3[] source = new[] { new Vector3(_p0.x, _p0.y, 0), new Vector3(_p2.x, _p2.y, 0), new Vector3(_p1.x, _p1.y, 0), new Vector3(_p3.x, _p3.y, 0) }; Vector3[] destination = new[] { new Vector3(_vertex0.y, _vertex0.z, 0), new Vector3(_vertex2.y, _vertex2.z, 0), new Vector3(_vertex1.y, _vertex1.z, 0), new Vector3(_vertex3.y, _vertex3.z, 0) }; FindHomography(ref source, ref destination, ref _matrix); _inverseMatrix = CalcInverseMatrix(_matrix); _propBlock.SetFloatArray("_Homography", _matrix); _propBlock.SetFloatArray("_InvHomography", _inverseMatrix); _homographyVertex = CalcHomographyMatrix(vertex0, vertex2, vertex1, vertex3); _invHomographyVertex = CalcInverseMatrix(_homographyVertex); _homography = CalcHomographyMatrix(p0, p2, p1, p3); _invHomography = CalcInverseMatrix(_homography); _propBlock.SetFloatArray("_InvHomographyUV", _invHomography); _propBlock.SetVectorArray("_VertexValues", new Vector4[] { new Vector4(vertex0.x, vertex0.y, 0.0f, 1.0f), new Vector4(vertex1.x, vertex1.y, 0.0f, 1.0f), new Vector4(vertex2.x, vertex2.y, 0.0f, 1.0f), new Vector4(vertex3.x, vertex3.y, 0.0f, 1.0f) }); _propBlock.SetVectorArray("_UVValues", new Vector4[] { new Vector4(0, 0, 0.0f, 1.0f), new Vector4(1, 0, 0.0f, 1.0f), new Vector4(0, 1, 0.0f, 1.0f), new Vector4(1, 1, 0.0f, 1.0f) //new Vector4(p0.x, p0.y, 0.0f, 1.0f), //new Vector4(p1.x, p1.y, 0.0f, 1.0f), //new Vector4(p2.x, p2.y, 0.0f, 1.0f), //new Vector4(p3.x, p3.y, 0.0f, 1.0f) }); _propBlock.SetTexture("_SpoutTex", _checkboard);// SpoutRT); // //Graphics.Blit(null, _uvQuadRef, _mat, 0); _meshRenderer.SetPropertyBlock(_propBlock); }
public void Draw() { if (m_IsChangeMatPB_Float) { foreach (var kv in m_MatPropertyBlock_Float) { m_MatPB.SetFloatArray(kv.Key, kv.Value.GetArray()); } m_IsChangeMatPB_Float = false; } if (m_IsChangeMatPB_Vec4) { foreach (var kv in m_MatPropertyBlock_Vec4) { m_MatPB.SetVectorArray(kv.Key, kv.Value.GetArray()); } m_IsChangeMatPB_Vec4 = false; } int count = m_Matrix4x4s.Count; if (count > 0) { Graphics.DrawMeshInstanced(m_Mesh, 0, m_Material, m_Matrix4x4s.GetArray(), count, m_MatPB); } }
void Update() { for (int i = 0; i < birdData.Count; i++) { if (birdData[i].curTime >= birdData[i].totalTime) { birdData[i].curTime = 0f; birdData[i].positions.Clear(); for (int j = 0; j < birdData[i].chunkLength; j++) { birdData[i].positions.Add(birdData[i].RandomPos()); } } birdData[i].curTime += Time.deltaTime; birdData[i].birdAlpha = Mathf.Clamp01(birdData[i].curTime); if (birdData[i].curTime >= birdData[i].totalTime - 1f) { birdData[i].birdAlpha = Mathf.Clamp01(Mathf.Abs(birdData[i].totalTime - birdData[i].curTime)); } for (int j = 0; j < birdData[i].chunkLength; j++) { birdData[i].curPos = Vector3.Lerp(birdData[i].positions[j], birdData[i].positions[j] + birdData[i].chunk2End, birdData[i].curTime / birdData[i].totalTime); birdData[i].mPos = birdData[i].curPos; matrixs.Add(Matrix4x4.TRS(birdData[i].mPos, birdData[i].mRot, birdData[i].mScale)); alphas.Add(birdData[i].birdAlpha); } } mpb.SetFloatArray(birdAlphaIndex, alphas); Graphics.DrawMeshInstanced(meshFilter.sharedMesh, 0, meshRenderer.sharedMaterial, matrixs, mpb); matrixs.Clear(); alphas.Clear(); }
private void OnValidate() { if (count <= 0) { count = 1; } if (block == null) { block = new MaterialPropertyBlock(); } block.Clear(); colorList.Clear(); offList.Clear(); cutOffList.Clear(); matrix = new Matrix4x4[count]; for (int i = 0; i < count; i++) { matrix[i] = Matrix4x4.TRS((transform.position + Random.insideUnitSphere * range), Quaternion.identity, Vector3.one); colorList.Add(new Vector4(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f), 1)); offList.Add(new Vector4(Random.Range(0.1f, 5f), Random.Range(0.1f, 5f), Random.Range(0.1f, 1f), Random.Range(0.1f, 1f))); cutOffList.Add(Random.Range(0.0f, 1)); } block.SetFloatArray(cutOff, cutOffList); block.SetVectorArray(maintexST, offList); block.SetVectorArray(baseColorId, colorList); }
void Start() { propertyBlock = new MaterialPropertyBlock(); m_mesh = m_meshFilter.mesh; positions = new List <Vector3>(); targets = new List <Vector3>(); colors = new List <Vector4>(); matrices = new Matrix4x4[num]; var dtList = new float[num]; var colList = new Vector4[num]; for (int i = 0; i < 2; i++) { colors.Add(new Vector4(Random.value, Random.value, Random.value, 1f)); } for (int i = 0; i < num; i++) { positions.Add(new Vector3(Random.Range(-50f, 50f), 0, Random.Range(-50f, 50f))); targets.Add(new Vector3()); matrices[i] = new Matrix4x4(); dtList[i] = i / 1000f; colList[i] = colors[i % colors.Count]; } propertyBlock.SetFloatArray("_DT", dtList); propertyBlock.SetVectorArray("_Col", colList); }
protected JobHandle Render(JobHandle inputDeps) { if (injectRendererDatas.Length != rendererData.Length) { DestroyRendererArrays(); CreateRendererArrays(injectRendererDatas.Length); rendererData = new RendererData(injectRendererDatas.Length); } var handle = new UpdateDataJob { matrices = matrices, yPositions = yPositions, units = injectRendererDatas.stateDatas }.Schedule(injectRendererDatas.Length, 64, inputDeps); handle.Complete(); matrices.CopyTo(rendererData.matrices); yPositions.CopyTo(rendererData.yPositions); for (int i = 0; i < injectRendererDatas.Length; i += 1023) { var len = Mathf.Min(injectRendererDatas.Length - i, 1023); Array.Copy(rendererData.matrices, i, tempMatrices, 0, len); Array.Copy(rendererData.yPositions, i, tempYPositions, 0, len); materialPropertyBlock.SetFloatArray("_YPos", tempYPositions); for (int j = 0; j < StateGraph.rendererData.SubMeshCount; j++) { Graphics.DrawMeshInstanced(StateGraph.rendererData.Mesh, j, StateGraph.rendererData.Materials[j], tempMatrices, len, materialPropertyBlock, StateGraph.rendererData.ShadowCastingMode, StateGraph.rendererData.ReceivesShadows); } } return(handle); }
private void Update() { if (enableAlphaClipCache != enableAlphaClip) { enableAlphaClipCache = enableAlphaClip; if (enableAlphaClip) { material.EnableKeyword(CLIPPING); } else { material.DisableKeyword(CLIPPING); } } if (cacheVec3 != transform.position) { for (int i = 0; i < ARRAY_LENGTH; ++i) { matrices[i] = matrices[i] * Matrix4x4.Translate(transform.position - cacheVec3); } cacheVec3 = transform.position; } if (block == null) { block = new MaterialPropertyBlock(); block.SetVectorArray(baseColorId, baseColors); } for (int i = 0; i < ARRAY_LENGTH; ++i) { cutoffs[i] = cutoff; } block.SetFloatArray(cutoffId, cutoffs); Graphics.DrawMeshInstanced(mesh, 0, material, matrices, ARRAY_LENGTH, block); }
void Start() { // 引数バッファを初期化 argsBuffer = new ComputeBuffer(1, args.Length * sizeof(uint), ComputeBufferType.IndirectArguments); ///uv に idxを入れる Vector2[] uv = InstanceMesh.uv; for (int i = 0; i < uv.Length; i++) { uv[i] = new Vector2(i, 0); } InstanceMesh.uv = uv; // var num = 4096; property = new MaterialPropertyBlock(); var dt = new float[num]; for (int i = 0; i < num; i++) { dt[i] = Random.value * 2f; } property.SetFloatArray("_DT", dt); }
public void SetFloatArray( int propertyId, float[] value, bool isInstanced) { if (isInstanced) { materialPropertyBlock.SetFloatArray(propertyId, value); renderer.SetPropertyBlock(materialPropertyBlock); instancedProperties.Add(propertyId); } else { material.SetFloatArray(propertyId, value); } }
void Update() { Matrix4x4 ringMat; Matrix4x4 cubeMat; Vector3 ringPos, cubePos; Quaternion ringRot, cubeRot; Vector3 cubeScale; t += speed * Time.deltaTime; for (int i = 0; i < rings; i++) { float angle1 = i * 2 * Mathf.PI / rings; ringRot = Quaternion.Euler(0, angle1 * 180 / Mathf.PI, 0); ringPos = new Vector3(Radius * Mathf.Sin(angle1), 0, Radius * Mathf.Cos(angle1)); for (int j = 0; j < cubesPerRing; j++) { float angle2 = j * 2 * Mathf.PI / cubesPerRing; float angle3 = angle1 + angle2 + t; float d = shapeRadius + sideSize * Mathf.Sin(shapeSides * angle3); cubePos = new Vector3(0, d * Mathf.Cos(angle3 + angle1), d * Mathf.Sin(angle3 + angle1)); cubeRot = Quaternion.Euler(angle3 * 180 / Mathf.PI, 0, 0); cubeScale = Vector3.one * (1.5f + 1.5f * Mathf.Sin(angle1 * 2 + j * 2 * Mathf.PI / cubesPerRing + t * 10)); //operations apply right to left ringMat = Matrix4x4.Translate(ringPos) * Matrix4x4.Rotate(ringRot); cubeMat = Matrix4x4.Translate(cubePos) * Matrix4x4.Rotate(cubeRot) * Matrix4x4.Scale(cubeScale); cubes[i * cubesPerRing + j] = transform.localToWorldMatrix * ringMat * cubeMat; matParams[i * cubesPerRing + j] = 2f * Mathf.Cos(angle1 + j * 2 * Mathf.PI / cubesPerRing + t * 20); } } int objectCount = rings * cubesPerRing; int loopRenderBatchSize; MaterialPropertyBlock mpb = new MaterialPropertyBlock(); for (int i = 0; i < objectCount; i += renderBatchSize) { if ((i + renderBatchSize) < objectCount) { loopRenderBatchSize = renderBatchSize; } else { loopRenderBatchSize = objectCount - i; } if (renderBatchMat.Length != loopRenderBatchSize) { renderBatchMat = new Matrix4x4[loopRenderBatchSize]; instMatParamsArray = new float[loopRenderBatchSize]; } renderBatchMat = cubes.GetRange(i, loopRenderBatchSize).ToArray(); instMatParamsArray = matParams.GetRange(i, loopRenderBatchSize).ToArray(); mpb.SetFloatArray("FloatMPB", instMatParamsArray); Graphics.DrawMeshInstanced(mesh, 0, material, renderBatchMat, loopRenderBatchSize, mpb); } }
public static unsafe void SetFloatArrayNative(this MaterialPropertyBlock MPB, string name, NativeList <float> data, ref List <float> buffer) { if (data.Length > 0) // SetFloatArray with length 0 not allowed { assignNativeListToBuffer(data, ref buffer); MPB.SetFloatArray(name, buffer); } }
/// <summary> /// Updates the command buffer. /// </summary> private void UpdateCommandBuffer() { // Determine which cameras are omnidirectional. List <float> sourceCamAreOmnidirectional = new List <float>(); foreach (CameraModel cameraModel in cameraSetup.cameraModels) { sourceCamAreOmnidirectional.Add(cameraModel.isOmnidirectional ? 1 : 0); } // Determine camera parameters to pass to the material as properties. List <float> sourceCamIndices; List <Vector4> sourceCamPositions; List <Matrix4x4> meshTransformationMatrices; _helperDiskBlending.UpdateBlendingParameters(ref blendingMaterial, cameraSetup.cameraModels, PMPerViewMeshesFS.meshTransforms, out sourceCamIndices, out sourceCamPositions, out meshTransformationMatrices); // Update the blending material with the current focal length. _helperFocalSurfaces.SendFocalLengthToBlendingMaterial(ref blendingMaterial); // Indicate the cameras' indices and positions. MaterialPropertyBlock properties = new MaterialPropertyBlock(); properties.SetFloatArray(_shaderNameSourceCamIndex, sourceCamIndices); properties.SetVectorArray(_shaderNameSourceCamPosXYZ, sourceCamPositions); properties.SetFloatArray(_shaderNameSourceCamIsOmnidirectional, sourceCamAreOmnidirectional); // Clear the instructions in the command buffer. _helperCommandBuffer.commandBuffer.Clear(); // Copy the camera target to a temporary render texture, e.g. to copy the skybox in the scene view. int tempID = Shader.PropertyToID("TempCopyColorBuffer"); _helperCommandBuffer.commandBuffer.GetTemporaryRT(tempID, -1, -1, 0, FilterMode.Bilinear); _helperCommandBuffer.commandBuffer.SetRenderTarget(tempID); _helperCommandBuffer.commandBuffer.ClearRenderTarget(true, true, Color.clear); _helperCommandBuffer.commandBuffer.Blit(BuiltinRenderTextureType.CameraTarget, tempID); // Clear the camera target's color and depth buffers. _helperCommandBuffer.commandBuffer.SetRenderTarget(BuiltinRenderTextureType.CameraTarget); _helperCommandBuffer.commandBuffer.ClearRenderTarget(true, true, Color.clear); // Render the focal surfaces to the depth and color buffer using GPU instancing. _helperCommandBuffer.commandBuffer.DrawMeshInstanced(PMPerViewMeshesFS.meshTransforms[0].GetComponent <MeshFilter>().sharedMesh, 0, blendingMaterial, 0, meshTransformationMatrices.ToArray(), PMPerViewMeshesFS.meshTransforms.Length, properties); // Normalize the RGB channels of the color buffer by the alpha channel, by copying into a temporary render texture. // Note: Be sure to use ZWrite Off. Blit renders a quad, and thus - if ZWrite On - provides the target with the quad's depth, not the render texture's depth. _helperCommandBuffer.commandBuffer.Blit(BuiltinRenderTextureType.CameraTarget, tempID, blendingMaterial, 1); _helperCommandBuffer.commandBuffer.Blit(tempID, BuiltinRenderTextureType.CameraTarget); _helperCommandBuffer.commandBuffer.ReleaseTemporaryRT(tempID); }
void Update() { if (_block == null) { _block = new MaterialPropertyBlock(); _block.SetVectorArray(_baseColorId, _baseColors); _block.SetFloatArray(_metallicId, _metallic); _block.SetFloatArray(_smoothnessId, _smoothness); if (!_lightProbeVolume) { var positions = new Vector3[1023]; for (int i = 0; i < _matrices.Length; i++) { positions[i] = _matrices[i].GetColumn(3); } var lightProbes = new SphericalHarmonicsL2[1023]; var occlusionProbes = new Vector4[1023]; LightProbes.CalculateInterpolatedLightAndOcclusionProbes( positions, lightProbes, occlusionProbes ); _block.CopySHCoefficientArraysFrom(lightProbes); _block.CopyProbeOcclusionArrayFrom(occlusionProbes); } } Graphics.DrawMeshInstanced( _mesh, submeshIndex: 0, _material, _matrices, count: 1023, _block, ShadowCastingMode.On, receiveShadows: true, layer: 0, camera: null, _lightProbeVolume ? LightProbeUsage.UseProxyVolume : LightProbeUsage.CustomProvided, _lightProbeVolume ); }
// Update is called once per frame void Update() { if (_block == null) { _block = new MaterialPropertyBlock(); _block.SetVectorArray(_baseColorId, colors); _block.SetFloatArray(_cutOffId, cutOffs); } Graphics.DrawMeshInstanced(_mesh, 0, _material, matrixs, number, _block); }