public void SetLocalDataFromWowBone(WowBone bone) { var matrix = Mat4.Identity(); matrix = Mat4.Translate(matrix, bone.LocalPosition); // Поворота и скейла у базовых костей нет LocalMatrix = matrix; }
public void SetLocalDataFromBlendshapeBone(WowBone bone, WowVrcFileData.BlendshapeData.BoneData blendshapeBoneChange, float scale) { var matrix = Mat4.Identity(); Vec3 localPosition = bone.LocalPosition; localPosition.X += blendshapeBoneChange.LocalTransform.position.X * scale; localPosition.Y += blendshapeBoneChange.LocalTransform.position.Y * scale; localPosition.Z += blendshapeBoneChange.LocalTransform.position.Z * scale; matrix = Mat4.Translate(matrix, localPosition); // Поврота и скейла у базовых костей нет, так что учитываем только поврот и скейл из изменений блендшейпа matrix = Mat4.Multiply(matrix, Mat4.FromQuat(blendshapeBoneChange.LocalTransform.rotation)); matrix = Mat4.Scale(matrix, blendshapeBoneChange.LocalTransform.scale); LocalMatrix = matrix; }
public void DrawScene() { gl.Viewport(0, 0, canvas.Width, canvas.Height); gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); Mat4.Perspective(45, (double)canvas.Width / canvas.Height, 0.1, 100, pMatrix); Mat4.Identity(mvMatrix); Mat4.Translate(mvMatrix, new double[] { 0.0, 0.0, z }); Mat4.Rotate(mvMatrix, this.DegToRad(xRotation), new double[] { 1, 0, 0 }); Mat4.Rotate(mvMatrix, this.DegToRad(yRotation), new double[] { 0, 1, 0 }); gl.BindBuffer(gl.ARRAY_BUFFER, this.cubeVertexPositionBuffer); gl.VertexAttribPointer(this.vertexPositionAttribute, 3, gl.FLOAT, false, 0, 0); gl.BindBuffer(gl.ARRAY_BUFFER, this.cubeVertexNormalBuffer); gl.VertexAttribPointer(this.vertexNormalAttribute, 3, gl.FLOAT, false, 0, 0); gl.BindBuffer(gl.ARRAY_BUFFER, this.cubeVertexTextureCoordBuffer); gl.VertexAttribPointer(this.textureCoordAttribute, 2, gl.FLOAT, false, 0, 0); gl.ActiveTexture(gl.TEXTURE0); gl.BindTexture(gl.TEXTURE_2D, this.texture); gl.Uniform1i(this.samplerUniform, 0); // Add Blending if (this.useBlending) { gl.BlendFunc(gl.SRC_ALPHA, gl.ONE); gl.Enable(gl.BLEND); gl.Disable(gl.DEPTH_TEST); gl.Uniform1f(this.alphaUniform, this.alpha); } else { gl.Disable(gl.BLEND); gl.Enable(gl.DEPTH_TEST); gl.Uniform1f(this.alphaUniform, 1); } // Add Lighting gl.Uniform1i(this.useLightingUniform, this.useLighting); if (this.useLighting) { gl.Uniform3f(this.ambientColorUniform, this.ambientR, this.ambientG, this.ambientB); var lightingDirection = new double[] { this.lightDirectionX, this.lightDirectionY, this.lightDirectionZ }; var adjustedLD = Vec3.Create(); Vec3.Normalize(lightingDirection, adjustedLD); Vec3.Scale(adjustedLD, -1); gl.Uniform3fv(this.lightingDirectionUniform, adjustedLD); gl.Uniform3f(this.directionalColorUniform, this.directionalR, this.directionalG, this.directionalB); } gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.cubeVertexIndexBuffer); this.SetMatrixUniforms(); gl.DrawElements(gl.TRIANGLES, 36, gl.UNSIGNED_SHORT, 0); }
public Mat4 transform() { return(Mat4.Identity() * (Mat4.Translation(0, 0, -distance) * Mat4.RotatationX(ry) * Mat4.RotatationY(rx))); }
public void ComputeBoundingBox() { BBox obj_bbox = obj.BoundingBox; Vec3[] v = new Vec3[] { new Vec3(obj_bbox.x0, obj_bbox.y0, obj_bbox.z0), new Vec3(obj_bbox.x1, obj_bbox.y0, obj_bbox.z0), new Vec3(obj_bbox.x1, obj_bbox.y1, obj_bbox.z0), new Vec3(obj_bbox.x0, obj_bbox.y1, obj_bbox.z0), new Vec3(obj_bbox.x0, obj_bbox.y0, obj_bbox.z1), new Vec3(obj_bbox.x1, obj_bbox.y0, obj_bbox.z1), new Vec3(obj_bbox.x1, obj_bbox.y1, obj_bbox.z1), new Vec3(obj_bbox.x0, obj_bbox.y1, obj_bbox.z1) }; v[0] = MathUtils.TransformPoint(forwardMatrix, v[0]); v[1] = MathUtils.TransformPoint(forwardMatrix, v[1]); v[2] = MathUtils.TransformPoint(forwardMatrix, v[2]); v[3] = MathUtils.TransformPoint(forwardMatrix, v[3]); v[4] = MathUtils.TransformPoint(forwardMatrix, v[4]); v[5] = MathUtils.TransformPoint(forwardMatrix, v[5]); v[6] = MathUtils.TransformPoint(forwardMatrix, v[6]); v[7] = MathUtils.TransformPoint(forwardMatrix, v[7]); forwardMatrix.Identity(); double x0 = MathUtils.HugeValue; double y0 = MathUtils.HugeValue; double z0 = MathUtils.HugeValue; for (int j = 0; j <= 7; j++) { if (v[j].X < x0) { x0 = v[j].X; } if (v[j].Y < y0) { y0 = v[j].Y; } if (v[j].Z < z0) { z0 = v[j].Z; } } double x1 = -MathUtils.HugeValue; double y1 = -MathUtils.HugeValue; double z1 = -MathUtils.HugeValue; for (int j = 0; j <= 7; j++) { if (v[j].X > x1) { x1 = v[j].X; } if (v[j].Y > y1) { y1 = v[j].Y; } if (v[j].Z > z1) { z1 = v[j].Z; } } bbox = new BBox(x0, x1, y0, y1, z0, z1); }
private void DrawSprite(Sprite sprite) { Mat4.Perspective(45, (double)canvas.Width / canvas.Height, 0.1, 1000, pMatrix); Mat4.Identity(mvMatrix); Scale(mvMatrix, sprite.Width, sprite.Height); Mat4.Translate(mvMatrix, new double[] { sprite.X / sprite.Width, sprite.Y / sprite.Height, -12 }); //Mat4.Rotate(mvMatrix, this.DegToRad(xRotation), new double[] { 1, 0, 0 }); //Mat4.Rotate(mvMatrix, this.DegToRad(yRotation), new double[] { 0, 1, 0 }); WebGlRenderingContext.BindBuffer(WebGlRenderingContext.ARRAY_BUFFER, this.cubeVertexPositionBuffer); WebGlRenderingContext.VertexAttribPointer(this.vertexPositionAttribute, 3, WebGlRenderingContext.FLOAT, false, 0, 0); WebGlRenderingContext.BindBuffer(WebGlRenderingContext.ARRAY_BUFFER, this.cubeVertexNormalBuffer); WebGlRenderingContext.VertexAttribPointer(this.vertexNormalAttribute, 3, WebGlRenderingContext.FLOAT, false, 0, 0); WebGlRenderingContext.BindBuffer(WebGlRenderingContext.ARRAY_BUFFER, this.cubeVertexTextureCoordBuffer); WebGlRenderingContext.VertexAttribPointer(this.textureCoordAttribute, 2, WebGlRenderingContext.FLOAT, false, 0, 0); WebGlRenderingContext.ActiveTexture(WebGlRenderingContext.TEXTURE0); WebGlRenderingContext.BindTexture(WebGlRenderingContext.TEXTURE_2D, sprite.Texture.WebGLTexture); WebGlRenderingContext.Uniform1i(this.samplerUniform, 0); // Add Blending if (this.useBlending) { WebGlRenderingContext.BlendFunc(WebGlRenderingContext.SRC_ALPHA, WebGlRenderingContext.ONE); WebGlRenderingContext.Enable(WebGlRenderingContext.BLEND); WebGlRenderingContext.Disable(WebGlRenderingContext.DEPTH_TEST); WebGlRenderingContext.Uniform1f(this.alphaUniform, this.alpha); } else { WebGlRenderingContext.Disable(WebGlRenderingContext.BLEND); WebGlRenderingContext.Enable(WebGlRenderingContext.DEPTH_TEST); WebGlRenderingContext.Uniform1f(this.alphaUniform, 1); } // Add Lighting WebGlRenderingContext.Uniform1i(this.useLightingUniform, this.useLighting); if (this.useLighting) { WebGlRenderingContext.Uniform3f(this.ambientColorUniform, this.ambientR, this.ambientG, this.ambientB); var lightingDirection = new double[] { this.lightDirectionX, this.lightDirectionY, this.lightDirectionZ }; var adjustedLD = Vec3.Create(); Vec3.Normalize(lightingDirection, adjustedLD); Vec3.Scale(adjustedLD, -1); WebGlRenderingContext.Uniform3fv(this.lightingDirectionUniform, adjustedLD); WebGlRenderingContext.Uniform3f(this.directionalColorUniform, this.directionalR, this.directionalG, this.directionalB); } WebGlRenderingContext.BindBuffer(WebGlRenderingContext.ELEMENT_ARRAY_BUFFER, this.cubeVertexIndexBuffer); this.SetMatrixUniforms(); WebGlRenderingContext.DrawElements(WebGlRenderingContext.TRIANGLES, 6, WebGlRenderingContext.UNSIGNED_SHORT, 0); }