private void Update() { if (_sheet == null) { _sheet = new MaterialPropertyBlock(); } _sheet.SetVector("_EffectVector", _effectVector); if (_linkedMeshRenderers != null) { foreach (var ren in _linkedMeshRenderers) { ren.SetPropertyBlock(_sheet); } } }
void Start() { Debug.Log(m_renderer.sprite.vertices.Length); foreach (Vector2 var in m_renderer.sprite.vertices) { Debug.Log(string.Format("x={0} y={1}", var.x, var.y)); } MaterialPropertyBlock mpb = new MaterialPropertyBlock(); mpb.Clear(); mpb.SetVector(1, new Vector4(1, 1, 0, 0.5f)); m_renderer.SetPropertyBlock(mpb); }
private ShadowFactory() { cmd = new CommandBuffer { name = "Shadow Commands" }; materialProps = new MaterialPropertyBlock(); materialProps.SetVector(ShaderId.CLIP_RECT, new Vector4(float.NegativeInfinity, float.NegativeInfinity, float.PositiveInfinity, float.PositiveInfinity)); materialProps.SetInt(ShaderId.COLOR_MASK, (int)ColorWriteMask.All); // Render shadow even if mask hide graphic ShaderProperties.Init(8); blurConfig = ScriptableObject.CreateInstance <ScalableBlurConfig>(); blurConfig.hideFlags = HideFlags.HideAndDontSave; blurProcessor = new ScalableBlur(); blurProcessor.Configure(blurConfig); }
// Use this for initialization void Start() { x_pos = Random.Range(1, 9); y_pos = Random.Range(1, 9); tilling = 1.0f / 9.0f; var renderer = GetComponent <Renderer>(); MaterialPropertyBlock matblock = new MaterialPropertyBlock(); if (_Texture2D != null) { matblock.SetTexture("_MainTex", _Texture2D); } matblock.SetVector("_MainTex_ST", new Vector4(tilling, tilling, tilling * x_pos, tilling * y_pos)); matblock.SetColor("_Color", _Color); renderer.SetPropertyBlock(matblock); }
public void SetTestTexture() { var texture = new Texture2D(2048, 1024); var colors = texture.GetPixels(); for (int i = 0; i < colors.Length; i++) { colors[i] = Color.white; } texture.SetPixels(colors); var blockMaterial = new MaterialPropertyBlock(); _meshRenderer.GetPropertyBlock(blockMaterial); blockMaterial.SetTexture(_shaderPropertyTexture, texture); blockMaterial.SetVector(_shaderPropertyColor, Color.white); _meshRenderer.SetPropertyBlock(blockMaterial); }
void Update() { if (_needsReset) { ResetResources(); } if (Application.isPlaying) { // Execute the kernel shaders. SwapBuffers(); UpdateKernelShader(); Graphics.Blit(_positionBuffer1, _positionBuffer2, _kernelMaterial, 2); Graphics.Blit(_velocityBuffer1, _velocityBuffer2, _kernelMaterial, 3); } else { // Reset and execute the kernel shaders repeatedly. Graphics.Blit(null, _positionBuffer2, _kernelMaterial, 0); Graphics.Blit(null, _velocityBuffer2, _kernelMaterial, 1); for (var i = 0; i < 32; i++) { SwapBuffers(); UpdateKernelShader(); Graphics.Blit(_positionBuffer1, _positionBuffer2, _kernelMaterial, 2); Graphics.Blit(_velocityBuffer1, _velocityBuffer2, _kernelMaterial, 3); } } // Draw lines. UpdateLineShader(); var matrix = transform.localToWorldMatrix; var stride = LinesPerDraw; var total = TotalLineCount; var props = new MaterialPropertyBlock(); var uv = new Vector2(0.5f / _historyLength, 0); for (var i = 0; i < total; i += stride) { uv.y = (0.5f + i) / total; props.SetVector("_BufferOffset", uv); Graphics.DrawMesh(_mesh, matrix, _lineMaterial, 0, null, 0, props); } }
private void OnEnable() { int length = resolution * resolution; hashes = new NativeArray <uint>(length, Allocator.Persistent); hashesBuffer = new ComputeBuffer(length, 4); new HashJob { hashes = hashes }.ScheduleParallel(hashes.Length, resolution, default).Complete(); hashesBuffer.SetData(hashes); propertyBlock ??= new MaterialPropertyBlock(); propertyBlock.SetBuffer(hashesId, hashesBuffer); propertyBlock.SetVector(configId, new Vector4(resolution, 1f / resolution)); }
public static PictureFlusher.RenderDraw stencil1(PictureFlusher.RenderLayer layer, MeshMesh mesh) { Vector4 viewport = layer.viewport; var mat = _stencilMat; var pass = 2; var props = new MaterialPropertyBlock(); props.SetVector("_viewport", viewport); return(new PictureFlusher.RenderDraw { mesh = mesh, pass = pass, material = mat, properties = props, }); }
void Update() { if (targetRenderer == null) { return; } if (mpb == null) { mpb = new MaterialPropertyBlock(); } targetRenderer.GetPropertyBlock(mpb); mpb.SetVector("_LitPos", transform.position); mpb.SetFloat("_Intensity", intensity); mpb.SetColor("_LitCol", color); targetRenderer.SetPropertyBlock(mpb); }
// ------------------------------------------------------------------------ // end: sphere // capsule // ------------------------------------------------------------------------ public static void DrawCapsule(Vector3 center, Quaternion rotation, float height, float radius, int latSegmentsPerCap, int longSegmentsPerCap, Color color, bool depthTest = true) { Mesh mesh = PrimitiveMeshFactory.Capsule(latSegmentsPerCap, longSegmentsPerCap); if (mesh == null) { return; } Material material = GetMaterial(depthTest); MaterialPropertyBlock materialProperties = GetMaterialPropertyBlock(); materialProperties.SetColor("_Color", color); materialProperties.SetVector("_Dimensions", new Vector4(radius, radius, radius, height)); Graphics.DrawMesh(mesh, center, rotation, material, 0, null, 0, materialProperties, false, false, false); }
public static PictureFlusher.RenderDraw fill0(PictureFlusher.RenderLayer layer, MeshMesh mesh) { Vector4 viewport = layer.viewport; var mat = _fill0Mat.getMaterial(layer.ignoreClip); var pass = 0; var props = new MaterialPropertyBlock(); props.SetVector("_viewport", viewport); return(new PictureFlusher.RenderDraw { mesh = mesh, pass = pass, material = mat, properties = props, }); }
private void Update() { if (mask != null) { mask.GetWorldCorners(corners); Vector4 r = new Vector4(corners[0].x, corners[0].y, corners[2].x, corners[2].y); if (r != clipRect) { clipRect = r; materialBlock.SetVector(clipRectId, clipRect); foreach (Renderer renderer in renderers) { renderer.SetPropertyBlock(materialBlock); } } } }
public static void Play(this MeshAnimationAsset asset, MaterialPropertyBlock block, string animationName, float speed = 1f, float time = 0f) { var data = asset.animationData.Find(d => d.clip.name == animationName); var start = data.startFrame; var length = data.lengthFrames; speed = Mathf.Max(0.01f, speed); speed /= Mathf.Max(data.clip.length, 0.01f); time += Time.timeSinceLevelLoad; block.SetVector(AnimationTimeProp, new Vector4(start, length, speed, time)); }
protected override void SetScreenUniforms(TerrainNode node, TerrainQuad quad, MaterialPropertyBlock matPropertyBlock) { var ox = quad.Ox; var oy = quad.Oy; var l = quad.Length; var p0 = new Vector3d(ox, oy, R); var p1 = new Vector3d(ox + l, oy, R); var p2 = new Vector3d(ox, oy + l, R); var p3 = new Vector3d(ox + l, oy + l, R); var pc = (p0 + p3) * 0.5; double l0 = 0.0, l1 = 0.0, l2 = 0.0, l3 = 0.0; var v0 = p0.Normalized(ref l0); var v1 = p1.Normalized(ref l1); var v2 = p2.Normalized(ref l2); var v3 = p3.Normalized(ref l3); Matrix4x4d deformedCorners = new Matrix4x4d(v0.x * R, v1.x * R, v2.x * R, v3.x * R, v0.y * R, v1.y * R, v2.y * R, v3.y * R, v0.z * R, v1.z * R, v2.z * R, v3.z * R, 1.0, 1.0, 1.0, 1.0); Matrix4x4d deformedVerticals = new Matrix4x4d(v0.x, v1.x, v2.x, v3.x, v0.y, v1.y, v2.y, v3.y, v0.z, v1.z, v2.z, v3.z, 0.0, 0.0, 0.0, 0.0); var uz = pc.Normalized(); var ux = (new Vector3d(0, 1, 0)).Cross(uz).Normalized(); var uy = uz.Cross(ux); var ltow = node.LocalToWorld; Matrix3x3d tangentFrameToWorld = new Matrix3x3d(ltow.m[0, 0], ltow.m[0, 1], ltow.m[0, 2], ltow.m[1, 0], ltow.m[1, 1], ltow.m[1, 2], ltow.m[2, 0], ltow.m[2, 1], ltow.m[2, 2]); Matrix3x3d m = new Matrix3x3d(ux.x, uy.x, uz.x, ux.y, uy.y, uz.y, ux.z, uy.z, uz.z); matPropertyBlock.SetMatrix(uniforms.screenQuadCorners, (localToScreen * deformedCorners).ToMatrix4x4()); matPropertyBlock.SetMatrix(uniforms.screenQuadVerticals, (localToScreen * deformedVerticals).ToMatrix4x4()); matPropertyBlock.SetVector(uniforms.screenQuadCornerNorms, new Vector4((float)l0, (float)l1, (float)l2, (float)l3)); matPropertyBlock.SetMatrix(uniforms.tangentFrameToWorld, (tangentFrameToWorld * m).ToMatrix4x4()); }
public void EvaluateClusterDebugView(CommandBuffer cmd, HDCamera hdCamera) { ComputeShader lightClusterDebugCS = m_RenderPipelineRayTracingResources.lightClusterDebugCS; if (lightClusterDebugCS == null) { return; } // Bind the output texture CoreUtils.SetRenderTarget(cmd, m_DebugLightClusterTexture, m_RenderPipeline.sharedRTManager.GetDepthStencilBuffer(), clearFlag: ClearFlag.Color, clearColor: Color.black); // Grab the kernel int m_LightClusterDebugKernel = lightClusterDebugCS.FindKernel("DebugLightCluster"); // Inject all the parameters to the debug compute cmd.SetComputeBufferParam(lightClusterDebugCS, m_LightClusterDebugKernel, HDShaderIDs._RaytracingLightCluster, m_LightCluster); cmd.SetComputeVectorParam(lightClusterDebugCS, _ClusterCellSize, clusterCellSize); cmd.SetComputeTextureParam(lightClusterDebugCS, m_LightClusterDebugKernel, HDShaderIDs._CameraDepthTexture, m_RenderPipeline.sharedRTManager.GetDepthStencilBuffer()); // Target output texture cmd.SetComputeTextureParam(lightClusterDebugCS, m_LightClusterDebugKernel, _DebutLightClusterTexture, m_DebugLightClusterTexture); // Texture dimensions int texWidth = hdCamera.actualWidth; int texHeight = hdCamera.actualHeight; // Dispatch the compute int lightVolumesTileSize = 8; int numTilesX = (texWidth + (lightVolumesTileSize - 1)) / lightVolumesTileSize; int numTilesY = (texHeight + (lightVolumesTileSize - 1)) / lightVolumesTileSize; cmd.DispatchCompute(lightClusterDebugCS, m_LightClusterDebugKernel, numTilesX, numTilesY, 1); // Bind the parameters m_DebugMaterialProperties.SetBuffer(HDShaderIDs._RaytracingLightCluster, m_LightCluster); m_DebugMaterialProperties.SetVector(_ClusterCellSize, clusterCellSize); m_DebugMaterialProperties.SetTexture(HDShaderIDs._CameraDepthTexture, m_RenderPipeline.sharedRTManager.GetDepthTexture()); // Draw the faces cmd.DrawProcedural(Matrix4x4.identity, m_DebugMaterial, 1, MeshTopology.Lines, 48, 64 * 64 * 32, m_DebugMaterialProperties); cmd.DrawProcedural(Matrix4x4.identity, m_DebugMaterial, 0, MeshTopology.Triangles, 36, 64 * 64 * 32, m_DebugMaterialProperties); // Bind the result (RenderPipelineManager.currentPipeline as HDRenderPipeline).PushFullScreenDebugTexture(hdCamera, cmd, m_DebugLightClusterTexture, FullScreenDebugMode.LightCluster); }
public static CommandBufferCanvas.RenderDraw stencil0(CommandBufferCanvas.RenderLayer layer, MeshMesh mesh) { Vector4 viewport = layer.viewport; var mat = _stencilMat; var pass = 1; var props = new MaterialPropertyBlock(); props.SetVector("_viewport", viewport); return(new CommandBufferCanvas.RenderDraw { mesh = mesh, pass = pass, material = mat, properties = props, }); }
void LateUpdate() { if (!spriteRenderer) { return; } FlipX = spriteRenderer.flipX; FlipY = spriteRenderer.flipY; if (isDirty) { spriteRenderer.GetPropertyBlock(propertyBlock); propertyBlock.SetVector(flipPropertyID, new Vector4(FlipX ? -1 : 1, FlipY ? -1 : 1)); spriteRenderer.SetPropertyBlock(propertyBlock); } }
// ------------------------------------------------------------------------ // end: line // rect // ------------------------------------------------------------------------ // draw a rectangle on the XZ plane centered at origin in object space, dimensions = (X dimension, Z dimension) public static void DrawRect(Vector3 center, Quaternion rotation, Vector2 dimensions, Color color, bool depthTest = true) { Mesh mesh = PrimitiveMeshFactory.Rect(); if (mesh == null) { return; } Material material = GetMaterial(depthTest); MaterialPropertyBlock materialProperties = GetMaterialPropertyBlock(); materialProperties.SetColor("_Color", color); materialProperties.SetVector("_Dimensions", new Vector4(dimensions.x, 0.0f, dimensions.y, 0.0f)); Graphics.DrawMesh(mesh, center, rotation, material, 0, null, 0, materialProperties, false, false, false); }
public static void DrawCapsule2D(Vector3 center, float rotationDeg, float height, float radius, int capSegments, Color color, bool depthTest = true) { Mesh mesh = PrimitiveMeshFactory.Capsule2D(capSegments); if (mesh == null) { return; } Material material = GetMaterial(depthTest); MaterialPropertyBlock materialProperties = GetMaterialPropertyBlock(); materialProperties.SetColor("_Color", color); materialProperties.SetVector("_Dimensions", new Vector4(radius, radius, radius, height)); Graphics.DrawMesh(mesh, center, Quaternion.AngleAxis(rotationDeg, Vector3.forward), material, 0, null, 0, materialProperties, false, false, false); }
static void ApplyNewProperties() { var objs = ftBuildGraphics.atlasOnlyObj; if (objs == null) { return; } var scaleOffset = ftBuildGraphics.atlasOnlyScaleOffset; var size = ftBuildGraphics.atlasOnlySize; var ids = ftBuildGraphics.atlasOnlyID; var existingLmaps = LightmapSettings.lightmaps.ToList(); tempTextures = new List <Texture2D>(); for (int i = 0; i < objs.Count; i++) { if (objs[i] == null) { continue; } objs[i].lightmapScaleOffset = scaleOffset[i]; if (objs[i].lightmapIndex < 0 || objs[i].lightmapIndex >= existingLmaps.Count || existingLmaps[objs[i].lightmapIndex] == null || existingLmaps[objs[i].lightmapIndex].lightmapColor == null || existingLmaps[objs[i].lightmapIndex].lightmapColor.width != size[i]) { int s = 1;//Math.Max(size[i],1); var tex = new Texture2D(s, s); tempTextures.Add(tex); tex.SetPixels32(new Color32[s * s]); tex.Apply(); var ldata = new LightmapData(); ldata.lightmapColor = tex; existingLmaps.Add(ldata); objs[i].lightmapIndex = existingLmaps.Count - 1; } var prop = new MaterialPropertyBlock(); objs[i].GetPropertyBlock(prop); prop.SetFloat("bakeryLightmapSize", size[i]); UnityEngine.Random.InitState(ids[i]); prop.SetVector("bakeryLightmapID", UnityEngine.Random.ColorHSV(0, 1, 0.3f, 0.3f, 1, 1)); objs[i].SetPropertyBlock(prop); } LightmapSettings.lightmaps = existingLmaps.ToArray(); }
protected override bool DisplayFrame(int previousFrame) { if (currentFrame == previousFrame || currentAnimIndex < 0) { return(base.DisplayFrame(previousFrame)); } float currentStartTime = timeBlockData.z; float currentEndTime = timeBlockData.w; float newEndTime = currentStartTime + (currentAnimation.length / (speed * currentAnimation.playbackSpeed)); if (currentEndTime != newEndTime) { timeBlockData.w = newEndTime; meshRenderer.GetPropertyBlock(materialPropertyBlock); materialPropertyBlock.SetVector(_animTimeProp, timeBlockData); meshRenderer.SetPropertyBlock(materialPropertyBlock); } return(base.DisplayFrame(previousFrame)); }
void Update() { if (_surfaceMaterial == null) { _surfaceMaterial = new Material(_surfaceShader); _surfaceMaterial.hideFlags = HideFlags.DontSave; } if (_lineMaterial == null) { _lineMaterial = new Material(_lineShader); _lineMaterial.hideFlags = HideFlags.DontSave; } if (_materialProperties == null) { _materialProperties = new MaterialPropertyBlock(); } _noiseOffset += new Vector3(0.13f, 0.82f, 0.11f) * _noiseMotion * Time.deltaTime; _surfaceMaterial.color = _surfaceColor; _lineMaterial.color = _lineColor; _lineMaterial.SetFloat("_Radius", _radius * 1.05f); _surfaceMaterial.SetFloat("_Metallic", _metallic); _surfaceMaterial.SetFloat("_Glossiness", _smoothness); _surfaceMaterial.SetFloat("_Radius", _radius); _materialProperties.SetFloat("_NoiseAmplitude", _noiseAmplitude); _materialProperties.SetFloat("_NoiseFrequency", _noiseFrequency); _materialProperties.SetVector("_NoiseOffset", _noiseOffset); Graphics.DrawMesh( _mesh.sharedMesh, transform.localToWorldMatrix, _surfaceMaterial, 0, null, 0, _materialProperties, _castShadows, _receiveShadows, transform ); Graphics.DrawMesh( _mesh.sharedMesh, transform.localToWorldMatrix, _lineMaterial, 0, null, 1, _materialProperties, _castShadows, _receiveShadows, transform ); }
void Update() { if (_needsReset) { ResetResources(); } if (Application.isPlaying) { UpdateKernelShader(); SwapBuffersAndInvokeKernels(); } else { InitializeAndPrewarmBuffers(); } // Make a material property block for the following drawcalls. var props = new MaterialPropertyBlock(); props.SetTexture("_PositionBuffer", _positionBuffer2); props.SetTexture("_VelocityBuffer", _velocityBuffer2); props.SetFloat("_ScaleMin", _scale * (1 - _scaleRandomness)); props.SetFloat("_ScaleMax", _scale); props.SetFloat("_RandomSeed", _randomSeed); // Temporary variables var mesh = _bulkMesh.mesh; var position = transform.position; var rotation = transform.rotation; var material = _material ? _material : _defaultMaterial; var uv = new Vector2(0.5f / _positionBuffer2.width, 0); // Draw a bulk mesh repeatedly. for (var i = 0; i < _positionBuffer2.height; i++) { uv.y = (0.5f + i) / _positionBuffer2.height; props.SetVector("_BufferOffset", uv); Graphics.DrawMesh( mesh, position, rotation, material, 0, null, 0, props, _castShadows, _receiveShadows); } }
// adjusts the scaleing of the material after every value change private void OnValidate() { if (m_renderer == null) { m_renderer = GetComponent <Renderer>(); } if (m_matPropBlock == null) { m_matPropBlock = new MaterialPropertyBlock(); } m_renderer.GetPropertyBlock(m_matPropBlock); m_matPropBlock.SetVector("_MainTex_ST", m_scale); m_renderer.SetPropertyBlock(m_matPropBlock); }
public static void DrawLineStrip(Vector3[] aVert, Color color, bool depthTest = true) { Mesh mesh = PrimitiveMeshFactory.LineStrip(aVert); if (mesh == null) { return; } Material material = GetMaterial(Style.Wireframe, depthTest, false); MaterialPropertyBlock materialProperties = GetMaterialPropertyBlock(); materialProperties.SetColor("_Color", color); materialProperties.SetVector("_Dimensions", new Vector4(1.0f, 1.0f, 1.0f, 0.0f)); materialProperties.SetFloat("_ZBias", s_wireframeZBias); Graphics.DrawMesh(mesh, Vector3.zero, Quaternion.identity, material, 0, null, 0, materialProperties, false, false, false); }
private void RefreshMaskPropertyBlock() { if (MaskPropertyBlock == null) { MaskPropertyBlock = new MaterialPropertyBlock(); } GetComponent <Renderer>().GetPropertyBlock(MaskPropertyBlock); if (MainTex != null) { MaskPropertyBlock.SetTexture("_MainTex", MainTex); } MaskPropertyBlock.SetVector("_MainTex_ST", new Vector4(MainTexTiling.x, MainTexTiling.y, MainTexOffset.x, MainTexOffset.y)); GetComponent <Renderer>().SetPropertyBlock(MaskPropertyBlock); }
public void Update() { if (block_ == null) { block_ = new MaterialPropertyBlock(); } if (renderer_ == null) { renderer_ = GetComponent <Renderer>(); } block_.SetColor(ColorID, color); if (texture != null) { block_.SetTexture(MainTexID, texture); } block_.SetVector(MainTex_ST_ID, textureST); renderer_.SetPropertyBlock(block_); }
void UpdateData() { if (null == block) { block = new MaterialPropertyBlock(); } for (int i = 0, childCount = meshRenders.Length; i < childCount; i++) { var r = meshRenders[i]; if (null == r) { continue; } var g = r.gameObject; block.SetVector("instance_world_position", g.transform.position); r.SetPropertyBlock(block); } }
private void UpdateBlock() { if (Names.Length != Values.Length) { return; } Renderer r = MyRenderer; MaterialPropertyBlock block = new MaterialPropertyBlock(); r.GetPropertyBlock(block); for (int i = 0; i < Names.Length; i++) { block.SetVector(Names[i], Values[i]); } r.SetPropertyBlock(block); }
void Update() { if (_sheet == null) { _sheet = new MaterialPropertyBlock(); } var fwd = transform.forward / transform.localScale.z; var dist = Vector3.Dot(fwd, transform.position); var vector = new Vector4(fwd.x, fwd.y, fwd.z, dist); _sheet.SetVector("_EffectVector", vector); foreach (var r in _linkedRenderers) { r.SetPropertyBlock(_sheet); } }